bobmarley12345

blank win32 application

Jun 29th, 2019
364
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <string.h>
  5.  
  6. // Global Variables
  7. HINSTANCE hInstance;         //the current instance
  8. const char* WindowTitle;     //The title of the window
  9. const char* WindowClassName; //the class name
  10.  
  11. LRESULT    CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM); //Used for getting messages and stuff from e.g, moving your mouse
  12. WNDCLASS               RegisterWindowClass(HINSTANCE); //registers the class in windows so you can show it
  13. BOOL                   InitInstance(HINSTANCE, int);  //initialises the class and window
  14.  
  15. void                   InitVariables(); //used to declare the window title, class name, etc.
  16.  
  17. #define DefaultWindowStyle (WS_POPUP | WS_HSCROLL | WS_SYSMENU | WS_CAPTION| WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX |WS_TABSTOP| WS_MAXIMIZEBOX)
  18.  
  19. int WINAPI WinMain(HINSTANCE currentInstance, HINSTANCE prevInstance /*this is always null*/, PSTR cmdLineArgs, INT cmdShow)
  20. {
  21.     InitVariables();
  22.  
  23.     //the & fetches the address of the RegisterWindowClass "variable" so it can be pointed to. cpp magic basically
  24.     RegisterClass(&RegisterWindowClass(currentInstance));
  25.  
  26.     if (!InitInstance(hInstance, cmdShow))
  27.         return FALSE;
  28.  
  29.     MSG msg{};
  30.     //this will be running forever until the window is closed. GetMessage constantly loops getting system messages like mouse moves, etc
  31.     while (GetMessageA(&msg, nullptr, 0, 0))
  32.     {
  33.         TranslateMessage(&msg);
  34.         DispatchMessageA(&msg);
  35.     }
  36.  
  37.     return 0;
  38. }
  39.  
  40. //Setting up the variable init thing
  41. void InitVariables()
  42. {
  43.     WindowTitle     = "A title here";
  44.     WindowClassName = "A class name here";
  45. }
  46.  
  47. LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  48. {
  49.     switch (msg)
  50.     {
  51.     case WM_DESTROY: //clicking the X button on the window basically
  52.         PostQuitMessage(0); //tells the program to exit
  53.         break;
  54.     default:
  55.         return DefWindowProcA(hWnd, msg, wParam, lParam);
  56.         break;
  57.     }
  58. }
  59.  
  60. WNDCLASS RegisterWindowClass(HINSTANCE instance)
  61. {
  62.     WNDCLASS wc{};
  63.     wc.hInstance = instance;
  64.     wc.lpszClassName = WindowClassName;
  65.     wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
  66.     wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  67.     wc.lpfnWndProc = WndProc;
  68.     return wc;
  69. }
  70.  
  71. BOOL InitInstance(HINSTANCE instance, int cmdShow)
  72. {
  73.     hInstance = instance; //stores the global instance as this method's instance
  74.  
  75.     HWND hWnd =
  76.         CreateWindowA(
  77.             WindowClassName, WindowTitle,     //Gives it the class and title
  78.             DefaultWindowStyle,               //Window style
  79.             CW_USEDEFAULT, CW_USEDEFAULT,     //initial window position
  80.             900 /*width*/, 450/*height*/,
  81.             nullptr, nullptr, hInstance, nullptr
  82.         );
  83.  
  84.     if (!hWnd)
  85.         return FALSE; //same as 0
  86.  
  87.     ShowWindow(hWnd, cmdShow); //actually shows the window on your screen
  88.     UpdateWindow(hWnd);
  89.     return TRUE; //lets other code know it's shown correctly
  90. }
RAW Paste Data