View Single Post
Old Jan 22nd, 2008, 1:12 PM   #6
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 547
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: Multiple windows -

This is using VC++ 2008 Express. It creates two identical windows. Most of the code was generated by the IDE when I created the project. All I did was add a second class name and changed the *.rc file to contain a second string.

c++ Syntax (Toggle Plain Text)
  1. // testwin.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "testwin.h"
  6.  
  7. #define MAX_LOADSTRING 100
  8.  
  9. // Global Variables:
  10. HINSTANCE hInst; // current instance
  11. TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
  12. TCHAR szWindowClass1[MAX_LOADSTRING]; // the main window class name
  13. TCHAR szWindowClass2[MAX_LOADSTRING]; // the main window class name
  14.  
  15. // Forward declarations of functions included in this code module:
  16. ATOM MyRegisterClass(HINSTANCE hInstance, LPCTSTR szWindowClass);
  17. BOOL InitInstance(HINSTANCE, int, LPCTSTR);
  18. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  19. INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  20.  
  21. int APIENTRY _tWinMain(HINSTANCE hInstance,
  22. HINSTANCE hPrevInstance,
  23. LPTSTR lpCmdLine,
  24. int nCmdShow)
  25. {
  26. UNREFERENCED_PARAMETER(hPrevInstance);
  27. UNREFERENCED_PARAMETER(lpCmdLine);
  28.  
  29. // TODO: Place code here.
  30. MSG msg;
  31. HACCEL hAccelTable;
  32.  
  33. // Initialize global strings
  34. LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  35. LoadString(hInstance, IDC_TESTWIN1, szWindowClass1, MAX_LOADSTRING);
  36. LoadString(hInstance, IDC_TESTWIN2, szWindowClass2, MAX_LOADSTRING);
  37. MyRegisterClass(hInstance, szWindowClass1);
  38. MyRegisterClass(hInstance, szWindowClass2);
  39.  
  40. // Perform application initialization:
  41. if (!InitInstance (hInstance, nCmdShow, szWindowClass1))
  42. {
  43. return FALSE;
  44. }
  45. if (!InitInstance (hInstance, nCmdShow, szWindowClass2))
  46. {
  47. return FALSE;
  48. }
  49.  
  50. hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTWIN));
  51.  
  52. // Main message loop:
  53. while (GetMessage(&msg, NULL, 0, 0))
  54. {
  55. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  56. {
  57. TranslateMessage(&msg);
  58. DispatchMessage(&msg);
  59. }
  60. }
  61.  
  62. return (int) msg.wParam;
  63. }
  64.  
  65.  
  66.  
  67. //
  68. // FUNCTION: MyRegisterClass()
  69. //
  70. // PURPOSE: Registers the window class.
  71. //
  72. // COMMENTS:
  73. //
  74. // This function and its usage are only necessary if you want this code
  75. // to be compatible with Win32 systems prior to the 'RegisterClassEx'
  76. // function that was added to Windows 95. It is important to call this function
  77. // so that the application will get 'well formed' small icons associated
  78. // with it.
  79. //
  80. ATOM MyRegisterClass(HINSTANCE hInstance, LPCTSTR szWindowClass)
  81. {
  82. WNDCLASSEX wcex;
  83.  
  84. wcex.cbSize = sizeof(WNDCLASSEX);
  85.  
  86. wcex.style = CS_HREDRAW | CS_VREDRAW;
  87. wcex.lpfnWndProc = WndProc;
  88. wcex.cbClsExtra = 0;
  89. wcex.cbWndExtra = 0;
  90. wcex.hInstance = hInstance;
  91. wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TESTWIN));
  92. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  93. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  94. wcex.lpszMenuName = MAKEINTRESOURCE(IDC_TESTWIN);
  95. wcex.lpszClassName = szWindowClass;
  96. wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  97.  
  98. return RegisterClassEx(&wcex);
  99. }
  100.  
  101. //
  102. // FUNCTION: InitInstance(HINSTANCE, int)
  103. //
  104. // PURPOSE: Saves instance handle and creates main window
  105. //
  106. // COMMENTS:
  107. //
  108. // In this function, we save the instance handle in a global variable and
  109. // create and display the main program window.
  110. //
  111. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow, LPCTSTR szWindowClass)
  112. {
  113. HWND hWnd;
  114.  
  115. hInst = hInstance; // Store instance handle in our global variable
  116.  
  117. hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  118. CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  119.  
  120. if (!hWnd)
  121. {
  122. return FALSE;
  123. }
  124.  
  125. ShowWindow(hWnd, nCmdShow);
  126. UpdateWindow(hWnd);
  127.  
  128. return TRUE;
  129. }
  130.  
  131. //
  132. // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  133. //
  134. // PURPOSE: Processes messages for the main window.
  135. //
  136. // WM_COMMAND - process the application menu
  137. // WM_PAINT - Paint the main window
  138. // WM_DESTROY - post a quit message and return
  139. //
  140. //
  141. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  142. {
  143. int wmId, wmEvent;
  144. PAINTSTRUCT ps;
  145. HDC hdc;
  146.  
  147. switch (message)
  148. {
  149. case WM_COMMAND:
  150. wmId = LOWORD(wParam);
  151. wmEvent = HIWORD(wParam);
  152. // Parse the menu selections:
  153. switch (wmId)
  154. {
  155. case IDM_about:
  156. DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  157. break;
  158. case IDM_EXIT:
  159. DestroyWindow(hWnd);
  160. break;
  161. default:
  162. return DefWindowProc(hWnd, message, wParam, lParam);
  163. }
  164. break;
  165. case WM_PAINT:
  166. hdc = BeginPaint(hWnd, &ps);
  167. // TODO: Add any drawing code here...
  168. EndPaint(hWnd, &ps);
  169. break;
  170. case WM_DESTROY:
  171. PostQuitMessage(0);
  172. break;
  173. default:
  174. return DefWindowProc(hWnd, message, wParam, lParam);
  175. }
  176. return 0;
  177. }
  178.  
  179. // Message handler for about box.
  180. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  181. {
  182. UNREFERENCED_PARAMETER(lParam);
  183. switch (message)
  184. {
  185. case WM_INITDIALOG:
  186. return (INT_PTR)TRUE;
  187.  
  188. case WM_COMMAND:
  189. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  190. {
  191. EndDialog(hDlg, LOWORD(wParam));
  192. return (INT_PTR)TRUE;
  193. }
  194. break;
  195. }
  196. return (INT_PTR)FALSE;
  197. }
Ancient Dragon is offline   Reply With Quote