RTWnd.cpp
上传用户:qhonly
上传日期:2013-06-10
资源大小:487k
文件大小:24k
源码类别:

界面编程

开发平台:

Visual C++

  1. #include "StdAfx.h"
  2. #include "rtwnd.h"
  3. #include "RTDraw.h"
  4. CRTWnd::CRTWnd(void)
  5. {
  6. m_bActive = FALSE;
  7. m_ExitButtonState = StateNormal;
  8. m_MinimizeButtonState = StateNormal;
  9. m_MaximizeButtonState = StateNormal;
  10. m_NcMouseState = MouseStateNormal;
  11. m_SelTitleButton = NoneButton;
  12. }
  13. CRTWnd::~CRTWnd(void)
  14. {
  15. }
  16. COLORREF CRTWnd::m_BorderColor[4] = {0xAD9E9C,0xCEBEB5,0xCEBEB5,0xDED3CE};
  17. BOOL     CRTWnd::m_bUseRTStyle = TRUE;
  18. BOOL     CRTWnd::m_bUsedDefTitleBar = TRUE;
  19. CBitmap* CRTWnd::m_TitleBarBitmap[5] = {NULL,NULL,NULL,NULL,NULL};
  20. CBitmap* CRTWnd::m_MenuBarBitmap[5] = {NULL,NULL,NULL,NULL,NULL};
  21. CBitmap* CRTWnd::m_MaximizeButtonBitmap[5] = {NULL,NULL,NULL,NULL,NULL};
  22. CBitmap* CRTWnd::m_MinimizeButtonBitmap[5] = {NULL,NULL,NULL,NULL,NULL};
  23. CBitmap* CRTWnd::m_RestoreButtonBitmap[5] = {NULL,NULL,NULL,NULL,NULL};
  24. CBitmap* CRTWnd::m_ExitButtonBitmap[5] = {NULL,NULL,NULL,NULL,NULL};
  25. UINT     CRTWnd::m_TitleBarBitmapDrawMode[5] = {0,0,0,0,0};
  26. UINT     CRTWnd::m_MenuBarBitmapDrawMode[5] = {0,0,0,0,0};
  27. UINT     CRTWnd::m_MaximizeButtonBitmapDrawMode[5] = {0,0,0,0,0};
  28. UINT     CRTWnd::m_MinimizeButtonBitmapDrawMode[5] = {0,0,0,0,0};
  29. UINT     CRTWnd::m_ExitButtonBitmapDrawMode[5] = {0,0,0,0,0};
  30. UINT     CRTWnd::m_RestoreButtonBitmapDrawMode[5] = {0,0,0,0,0};
  31. BOOL CRTWnd::RTDrawFrameBorder(CDC *pDC,HRGN hRgn,HWND hWnd)
  32. {
  33. //画框架
  34. int Frame_BorderWidth = 0;
  35. int Frame_BorderHeight = 0;
  36. DWORD style = GetWindowLong(hWnd,GWL_STYLE);
  37. if( (style & WS_DLGFRAME) == WS_DLGFRAME)
  38. {
  39. int Frame_BorderWidth = GetSystemMetrics(SM_CXDLGFRAME);
  40. int Frame_BorderHeight = GetSystemMetrics(SM_CYDLGFRAME);
  41. }
  42. else if((style & WS_THICKFRAME) == WS_THICKFRAME)
  43. {
  44. int Frame_BorderWidth = GetSystemMetrics(SM_CXFRAME);
  45. int Frame_BorderHeight = GetSystemMetrics(SM_CYFRAME);
  46. }
  47. //画立体边框
  48. CRect rtWnd,rtBorder;
  49. GetWindowRect(hWnd,&rtWnd);
  50. rtBorder.SetRect(0,0,rtWnd.Width(),rtWnd.Height());
  51. if(m_bUsedDefTitleBar)
  52. {
  53. DefWindowProc(hWnd,WM_NCPAINT,1,0);
  54. return TRUE;
  55. }
  56. for(int i = 0; i < Frame_BorderWidth ; i ++)
  57. {
  58. if(i < 4)
  59. {
  60. pDC->Draw3dRect(i,i,rtWnd.Width() - 2*i,rtWnd.Height() - 2*i,m_BorderColor[i],m_BorderColor[i]);
  61. }
  62. else
  63. {
  64. pDC->Draw3dRect(i,i,rtWnd.Width() - 2*i,rtWnd.Height() - 2*i,m_BorderColor[3],m_BorderColor[3]);
  65. }
  66. }
  67. return TRUE;
  68. }
  69. BOOL CRTWnd::RTDrawMenuBar(CDC *pDC,HRGN hRgn,LPARAM lParam,HWND hWnd)
  70. {
  71. HMENU hMenu = GetMenu(hWnd);
  72. if(hMenu == NULL)return TRUE;
  73. if(m_MenuBarBitmap[0] == NULL)return FALSE;
  74. CRect rtMenu;
  75. CRect rtWnd;
  76. GetWindowRect(hWnd,&rtWnd);
  77. rtMenu.left = GetSystemMetrics(SM_CXFRAME);
  78. rtMenu.top = GetSystemMetrics(SM_CYSIZE) + GetSystemMetrics(SM_CXFRAME) + 1;
  79. rtMenu.right = rtWnd.Width() - GetSystemMetrics(SM_CXFRAME);
  80. rtMenu.bottom = rtMenu.top +  GetSystemMetrics(SM_CYMENU);
  81. UINT ItemCount = GetMenuItemCount(hMenu);
  82. int PrevTop = rtMenu.top;
  83. int PrevLeft = rtMenu.left;
  84. int MenuItemHeight = GetSystemMetrics(SM_CYMENU);
  85. CRect rtMenuItem;
  86. //画菜单上部线
  87. CBrush brMenu(GetSysColor(COLOR_MENU));
  88. CBrush* pOldBrush = pDC->SelectObject(&brMenu);
  89. pDC->PatBlt(rtMenu.left,rtMenu.top - 1,rtMenu.Width(),1, PATCOPY);
  90. pDC->PatBlt(rtMenu.left,rtMenu.top - 1,rtMenu.Width(),1, PATCOPY);
  91. for(UINT items = 0; items < ItemCount; items++)
  92. {
  93. GetMenuItemRect(hWnd,hMenu,items,&rtMenuItem);
  94. int MenuTop = rtMenuItem.top - rtWnd.top;
  95. MenuItemHeight = rtMenuItem.Height();
  96. if(MenuTop != PrevTop)
  97. {
  98. CRect rtDraw(PrevLeft,PrevTop,rtMenu.right,PrevTop + MenuItemHeight);
  99. CRTDraw::RTDrawBitmap(pDC,&rtDraw,m_MenuBarBitmap[BMP_BACK],m_MenuBarBitmapDrawMode[BMP_BACK]);
  100. PrevTop = MenuTop;
  101. PrevLeft = rtMenu.left + rtMenuItem.Width();
  102. }
  103. else
  104. {
  105. PrevLeft += rtMenuItem.Width();
  106. }
  107. }
  108. CRect rtDraw(PrevLeft,PrevTop,rtMenu.right,PrevTop + MenuItemHeight);
  109. CRTDraw::RTDrawBitmap(pDC,&rtDraw,m_MenuBarBitmap[BMP_BACK],m_MenuBarBitmapDrawMode[BMP_BACK]);
  110. //画菜单上部线
  111. pDC->PatBlt(rtMenu.left,PrevTop + MenuItemHeight,rtMenu.Width(),1, PATCOPY);
  112. pDC->SelectObject(pOldBrush);
  113. return TRUE;
  114. }
  115. BOOL CRTWnd::SetMinimizeButtonBitmap(CBitmap *MinimizeButtonBitmap[],UINT DrawMode[])
  116. {
  117. for(int i = 0; i < 5; i ++)
  118. {
  119. m_MinimizeButtonBitmap[i] = MinimizeButtonBitmap[i];
  120. m_MinimizeButtonBitmapDrawMode[i] = DrawMode[i];
  121. }
  122. return TRUE;
  123. }
  124. BOOL CRTWnd::SetTitleBarBitmap(CBitmap *TitleBarBitmap[],UINT DrawMode[])
  125. {
  126. for(int i = 0; i < 5; i ++)
  127. {
  128. m_TitleBarBitmap[i] = TitleBarBitmap[i];
  129. m_TitleBarBitmapDrawMode[i] = DrawMode[i];
  130. }
  131. return TRUE;
  132. }
  133. BOOL CRTWnd::SetMenuBarBitmap(CBitmap* MenuBarBitmap[],UINT DrawMode[])
  134. {
  135. for(int i = 0; i < 5; i ++)
  136. {
  137. m_MenuBarBitmap[i] = MenuBarBitmap[i];
  138. m_MenuBarBitmapDrawMode[i] = DrawMode[i];
  139. }
  140. return TRUE;
  141. }
  142. BOOL CRTWnd::SetMaximizeButtonBitmap(CBitmap* MaximizeButtonBitmap[],UINT DrawMode[])
  143. {
  144. for(int i = 0; i < 5; i ++)
  145. {
  146. m_MaximizeButtonBitmap[i] =  MaximizeButtonBitmap[i];
  147. m_MinimizeButtonBitmapDrawMode[i] = DrawMode[i];
  148. }
  149. return TRUE;
  150. }
  151. BOOL CRTWnd::SetBorderColor(COLORREF ColorArray[])
  152. {
  153. for(int i = 0; i < 5 ; i++)
  154. m_BorderColor[i] = ColorArray[i];
  155. return TRUE;
  156. }
  157. BOOL CRTWnd::RTDrawTitleBar(CDC* pDC,HRGN hRgn,BOOL bActive,HWND hWnd)
  158. {
  159. //取得标题栏的位置
  160. CRect rtWnd;
  161. CRect rtTitle;
  162. GetWindowRect(hWnd,&rtWnd);
  163. if(m_bUsedDefTitleBar)
  164. {
  165. rtTitle.left = rtWnd.left + GetSystemMetrics(SM_CXFRAME);
  166. rtTitle.top =  rtWnd.top + GetSystemMetrics(SM_CYFRAME);
  167. rtTitle.right = rtWnd.right - rtWnd.left - GetSystemMetrics(SM_CXFRAME);
  168. rtTitle.bottom = rtTitle.top + GetSystemMetrics(SM_CYSIZE);
  169. return TRUE;
  170. }
  171. rtTitle.left = GetSystemMetrics(SM_CXFRAME);
  172. rtTitle.top = GetSystemMetrics(SM_CYFRAME);
  173. rtTitle.right = rtWnd.right - rtWnd.left - GetSystemMetrics(SM_CXFRAME);
  174. rtTitle.bottom = rtTitle.top + GetSystemMetrics(SM_CYSIZE);
  175. CBitmap*  BackBitmap;
  176. COLORREF TitleTextColor;
  177. UINT     DrawMode;
  178. if(bActive)
  179. {
  180. BackBitmap = m_TitleBarBitmap[BMP_ACTIVE];
  181. DrawMode = m_TitleBarBitmapDrawMode[BMP_ACTIVE];
  182. TitleTextColor = GetSysColor(COLOR_CAPTIONTEXT);
  183. }
  184. else
  185. {
  186. BackBitmap = m_TitleBarBitmap[BMP_INACTIVE];
  187. DrawMode = m_TitleBarBitmapDrawMode[BMP_INACTIVE];
  188. TitleTextColor = GetSysColor(COLOR_INACTIVECAPTIONTEXT);
  189. }
  190. if(BackBitmap == NULL)return FALSE;
  191. //用位图画标题栏
  192. CRTDraw::RTDrawBitmap(pDC,&rtTitle,BackBitmap,DrawMode);
  193. //画标题栏图标
  194. HICON hIcon =  (HICON)DefWindowProc(hWnd,WM_GETICON,ICON_SMALL,0);
  195. if(hIcon != NULL)
  196. {
  197. DrawIconEx(pDC->m_hDC,rtTitle.left+1,rtTitle.top+1,hIcon,rtTitle.Height() - 2,rtTitle.Height() - 2,0,NULL,DI_NORMAL);
  198. }
  199. //画标题栏文字
  200. CFont TitleFont;
  201. NONCLIENTMETRICS nif;
  202. nif.cbSize = sizeof(nif);
  203. SystemParametersInfo(SPI_GETNONCLIENTMETRICS,sizeof(NONCLIENTMETRICS),&nif,0);
  204. TitleFont.CreateFontIndirect(&nif.lfCaptionFont);
  205. CFont *pOldFont = pDC->SelectObject(&TitleFont);
  206. char bufText[1024];
  207. int nText = GetWindowText(hWnd,bufText,1024);
  208. bufText[nText] = NULL;
  209. CString titleText = bufText; 
  210. CRect rtTitleText;
  211. rtTitleText.SetRect(rtTitle.left + rtTitle.Height(),rtTitle.top,rtTitle.right - 60,rtTitle.bottom);
  212. int oldBkMode = pDC->SetBkMode(TRANSPARENT);
  213. int oldTextColor = pDC->SetTextColor(TitleTextColor);
  214. TEXTMETRIC Metrics;
  215. pDC->GetTextMetrics(&Metrics);
  216. rtTitleText.top = rtTitle.top + (rtTitle.Height() - Metrics.tmHeight)/2;
  217. pDC->DrawText(titleText,&rtTitleText,DT_LEFT | DT_SINGLELINE);
  218. pDC->SetBkMode(oldBkMode);
  219. pDC->SetTextColor(oldTextColor);
  220. pDC->SelectObject(&pOldFont);
  221. //画标题栏按扭
  222. RTDrawTitleBarButtons(pDC,hWnd);
  223. return TRUE;
  224. }
  225. BOOL CRTWnd::OnRTNcPaint(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  226. {
  227. //绘制非客户区
  228. HDC hDC = GetWindowDC(hWnd);
  229. if(hDC != NULL)
  230. {
  231. CDC *pDC = CDC::FromHandle(hDC);
  232. HRGN hrgn = NULL;
  233. CRect rtWnd;
  234. GetWindowRect(hWnd,&rtWnd);
  235. if(wParam != 1)
  236. {
  237. //设置CLIPBOX
  238. hrgn = (HRGN)wParam;
  239. ::SelectClipRgn(pDC->m_hDC,hrgn);
  240. ::OffsetClipRgn(pDC->m_hDC,0 - rtWnd.left,0 - rtWnd.top);
  241. }
  242. else if(wParam == 1)
  243. {
  244. hrgn = CreateRectRgn(rtWnd.left,rtWnd.top,rtWnd.right,rtWnd.bottom);
  245. ::SelectClipRgn(pDC->m_hDC,hrgn);
  246. ::OffsetClipRgn(pDC->m_hDC,0 - rtWnd.left,0 - rtWnd.top);
  247. }
  248. RTDrawFrameBorder(pDC,hrgn,hWnd);
  249. RTDrawTitleBar(pDC,hrgn,m_bActive,hWnd);
  250. RTDrawMenuBar(pDC,hrgn,lParam,hWnd);
  251. RTDrawDefaultMenu(lParam,hrgn,hWnd,hrgn);
  252. ::SelectClipRgn(pDC->m_hDC,hrgn,NULL);
  253. DeleteObject(hrgn);
  254. if(hrgn != NULL)DeleteObject(hrgn);
  255. pDC->Detach();
  256. ReleaseDC(hWnd,hDC);
  257. }
  258. return TRUE;
  259. }
  260. BOOL CRTWnd::EnableRTStyle(BOOL bEnable)
  261. {
  262. m_bUseRTStyle = bEnable;
  263. return bEnable;
  264. }
  265. BOOL CRTWnd::OnRTNcActive(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  266. {
  267. return FALSE;
  268. }
  269. BOOL CRTWnd::OnRTActive(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  270. {
  271. *pResult = DefWindowProc(hWnd,uMsg,wParam,lParam);
  272. if(!IsWindow(hWnd))return FALSE;
  273. if(wParam == WA_INACTIVE)
  274. {
  275. m_bActive = FALSE;
  276. }
  277. else
  278. {
  279. m_bActive = TRUE;
  280. }
  281. LRESULT lResult;
  282. OnRTNcPaint(hWnd,uMsg,1,0,&lResult);
  283. return TRUE;
  284. }
  285. BOOL CRTWnd::OnRTNodify(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  286. {
  287. *pResult = DefWindowProc(hWnd,uMsg,wParam,lParam);
  288. HDC hDC = GetWindowDC(hWnd);
  289. if(hDC != NULL)
  290. {
  291. CDC *pDC = CDC::FromHandle(hDC);
  292. RTDrawTitleBar(pDC,m_bActive,hWnd);
  293. pDC->Detach();
  294. ReleaseDC(hWnd,hDC);
  295. }
  296. return TRUE;
  297. }
  298. BOOL CRTWnd::OnRTActiveApp(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  299. {
  300. *pResult = DefWindowProc(hWnd,uMsg,wParam,lParam);
  301. m_bActive = (BOOL)wParam;
  302. return TRUE;
  303. }
  304. BOOL CRTWnd::OnRTSetText(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  305. {
  306. *pResult = DefWindowProc(hWnd,uMsg,wParam,lParam);
  307. HDC hDC = GetWindowDC(hWnd);
  308. if(hDC != NULL)
  309. {
  310. CDC *pDC = CDC::FromHandle(hDC);
  311. RTDrawTitleBar(pDC,m_bActive,hWnd);
  312. pDC->Detach();
  313. ReleaseDC(hWnd,hDC);
  314. }
  315. return TRUE;
  316. }
  317. BOOL CRTWnd::SetRestoreButtonBitmap(CBitmap* RestoreButtonBitmap[],UINT DrawMode[])
  318. {
  319. for(int i = 0; i < 5; i ++)
  320. {
  321. m_RestoreButtonBitmap[i] = RestoreButtonBitmap[i];
  322. m_RestoreButtonBitmapDrawMode[i] = DrawMode[i];
  323. }
  324. return TRUE;
  325. }
  326. BOOL CRTWnd::SetExitButtonBitmap(CBitmap* ExitButtonBitmap[],UINT DrawMode[])
  327. {
  328. for(int i = 0; i < 5; i ++)
  329. {
  330. m_ExitButtonBitmap[i] = ExitButtonBitmap[i];
  331. m_ExitButtonBitmapDrawMode[i] = DrawMode[i];
  332. }
  333. return TRUE;
  334. }
  335. BOOL CRTWnd::RTDrawTitleBarButtons(CDC* pDC,HRGN hRgn,HWND hWnd)
  336. {
  337. //取得标题栏的位置
  338. if(m_bUsedDefTitleBar)return TRUE;
  339. CRect rtWnd;
  340. CRect rtTitle;
  341. GetWindowRect(hWnd,&rtWnd);
  342. rtTitle.left = GetSystemMetrics(SM_CXFRAME);
  343. rtTitle.top = GetSystemMetrics(SM_CYFRAME);
  344. rtTitle.right = rtWnd.right - rtWnd.left - GetSystemMetrics(SM_CXFRAME);
  345. rtTitle.bottom = rtTitle.top + GetSystemMetrics(SM_CYSIZE);
  346. CRect rtButtons;
  347. //画标题栏按扭
  348. DWORD dwStyle = GetWindowLong(hWnd,GWL_STYLE);
  349. CBitmap* pCloseBox = NULL;
  350. CBitmap* pMaximizeBox = NULL;
  351. CBitmap* pMinimizeBox = NULL;
  352. if(TRUE)
  353. {
  354. if(m_ExitButtonState == StateNormal)
  355. {
  356. pCloseBox = m_ExitButtonBitmap[BMP_NORMAL];
  357. }
  358. else if(m_ExitButtonState == StateDown)
  359. {
  360. pCloseBox = m_ExitButtonBitmap[BMP_DOWN];
  361. }
  362. else if(m_ExitButtonState == StateFocus)
  363. {
  364. pCloseBox = m_ExitButtonBitmap[BMP_FOCUS];
  365. }
  366. }
  367. if(WS_MAXIMIZEBOX & dwStyle)
  368. {
  369. if(IsZoomed(hWnd))
  370. {
  371. if(m_MaximizeButtonState == StateNormal)
  372. {
  373. pMaximizeBox = m_RestoreButtonBitmap[BMP_NORMAL];
  374. }
  375. else if(m_MaximizeButtonState == StateDown)
  376. {
  377. pMaximizeBox = m_RestoreButtonBitmap[BMP_DOWN];
  378. }
  379. else if(m_MaximizeButtonState == StateFocus)
  380. {
  381. pMaximizeBox = m_RestoreButtonBitmap[BMP_FOCUS];
  382. }
  383. }
  384. else
  385. {
  386. if(m_MaximizeButtonState == StateNormal)
  387. {
  388. pMaximizeBox = m_MaximizeButtonBitmap[BMP_NORMAL];
  389. }
  390. else if(m_MaximizeButtonState == StateDown)
  391. {
  392. pMaximizeBox = m_MaximizeButtonBitmap[BMP_DOWN];
  393. }
  394. else if(m_MaximizeButtonState == StateFocus)
  395. {
  396. pMaximizeBox = m_MaximizeButtonBitmap[BMP_FOCUS];
  397. }
  398. }
  399. }
  400. else if(WS_MINIMIZEBOX & dwStyle)
  401. {
  402. if(IsZoomed(hWnd))
  403. {
  404. pMaximizeBox = m_RestoreButtonBitmap[BMP_DISABLE];
  405. }
  406. else
  407. {
  408. pMaximizeBox = m_MaximizeButtonBitmap[BMP_DISABLE];
  409. }
  410. }
  411. if(WS_MINIMIZEBOX & dwStyle)
  412. {
  413. if(m_MinimizeButtonState == StateNormal)
  414. {
  415. pMinimizeBox = m_MinimizeButtonBitmap[BMP_NORMAL];
  416. }
  417. else if(m_MinimizeButtonState == StateDown)
  418. {
  419. pMinimizeBox = m_MinimizeButtonBitmap[BMP_DOWN];
  420. }
  421. else if(m_MinimizeButtonState == StateFocus)
  422. {
  423. pMinimizeBox = m_MinimizeButtonBitmap[BMP_FOCUS];
  424. }
  425. }
  426. else if(WS_MAXIMIZEBOX & dwStyle)
  427. {
  428. pMinimizeBox = m_MinimizeButtonBitmap[BMP_DISABLE];
  429. }
  430. //重画关闭button
  431. if(dwStyle & WS_SYSMENU)
  432. {
  433. rtButtons.left = rtTitle.right - rtTitle.Height();
  434. rtButtons.top = rtTitle.top + 2;
  435. rtButtons.right = rtTitle.right - 2;
  436. rtButtons.bottom = rtTitle.bottom - 2;
  437. CRTDraw::RTDrawBitmap(pDC,&rtButtons,pCloseBox,CRTDraw::DrawModeAllStretch);
  438. }
  439. //重画最大化/恢复button
  440. if(dwStyle & WS_MAXIMIZEBOX || dwStyle & WS_MINIMIZEBOX)
  441. {
  442. rtButtons.right = rtButtons.left - 2;
  443. rtButtons.left = rtButtons.left - rtTitle.Height();
  444. CRTDraw::RTDrawBitmap(pDC,&rtButtons,pMaximizeBox,CRTDraw::DrawModeAllStretch);
  445. }
  446. //重画最小化button
  447. if(dwStyle & WS_MAXIMIZEBOX || dwStyle & WS_MINIMIZEBOX)
  448. {
  449. rtButtons.right = rtButtons.left;
  450. rtButtons.left = rtButtons.left - rtTitle.Height() + 2;
  451. CRTDraw::RTDrawBitmap(pDC,&rtButtons,pMinimizeBox,CRTDraw::DrawModeAllStretch);
  452. }
  453. return TRUE;
  454. }
  455. BOOL CRTWnd::OnRTSetIcon(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  456. {
  457. *pResult = DefWindowProc(hWnd,uMsg,wParam,lParam);
  458. if(IsWindow(hWnd))
  459. {
  460. HDC hDC = GetWindowDC(hWnd);
  461. if(hDC != NULL)
  462. {
  463. CDC *pDC = CDC::FromHandle(hDC);
  464. RTDrawTitleBar(pDC,m_bActive,hWnd);
  465. pDC->Detach();
  466. ReleaseDC(hWnd,hDC);
  467. }
  468. }
  469. return TRUE;
  470. }
  471. BOOL CRTWnd::OnRTNcMouseMove(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  472. {
  473. if(m_bUsedDefTitleBar)return FALSE;
  474. *pResult = 0;
  475. HDC hDC = GetWindowDC(hWnd);
  476. if(hDC == NULL)return TRUE;
  477. CDC* pDC = CDC::FromHandle(hDC);
  478. switch(wParam)
  479. {
  480. case HTMAXBUTTON:
  481. if(m_NcMouseState == MouseStateNormal)
  482. {
  483. if(m_MaximizeButtonState != StateFocus)
  484. {
  485. m_MaximizeButtonState = StateFocus;
  486. m_MinimizeButtonState = StateNormal;
  487. m_ExitButtonState = StateNormal;
  488. RTDrawTitleBarButtons(pDC,hWnd);
  489. }
  490. }
  491. else if(m_NcMouseState == MouseStateDown)
  492. {
  493. if(m_SelTitleButton == MaximizeButton)
  494. {
  495. if(m_MaximizeButtonState != StateDown)
  496. {
  497. m_MaximizeButtonState = StateDown;
  498. RTDrawTitleBarButtons(pDC,hWnd);
  499. }
  500. }
  501. else if(m_SelTitleButton != NoneButton)
  502. {
  503. m_MinimizeButtonState = StateNormal;
  504. m_ExitButtonState = StateNormal;
  505. RTDrawTitleBarButtons(pDC,hWnd);
  506. }
  507. }
  508. break;
  509. case HTMINBUTTON:
  510. if(m_NcMouseState == MouseStateNormal)
  511. {
  512. if(m_MinimizeButtonState != StateFocus)
  513. {
  514. m_MinimizeButtonState = StateFocus;
  515. m_MaximizeButtonState = StateNormal;
  516. m_ExitButtonState = StateNormal;
  517. RTDrawTitleBarButtons(pDC,hWnd);
  518. }
  519. }
  520. else if(m_NcMouseState == MouseStateDown)
  521. {
  522. if(m_SelTitleButton == MinimizeButton)
  523. {
  524. if(m_MinimizeButtonState != StateDown)
  525. {
  526. m_MinimizeButtonState = StateDown;
  527. RTDrawTitleBarButtons(pDC,hWnd);
  528. }
  529. }
  530. else if(m_SelTitleButton != NoneButton)
  531. {
  532. m_MaximizeButtonState = StateNormal;
  533. m_ExitButtonState = StateNormal;
  534. RTDrawTitleBarButtons(pDC,hWnd);
  535. }
  536. }
  537. break;
  538. case HTCLOSE:
  539. if(m_NcMouseState == MouseStateNormal)
  540. {
  541. if(m_ExitButtonState != StateFocus)
  542. {
  543. m_ExitButtonState = StateFocus;
  544. m_MaximizeButtonState = StateNormal;
  545. m_MinimizeButtonState = StateNormal;
  546. RTDrawTitleBarButtons(pDC,hWnd);
  547. }
  548. }
  549. else if(m_NcMouseState == MouseStateDown)
  550. {
  551. if(m_SelTitleButton == ExitButton)
  552. {
  553. if(m_ExitButtonState != StateDown)
  554. {
  555. m_ExitButtonState = StateDown;
  556. RTDrawTitleBarButtons(pDC,hWnd);
  557. }
  558. }
  559. else if(m_SelTitleButton != NoneButton)
  560. {
  561. m_MaximizeButtonState = StateNormal;
  562. m_MinimizeButtonState = StateNormal;
  563. RTDrawTitleBarButtons(pDC,hWnd);
  564. }
  565. }
  566. break;
  567. default:
  568. if(m_ExitButtonState != StateNormal || m_MinimizeButtonState != StateNormal || m_MaximizeButtonState != StateNormal)
  569. {
  570. m_MaximizeButtonState = StateNormal;
  571. m_MinimizeButtonState = StateNormal;
  572. m_ExitButtonState = StateNormal;
  573. RTDrawTitleBarButtons(pDC,hWnd);
  574. }
  575. break;
  576. }
  577. pDC->Detach();
  578. ReleaseDC(hWnd,hDC);
  579. return TRUE;
  580. }
  581. BOOL CRTWnd::OnRTNcLMouseDown(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  582. {
  583. if(m_bUsedDefTitleBar)return FALSE;
  584. HDC hDC = GetWindowDC(hWnd);
  585. if(hDC == NULL)return TRUE;
  586. CDC *pDC = CDC::FromHandle(hDC);
  587. switch(wParam)
  588. {
  589. case HTMAXBUTTON:
  590. m_NcMouseState = MouseStateDown;
  591. m_SelTitleButton = MaximizeButton;
  592. m_MaximizeButtonState = StateDown;
  593. RTDrawTitleBarButtons(pDC,hWnd);
  594. SetCapture(hWnd);
  595. break;
  596. case HTMINBUTTON:
  597. m_NcMouseState = MouseStateDown;
  598. m_SelTitleButton = MinimizeButton;
  599. m_MinimizeButtonState = StateDown;
  600. RTDrawTitleBarButtons(pDC,hWnd);
  601. SetCapture(hWnd);
  602. break;
  603. case HTCLOSE:
  604. m_NcMouseState = MouseStateDown;
  605. m_SelTitleButton = ExitButton;
  606. m_ExitButtonState = StateDown;
  607. RTDrawTitleBarButtons(pDC,hWnd);
  608. SetCapture(hWnd);
  609. break;
  610. default:
  611. pDC->Detach();
  612. ReleaseDC(hWnd,hDC);
  613. return FALSE;
  614. break;
  615. }
  616. pDC->Detach();
  617. ReleaseDC(hWnd,hDC);
  618. return TRUE;
  619. }
  620. BOOL CRTWnd::OnRTNcLMouseUp(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  621. {
  622. if(m_bUsedDefTitleBar)return FALSE;
  623. if(m_NcMouseState == MouseStateNormal)
  624. {
  625. return FALSE;
  626. }
  627. m_NcMouseState = MouseStateNormal;
  628. ReleaseCapture();
  629. pResult = 0;
  630. HDC hDC = GetWindowDC(hWnd);
  631. CDC* pDC = CDC::FromHandle(hDC);
  632. if(wParam == HTMAXBUTTON)
  633. {
  634. if(m_SelTitleButton == MaximizeButton)
  635. {
  636. m_SelTitleButton = NoneButton;
  637. m_MaximizeButtonState = StateNormal;
  638. RTDrawTitleBarButtons(pDC,hWnd);
  639. if(IsZoomed(hWnd))
  640. {
  641. SendMessage(hWnd,WM_SYSCOMMAND, SC_RESTORE, lParam);
  642. }
  643. else
  644. {
  645. SendMessage(hWnd,WM_SYSCOMMAND, SC_MAXIMIZE,lParam);
  646. }
  647. }
  648. }
  649. else if(wParam == HTMINBUTTON)
  650. {
  651. if(m_SelTitleButton == MinimizeButton)
  652. {
  653. m_SelTitleButton = NoneButton;
  654. m_MinimizeButtonState = StateNormal;
  655. RTDrawTitleBarButtons(pDC,hWnd);
  656. SendMessage(hWnd,WM_SYSCOMMAND, SC_MINIMIZE,lParam);
  657. }
  658. }
  659. else if(wParam == HTCLOSE)
  660. {
  661. if(m_SelTitleButton == ExitButton)
  662. {
  663. m_SelTitleButton = NoneButton;
  664. m_ExitButtonState = StateNormal;
  665. SendMessage(hWnd,WM_SYSCOMMAND,SC_CLOSE,lParam);
  666. }
  667. }
  668. else
  669. {
  670. m_MinimizeButtonState = StateNormal;
  671. m_MaximizeButtonState = StateNormal;
  672. m_ExitButtonState = StateNormal;
  673. pDC->Detach();
  674. ReleaseDC(hWnd,hDC);
  675. return FALSE;
  676. }
  677. pDC->Detach();
  678. ReleaseDC(hWnd,hDC);
  679. return TRUE;
  680. }
  681. BOOL CRTWnd::OnRTMouseMove(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  682. {
  683. if(m_bUsedDefTitleBar)return FALSE;
  684. if(m_NcMouseState != MouseStateNormal)
  685. {
  686. CRect rtWnd;
  687. GetWindowRect(hWnd,&rtWnd);
  688. CPoint point(lParam);
  689. ClientToScreen(hWnd,&point);
  690. UINT hit = (UINT)DefWindowProc(hWnd,WM_NCHITTEST,wParam,MAKELPARAM(point.x,point.y));
  691. return OnRTNcMouseMove(hWnd,WM_NCMOUSEMOVE,hit,MAKELPARAM(point.x,point.y),pResult);
  692. }
  693. return FALSE;
  694. }
  695. BOOL CRTWnd::OnRTLButtonUp(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  696. {
  697. if(m_bUsedDefTitleBar)return FALSE;
  698. if(m_NcMouseState != MouseStateNormal)
  699. {
  700. CRect rtWnd;
  701. GetWindowRect(hWnd,&rtWnd);
  702. CPoint point(lParam);
  703. ClientToScreen(hWnd,&point);
  704. UINT hit = (UINT)DefWindowProc(hWnd,WM_NCHITTEST,wParam,MAKELPARAM(point.x,point.y));
  705. return OnRTNcLMouseUp(hWnd,WM_NCLBUTTONUP,hit,MAKELPARAM(point.x,point.y),pResult);
  706. }
  707. return FALSE;
  708. }
  709. BOOL CRTWnd::RTDrawDefaultMenu(LPARAM lParam,HRGN hRgn,HWND hWnd)
  710. {
  711. HMENU hMenu = GetMenu(hWnd);
  712. if(hMenu == NULL)return TRUE;
  713. CRect rtMenu;
  714. CRect rtWnd;
  715. GetWindowRect(hWnd,&rtWnd);
  716. rtMenu.left = GetSystemMetrics(SM_CXFRAME);
  717. rtMenu.top = GetSystemMetrics(SM_CYSIZE) + GetSystemMetrics(SM_CXFRAME) + 1;
  718. rtMenu.right = rtWnd.Width() - GetSystemMetrics(SM_CXFRAME);
  719. rtMenu.bottom = rtMenu.top +  GetSystemMetrics(SM_CYMENU);
  720. UINT ItemCount = GetMenuItemCount(hMenu);
  721. int PrevTop = rtMenu.top;
  722. int PrevLeft = rtMenu.left;
  723. int MenuItemHeight = GetSystemMetrics(SM_CYMENU);
  724. CRect rtMenuItem;
  725. for(UINT items = 0; items < ItemCount; items++)
  726. {
  727. GetMenuItemRect(hWnd,hMenu,items,&rtMenuItem);
  728. HRGN rgn = CreateRectRgn(rtMenuItem.left,rtMenuItem.top,rtMenuItem.right,rtMenuItem.bottom);
  729. if(hRgn != NULL)
  730. {
  731. if(NULLREGION != CombineRgn(rgn,rgn,hRgn,RGN_AND))
  732. {
  733. DefWindowProc(hWnd,WM_NCPAINT,(WPARAM)(rgn),lParam);
  734. DeleteObject(rgn);
  735. }
  736. }
  737. }
  738. return TRUE;
  739. }
  740. BOOL CRTWnd::OnRTSetCursor(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  741. {
  742. if(m_bUsedDefTitleBar)return FALSE;
  743. *pResult = DefWindowProc(hWnd,uMsg,wParam,lParam);
  744. UINT uHitTest = LOWORD(lParam);
  745. HDC hDC = GetWindowDC(hWnd);
  746. CDC *pDC = CDC::FromHandle(hDC);
  747. switch(uHitTest)
  748. {
  749. case HTBORDER:
  750. case HTLEFT:
  751. case HTRIGHT:
  752. case HTTOP:
  753. case HTBOTTOM:
  754. case HTBOTTOMLEFT:
  755. case HTBOTTOMRIGHT:
  756. case HTTOPLEFT:
  757. case HTTOPRIGHT:
  758. RTDrawTitleBar(pDC,m_bActive,hWnd);
  759. break;
  760. default:
  761. break;
  762. }
  763. pDC->Detach();
  764. ReleaseDC(hWnd,hDC);
  765. return TRUE;
  766. }
  767. BOOL CRTWnd::OnRTSysCommand(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  768. {
  769. if(m_bUsedDefTitleBar)return FALSE;
  770. HDC hDC = GetWindowDC(hWnd);
  771. if(hDC != NULL)
  772. {
  773. CDC *pDC = CDC::FromHandle(hDC);
  774. RTDrawTitleBar(pDC,m_bActive,hWnd);
  775. pDC->Detach();
  776. ReleaseDC(hWnd,hDC);
  777. }
  778. return FALSE;
  779. }
  780. BOOL CRTWnd::OnRTNcHitTest(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  781. {
  782. return FALSE;
  783. }
  784. BOOL CRTWnd::OnRTInitMenuPop(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  785. {
  786. if(m_bUsedDefTitleBar)return FALSE;
  787. HDC hDC = GetWindowDC(hWnd);
  788. if(hDC != NULL)
  789. {
  790. CDC *pDC = CDC::FromHandle(hDC);
  791. RTDrawTitleBar(pDC,m_bActive,hWnd);
  792. pDC->Detach();
  793. ReleaseDC(hWnd,hDC);
  794. }
  795. return FALSE;
  796. }
  797. BOOL CRTWnd::RTWindowProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,LRESULT* pResult)
  798. {
  799. BOOL bPass = FALSE;
  800. if(!m_bUseRTStyle)return bPass;
  801. switch(uMsg)
  802. {
  803. case WM_NCPAINT:
  804. bPass = OnRTNcPaint(hWnd,uMsg,wParam,lParam,pResult);
  805. break;
  806. case WM_NCMOUSEMOVE:
  807. bPass = OnRTNcMouseMove(hWnd,uMsg,wParam,lParam,pResult);
  808. break;
  809. case WM_NCLBUTTONDOWN:
  810. bPass = OnRTNcLMouseDown(hWnd,uMsg,wParam,lParam,pResult);
  811. break;
  812. case WM_NCLBUTTONUP:
  813. bPass = OnRTNcLMouseUp(hWnd,uMsg,wParam,lParam,pResult);
  814. break;
  815. case WM_NCACTIVATE:
  816. bPass = OnRTNcActive(hWnd,uMsg,wParam,lParam,pResult);
  817. break;
  818. case WM_ACTIVATE:
  819. bPass = OnRTActive(hWnd,uMsg,wParam,lParam,pResult);
  820. break;
  821. case WM_ACTIVATEAPP:
  822. bPass = OnRTActiveApp(hWnd,uMsg,wParam,lParam,pResult);
  823. break;
  824. case WM_SYSCOMMAND:
  825. bPass = OnRTSysCommand(hWnd,uMsg,wParam,lParam,pResult);
  826. break;
  827. case WM_INITMENUPOPUP:
  828. bPass = OnRTInitMenuPop(hWnd,uMsg,wParam,lParam,pResult);
  829. break;
  830. case WM_NCHITTEST:
  831. bPass = OnRTNcHitTest(hWnd,uMsg,wParam,lParam,pResult);
  832. break;
  833. case WM_SETCURSOR:
  834. bPass = OnRTSetCursor(hWnd,uMsg,wParam,lParam,pResult);
  835. break;
  836. default:
  837. break;
  838. }
  839. return bPass;
  840. }