WndEx.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. // WndEx.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "WndEx.h"
  6. #include ".\wndex.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CWndEx
  14. #define CLOSE_WIDTH 12
  15. #define CLOSE_HEIGHT 11
  16. #define CLOSE_BORDER 2
  17. #define TIMER_AUTO_MAX 1
  18. #define TIMER_AUTO_MIN 2
  19. CWndEx::CWndEx()
  20. {
  21. m_bResizable = true;
  22. m_bMouseDownOnClose = false;
  23. m_bMouseOverClose = false;
  24. m_bMouseDownOnCaption = false;
  25. m_bMouseOverMinimize = false;
  26. m_bMouseDownOnMinimize = false;
  27. m_bMinimized = false;
  28. m_bMaxSetTimer = false;
  29. m_lTopBorder = BORDER;
  30. m_lRightBorder = BORDER;
  31. m_lBottomBorder = BORDER;
  32. m_lLeftBorder = BORDER;
  33. SetCaptionColorActive(false, theApp.GetConnectCV());
  34. }
  35. CWndEx::~CWndEx()
  36. {
  37. }
  38. void CWndEx::InvalidateNc()
  39. {
  40. ::RedrawWindow( m_hWnd, NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_NOCHILDREN );
  41. }
  42. void CWndEx::GetWindowRectEx(LPRECT lpRect)
  43. {
  44. if(m_bMinimized)
  45. {
  46. *lpRect = m_crFullSizeWindow;
  47. return;
  48. }
  49. CWnd::GetWindowRect(lpRect);
  50. }
  51. bool CWndEx::SetCaptionColors( COLORREF left, COLORREF right )
  52. {
  53. if( left == m_CaptionColorLeft || right == m_CaptionColorRight )
  54. return false;
  55. m_CaptionColorLeft = left;
  56. m_CaptionColorRight = right;
  57. return true;
  58. }
  59. bool CWndEx::SetCaptionColorActive(bool bActive, bool ConnectedToClipboard)
  60. {
  61. bool bResult;
  62. if(ConnectedToClipboard == false)
  63. {
  64. bResult = SetCaptionColors(RGB(255, 0, 0), RGB(255, 0, 0));
  65. }
  66. else
  67. {
  68. if(bActive)
  69. bResult = SetCaptionColors( ::GetSysColor(COLOR_ACTIVECAPTION), ::GetSysColor(COLOR_GRADIENTACTIVECAPTION) );
  70. else
  71. bResult = SetCaptionColors( ::GetSysColor(COLOR_INACTIVECAPTION), ::GetSysColor(COLOR_GRADIENTINACTIVECAPTION) );
  72. }
  73. return bResult;
  74. }
  75. BEGIN_MESSAGE_MAP(CWndEx, CWnd)
  76. //{{AFX_MSG_MAP(CWndEx)
  77. ON_WM_CREATE()
  78. ON_WM_NCPAINT()
  79. ON_WM_NCCALCSIZE()
  80. ON_WM_NCHITTEST()
  81. ON_WM_NCLBUTTONDOWN()
  82. ON_WM_NCMOUSEMOVE()
  83. ON_WM_NCLBUTTONUP()
  84. ON_WM_ERASEBKGND()
  85. ON_WM_TIMER()
  86. ON_WM_WINDOWPOSCHANGING()
  87. ON_WM_INITMENUPOPUP()
  88. //}}AFX_MSG_MAP
  89. ON_WM_SIZE()
  90. END_MESSAGE_MAP()
  91. /////////////////////////////////////////////////////////////////////////////
  92. // CWndEx message handlers
  93. BOOL CWndEx::Create(const CRect& crStart, CWnd* pParentWnd)
  94. {
  95. WNDCLASS wc;
  96. wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  97. wc.lpfnWndProc = AfxWndProc;
  98. wc.cbClsExtra = 0;
  99. wc.cbWndExtra = 0;
  100. wc.hInstance = AfxGetInstanceHandle();
  101. wc.hIcon = NULL;
  102. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  103. wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  104. wc.lpszMenuName = NULL;
  105. wc.lpszClassName = "QPasteClass";
  106. // Create the QPaste window class
  107. if (!AfxRegisterClass(&wc))
  108. return FALSE;
  109. return CWndEx::CreateEx(0, "QPasteClass", "Quick Paste", WS_POPUP,
  110. crStart, pParentWnd, 0);
  111. }
  112. int CWndEx::OnCreate(LPCREATESTRUCT lpCreateStruct)
  113. {
  114. if (CWnd::OnCreate(lpCreateStruct) == -1)
  115. return -1;
  116. m_TitleFont.CreateFont(14,0,-900,0,400,FALSE,FALSE,0,ANSI_CHARSET,
  117. OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
  118. DEFAULT_PITCH|FF_SWISS,"Arial");
  119. m_HorFont.CreateFont(14,0,0,0,400,FALSE,FALSE,0,ANSI_CHARSET,
  120. OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
  121. DEFAULT_PITCH|FF_SWISS,"Arial");
  122. SetCaptionOn(CGetSetOptions::GetCaptionPos(), true);
  123. SetAutoHide(CGetSetOptions::GetAutoHide());
  124. return 0;
  125. }
  126. void CWndEx::SetAutoHide(BOOL bAutoHide)
  127. {
  128. if(bAutoHide)
  129. {
  130. SetTimer(TIMER_AUTO_MIN, 500, NULL);
  131. }
  132. else
  133. {
  134. KillTimer(TIMER_AUTO_MIN);
  135. }
  136. }
  137. void CWndEx::SetCaptionOn(int nPos, bool bOnstartup)
  138. {
  139. m_lTopBorder = BORDER;
  140. m_lRightBorder = BORDER;
  141. m_lBottomBorder = BORDER;
  142. m_lLeftBorder = BORDER;
  143. if(nPos == CAPTION_RIGHT)
  144. m_lRightBorder = CAPTION_BORDER;
  145. if(nPos == CAPTION_BOTTOM)
  146. m_lBottomBorder = CAPTION_BORDER;
  147. if(nPos == CAPTION_LEFT)
  148. m_lLeftBorder = CAPTION_BORDER;
  149. if(nPos == CAPTION_TOP)
  150. m_lTopBorder = CAPTION_BORDER;
  151. SetRegion();
  152. if(!bOnstartup)
  153. {
  154. SetWindowPos (NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
  155. }
  156. RedrawWindow();
  157. }
  158. void CWndEx::OnNcPaint()
  159. {
  160. CWindowDC dc(this);
  161. CRect rcFrame;
  162. GetWindowRect(rcFrame);
  163. ScreenToClient(rcFrame);
  164. CRect rc;
  165. GetClientRect(rc);
  166. ClientToScreen(rc);
  167. long lWidth = rcFrame.Width();
  168. // Draw the window border
  169. CRect rcBorder(0, 0, lWidth, rcFrame.Height());
  170. COLORREF left = m_CaptionColorLeft;
  171. COLORREF right = m_CaptionColorRight;
  172. dc.Draw3dRect(rcBorder, left, left);
  173. rcBorder.DeflateRect(1, 1, 1, 1);
  174. dc.Draw3dRect(rcBorder, left, left);
  175. rcBorder.InflateRect(1, 1, 1, 1);
  176. BOOL bVertical;
  177. if(m_lRightBorder == CAPTION_BORDER)
  178. {
  179. m_crCloseBT.SetRect(rcBorder.right - m_lRightBorder + 2,
  180. 7,
  181. rcBorder.right - m_lRightBorder + 14,
  182. 18);
  183. m_crMinimizeBT.SetRect(rcBorder.right - m_lRightBorder + 2,
  184. rcBorder.bottom - 18,
  185. rcBorder.right - m_lRightBorder + 14,
  186. rcBorder.bottom - 7);
  187. rcBorder.left = rcBorder.right - m_lRightBorder;
  188. bVertical = TRUE;
  189. }
  190. else if(m_lLeftBorder == CAPTION_BORDER)
  191. {
  192. m_crCloseBT.SetRect(2,
  193. 7,
  194. 2 + 12,
  195. 7 + 11);
  196. m_crMinimizeBT.SetRect(2,
  197. rcBorder.bottom - 18,
  198. 2 + 12,
  199. rcBorder.bottom - 7);
  200. rcBorder.right = rcBorder.left + m_lLeftBorder;
  201. bVertical = TRUE;
  202. }
  203. else if(m_lTopBorder == CAPTION_BORDER)
  204. {
  205. m_crCloseBT.SetRect(rcBorder.right - 18,
  206. 3,
  207. rcBorder.right - 6,
  208. 3 + 11);
  209. m_crMinimizeBT.SetRect(4,
  210. 2,
  211. 15,
  212. 2 + 12);
  213. rcBorder.bottom = rcBorder.top + m_lTopBorder;
  214. bVertical = FALSE;
  215. }
  216. else if(m_lBottomBorder == CAPTION_BORDER)
  217. {
  218. m_crCloseBT.SetRect(rcBorder.right - 18,
  219. rcBorder.bottom - 13,
  220. rcBorder.right - 6,
  221. rcBorder.bottom - 2);
  222. m_crMinimizeBT.SetRect(4,
  223. rcBorder.bottom - 14,
  224. 15,
  225. rcBorder.bottom - 2);
  226. rcBorder.top = rcBorder.bottom - m_lBottomBorder;
  227. bVertical = FALSE;
  228. }
  229. float gR = 0;
  230. float gG = 0;
  231. float gB = 0;
  232. float sR = GetRValue(left);
  233. float sG = GetGValue(left);
  234. float sB = GetBValue(left);
  235. float eR = GetRValue(right);
  236. float eG = GetGValue(right);
  237. float eB = GetBValue(right);
  238. bool bGradient = true;
  239. if(left == right)
  240. {
  241. gR = eR;
  242. gG = eG;
  243. gB = eB;
  244. bGradient = false;
  245. }
  246. // calculate the slope for color gradient
  247. else if(bVertical)
  248. {
  249. gR = (eR - sR) / rcBorder.Height();
  250. gG = (eG - sG) / rcBorder.Height();
  251. gB = (eB - sB) / rcBorder.Height();
  252. }
  253. else
  254. {
  255. gR = (eR - sR) / rcBorder.Width();
  256. gG = (eG - sG) / rcBorder.Width();
  257. gB = (eB - sB) / rcBorder.Width();
  258. }
  259. HBRUSH color;
  260. long lHeight = rcBorder.Height();
  261. CRect cr = rcBorder;
  262. long lCount = rcBorder.Width();
  263. if(bVertical)
  264. lCount = lHeight;
  265. for(int i = 0; i < lCount; i++)
  266. {
  267. if(bVertical)
  268. {
  269. cr.top = i;
  270. cr.bottom = i + 1;
  271. }
  272. else
  273. {
  274. cr.left = i;
  275. cr.right = i + 1;
  276. }
  277. if(bGradient || i == 0)
  278. {
  279. color = CreateSolidBrush(RGB(int(gR * (float) i + gR),
  280. int(gG * (float) i + sG),
  281. int(gB * (float) i + sB)));
  282. }
  283. ::FillRect(dc, &cr, color);
  284. if(bGradient)
  285. DeleteObject(color);
  286. }
  287. if(bGradient == false)
  288. DeleteObject(color);
  289. /*
  290. HBRUSH color;
  291. color = CreateSolidBrush(left);
  292. ::FillRect(dc, &rcBorder, color);
  293. DeleteObject(color);
  294. */
  295. int nOldBKMode = dc.SetBkMode(TRANSPARENT);
  296. COLORREF oldColor = dc.SetTextColor(RGB(255, 255, 255));
  297. CFont *pOldFont = NULL;
  298. if(bVertical)
  299. pOldFont=dc.SelectObject(&m_TitleFont);
  300. else
  301. pOldFont=dc.SelectObject(&m_HorFont);
  302. CString csText;
  303. GetWindowText(csText);
  304. if(m_lRightBorder == CAPTION_BORDER)
  305. {
  306. CRect cr;
  307. cr.SetRect(rcBorder.right-1, 20, rcBorder.right - 13, rcBorder.bottom - 20);
  308. dc.DrawText(csText, cr, DT_SINGLELINE);
  309. }
  310. else if(m_lBottomBorder == CAPTION_BORDER)
  311. {
  312. CRect cr;
  313. cr.SetRect(20, rcBorder.bottom - 15, rcBorder.right - 20, rcBorder.bottom - 1);
  314. dc.DrawText(csText, cr, DT_SINGLELINE);
  315. }
  316. else if(m_lLeftBorder == CAPTION_BORDER)
  317. {
  318. CRect cr;
  319. cr.SetRect(15, 20, 2, rcBorder.bottom - 20);
  320. dc.DrawText(csText, cr, DT_SINGLELINE);
  321. }
  322. else if(m_lTopBorder == CAPTION_BORDER)
  323. {
  324. CRect cr;
  325. cr.SetRect(20, 1, rcBorder.right - 20, 15);
  326. dc.DrawText(csText, cr, DT_SINGLELINE);
  327. }
  328. DrawCloseBtn(dc, left);
  329. DrawMinimizeBtn(dc, left);
  330. dc.SelectObject(pOldFont);
  331. dc.SetTextColor(oldColor);
  332. dc.SetBkMode(nOldBKMode);
  333. }
  334. void CWndEx::DrawCloseBtn(CWindowDC &dc, COLORREF left)
  335. {
  336. if(left == 0)
  337. left = GetSysColor(COLOR_ACTIVECAPTION);
  338. //rows first then columns
  339. int Points[5][6] =
  340. {
  341. 1,1,0,0,1,1,
  342. 0,1,1,1,1,0,
  343. 0,0,1,1,0,0,
  344. 0,1,1,1,1,0,
  345. 1,1,0,0,1,1
  346. };
  347. CPoint ptShift = m_crCloseBT.TopLeft();
  348. ptShift.Offset(3, 3);
  349. COLORREF shaddow = RGB(GetRValue(left) * 1.16,
  350. GetGValue(left) * 1.12,
  351. GetBValue(left) * 1.12);
  352. if(m_bMouseDownOnClose)
  353. dc.Draw3dRect(m_crCloseBT, shaddow, RGB(255, 255, 255));
  354. else if(m_bMouseOverClose)
  355. dc.Draw3dRect(m_crCloseBT, RGB(255, 255, 255), shaddow);
  356. for (int iRow = 0; iRow < 5; iRow++)
  357. {
  358. for (int iCol = 0; iCol < 6; iCol++)
  359. {
  360. if (Points[iRow][iCol] == 1)
  361. dc.SetPixel(ptShift+CPoint(iCol, iRow), RGB(255, 255, 255));
  362. }
  363. }
  364. }
  365. void CWndEx::DrawMinimizeBtn(CWindowDC &dc, COLORREF left)
  366. {
  367. if(left == 0)
  368. left = GetSysColor(COLOR_ACTIVECAPTION);
  369. bool bTopOrBottom = false;
  370. int Points[5][8];
  371. int Points2[8][5];
  372. if(((m_lRightBorder == CAPTION_BORDER) && (m_bMinimized == false)) ||
  373. ((m_lLeftBorder == CAPTION_BORDER) && (m_bMinimized)))
  374. {
  375. int nTemp[5][8] =
  376. {
  377. 1,1,0,0,1,1,0,0,
  378. 0,1,1,0,0,1,1,0,
  379. 0,0,1,1,0,0,1,1,
  380. 0,1,1,0,0,1,1,0,
  381. 1,1,0,0,1,1,0,0
  382. };
  383. memcpy(&Points, &nTemp, sizeof(nTemp));
  384. }
  385. else if(((m_lRightBorder == CAPTION_BORDER) && (m_bMinimized)) ||
  386. ((m_lLeftBorder == CAPTION_BORDER) && (m_bMinimized == false)))
  387. {
  388. int nTemp[5][8] =
  389. {
  390. 0,0,1,1,0,0,1,1,
  391. 0,1,1,0,0,1,1,0,
  392. 1,1,0,0,1,1,0,0,
  393. 0,1,1,0,0,1,1,0,
  394. 0,0,1,1,0,0,1,1
  395. };
  396. memcpy(&Points, &nTemp, sizeof(nTemp));
  397. }
  398. else if(((m_lTopBorder == CAPTION_BORDER) && (m_bMinimized == false)) ||
  399. ((m_lBottomBorder == CAPTION_BORDER) && (m_bMinimized)))
  400. {
  401. bTopOrBottom = true;
  402. int nTemp[8][5] =
  403. {
  404. 0,0,1,0,0,
  405. 0,1,1,1,0,
  406. 1,1,0,1,1,
  407. 1,0,0,0,1,
  408. 0,0,1,0,0,
  409. 0,1,1,1,0,
  410. 1,1,0,1,1,
  411. 1,0,0,0,1
  412. };
  413. memcpy(&Points2, &nTemp, sizeof(nTemp));
  414. }
  415. else if(((m_lTopBorder == CAPTION_BORDER) && (m_bMinimized)) ||
  416. ((m_lBottomBorder == CAPTION_BORDER) && (m_bMinimized == false)))
  417. {
  418. bTopOrBottom = true;
  419. int nTemp[8][5] =
  420. {
  421. 1,0,0,0,1,
  422. 1,1,0,1,1,
  423. 0,1,1,1,0,
  424. 0,0,1,0,0,
  425. 1,0,0,0,1,
  426. 1,1,0,1,1,
  427. 0,1,1,1,0,
  428. 0,0,1,0,0
  429. };
  430. memcpy(&Points2, &nTemp, sizeof(nTemp));
  431. }
  432. COLORREF shaddow = RGB(GetRValue(left) * 1.16,
  433. GetGValue(left) * 1.12,
  434. GetBValue(left) * 1.12);
  435. if(m_bMouseDownOnMinimize)
  436. dc.Draw3dRect(m_crMinimizeBT, shaddow, RGB(255, 255, 255));
  437. else if(m_bMouseOverMinimize)
  438. dc.Draw3dRect(m_crMinimizeBT, RGB(255, 255, 255), shaddow);
  439. if(bTopOrBottom == false)
  440. {
  441. CPoint ptShift = m_crMinimizeBT.TopLeft();
  442. ptShift.Offset(2, 3);
  443. for (int iRow = 0; iRow < 5; iRow++)
  444. {
  445. for (int iCol = 0; iCol < 8; iCol++)
  446. {
  447. if (Points[iRow][iCol] == 1)
  448. dc.SetPixel(ptShift+CPoint(iCol, iRow), RGB(255, 255, 255));
  449. }
  450. }
  451. }
  452. else
  453. {
  454. CPoint ptShift = m_crMinimizeBT.TopLeft();
  455. ptShift.Offset(3, 2);
  456. for (int iRow = 0; iRow < 8; iRow++)
  457. {
  458. for (int iCol = 0; iCol < 5; iCol++)
  459. {
  460. if (Points2[iRow][iCol] == 1)
  461. dc.SetPixel(ptShift+CPoint(iCol, iRow), RGB(255, 255, 255));
  462. }
  463. }
  464. }
  465. }
  466. void CWndEx::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
  467. {
  468. CWnd::OnNcCalcSize(bCalcValidRects, lpncsp);
  469. //Decrease the client area
  470. lpncsp->rgrc[0].left+= m_lLeftBorder;
  471. lpncsp->rgrc[0].top+= m_lTopBorder;
  472. lpncsp->rgrc[0].right-= m_lRightBorder;
  473. lpncsp->rgrc[0].bottom-= m_lBottomBorder;
  474. }
  475. UINT CWndEx::OnNcHitTest(CPoint point)
  476. {
  477. if(!m_bResizable)
  478. return CWnd::OnNcHitTest(point);
  479. //Hit the close button
  480. // CPoint clPoint(point);
  481. // ScreenToClient(&clPoint);
  482. // if(m_crCloseBT.PtInRect(clPoint))
  483. // return HTCLOSE;
  484. CRect crWindow;
  485. GetWindowRect(crWindow);
  486. if(crWindow.PtInRect(point) == false)
  487. {
  488. return CWnd::OnNcHitTest(point);
  489. }
  490. if(m_bMinimized == false)
  491. {
  492. if ((point.y < crWindow.top + BORDER * 2) &&
  493. (point.x < crWindow.left + BORDER * 2))
  494. return HTTOPLEFT;
  495. else if ((point.y < crWindow.top + BORDER * 2) &&
  496. (point.x > crWindow.right - BORDER * 2))
  497. return HTTOPRIGHT;
  498. else if ((point.y > crWindow.bottom - BORDER * 2) &&
  499. (point.x > crWindow.right - BORDER * 2))
  500. return HTBOTTOMRIGHT;
  501. else if ((point.y > crWindow.bottom - BORDER * 2) &&
  502. (point.x < crWindow.left + BORDER * 2))
  503. return HTBOTTOMLEFT;
  504. }
  505. if((((m_lTopBorder == CAPTION_BORDER) || (m_lBottomBorder == CAPTION_BORDER)) &&
  506. (m_bMinimized)) == false)
  507. {
  508. if (point.y < crWindow.top + BORDER * 2)
  509. return HTTOP;
  510. if (point.y > crWindow.bottom - BORDER * 2)
  511. return HTBOTTOM;
  512. }
  513. if((((m_lLeftBorder == CAPTION_BORDER) || (m_lRightBorder == CAPTION_BORDER)) &&
  514. (m_bMinimized)) == false)
  515. {
  516. if (point.x > crWindow.right - BORDER * 2)
  517. return HTRIGHT;
  518. if (point.x < crWindow.left + BORDER * 2)
  519. return HTLEFT;
  520. }
  521. if(m_lRightBorder == CAPTION_BORDER)
  522. {
  523. if (point.x > crWindow.right - m_lRightBorder)
  524. return HTCAPTION;
  525. }
  526. else if(m_lBottomBorder == CAPTION_BORDER)
  527. {
  528. if(point.y > crWindow.bottom - m_lBottomBorder)
  529. return HTCAPTION;
  530. }
  531. else if(m_lLeftBorder == CAPTION_BORDER)
  532. {
  533. if (point.x < crWindow.left + m_lLeftBorder)
  534. return HTCAPTION;
  535. }
  536. else if(m_lTopBorder == CAPTION_BORDER)
  537. {
  538. if (point.y < crWindow.top + m_lTopBorder)
  539. return HTCAPTION;
  540. }
  541. return CWnd::OnNcHitTest(point); // The default handler
  542. }
  543. void CWndEx::OnNcLButtonDown(UINT nHitTest, CPoint point)
  544. {
  545. CPoint clPoint(point);
  546. ScreenToClient(&clPoint);
  547. clPoint.x += m_lLeftBorder;
  548. clPoint.y += m_lTopBorder;
  549. if(m_crCloseBT.PtInRect(clPoint))
  550. {
  551. SetCapture();
  552. m_bMouseDownOnClose = true;
  553. CWindowDC dc(this);
  554. DrawCloseBtn(dc);
  555. }
  556. else if(m_crMinimizeBT.PtInRect(clPoint))
  557. {
  558. SetCapture();
  559. m_bMouseDownOnMinimize = true;
  560. CWindowDC dc(this);
  561. DrawMinimizeBtn(dc);
  562. }
  563. CWnd::OnNcLButtonDown(nHitTest, point);
  564. }
  565. void CWndEx::OnNcLButtonUp(UINT nHitTest, CPoint point)
  566. {
  567. if(m_bMouseDownOnClose)
  568. {
  569. ReleaseCapture();
  570. m_bMouseDownOnClose = false;
  571. m_bMouseOverClose = false;
  572. OnNcPaint();
  573. CPoint clPoint(point);
  574. clPoint.x += m_lLeftBorder;
  575. clPoint.y += m_lTopBorder;
  576. if(m_crCloseBT.PtInRect(clPoint))
  577. SendMessage(WM_CLOSE, 0, 0);
  578. }
  579. else if(m_bMouseDownOnMinimize)
  580. {
  581. ReleaseCapture();
  582. m_bMouseDownOnMinimize = false;
  583. m_bMouseOverMinimize = false;
  584. OnNcPaint();
  585. CPoint clPoint(point);
  586. clPoint.x += m_lLeftBorder;
  587. clPoint.y += m_lTopBorder;
  588. if(m_crMinimizeBT.PtInRect(clPoint))
  589. {
  590. MinMaxWindow();
  591. }
  592. }
  593. CWnd::OnNcLButtonUp(nHitTest, point);
  594. }
  595. void CWndEx::MinMaxWindow(long lOption)
  596. {
  597. if((m_bMinimized) && (lOption == FORCE_MIN))
  598. return;
  599. if((m_bMinimized == false) && (lOption == FORCE_MAX))
  600. return;
  601. if(m_lRightBorder == CAPTION_BORDER)
  602. {
  603. if(m_bMinimized == false)
  604. {
  605. GetWindowRect(m_crFullSizeWindow);
  606. MoveWindow(m_crFullSizeWindow.right - CAPTION_BORDER,
  607. m_crFullSizeWindow.top, CAPTION_BORDER,
  608. m_crFullSizeWindow.Height());
  609. m_bMinimized = true;
  610. OnNcPaint();
  611. }
  612. else
  613. {
  614. CRect cr;
  615. GetWindowRect(cr);
  616. MoveWindow(cr.right - m_crFullSizeWindow.Width(),
  617. cr.top, m_crFullSizeWindow.Width(), cr.Height());
  618. m_crFullSizeWindow.SetRectEmpty();
  619. m_bMinimized = false;
  620. OnNcPaint();
  621. }
  622. }
  623. if(m_lLeftBorder == CAPTION_BORDER)
  624. {
  625. if(m_bMinimized == false)
  626. {
  627. GetWindowRect(m_crFullSizeWindow);
  628. MoveWindow(m_crFullSizeWindow.left,
  629. m_crFullSizeWindow.top, CAPTION_BORDER,
  630. m_crFullSizeWindow.Height());
  631. m_bMinimized = true;
  632. OnNcPaint();
  633. }
  634. else
  635. {
  636. CRect cr;
  637. GetWindowRect(cr);
  638. MoveWindow(cr.left, cr.top,
  639. m_crFullSizeWindow.Width(), cr.Height());
  640. m_crFullSizeWindow.SetRectEmpty();
  641. m_bMinimized = false;
  642. OnNcPaint();
  643. }
  644. }
  645. else if(m_lTopBorder == CAPTION_BORDER)
  646. {
  647. if(m_bMinimized == false)
  648. {
  649. GetWindowRect(m_crFullSizeWindow);
  650. MoveWindow(m_crFullSizeWindow.left,
  651. m_crFullSizeWindow.top,
  652. m_crFullSizeWindow.Width(),
  653. CAPTION_BORDER);
  654. m_bMinimized = true;
  655. OnNcPaint();
  656. }
  657. else
  658. {
  659. CRect cr;
  660. GetWindowRect(cr);
  661. MoveWindow(cr.left, cr.top,
  662. cr.Width(), m_crFullSizeWindow.Height());
  663. m_crFullSizeWindow.SetRectEmpty();
  664. m_bMinimized = false;
  665. OnNcPaint();
  666. }
  667. }
  668. else if(m_lBottomBorder == CAPTION_BORDER)
  669. {
  670. if(m_bMinimized == false)
  671. {
  672. GetWindowRect(m_crFullSizeWindow);
  673. MoveWindow(m_crFullSizeWindow.left,
  674. m_crFullSizeWindow.bottom - CAPTION_BORDER,
  675. m_crFullSizeWindow.Width(),
  676. CAPTION_BORDER);
  677. m_bMinimized = true;
  678. OnNcPaint();
  679. }
  680. else
  681. {
  682. CRect cr;
  683. GetWindowRect(cr);
  684. MoveWindow(cr.left,
  685. cr.bottom - m_crFullSizeWindow.Height(),
  686. cr.Width(), m_crFullSizeWindow.Height());
  687. m_crFullSizeWindow.SetRectEmpty();
  688. m_bMinimized = false;
  689. OnNcPaint();
  690. }
  691. }
  692. }
  693. void CWndEx::OnNcMouseMove(UINT nHitTest, CPoint point)
  694. {
  695. CPoint clPoint(point);
  696. ScreenToClient(&clPoint);
  697. clPoint.x += m_lLeftBorder;
  698. clPoint.y += m_lTopBorder;
  699. if(m_crCloseBT.PtInRect(clPoint))
  700. {
  701. m_bMouseOverClose = true;
  702. CWindowDC dc(this);
  703. DrawCloseBtn(dc);
  704. }
  705. else if(m_bMouseOverClose)
  706. {
  707. m_bMouseOverClose = false;
  708. OnNcPaint();
  709. }
  710. if(m_crMinimizeBT.PtInRect(clPoint))
  711. {
  712. m_bMouseOverMinimize = true;
  713. CWindowDC dc(this);
  714. DrawMinimizeBtn(dc);
  715. }
  716. else if(m_bMouseOverMinimize)
  717. {
  718. m_bMouseOverMinimize = false;
  719. OnNcPaint();
  720. }
  721. if((m_bMaxSetTimer == false) && m_bMinimized)
  722. {
  723. SetTimer(TIMER_AUTO_MAX, 1000, NULL);
  724. m_bMaxSetTimer = true;
  725. }
  726. CWnd::OnNcMouseMove(nHitTest, point);
  727. }
  728. BOOL CWndEx::PreTranslateMessage(MSG* pMsg)
  729. {
  730. if (pMsg->message == WM_NCLBUTTONDOWN)
  731. {
  732. m_bMouseDownOnCaption = true;
  733. }
  734. if ((pMsg->message == WM_LBUTTONUP) && (m_bMouseDownOnCaption))
  735. {
  736. m_bMouseDownOnCaption = false;
  737. pMsg->message = WM_NCLBUTTONUP;
  738. }
  739. return CWnd::PreTranslateMessage(pMsg);
  740. }
  741. /*
  742. CBitmap m_bitmap;
  743. CDC dcMem;
  744. void MakeBitmap();
  745. CDC dcMem;
  746. dcMem.CreateCompatibleDC(&dc);
  747. CBitmap *pOldBmp = (CBitmap *)(dcMem.SelectObject(&m_bitmap));
  748. dc.BitBlt(rcBorder.left, 0, rcBorder.Width(), rcBorder.Height(), &dcMem, 0, 0, SRCCOPY);
  749. dcMem.SelectObject(pOldBmp);
  750. dcMem.DeleteDC();
  751. void CWndEx::MakeBitmap()
  752. {
  753. CWindowDC dc(this);
  754. CRect rect;
  755. GetWindowRect(&rect);
  756. CRect rcBorder(0, 0, rect.Width(), rect.Height());
  757. rcBorder.left = rcBorder.right - RIGHT_CAPTION - BORDER + 1;
  758. rect = rcBorder;
  759. int r1=245,g1=190,b1=240;
  760. int r2=130,g2=0,b2=0;
  761. int x1=0,y1=0;
  762. int x2=0,y2=0;
  763. CDC dc2;
  764. dc2.CreateCompatibleDC(&dc);
  765. if(m_bitmap.m_hObject)
  766. m_bitmap.DeleteObject();
  767. m_bitmap.CreateCompatibleBitmap(&dc,rect.Width(),
  768. rect.Height());
  769. CBitmap *oldbmap=dc2.SelectObject(&m_bitmap);
  770. while(x1 < rect.Width() && y1 < rect.Height())
  771. {
  772. if(y1 < rect.Height()-1)
  773. y1++;
  774. else
  775. x1++;
  776. if(x2 < rect.Width()-1)
  777. x2++;
  778. else
  779. y2++;
  780. int r,g,b;
  781. int i = x1+y1;
  782. r = r1 + (i * (r2-r1) / (rect.Width()+rect.Height()));
  783. g = g1 + (i * (g2-g1) / (rect.Width()+rect.Height()));
  784. b = b1 + (i * (b2-b1) / (rect.Width()+rect.Height()));
  785. CPen p(PS_SOLID,1,RGB(r,g,b));
  786. CPen *oldpen = dc2.SelectObject(&p);
  787. dc2.MoveTo(x1,y1);
  788. dc2.LineTo(x2,y2);
  789. dc2.SelectObject(oldpen);
  790. }
  791. dc2.SelectObject(oldbmap);
  792. }
  793. */
  794. BOOL CWndEx::OnEraseBkgnd(CDC* pDC)
  795. {
  796. return CWnd::OnEraseBkgnd(pDC);
  797. }
  798. void CWndEx::OnTimer(UINT nIDEvent)
  799. {
  800. if(nIDEvent == TIMER_AUTO_MAX)
  801. {
  802. if(m_bMinimized)
  803. {
  804. CPoint cp;
  805. GetCursorPos(&cp);
  806. UINT nHitTest = OnNcHitTest(cp);
  807. ScreenToClient(&cp);
  808. if(nHitTest == HTCAPTION)
  809. {
  810. if(m_crCloseBT.PtInRect(cp) == false)
  811. {
  812. if(m_crMinimizeBT.PtInRect(cp) == false)
  813. {
  814. MinMaxWindow(FORCE_MAX);
  815. }
  816. }
  817. }
  818. }
  819. KillTimer(TIMER_AUTO_MAX);
  820. m_bMaxSetTimer = false;
  821. }
  822. else if(nIDEvent == TIMER_AUTO_MIN)
  823. {
  824. if((m_bMinimized == false) && (g_Opt.m_bShowPersistent))
  825. {
  826. CPoint cp;
  827. CRect cr;
  828. GetCursorPos(&cp);
  829. GetWindowRect(&cr);
  830. if(cr.PtInRect(cp) == false)
  831. {
  832. if(GetFocus() == NULL)
  833. {
  834. MinMaxWindow(FORCE_MIN);
  835. }
  836. }
  837. }
  838. }
  839. CWnd::OnTimer(nIDEvent);
  840. }
  841. void CWndEx::OnWindowPosChanging(WINDOWPOS* lpwndpos)
  842. {
  843. CWnd::OnWindowPosChanging(lpwndpos);
  844. if(m_bMaxSetTimer)
  845. {
  846. KillTimer(TIMER_AUTO_MAX);
  847. m_bMaxSetTimer = false;
  848. }
  849. }
  850. void CWndEx::SetRegion()
  851. {
  852. if((m_lRightBorder == CAPTION_BORDER) ||
  853. (m_lTopBorder == CAPTION_BORDER))
  854. {
  855. //Create the region for drawing the rounded top edge
  856. CRect rect;
  857. GetWindowRect(rect);
  858. CRgn rgnRect, rgnRect2, rgnRound, rgnFinalA, rgnFinalB;
  859. rgnRect.CreateRectRgn(0, 0, rect.Width() - 7, rect.Height());
  860. rgnRound.CreateRoundRectRgn(0, 0, rect.Width()+1, rect.Height(), 15, 15);
  861. rgnFinalB.CreateRectRgn(0, 0, 0, 0);
  862. rgnFinalB.CombineRgn(&rgnRect, &rgnRound, RGN_OR);
  863. rgnRect2.CreateRectRgn(0, 7, rect.Width(), rect.Height());
  864. rgnFinalA.CreateRectRgn(0, 0, 0, 0);
  865. rgnFinalA.CombineRgn(&rgnRect2, &rgnFinalB, RGN_OR);
  866. //Set the region
  867. SetWindowRgn(rgnFinalA, TRUE);
  868. }
  869. else if(m_lLeftBorder == CAPTION_BORDER)
  870. {
  871. CRect rect;
  872. GetWindowRect(rect);
  873. CRgn rgnRect, rgnRect2, rgnRound, rgnFinalA, rgnFinalB;
  874. rgnRect.CreateRectRgn(0, 7, rect.Width(), rect.Height());
  875. rgnRound.CreateRoundRectRgn(0, 0, rect.Width(), rect.Height(), 15, 15);
  876. rgnFinalB.CreateRectRgn(0, 0, 0, 0);
  877. rgnFinalB.CombineRgn(&rgnRect, &rgnRound, RGN_OR);
  878. rgnRect2.CreateRectRgn(7, 0, rect.Width(), rect.Height());
  879. rgnFinalA.CreateRectRgn(0, 0, 0, 0);
  880. rgnFinalA.CombineRgn(&rgnRect2, &rgnFinalB, RGN_OR);
  881. //Set the region
  882. SetWindowRgn(rgnFinalA, TRUE);
  883. }
  884. else if(m_lBottomBorder == CAPTION_BORDER)
  885. {
  886. CRect rect;
  887. GetWindowRect(rect);
  888. CRgn rgnRect, rgnRect2, rgnRound, rgnFinalA, rgnFinalB;
  889. rgnRect.CreateRectRgn(0, 0, rect.Width(), rect.Height()-7);
  890. rgnRound.CreateRoundRectRgn(0, 0, rect.Width()+1, rect.Height()+1, 15, 15);
  891. rgnFinalB.CreateRectRgn(0, 0, 0, 0);
  892. rgnFinalB.CombineRgn(&rgnRect, &rgnRound, RGN_OR);
  893. rgnRect2.CreateRectRgn(0, 0, rect.Width()-15, rect.Height());
  894. rgnFinalA.CreateRectRgn(0, 0, 0, 0);
  895. rgnFinalA.CombineRgn(&rgnRect2, &rgnFinalB, RGN_OR);
  896. //Set the region
  897. SetWindowRgn(rgnFinalA, TRUE);
  898. }
  899. }
  900. void CWndEx::OnSize(UINT nType, int cx, int cy)
  901. {
  902. CWnd::OnSize(nType, cx, cy);
  903. SetRegion();
  904. }
  905. void CWndEx::OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex,BOOL bSysMenu)
  906. {
  907. ASSERT(pPopupMenu != NULL);
  908. // Check the enabled state of various menu items.
  909. CCmdUI state;
  910. state.m_pMenu = pPopupMenu;
  911. ASSERT(state.m_pOther == NULL);
  912. ASSERT(state.m_pParentMenu == NULL);
  913. // Determine if menu is popup in top-level menu and set m_pOther to
  914. // it if so (m_pParentMenu == NULL indicates that it is secondary popup).
  915. HMENU hParentMenu;
  916. if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)
  917. {
  918. state.m_pParentMenu = pPopupMenu; // Parent == child for tracking popup.
  919. }
  920. else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)
  921. {
  922. CWnd* pParent = this;
  923. // Child windows don't have menus--need to go to the top!
  924. if (pParent != NULL &&
  925. (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
  926. {
  927. int nIndexMax = ::GetMenuItemCount(hParentMenu);
  928. for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
  929. {
  930. if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)
  931. {
  932. // When popup is found, m_pParentMenu is containing menu.
  933. state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
  934. break;
  935. }
  936. }
  937. }
  938. }
  939. state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
  940. for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
  941. state.m_nIndex++)
  942. {
  943. state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
  944. if (state.m_nID == 0)
  945. continue; // Menu separator or invalid cmd - ignore it.
  946. ASSERT(state.m_pOther == NULL);
  947. ASSERT(state.m_pMenu != NULL);
  948. if (state.m_nID == (UINT)-1)
  949. {
  950. // Possibly a popup menu, route to first item of that popup.
  951. state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);
  952. if (state.m_pSubMenu == NULL ||
  953. (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
  954. state.m_nID == (UINT)-1)
  955. {
  956. continue; // First item of popup can't be routed to.
  957. }
  958. state.DoUpdate(this, TRUE); // Popups are never auto disabled.
  959. }
  960. else
  961. {
  962. // Normal menu item.
  963. // Auto enable/disable if frame window has m_bAutoMenuEnable
  964. // set and command is _not_ a system command.
  965. state.m_pSubMenu = NULL;
  966. state.DoUpdate(this, FALSE);
  967. }
  968. // Adjust for menu deletions and additions.
  969. UINT nCount = pPopupMenu->GetMenuItemCount();
  970. if (nCount < state.m_nIndexMax)
  971. {
  972. state.m_nIndex -= (state.m_nIndexMax - nCount);
  973. while (state.m_nIndex < nCount &&
  974. pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
  975. {
  976. state.m_nIndex++;
  977. }
  978. }
  979. state.m_nIndexMax = nCount;
  980. }
  981. }