Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

tonymontana

Win 32 api question??? help

This topic is 5331 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <mmsystem.h>

/////////////////////////////////////GLOBALS//////////////////////////////////////////////////

int WindowCounter=0;   //Globally tracks the number of Windows

int SparehInstance=NULL; //Globally tracks the ýnstance of application


///////////////////////////////////CONSTANTS//////////////////////////////////////////////////

const char* const WindowName="One of My Windows"; //Caption of Windows

const char* const ClassName="Myclass";            //Name of WindowClass


/////////////////////////////////////////////////////////////////////////////////////////////

LRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	switch(wparam)
	{
	case WM_CREATE:
		{
			++WindowCounter;
			return 0;
		}
		break;
	case WM_ACTIVATE:
		{
			HDC TempHdc=GetDC(hwnd);  //Get a new hdc

			char buffer[60];          //Create a buffer(array of char)

			SetBkColor(TempHdc,RGB(rand()%255,rand()%255,rand()%255));   //Arrange the fore and

			SetTextColor(TempHdc,RGB(rand()%255,rand()%255,rand()%255)); // back colors

			switch(LOWORD(wparam))
			{
			case WA_ACTIVE:
				{
					strcmp(buffer,"Window is Activated");
					TextOut(TempHdc,50,50,buffer,strlen(buffer));
					ReleaseDC(hwnd,TempHdc);
					return 0;
				}
				break;
			case WA_CLICKACTIVE:
				{
					strcmp(buffer,"Windows is Activated by Mouse");
					TextOut(TempHdc,50,50,buffer,strlen(buffer));
					ReleaseDC(hwnd,TempHdc);
					return 0;
				}
				break;
			case WA_INACTIVE:
				{
					strcmp(buffer,"Window is Deactivated");
					TextOut(TempHdc,50,50,buffer,strlen(buffer));
					ReleaseDC(hwnd,TempHdc);
					return 0;
				}
				break;
			}
			return 0;
		}
		break;
	case WM_PAINT:
		{
			
			hdc=BeginPaint(hwnd,&ps);
			EndPaint(hwnd,&ps);
			return 0;
		}
		break;
	case WM_CLOSE:
		{
			int Answer=NULL;
			Answer=MessageBox(hwnd,"Are You Sure","Sure?",MB_YESNO);
			switch(Answer)
			{
			case (IDYES):
				{
					PostMessage(hwnd,WM_DESTROY,0,0);
					return 0;
				}
				break;
			case (IDNO):
				{
					return 0;
				}
				break;
			}
			return 0;
		}
	case WM_DESTROY:
		{
			-- WindowCounter;
			if(WindowCounter<=0)
			{
				PostQuitMessage(0);
				return 0;
			}
			else
			{
				return (DefWindowProc(hwnd,msg,wparam,lparam));
			}
			return 0;
		}
		break;
	}
	return (DefWindowProc(hwnd,msg,wparam,lparam));
}


int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
	WNDCLASSEX myclass;
	MSG msg;
	HWND hwnd;
	myclass.cbClsExtra=0;
	myclass.cbSize=sizeof(WNDCLASSEX);
	myclass.cbWndExtra=0;
	myclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	myclass.hCursor=LoadCursor(hInstance,MAKEINTRESOURCE(IDC_CURSOR1));
	myclass.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
	myclass.hIconSm=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
	myclass.hInstance=hInstance;
	myclass.lpfnWndProc=WinProc;
	myclass.lpszClassName=ClassName;
	myclass.lpszMenuName=NULL;
	myclass.style=CS_HREDRAW|CS_VREDRAW|CS_OWNDC|CS_DBLCLKS;
	if(!(RegisterClassEx(&myclass)))
	{
		MessageBox(NULL,"Class Registration Error","Registration Error",MB_OK);
		return 0;
	}
	if(!(hwnd=CreateWindowEx(NULL,ClassName,WindowName,WS_OVERLAPPEDWINDOW|WS_VISIBLE,30,30,330,330,
		NULL,NULL,hInstance,NULL)))
	{
		MessageBox(hwnd,"Class Creation Error","Creation Error",MB_OK);
		return 0;
	}
	while(TRUE)
	{
		if(PeekMessage(&msg,hwnd,0,0,PM_REMOVE))
		{
			if(msg.message==WM_QUIT)
			{
				return 0;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	return (msg.wParam);
}
	


i wanted to make an OVERLAPPEDWINDOW but the code is generating a white screen why_?

Share this post


Link to post
Share on other sites
Advertisement
I''m not sure what could cause that, but there''s something you should change in your code. You''re doing this:

strcmp(buffer,"Window is Activated");

That tries to compare the two strings and since the buffer contains only some crap and you''re not checking the result, it doesn''t do practically anything. The buffer will stay filled with something not-defined which may, and probably will, cause problems sooner or later. You probably meant

strcpy(buffer,"Window is Activated");

which will copy the string to your buffer.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!