My Window Class

Posts 1–7 of 7 · Page 1 of 1
My Window Class
Okay, so I have been always asking questions on this forum (and they were all answered ) , but i noticed that i am not really contributing anything, and i don't like being a freeloader.


So without further ado, I made a window class for myself. It is something that is extremely simple, but it saves me A LOT OF TIME when i am programming win32 gui applications. Here is my class code:

Code:
#ifndef WINDOWHELPER_H
#define WINDOWHELPER_H

#include <windows.h>

#define WindowProcedure LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
#define DefaultWindowProcedure DefWindowProc(hwnd, message, wParam, lParam);
#define WinMain int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
#define DialogProcedure BOOL CALLBACK DlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)


LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

class WINDOW;

class WINDOW
{

public:

	
int MakeWindow(LPCWSTR Title, int x_pos, int y_pos, int width, int height, HMENU menu, HBRUSH background)
{
	

 static TCHAR szAppName[] = TEXT ("HelloWin") ;
     HWND         hwnd ;
     WNDCLASS     wndclass ;

     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = GetModuleHandle(NULL) ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground =  background;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     hwnd = CreateWindow (szAppName,                  // window class name
                          Title, // window caption
                          WS_OVERLAPPEDWINDOW,        // window style
                          x_pos,              // initial x position
                          y_pos,              // initial y position
                          width,              // initial x size
                          height,              // initial y size
                          NULL,                       // parent window handle
                          menu,                       // window menu handle
                          GetModuleHandle(NULL),                  // program instance handle
                          NULL) ;                     // creation parameters
     
     ShowWindow (hwnd, SW_SHOW) ;
     UpdateWindow (hwnd) ;
	 
	 	MSG msg;
	while(GetMessage(&msg, NULL, 0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;


}


};


#endif


And I just put this in a header file and put it in my msvc++ include folder, so i can use it on any project. The way to use it is very simple here is a way:

Code:
#include <Windows.h>
#include <window_helper.h>

WINDOW *window;


WinMain
{
	window->MakeWindow(TEXT("Michael's Class"), CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, (HBRUSH) CreateSolidBrush(RGB(27,27,97)));
}


WindowProcedure
{
	switch(message)
	{
	case WM_CREATE:
		
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
 return DefaultWindowProcedure
}
As you can see the "#include <window_helper.h>" is my window class header. And then i declare window as a pointer to the window class so that i can access its members with the -> operator. Then I write WinMain instead of the entry point function because i have it already defined for WinMain. Then inside the entry point winmain, i have window->MakeWindow(); . This is basically the function that makes the window with the settings i have entered. The parameters of MakeWindow are: LPCWSTR Title, int x_pos, int y_pos, int width, int height, HMENU menu, HBRUSH background.

And then You don't need to type out the whole function for your WndProc and and DefWndProc (this is the laziness to the max ). Now just to show how much work it saves you, let's make a program that will basically have a blank window and if you press shift it will popup a MessageBox.


Here it is without the window_helper.h :

Code:
#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("HelloWin") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;

     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     hwnd = CreateWindow (szAppName,                  // window class name
                          TEXT ("The Hello Program"), // window caption
                          WS_OVERLAPPEDWINDOW,        // window style
                          CW_USEDEFAULT,              // initial x position
                          CW_USEDEFAULT,              // initial y position
                          CW_USEDEFAULT,              // initial x size
                          CW_USEDEFAULT,              // initial y size
                          NULL,                       // parent window handle
                          NULL,                       // window menu handle
                          hInstance,                  // program instance handle
                          NULL) ;                     // creation parameters
     
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
     
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     HDC         hdc ;
     PAINTSTRUCT ps ;
     RECT        rect ;
     
     switch (message)
     {
   

     case KEY_DOWN:
      switch(wParam)
    {
        case VK_SHIFT:
               MessageBoxA(NULL, "The Long way sucks.", "Old School API", MB_OK);
          break;
    }

       return 0;
          
     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

Here it is with my window class:

Code:
#include <Windows.h>
#include <window_helper.h>

WINDOW *window;


WinMain
{
	window->MakeWindow(TEXT("Michael's Class"), CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, (HBRUSH) CreateSolidBrush(RGB(27,27,97)));
}


WindowProcedure
{
	switch(message)
	{
	case WM_KEYDOWN:
                    switch(wParam)
                  {
                        case VK_SHIFT:
                        MessageBoxA(NULL, "258456's class really saves me time.", "Success", MB_OK);
                           break;
                  }           

              case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
 return DefaultWindowProcedure
}

As you can see my class really helps you save time, but it isn't close to being done yet. There is obviously much more to add like allowing you to add a menu and things of that sort.
Very Nice. Thanks for your contribution. Keep it up ;]
Quote Originally Posted by Hassan View Post
Very Nice. Thanks for your contribution. Keep it up ;]
Thanks, I just wanted to help you guys for a change, cuz you guys always help me.





Also, if you think that something is wrong in my class or if it could be written better, please don't be shy to say because i would love to know as we are all learning together.
Pretty good =)

Thanks
Quote Originally Posted by Cyberdyne View Post
Pretty good =)

Thanks
No problem, I am glad to help.
Nice one! I have my own windows class that makes a nice windows but also has methods to add buttons and text. In the end though, I found the Visual C++ interface easier to make windows with. The problem with creating a window class is you can't incorporate the procedure into the class any easy way but I read there's a way to do it with virtual functions. Thanks for the share anyway!
Quote Originally Posted by zhaoyun333 View Post
Nice one! I have my own windows class that makes a nice windows but also has methods to add buttons and text. In the end though, I found the Visual C++ interface easier to make windows with. The problem with creating a window class is you can't incorporate the procedure into the class any easy way but I read there's a way to do it with virtual functions. Thanks for the share anyway!
Well, I didn't want to include the window procedure in the class, because the window procedure is the meat of the program, so that is something that i would want to be written out in my cpp file. Plus, it would cause limitations if you were to add it into the class because each time you make a windows app you make the window proc. differently, but to intialize a window in winmain gets repetitive and makes things slightly messier.
Posts 1–7 of 7 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us