SkinDialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // DlgControlItem.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "SkinDialog.h"
  5. #include "RenderManager.h"
  6. #include "SkinComboBox.h"
  7. // CDlgControlItem 对话框
  8. //边框宽度
  9. CONST INT BORDERWIDTH = 3;
  10. IMPLEMENT_DYNAMIC(CSkinDialog, CDialog)
  11. CSkinDialog::CSkinDialog(UINT nIDTemplate,LPCTSTR lpszFileName/*=NULL*/,CWnd* pParent /*=NULL*/)
  12. : CDialog(nIDTemplate, pParent)
  13. {
  14. m_hMemDC = NULL;
  15. m_hMemBmp = m_hOldBmp = NULL;
  16. m_bClip = false;
  17. if( lpszFileName != NULL )
  18. ParseWindow(lpszFileName);
  19. else m_bDefaultSkin = true;
  20. m_rcWindowSizeNow.SetRectEmpty();
  21. }
  22. CSkinDialog::~CSkinDialog()
  23. {
  24. if (m_hMemDC != NULL && m_hMemBmp != NULL)
  25. {
  26. ::SelectObject(m_hMemDC, m_hOldBmp);
  27. ::DeleteObject(m_hMemBmp);
  28. ::DeleteDC(m_hMemDC);
  29. }
  30. m_hMemDC = NULL;
  31. m_hMemBmp = m_hOldBmp = NULL;
  32. }
  33. void CSkinDialog::DoDataExchange(CDataExchange* pDX)
  34. {
  35. CDialog::DoDataExchange(pDX);
  36. }
  37. BEGIN_MESSAGE_MAP(CSkinDialog, CDialog)
  38. ON_WM_PAINT()
  39. ON_WM_NCHITTEST()
  40. ON_WM_SIZE()
  41. ON_WM_GETMINMAXINFO()
  42. ON_WM_ERASEBKGND()
  43. ON_WM_NCCALCSIZE()
  44. ON_WM_NCACTIVATE()
  45. ON_WM_SYSCOMMAND()
  46. //ON_WM_WINDOWPOSCHANGING()
  47. END_MESSAGE_MAP()
  48. // CDlgControlItem 消息处理程序
  49. void CSkinDialog::OnPaint()
  50. {
  51. CPaintDC dc(this); // device context for painting
  52. CRect rcClient;
  53. GetClientRect(&rcClient);
  54. CMemoryDC BufferDC(&dc,rcClient);
  55. int cx = 0, cy = 0;
  56. if (m_hMemBmp != NULL)
  57. {
  58. BITMAP bmpInfo = {0};
  59. ::GetObject(m_hMemBmp, sizeof(BITMAP), &bmpInfo);
  60. cx = bmpInfo.bmWidth;
  61. cy = bmpInfo.bmHeight;
  62. }
  63. if (rcClient.Width() != cx || rcClient.Height() != cy)
  64. {
  65. if (m_hMemDC != NULL && m_hMemBmp != NULL)
  66. {
  67. ::SelectObject(m_hMemDC, m_hOldBmp);
  68. ::DeleteObject(m_hMemBmp);
  69. }
  70. m_hMemBmp = ::CreateCompatibleBitmap(BufferDC.GetSafeHdc(), rcClient.Width(), rcClient.Height());
  71. m_hOldBmp = (HBITMAP)::SelectObject(m_hMemDC, m_hMemBmp);
  72. }
  73. ::SetBkMode(m_hMemDC,TRANSPARENT);
  74. RenderEngine->DrawColor(m_hMemDC,rcClient,m_colBK);
  75. CDC*pDC = CDC::FromHandle(m_hMemDC);
  76. DrawClientArea(pDC,rcClient.Width(), rcClient.Height());
  77. DrawImage(pDC);
  78. DrawString(pDC);
  79. ::BitBlt(BufferDC.m_hDC, 0, 0, rcClient.Width(), rcClient.Height(), m_hMemDC, 0, 0, SRCCOPY);
  80. }
  81. BOOL CSkinDialog::OnInitDialog()
  82. {
  83. CDialog::OnInitDialog();
  84. SetOwnHwnd(GetSafeHwnd());
  85. HDC hDC = ::GetDC(m_hWnd);
  86. m_hMemDC = ::CreateCompatibleDC(hDC);
  87. m_hMemBmp = ::CreateCompatibleBitmap(hDC, m_szWindowSize.cx,m_szWindowSize.cy);
  88. m_hOldBmp = (HBITMAP)::SelectObject(m_hMemDC, m_hMemBmp);
  89. ::ReleaseDC(m_hWnd, hDC);
  90. SetExtrude(m_bExtrude);
  91. SetWindowPos(NULL,0,0,m_szWindowSize.cx,m_szWindowSize.cy,SWP_NOMOVE);
  92. ParseControl(this);
  93. RectifyControl(m_szWindowSize.cx,m_szWindowSize.cy);
  94. return TRUE; // return TRUE unless you set the focus to a control
  95. // 异常: OCX 属性页应返回 FALSE
  96. }
  97. LRESULT CSkinDialog::OnNcHitTest(CPoint point)
  98. {
  99. if( m_bExtrude )
  100. {
  101. CRect rcWindow;
  102. GetWindowRect(&rcWindow);
  103. if ((point.x <= rcWindow.left+BORDERWIDTH) && (point.y>BORDERWIDTH) && (point.y<rcWindow.bottom-BORDERWIDTH*2) )
  104. return HTLEFT;
  105. else if ((point.x >= rcWindow.right-BORDERWIDTH) && (point.y>BORDERWIDTH) && (point.y<rcWindow.bottom-BORDERWIDTH*2) )
  106. return HTRIGHT;
  107. else if ((point.y <= rcWindow.top+BORDERWIDTH) && (point.x>BORDERWIDTH) && (point.x<rcWindow.right-BORDERWIDTH*2))
  108. return HTTOP;
  109. else if ((point.y >= rcWindow.bottom-BORDERWIDTH) && (point.x>BORDERWIDTH) && (point.x<rcWindow.right-BORDERWIDTH*2))
  110. return HTBOTTOM;
  111. else if ((point.x <= rcWindow.left+BORDERWIDTH*2) && (point.y <= rcWindow.top+BORDERWIDTH*2))
  112. return HTTOPLEFT;
  113. else if ((point.x >= rcWindow.right-BORDERWIDTH*2) && (point.y <= rcWindow.top+BORDERWIDTH*2))
  114. return HTTOPRIGHT;
  115. else if ((point.x <= rcWindow.left+BORDERWIDTH*2) && (point.y >= rcWindow.bottom-BORDERWIDTH*2))
  116. return HTBOTTOMLEFT;
  117. else if ((point.x >= rcWindow.right-BORDERWIDTH*2) && (point.y >= rcWindow.bottom-BORDERWIDTH*2))
  118. return HTBOTTOMRIGHT;
  119. return CWnd::OnNcHitTest(point);
  120. }
  121. else
  122. return CDialog::OnNcHitTest(point);
  123. }
  124. void CSkinDialog::SetExtrude( bool bExtrude )
  125. {
  126. m_bExtrude = bExtrude;
  127. }
  128. BOOL CALLBACK CSkinDialog::EnumChildProc( HWND hWndChild, LPARAM lParam )
  129. {
  130. //获取位置
  131. CRect rcWindow;
  132. ::GetWindowRect(hWndChild, &rcWindow);
  133. //创建区域
  134. if ((rcWindow.Width()>0)&&(rcWindow.Height()>0))
  135. {
  136. //变量定义
  137. ASSERT(lParam!=0L);
  138. CWnd * pEnumChildInfo=(CWnd *)lParam;
  139. //窗口判断
  140. HWND hWndParent=::GetParent(hWndChild);
  141. if (hWndParent!=pEnumChildInfo->GetSafeHwnd())
  142. {
  143. return TRUE;
  144. }
  145. static DWORD time = GetTickCount();
  146. if ( GetTickCount() - time > 33)
  147. {
  148. pEnumChildInfo->Invalidate(FALSE);
  149. time = GetTickCount();
  150. }
  151. }
  152. return TRUE;
  153. }
  154. void CSkinDialog::SetClipChild( bool bClip )
  155. {
  156. m_bClip = bClip;
  157. }
  158. void CSkinDialog::OnSize(UINT nType, int cx, int cy)
  159. {
  160. __super::OnSize(nType, cx, cy);
  161. if ( m_bClip )
  162. {
  163. //EnumChildWindows(GetSafeHwnd(),EnumChildProc,(LPARAM)(CWnd*)this);
  164. }
  165. //设置圆角
  166. if( (m_szRcSize.cx !=0) && (m_szRcSize.cy != 0) )
  167. {
  168. #if defined(WIN32) && !defined(UNDER_CE)
  169. if( !IsIconic() && (m_szRcSize.cx != 0 || m_szRcSize.cy != 0) )
  170. {
  171. CRect rcWnd;
  172. GetWindowRect(&rcWnd);
  173. rcWnd.OffsetRect(-rcWnd.left, -rcWnd.top);
  174. rcWnd.right++; rcWnd.bottom++;
  175. HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, m_szRcSize.cx, m_szRcSize.cy);
  176. SetWindowRgn(hRgn, TRUE);
  177. DeleteObject(hRgn);
  178. }
  179. #endif
  180. }
  181. RectifyControl(cx, cy);
  182. if( !m_bDefaultSkin )
  183. OnNcPaint();
  184. }
  185. BOOL CSkinDialog::PreTranslateMessage(MSG* pMsg)
  186. {
  187. if ( pMsg->message == WM_KEYDOWN )
  188. {
  189. if ( pMsg->wParam == VK_ESCAPE || pMsg->wParam == VK_RETURN)
  190. {
  191. return TRUE;
  192. }
  193. }
  194. return __super::PreTranslateMessage(pMsg);
  195. }
  196. void CSkinDialog::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
  197. {
  198. if( m_bDefaultSkin ) return __super::OnGetMinMaxInfo(lpMMI);
  199. MONITORINFO oMonitor = {};
  200. oMonitor.cbSize = sizeof(oMonitor);
  201. ::GetMonitorInfo(::MonitorFromWindow(*this, MONITOR_DEFAULTTONEAREST), &oMonitor);
  202. CRect rcWork = oMonitor.rcWork;
  203. CRect rcMonitor = oMonitor.rcMonitor;
  204. rcWork.OffsetRect(-oMonitor.rcMonitor.left, -oMonitor.rcMonitor.top);
  205. // 计算最大化时,正确的原点坐标
  206. lpMMI->ptMaxPosition.x = rcWork.left;
  207. lpMMI->ptMaxPosition.y = rcWork.top;
  208. lpMMI->ptMaxTrackSize.x = m_szMaxSize.cx==0 ? rcWork.Width() : m_szMaxSize.cx;
  209. lpMMI->ptMaxTrackSize.y = m_szMaxSize.cy==0 ? rcWork.Height() : m_szMaxSize.cy;
  210. lpMMI->ptMinTrackSize.x =m_szMinSize.cx;
  211. lpMMI->ptMinTrackSize.y =m_szMinSize.cy;
  212. __super::OnGetMinMaxInfo(lpMMI);
  213. }
  214. void CSkinDialog::RectifyControl( int cx, int cy )
  215. {
  216. //移动准备
  217. HDWP hDwp=BeginDeferWindowPos(64);
  218. UINT uFlags=SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOZORDER;
  219. for (int i=0;i<(int)m_ControlArray.size();i++)
  220. {
  221. ISkinControl*pControl = m_ControlArray[i];
  222. if ( pControl != NULL )
  223. {
  224. CRect rcPos;
  225. //////////////////////////////位置////////////////////////////////////////////
  226. if (pControl->m_FixedPostion[0] == en_LTop )
  227. {
  228. rcPos.left = pControl->m_ptPosition.x;
  229. rcPos.top = pControl->m_ptPosition.y;
  230. }
  231. else if (pControl->m_FixedPostion[0] == en_RTop )
  232. {
  233. rcPos.left = cx-pControl->m_ptPosition.x;
  234. rcPos.top = pControl->m_ptPosition.y;
  235. }
  236. else if (pControl->m_FixedPostion[0] == en_LBottom )
  237. {
  238. rcPos.left = pControl->m_ptPosition.x;
  239. rcPos.top = cy-pControl->m_ptPosition.y;
  240. }
  241. else if (pControl->m_FixedPostion[0] == en_RBottom )
  242. {
  243. rcPos.left = cx-pControl->m_ptPosition.x;
  244. rcPos.top = cy-pControl->m_ptPosition.y;
  245. }
  246. ///////////////////////////////大小///////////////////////////////////////////
  247. if (pControl->m_FixedPostion[1] == en_LTop )
  248. {
  249. rcPos.right = pControl->m_szSize.cx;
  250. rcPos.bottom = pControl->m_szSize.cy;
  251. }
  252. else if (pControl->m_FixedPostion[1] == en_RTop )
  253. {
  254. rcPos.right = cx-pControl->m_szSize.cx;
  255. rcPos.bottom = pControl->m_szSize.cy;
  256. }
  257. else if (pControl->m_FixedPostion[1] == en_LBottom )
  258. {
  259. rcPos.right = pControl->m_szSize.cx;
  260. rcPos.bottom = cy-pControl->m_szSize.cy;
  261. }
  262. else if (pControl->m_FixedPostion[1] == en_RBottom )
  263. {
  264. rcPos.right = cx-pControl->m_szSize.cx;
  265. rcPos.bottom = cy-pControl->m_szSize.cy;
  266. }
  267. DeferWindowPos(hDwp,pControl->m_pOwnWnd->GetSafeHwnd(),NULL,rcPos.left,rcPos.top,rcPos.Width(),rcPos.Height(),SWP_NOACTIVATE);
  268. }
  269. }
  270. //结束调整
  271. LockWindowUpdate();
  272. EndDeferWindowPos(hDwp);
  273. UnlockWindowUpdate();
  274. ////重新设置相应的属性,个别属性在设置位置前设置是不好使的
  275. for (int i=0;i<(int)m_ControlArray.size();i++)
  276. {
  277. ISkinControl*pControl = m_ControlArray[i];
  278. if ( pControl != NULL )
  279. {
  280. if(pControl->m_bTransparent)
  281. pControl->SetParentBack(m_hMemDC);
  282. TCHAR szClassName[MAX_PATH]={0};
  283. GetClassName(pControl->m_pOwnWnd->GetSafeHwnd(),szClassName,_countof(szClassName));
  284. if ( _tcscmp(TEXT("ComboBox"),szClassName) == 0 )
  285. {
  286. CSkinComboBox*pComboBox = (CSkinComboBox*)pControl->m_pOwnWnd;
  287. pComboBox->SetItemHeight(-1,pComboBox->GetEditItemHeight());
  288. }
  289. }
  290. }
  291. GetWindowRect(&m_rcWindowSizeNow);
  292. Invalidate(FALSE);
  293. }
  294. BOOL CSkinDialog::OnEraseBkgnd(CDC* pDC)
  295. {
  296. return TRUE;
  297. }
  298. BOOL CSkinDialog::OnCommand(WPARAM wParam, LPARAM lParam)
  299. {
  300. switch ( LOWORD(wParam) )
  301. {
  302. case 5000:
  303. PostQuitMessage(0);
  304. break;
  305. case 5001:
  306. ShowWindow(SW_MINIMIZE);
  307. break;
  308. }
  309. return __super::OnCommand(wParam, lParam);
  310. }
  311. void CSkinDialog::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp)
  312. {
  313. if( m_bDefaultSkin ) return __super::OnNcCalcSize(bCalcValidRects,lpncsp);
  314. //最大化时,计算当前显示器最适合宽高度
  315. if ( IsZoomed())
  316. {
  317. MONITORINFO oMonitor = {};
  318. oMonitor.cbSize = sizeof(oMonitor);
  319. ::GetMonitorInfo(::MonitorFromWindow(*this, MONITOR_DEFAULTTONEAREST), &oMonitor);
  320. CRect rcWork = oMonitor.rcWork;
  321. CRect rcMonitor = oMonitor.rcMonitor;
  322. rcWork.OffsetRect(-oMonitor.rcMonitor.left, -oMonitor.rcMonitor.top);
  323. lpncsp->rgrc[0].left += GetSystemMetrics(SM_CXFRAME);
  324. lpncsp->rgrc[0].top += GetSystemMetrics(SM_CYSIZEFRAME);
  325. lpncsp->rgrc[0].right = lpncsp->rgrc[0].left + rcWork.Width();
  326. lpncsp->rgrc[0].bottom = lpncsp->rgrc[0].top + rcWork.Height();
  327. }
  328. return;
  329. }
  330. BOOL CSkinDialog::OnNcActivate(BOOL bActive)
  331. {
  332. if( !m_bDefaultSkin )
  333. {
  334. //OnNcPaint(); 不能直接调用该方法,Windows采用消息队列形式进行事件处理,直接调用未能将该消息增加到消息队列,所以会出现失去焦点时,出现大粗边框的情况
  335. SendMessage(WM_NCPAINT);
  336. return TRUE;
  337. }
  338. return __super::OnNcActivate(bActive);
  339. }
  340. LRESULT CSkinDialog::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  341. {
  342. if( !m_bDefaultSkin )
  343. {
  344. if(message == 0x00AE || // WM_NCUAHDRAWCAPTION
  345. message == 0x00AF ) // WM_NCUAHDRAWFRAME
  346. {
  347. return WM_NCPAINT;
  348. }
  349. }
  350. return __super::WindowProc(message, wParam, lParam);
  351. }
  352. void CSkinDialog::OnSysCommand(UINT nID, LPARAM lParam)
  353. {
  354. //最大化显示
  355. if ( nID == SC_MAXIMIZE )
  356. {
  357. SetMaxWindow();
  358. }
  359. else if ( nID == SC_RESTORE )
  360. {
  361. SetWindowPos(NULL,m_rcWindowSizeNow.left,m_rcWindowSizeNow.top,m_rcWindowSizeNow.Width(),m_rcWindowSizeNow.Height(),SWP_NOACTIVATE);
  362. }
  363. __super::OnSysCommand(nID, lParam);
  364. }
  365. void CSkinDialog::SetMaxWindow()
  366. {
  367. //CRect rcWork(0,0,0,0);
  368. //::SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWork, 0);
  369. //MoveWindow(&rcWork);
  370. MONITORINFO oMonitor = {};
  371. oMonitor.cbSize = sizeof(oMonitor);
  372. ::GetMonitorInfo(::MonitorFromWindow(*this, MONITOR_DEFAULTTONEAREST), &oMonitor);
  373. CRect rcWork = oMonitor.rcWork;
  374. CRect rcMonitor = oMonitor.rcMonitor;
  375. rcWork.OffsetRect(-oMonitor.rcMonitor.left, -oMonitor.rcMonitor.top);
  376. SetWindowPos(NULL,rcWork.left,rcWork.top,rcWork.Width(),rcWork.Height(),SWP_NOACTIVATE);
  377. }