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

界面编程

开发平台:

Visual C++

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