Creating Windows Will Be Easier...

Discussion in 'Win32' started by AhmedHan, Apr 28, 2006.

  1. AhmedHan

    AhmedHan New Member

    Joined:
    Oct 11, 2005
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.stirve.com
    I recently prepared a window creating header file for Win32 programming. I want to share it with you guys.
    Code:
     class CCommon
       {
       	public:
       	DWORD	dwExStyle;
       	char	lpClassName[MAX_LOADSTRING];
       	char	lpWindowName[MAX_LOADSTRING];
       	DWORD	dwStyle;
       	int		x;
       	int		y;
       	int		nWidth;
       	int		nHeight;
       	HWND	hWndParent;
       	HMENU	hMenu;
       	HINSTANCE hProgInst;
       	LPVOID	lpParam;
       	HWND	hWnd;
       	
       	HANDLE hImage;
       	CHAR szImageFile[MAX_LOADSTRING];
       	
       	CCommon()
       	{
       		dwExStyle		= 0;
       		StringCbCopy(lpClassName , MAX_LOADSTRING, "ClassName");
       		StringCbCopy(lpWindowName , MAX_LOADSTRING, "Caption");
       		dwStyle			= WS_VISIBLE | WS_CHILD;
       		x				= 5;
       		y				= 5;
       		nWidth			= 100;
       		nHeight			= 100;
       		hWndParent		= HWND_DESKTOP;
       		hMenu			= NULL;
       		lpParam			= NULL;
       	}
       
       	VOID SetInitials(INT x, INT y, INT nWidth, INT nHeight)
       	{
       		this->x = x;
       		this->y = y;
       		this->nWidth = nWidth;
       		this->nHeight = nHeight;
       	}
       	void Create(void)
       	{
       		hWnd = CreateWindowEx(	(DWORD)		dwExStyle,
       								(LPCTSTR)	lpClassName,
       								(LPCTSTR)	lpWindowName,
       								(DWORD)		dwStyle,
       								(int)		x,
       								(int)		y,
       								(int)		nWidth,
       								(int)		nHeight,
       								(HWND)		hWndParent,
       								(HMENU)		hMenu,
       								(HINSTANCE)	hProgInst,
       								(LPVOID)	lpParam);
       		DWORD ErrCode = GetLastError();
       		if (hWnd == NULL)
       		{
       			MessageBox(NULL, "Error creating control.", "Error", MB_OK);
       			DebugTest(ErrCode);
       		}
       	}
       	void Enable(bool State)
       	{
       		EnableWindow(hWnd, State);
       	}
       	char * GetText(unsigned long int MaxChars)
       	{
       		char * Buffer = new char[MaxChars];
       		SendMessage(hWnd, WM_GETTEXT, (WPARAM) MaxChars, (LPARAM) Buffer);
       		return Buffer;
       		delete Buffer;
       	}
       	void SetText(char * Buffer)
       	{
       		if(!SendMessage(hWnd, WM_SETTEXT, (WPARAM) NULL, (LPARAM) Buffer))
       		{
       			DWORD dwError = GetLastError();
       			DebugTest("Error setting text"); DebugTest(dwError);
       		}
       	}
       	void SetImage(void)
       	{
       		hImage = LoadImage(hInstance, szImageFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
       		SendMessage(hWnd, STM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) hImage);
       	}
       };
       
       class CUserDefined : public CCommon
       {
       public:
       	WNDCLASSEX WindowClass;
       	
       	CUserDefined()
       	{
       		StringCbCopy(lpClassName , MAX_LOADSTRING, "ClassName");
       		
       		WindowClass.cbSize			= (UINT)	sizeof(WNDCLASSEX);		//Defaults for WNDCLASSEX
       		WindowClass.style			= (UINT)	CS_DBLCLKS;
       		//WindowClass.lpfnWndProc		= (WNDPROC)	WindowProcedure;
       		WindowClass.cbClsExtra		= (INT)		0;
       		WindowClass.cbWndExtra		= (INT)		0;
       		//WindowClass.hInstance		= (HINSTANCE)	hProgInst;
       		WindowClass.hIcon			= (HICON)	LoadIcon (NULL, IDI_APPLICATION);
       		WindowClass.hCursor			= (HCURSOR)	LoadCursor (NULL, IDC_ARROW);
       		WindowClass.hbrBackground	= (HBRUSH)	COLOR_BACKGROUND;
       		WindowClass.lpszMenuName	= (LPCTSTR)	NULL;
       		WindowClass.lpszClassName	= (LPCTSTR)	lpClassName;
       		WindowClass.hIconSm			= (HICON)	LoadIcon (NULL, IDI_APPLICATION);
       	}
       	void RegisterAndCreate(void)
       	{
       		if (!RegisterClassEx(&WindowClass)) MessageBox(NULL, "Error registering.", "Error", MB_OK);
       		Create();
       	}
       	void Enable(bool State)
       	{
       		EnableWindow(hWnd, State);
       	}
       };
       class CEdit : public CCommon
       {
       	public:
       	
       	CEdit()
       	{
       		StringCbCopy(lpClassName , MAX_LOADSTRING, "EDIT");
       	}
       };
       
       class CStatic : public CCommon
       {
       	public:
       	
       	CStatic()
       	{
       		StringCbCopy(lpClassName , MAX_LOADSTRING, "STATIC");
       	}
       };
       
       class CImage : public CCommon
       {
       	public:
       	//HANDLE hImage;
       	//CHAR szImageFile[MAX_LOADSTRING];
       	
       	CImage()
       	{
       		StringCbCopy(lpClassName , MAX_LOADSTRING, "STATIC");
       		dwStyle |= SS_BITMAP;
       	}
       	//VOID SetImage(CHAR * szFileName)
       	//{
       	//	hImage = LoadImage(hInstance, szFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
       	//	SendMessage(this->hWnd, STM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) hImage);
       	//}
       	//VOID SetImage(VOID)
       	//{
       	//	hImage = LoadImage(hInstance, szImageFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
       	//	SendMessage(hWnd, STM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) hImage);
       	//}
       };
       class CButton : public CCommon
       {
       	public:
       	
       	CButton()
       	{
       		StringCbCopy(lpClassName , MAX_LOADSTRING, "BUTTON");
       	}
       };
       
       class CFrame : public CCommon
       {
       	public:
       	
       	CFrame()
       	{
       		StringCbCopy(lpClassName , MAX_LOADSTRING, "BUTTON");
       		dwStyle |= BS_GROUPBOX;
       	}
       };
       class CCheckBox : public CCommon
       {
       public:
       	
       	CCheckBox()
       	{
       		StringCbCopy(lpClassName , MAX_LOADSTRING, "BUTTON");
       	}
       };
       
       class CRadioButton : public CCommon
       {
       	public:
       	
       	CRadioButton()
       	{
       		StringCbCopy(lpClassName , MAX_LOADSTRING, "BUTTON");
       		dwStyle |= BS_AUTORADIOBUTTON;
       	}
       };
       
       class CMessageLoop
       {
       	public:
       	MSG	Message;
       	HWND hWnd;
       	UINT wMsgFilterMin;
       	UINT wMsgFilterMax;
       	BOOL bReturnValue;
       	BOOL bContinue;
       	DWORD dwLastError;
       	
       	CMessageLoop()
       	{
       		hWnd = NULL;
       		bContinue = TRUE;
       		wMsgFilterMin = 0;
       		wMsgFilterMax = 0;
       		dwLastError = 0;
       	}
       	VOID Loop(VOID)
       	{
       		do
       		{
       			switch(bReturnValue = GetMessage(&Message, NULL, wMsgFilterMin, wMsgFilterMax))
       			{
       				case 0:
       					bContinue = FALSE;
       					break;
       				case -1:
       					dwLastError = GetLastError();	//For future use
       					if(IDNO == MessageBox(NULL, "Error occured in the message loop.\nDo you want to continue?", "Message Loop", MB_YESNO | MB_ICONERROR))
       						bContinue = FALSE;
       					break;
       				default:
       					TranslateMessage(&Message);
       					DispatchMessage(&Message);
       			}
       		} while (bContinue);
       	}
       };
    Here is an example :
    Code:
     CUserDefined MainWnd;
       MainWnd.SetInitials(100, 100, 800, 480);
       MainWnd.hProgInst = hInstance;
       MainWnd.WindowClass.hInstance = hInstance;
       StringCbCopy(MainWnd.lpWindowName, MAX_LOADSTRING, "Window Title");
       MainWnd.WindowClass.lpfnWndProc = (WNDPROC) WindowProcedure;MainWnd.RegisterAndCreate();
    To create a button on this window :
    Code:
     CButton Button;
       Button.SetInitials(100, 100, 800, 480);
       Button.hWnd = MainWnd.hWnd;
       Button.Create();
       
    I am still improving this header. There may be bugs and errors. If you ever happen to come across any problem, or have any suggestions please write under this topic.

    Note : You don't have to copy/paste. I attached a zip file, just download it.
     

    Attached Files:

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice