Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. #ifndef UNICODE
  2. #define UNICODE
  3. #endif
  4.  
  5. #include <windows.h>
  6. #include <new>
  7.  
  8. using std::nothrow;
  9.  
  10. class StateInfo{
  11. //struct members not shown yet
  12. };
  13.  
  14. inline StateInfo* GetAppState(HWND hwnd){
  15. LONG_PTR ptr = GetWindowLongPtr(hwnd, GWLP_USERDATA);
  16. StateInfo* pState = reinterpret_cast<StateInfo*>(ptr);
  17. return pState;
  18. }
  19.  
  20. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  21.  
  22. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
  23. {
  24. // Register the window class.
  25. const wchar_t CLASS_NAME[] = L"Sample Window Class";
  26.  
  27. StateInfo *pState = new (std::nothrow) StateInfo;
  28. if (pState == NULL){
  29. return 0;
  30. }
  31. //initialize the struct members
  32.  
  33. WNDCLASS wc = {};
  34. wc.lpfnWndProc = WindowProc;
  35. wc.hInstance = hInstance;
  36. wc.lpszClassName = CLASS_NAME;
  37.  
  38. RegisterClass(&wc);
  39.  
  40. // Create the window.
  41.  
  42. HWND hwnd = CreateWindowEx(
  43. 0, // Optional window styles.
  44. CLASS_NAME, // Window class
  45. L"UnlikeSuika's Windows Programming Practice", // Window text
  46. WS_OVERLAPPEDWINDOW, // Window style
  47.  
  48. // Size and position
  49. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  50.  
  51. NULL, // Parent window
  52. NULL, // Menu
  53. hInstance, // Instance handle
  54. pState // Additional application data
  55. );
  56.  
  57. if (hwnd == NULL)
  58. {
  59. return 0;
  60. }
  61.  
  62. ShowWindow(hwnd, nCmdShow);
  63.  
  64. // Run the message loop.
  65.  
  66. MSG msg = {};
  67. while (GetMessage(&msg, NULL, 0, 0))
  68. {
  69. TranslateMessage(&msg);
  70. DispatchMessage(&msg);
  71. }
  72.  
  73. return 0;
  74. }
  75.  
  76. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  77. {
  78. StateInfo* pState;
  79. if (uMsg == WM_CREATE){
  80. CREATESTRUCT *pCreate = reinterpret_cast<CREATESTRUCT*>(lParam);
  81. pState = reinterpret_cast<StateInfo*>(pCreate->lpCreateParams);
  82. SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pState);
  83. }
  84. else{
  85. pState = GetAppState(hwnd);
  86. }
  87. switch (uMsg){
  88. case WM_DESTROY:
  89. PostQuitMessage(0);
  90. return 0;
  91.  
  92. case WM_CLOSE:
  93. if (MessageBox(hwnd, L"Quitting already? Aw", L"Obligatory Prompting Message", MB_YESNO | MB_ICONASTERISK) == IDYES){
  94. DestroyWindow(hwnd);
  95. }
  96. return 0;
  97.  
  98. case WM_PAINT:
  99. PAINTSTRUCT ps;
  100. HDC hdc = BeginPaint(hwnd, &ps);
  101.  
  102. FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
  103.  
  104. EndPaint(hwnd, &ps);
  105. return 0;
  106. }
  107. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement