Thursday, July 10, 2008

Simple Window Template

  1. #include


  2. // Step 4: the Window Procedure
  3. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  4. {
  5. switch(msg)
  6. {
  7. case WM_CREATE:

  8. break;
  9. case WM_CLOSE:
  10. DestroyWindow(hwnd);
  11. break;
  12. case WM_DESTROY:
  13. PostQuitMessage(0);
  14. break;
  15. default:
  16. return DefWindowProc(hwnd, msg, wParam, lParam);
  17. }
  18. return 0;
  19. }

  20. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  21. LPSTR lpCmdLine, int nCmdShow)
  22. {
  23. WNDCLASSEX wc;
  24. HWND hwnd;
  25. MSG Msg;
  26. static char appName[] = "Your Application";

  27. //Step 1: Registering the Window Class
  28. wc.cbSize = sizeof(WNDCLASSEX);
  29. wc.style = CS_HREDRAW | CS_VREDRAW;
  30. wc.lpfnWndProc = WndProc;
  31. wc.cbClsExtra = 0;
  32. wc.cbWndExtra = 0;
  33. wc.hInstance = hInstance;
  34. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  35. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  36. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  37. wc.lpszMenuName = NULL;
  38. wc.lpszClassName = appName;
  39. wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

  40. if(!RegisterClassEx(&wc))
  41. {
  42. MessageBox(NULL, "Window Registration Failed!", "Error!",
  43. MB_ICONERROR | MB_OK);
  44. return 0;
  45. }

  46. // Step 2: Creating the Window
  47. hwnd = CreateWindowEx(
  48. WS_EX_CLIENTEDGE,
  49. appName,
  50. "Your Window Title",
  51. WS_OVERLAPPEDWINDOW,
  52. CW_USEDEFAULT, CW_USEDEFAULT, 400, 400,
  53. NULL, NULL, hInstance, NULL);

  54. if(hwnd == NULL)
  55. {
  56. MessageBox(NULL, "Window Creation Failed!", "Error",
  57. MB_ICONERROR | MB_OK);
  58. return 0;
  59. }

  60. ShowWindow(hwnd, nCmdShow);
  61. UpdateWindow(hwnd);

  62. // Step 3: The Message Loop
  63. while(GetMessage(&Msg, NULL, 0, 0) > 0)
  64. {
  65. TranslateMessage(&Msg);
  66. DispatchMessage(&Msg);
  67. }
  68. return int(Msg.wParam);
  69. }

No comments: