Thread: My Window Class

Results 1 to 7 of 7
  1. #1
    258456's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    ghjghj
    Posts
    1,222
    Reputation
    18
    Thanks
    300
    My Mood
    Relaxed

    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.
    Last edited by 258456; 09-05-2011 at 12:24 PM.

  2. The Following 3 Users Say Thank You to 258456 For This Useful Post:

    .::SCHiM::. (09-05-2011),Hassan (09-05-2011),Terell. (09-05-2011)

  3. #2
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    Very Nice. Thanks for your contribution. Keep it up ;]

  4. The Following User Says Thank You to Hassan For This Useful Post:

    258456 (09-05-2011)

  5. #3
    258456's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    ghjghj
    Posts
    1,222
    Reputation
    18
    Thanks
    300
    My Mood
    Relaxed
    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.

  6. #4
    Cyberdyne's Avatar
    Join Date
    Sep 2011
    Gender
    male
    Posts
    244
    Reputation
    -15
    Thanks
    33
    My Mood
    Inspired
    Pretty good =)

    Thanks




  7. #5
    258456's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    ghjghj
    Posts
    1,222
    Reputation
    18
    Thanks
    300
    My Mood
    Relaxed
    Quote Originally Posted by Cyberdyne View Post
    Pretty good =)

    Thanks
    No problem, I am glad to help.

  8. #6
    zhaoyun333's Avatar
    Join Date
    Apr 2009
    Gender
    male
    Posts
    396
    Reputation
    11
    Thanks
    1,125
    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!
    There are five possible operations for any army. If you can fight, fight; if you cannot fight, defend; if you cannot defend, flee; if you cannot flee, surrender; if you cannot surrender, die." - Sima Yi

  9. #7
    258456's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    ghjghj
    Posts
    1,222
    Reputation
    18
    Thanks
    300
    My Mood
    Relaxed
    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.

Similar Threads

  1. Combat Arms Kernel Patch - Class Reversing - Window Mode
    By DeadLinez in forum Combat Arms Coding Help & Discussion
    Replies: 20
    Last Post: 06-24-2011, 03:45 PM
  2. New Windows Exploit
    By Dave84311 in forum General
    Replies: 6
    Last Post: 12-31-2009, 05:16 PM
  3. Windows Live Beta - Go Here Quick!
    By Dave84311 in forum General
    Replies: 6
    Last Post: 12-08-2008, 01:35 PM
  4. Playing Warrock In Multi Windows Need Help
    By Shadowguild in forum WarRock - International Hacks
    Replies: 27
    Last Post: 01-04-2006, 10:32 PM
  5. [Tutorial]Change class without respawn
    By vir2000 in forum Game Hacking Tutorials
    Replies: 0
    Last Post: 01-04-2006, 01:47 PM