Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
6.4 KiB

  1. #include "compatadmin.h"
  2. BOOL CWindow::Create(
  3. LPCTSTR szClassName,
  4. LPCTSTR szWindowTitle,
  5. int nX,
  6. int nY,
  7. int nWidth,
  8. int nHeight,
  9. CWindow * pParent,
  10. HMENU nMenuID,
  11. DWORD dwExFlags,
  12. DWORD dwFlags)
  13. {
  14. HWND hParent = NULL;
  15. HMENU hMenu = LoadMenu(g_hInstance,MAKEINTRESOURCE(nMenuID));
  16. int nTries = 0;
  17. if ( NULL == hMenu )
  18. hMenu = nMenuID;
  19. if ( NULL != pParent )
  20. hParent = pParent->m_hWnd;
  21. do {
  22. // Attempt to create the window as provided to the class.
  23. // If this fails that means that the window class does not exist and we have to make a new window
  24. //class
  25. m_hWnd = ::CreateWindowEx( dwExFlags,
  26. szClassName,
  27. szWindowTitle,
  28. dwFlags,
  29. nX,
  30. nY,
  31. nWidth,
  32. nHeight,
  33. hParent,
  34. hMenu,
  35. g_hInstance,
  36. this);// This is the window-creation application data
  37. // Failed?
  38. if ( NULL == m_hWnd ) {
  39. // If the creation failed, register the class
  40. // and try again.
  41. WNDCLASS wc;
  42. ZeroMemory(&wc,sizeof(wc));
  43. wc.style = CS_DBLCLKS;
  44. wc.lpfnWndProc = MsgProc;
  45. wc.hInstance = g_hInstance;
  46. wc.hIcon = ::LoadIcon(g_hInstance,MAKEINTRESOURCE(IDI_COMPATADMIN));
  47. wc.hCursor = ::LoadCursor(g_hInstance,TEXT("AppCursor"));
  48. if ( NULL == wc.hCursor )
  49. wc.hCursor = ::LoadCursor(NULL,IDC_ARROW);
  50. wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  51. wc.lpszClassName = szClassName;
  52. // If the registration failed, it's probably already
  53. // registered. Failure is then severe enough to fail
  54. // the call immediately.
  55. if ( 0 == ::RegisterClass(&wc)){
  56. MessageBox(NULL,TEXT("This program needs an operating system with UNICODE support to execute properly !"),TEXT("Error"),MB_ICONERROR);
  57. return FALSE;
  58. }
  59. }
  60. ++nTries;
  61. }
  62. while ( NULL == m_hWnd && 1 >= nTries );
  63. return(NULL == m_hWnd) ? FALSE:TRUE;
  64. }
  65. void CWindow::Refresh(void)
  66. {
  67. InvalidateRect(m_hWnd,NULL,TRUE);
  68. UpdateWindow(m_hWnd);
  69. }
  70. void CWindow::msgCommand(UINT uID,HWND hSender)
  71. {
  72. }
  73. void CWindow::msgCreate(void)
  74. {
  75. }
  76. void CWindow::msgClose(void)
  77. {
  78. }
  79. void CWindow::msgResize(UINT uWidth, UINT uHeight)
  80. {
  81. }
  82. void CWindow::msgPaint(HDC hDC)
  83. {
  84. }
  85. void CWindow::msgEraseBackground(HDC hDC)
  86. {
  87. RECT rRect;
  88. GetClientRect(m_hWnd,&rRect);
  89. ++rRect.right;
  90. ++rRect.bottom;
  91. FillRect(hDC,&rRect,(HBRUSH) (COLOR_WINDOW + 1));
  92. }
  93. void CWindow::msgChar(TCHAR chChar)
  94. {
  95. }
  96. void CWindow::msgNotify(LPNMHDR pHdr)
  97. {
  98. }
  99. LRESULT CWindow::MsgProc(
  100. UINT uMsg,
  101. WPARAM wParam,
  102. LPARAM lParam)
  103. {
  104. switch ( uMsg ) {
  105. case WM_KEYDOWN:
  106. {
  107. }
  108. break;
  109. case WM_CHAR:
  110. {
  111. msgChar((TCHAR)wParam);
  112. }
  113. break;
  114. case WM_CREATE:
  115. {
  116. msgCreate();
  117. }
  118. break;
  119. case WM_CLOSE:
  120. {
  121. msgClose();
  122. }
  123. break;
  124. case WM_COMMAND:
  125. {
  126. msgCommand(LOWORD(wParam),(HWND)lParam);
  127. }
  128. break;
  129. case WM_NOTIFY:
  130. {
  131. msgNotify((LPNMHDR) lParam);
  132. }
  133. break;
  134. case WM_PAINT:
  135. {
  136. HDC hDC;
  137. PAINTSTRUCT ps;
  138. int nSave;
  139. hDC = ::BeginPaint(m_hWnd,&ps);
  140. nSave = SaveDC(hDC);
  141. msgPaint(hDC);
  142. RestoreDC(hDC,nSave);
  143. ::EndPaint(m_hWnd,&ps);
  144. }
  145. break;
  146. case WM_ERASEBKGND:
  147. {
  148. HDC hDC = (HDC) wParam;
  149. msgEraseBackground(hDC);
  150. }
  151. return 1;
  152. case WM_SIZE:
  153. {
  154. UINT uWidth = LOWORD(lParam);
  155. UINT uHeight = HIWORD(lParam);
  156. msgResize(uWidth,uHeight);
  157. }
  158. break;
  159. default:
  160. return DefWindowProc(m_hWnd,uMsg,wParam,lParam);
  161. }
  162. return 0;
  163. }
  164. //RTTI needed.
  165. LRESULT CALLBACK CWindow::MsgProc(
  166. HWND hWnd,
  167. UINT uMsg,
  168. WPARAM wParam,
  169. LPARAM lParam)
  170. {
  171. CWindow * pWnd;
  172. // Wrap this in a try-except. If we assert inside the window proc,
  173. // RTTI may throw instead of return NULL.
  174. __try
  175. {
  176. pWnd = dynamic_cast<CWindow *> ((CWindow *)GetWindowLongPtr(hWnd,GWLP_USERDATA));
  177. }
  178. __except(1)
  179. {
  180. pWnd = NULL;
  181. }
  182. // If the window is being created, record the "this" pointer with the window.
  183. if ( WM_CREATE == uMsg ) {
  184. LPCREATESTRUCT lpCS = (LPCREATESTRUCT) lParam;
  185. pWnd = dynamic_cast<CWindow *> ((CWindow *) lpCS->lpCreateParams);
  186. assert(NULL != pWnd);//TEXT("CreateWindow was called on a class owned by CWindow outside of CWindow::Create()"));
  187. // Write the class pointer.
  188. if ( NULL != pWnd ) {
  189. pWnd->m_hWnd = hWnd;
  190. ::SetWindowLongPtr(hWnd,GWLP_USERDATA,(LONG_PTR) pWnd);
  191. } else {
  192. // Critical failure. CreateWindow was called on a class that we
  193. // own, but wasn't called through the class. Fail the create call.
  194. return -1;
  195. }
  196. }
  197. assert(((CWindow *) GetWindowLongPtr(hWnd,GWLP_USERDATA)) == pWnd);//,TEXT("GetWindowLongPtr data has been corrupted"));
  198. // Dispatch the message to the class based window proc
  199. if ( NULL != pWnd )
  200. return pWnd->MsgProc(uMsg,wParam,lParam);
  201. // The window hasn't recorded the class "this" pointer yet. So,
  202. // perform default processing on the message.
  203. return DefWindowProc(hWnd,uMsg,wParam,lParam);
  204. }