WndEx.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. CWndEx::CWndEx()
  15. {
  16. m_bResizable = true;
  17. m_bMouseDownOnClose = false;
  18. m_bMouseOverClose = false;
  19. m_bMouseDownOnCaption = false;
  20. SetCaptionColorActive(false);
  21. }
  22. CWndEx::~CWndEx()
  23. {
  24. }
  25. void CWndEx::InvalidateNc()
  26. {
  27. ::RedrawWindow( m_hWnd, NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_NOINTERNALPAINT );
  28. }
  29. bool CWndEx::SetCaptionColors( COLORREF left, COLORREF right )
  30. {
  31. if( left == m_CaptionColorLeft || right == m_CaptionColorRight )
  32. return false;
  33. m_CaptionColorLeft = left;
  34. m_CaptionColorRight = right;
  35. return true;
  36. }
  37. bool CWndEx::SetCaptionColorActive( bool bVal )
  38. {
  39. bool bResult;
  40. if( bVal )
  41. bResult = SetCaptionColors( ::GetSysColor(COLOR_ACTIVECAPTION), ::GetSysColor(COLOR_GRADIENTACTIVECAPTION) );
  42. else
  43. bResult = SetCaptionColors( ::GetSysColor(COLOR_INACTIVECAPTION), ::GetSysColor(COLOR_GRADIENTINACTIVECAPTION) );
  44. return bResult;
  45. }
  46. BEGIN_MESSAGE_MAP(CWndEx, CWnd)
  47. //{{AFX_MSG_MAP(CWndEx)
  48. ON_WM_CREATE()
  49. ON_WM_NCPAINT()
  50. ON_WM_NCCALCSIZE()
  51. ON_WM_NCHITTEST()
  52. ON_WM_NCLBUTTONDOWN()
  53. ON_WM_NCMOUSEMOVE()
  54. ON_WM_NCLBUTTONUP()
  55. ON_WM_ERASEBKGND()
  56. //}}AFX_MSG_MAP
  57. // ON_WM_NCLBUTTONDBLCLK()
  58. // ON_WM_NCACTIVATE()
  59. END_MESSAGE_MAP()
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CWndEx message handlers
  62. BOOL CWndEx::Create(const CRect& crStart, CWnd* pParentWnd)
  63. {
  64. WNDCLASS wc;
  65. wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  66. wc.lpfnWndProc = AfxWndProc;
  67. wc.cbClsExtra = 0;
  68. wc.cbWndExtra = 0;
  69. wc.hInstance = AfxGetInstanceHandle();
  70. wc.hIcon = NULL;
  71. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  72. wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  73. wc.lpszMenuName = NULL;
  74. wc.lpszClassName = "QPasteClass";
  75. // Create the QPaste window class
  76. if (!AfxRegisterClass(&wc))
  77. return FALSE;
  78. return CWndEx::CreateEx(0, "QPasteClass", "Quick Paste", WS_POPUP,
  79. crStart, pParentWnd, 0);
  80. }
  81. int CWndEx::OnCreate(LPCREATESTRUCT lpCreateStruct)
  82. {
  83. if (CWnd::OnCreate(lpCreateStruct) == -1)
  84. return -1;
  85. m_TitleFont.CreateFont(14,0,-900,0,400,FALSE,FALSE,0,ANSI_CHARSET,
  86. OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
  87. DEFAULT_PITCH|FF_SWISS,"Arial");
  88. return 0;
  89. }
  90. void CWndEx::OnNcPaint()
  91. {
  92. CWindowDC dc(this);
  93. CRect rcFrame;
  94. GetWindowRect(rcFrame);
  95. ScreenToClient(rcFrame);
  96. CRect rc;
  97. GetClientRect(rc);
  98. ClientToScreen(rc);
  99. long lWidth = rcFrame.Width();
  100. // Draw the window border
  101. CRect rcBorder(0, 0, lWidth, rcFrame.Height());
  102. COLORREF left = m_CaptionColorLeft;
  103. COLORREF right = m_CaptionColorRight;
  104. dc.Draw3dRect(rcBorder, left, left);
  105. rcBorder.DeflateRect(1, 1, 1, 1);
  106. dc.Draw3dRect(rcBorder, left, left);
  107. rcBorder.left = rcBorder.right - RIGHT_CAPTION - BORDER + 1;
  108. rcBorder.bottom += BORDER;
  109. rcBorder.top -= BORDER;
  110. float gR = 0;
  111. float gG = 0;
  112. float gB = 0;
  113. float sR = GetRValue(left);
  114. float sG = GetGValue(left);
  115. float sB = GetBValue(left);
  116. float eR = GetRValue(right);
  117. float eG = GetGValue(right);
  118. float eB = GetBValue(right);
  119. // calculate the slope for color gradient
  120. gR = (eR - sR) / rcBorder.Height();
  121. gG = (eG - sG) / rcBorder.Height();
  122. gB = (eB - sB) / rcBorder.Height();
  123. HBRUSH color;
  124. long lHeight = rcBorder.Height();
  125. CRect cr = rcBorder;
  126. for(int i = 0; i < lHeight; i++)
  127. {
  128. cr.top = i;
  129. cr.bottom = i + 1;
  130. color = CreateSolidBrush(RGB(int(gR * (float) i + gR),
  131. int(gG * (float) i + sG),
  132. int(gB * (float) i + sB)));
  133. ::FillRect(dc, &cr, color);
  134. DeleteObject(color);
  135. }
  136. /*
  137. HBRUSH color;
  138. color = CreateSolidBrush(left);
  139. ::FillRect(dc, &rcBorder, color);
  140. DeleteObject(color);
  141. */
  142. int nOldBKMode = dc.SetBkMode(TRANSPARENT);
  143. COLORREF oldColor = dc.SetTextColor(RGB(255, 255, 255));
  144. CFont* pOldFont=dc.SelectObject(&m_TitleFont);
  145. CString csText;
  146. GetWindowText(csText);
  147. dc.TextOut(rcBorder.right-1, 22, csText);
  148. DrawCloseBtn(dc, lWidth, left);
  149. dc.SelectObject(pOldFont);
  150. dc.SetTextColor(oldColor);
  151. dc.SetBkMode(nOldBKMode);
  152. }
  153. void CWndEx::DrawCloseBtn(CWindowDC &dc, long lRight, COLORREF left)
  154. {
  155. if(lRight == -1)
  156. {
  157. CRect cr;
  158. GetWindowRect(cr);
  159. lRight = cr.Width();
  160. }
  161. if(left == 0)
  162. left = GetSysColor(COLOR_ACTIVECAPTION);
  163. //rows first then columns
  164. int Points[5][6] =
  165. {
  166. 1,1,0,0,1,1,
  167. 0,1,1,1,1,0,
  168. 0,0,1,1,0,0,
  169. 0,1,1,1,1,0,
  170. 1,1,0,0,1,1
  171. };
  172. CPoint ptShift(lRight - 15, 7);
  173. m_crCloseBT.SetRect(ptShift, ptShift+CPoint(12, 11));
  174. ptShift.Offset(3, 3);
  175. COLORREF shaddow = RGB(GetRValue(left) * 1.16,
  176. GetGValue(left) * 1.12,
  177. GetBValue(left) * 1.12);
  178. if(m_bMouseDownOnClose)
  179. dc.Draw3dRect(m_crCloseBT, shaddow, RGB(255, 255, 255));
  180. else if(m_bMouseOverClose)
  181. dc.Draw3dRect(m_crCloseBT, RGB(255, 255, 255), shaddow);
  182. for (int iRow = 0; iRow < 5; iRow++)
  183. {
  184. for (int iCol = 0; iCol < 6; iCol++)
  185. {
  186. if (Points[iRow][iCol] == 1)
  187. dc.SetPixel(ptShift+CPoint(iCol, iRow), RGB(255, 255, 255));
  188. }
  189. }
  190. }
  191. void CWndEx::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
  192. {
  193. CWnd::OnNcCalcSize(bCalcValidRects, lpncsp);
  194. //Decrease the client area
  195. lpncsp->rgrc[0].left+= BORDER;
  196. lpncsp->rgrc[0].top+= BORDER;
  197. lpncsp->rgrc[0].right-= BORDER;
  198. lpncsp->rgrc[0].bottom-= BORDER;
  199. lpncsp->rgrc[0].right-= RIGHT_CAPTION;
  200. }
  201. UINT CWndEx::OnNcHitTest(CPoint point)
  202. {
  203. if(!m_bResizable)
  204. return CWnd::OnNcHitTest(point);
  205. //Hit the close button
  206. // CPoint clPoint(point);
  207. // ScreenToClient(&clPoint);
  208. // if(m_crCloseBT.PtInRect(clPoint))
  209. // return HTCLOSE;
  210. CRect crWindow;
  211. GetWindowRect(crWindow);
  212. if ((point.y < crWindow.top + BORDER * 2) &&
  213. (point.x < crWindow.left + BORDER * 2))
  214. return HTTOPLEFT;
  215. else if ((point.y < crWindow.top + BORDER * 2) &&
  216. (point.x > crWindow.right - RIGHT_CAPTION))
  217. return HTTOPRIGHT;
  218. else if ((point.y > crWindow.bottom - BORDER * 7) &&
  219. (point.x > crWindow.right - RIGHT_CAPTION))
  220. return HTBOTTOMRIGHT;
  221. else if ((point.y > crWindow.bottom - BORDER * 2) &&
  222. (point.x < crWindow.left + BORDER * 2))
  223. return HTBOTTOMLEFT;
  224. else if (point.y < crWindow.top + BORDER * 2)
  225. return HTTOP;
  226. else if (point.x > crWindow.right - BORDER * 2)
  227. return HTRIGHT;
  228. else if (point.x < crWindow.left + BORDER * 2)
  229. return HTLEFT;
  230. else if (point.y > crWindow.bottom - BORDER * 2)
  231. return HTBOTTOM;
  232. else if (point.x > crWindow.right - RIGHT_CAPTION)
  233. return HTCAPTION;
  234. else
  235. return CWnd::OnNcHitTest(point); // The default handler
  236. }
  237. void CWndEx::OnNcLButtonDown(UINT nHitTest, CPoint point)
  238. {
  239. CPoint clPoint(point);
  240. ScreenToClient(&clPoint);
  241. if(m_crCloseBT.PtInRect(clPoint))
  242. {
  243. SetCapture();
  244. m_bMouseDownOnClose = true;
  245. CWindowDC dc(this);
  246. DrawCloseBtn(dc);
  247. }
  248. CWnd::OnNcLButtonDown(nHitTest, point);
  249. }
  250. void CWndEx::OnNcLButtonUp(UINT nHitTest, CPoint point)
  251. {
  252. if(m_bMouseDownOnClose)
  253. {
  254. ReleaseCapture();
  255. m_bMouseDownOnClose = false;
  256. m_bMouseOverClose = false;
  257. OnNcPaint();
  258. CPoint clPoint(point);
  259. if(m_crCloseBT.PtInRect(point))
  260. SendMessage(WM_CLOSE, 0, 0);
  261. }
  262. CWnd::OnNcLButtonUp(nHitTest, point);
  263. }
  264. void CWndEx::OnNcMouseMove(UINT nHitTest, CPoint point)
  265. {
  266. CPoint clPoint(point);
  267. ScreenToClient(&clPoint);
  268. if(m_crCloseBT.PtInRect(clPoint))
  269. {
  270. m_bMouseOverClose = true;
  271. CWindowDC dc(this);
  272. DrawCloseBtn(dc);
  273. }
  274. else if(m_bMouseOverClose)
  275. {
  276. m_bMouseOverClose = false;
  277. OnNcPaint();
  278. }
  279. CWnd::OnNcMouseMove(nHitTest, point);
  280. }
  281. BOOL CWndEx::PreTranslateMessage(MSG* pMsg)
  282. {
  283. if (pMsg->message == WM_NCLBUTTONDOWN)
  284. m_bMouseDownOnCaption = true;
  285. if ((pMsg->message == WM_LBUTTONUP) && (m_bMouseDownOnCaption))
  286. {
  287. m_bMouseDownOnCaption = false;
  288. pMsg->message = WM_NCLBUTTONUP;
  289. }
  290. return CWnd::PreTranslateMessage(pMsg);
  291. }
  292. /*
  293. CBitmap m_bitmap;
  294. CDC dcMem;
  295. void MakeBitmap();
  296. CDC dcMem;
  297. dcMem.CreateCompatibleDC(&dc);
  298. CBitmap *pOldBmp = (CBitmap *)(dcMem.SelectObject(&m_bitmap));
  299. dc.BitBlt(rcBorder.left, 0, rcBorder.Width(), rcBorder.Height(), &dcMem, 0, 0, SRCCOPY);
  300. dcMem.SelectObject(pOldBmp);
  301. dcMem.DeleteDC();
  302. void CWndEx::MakeBitmap()
  303. {
  304. CWindowDC dc(this);
  305. CRect rect;
  306. GetWindowRect(&rect);
  307. CRect rcBorder(0, 0, rect.Width(), rect.Height());
  308. rcBorder.left = rcBorder.right - RIGHT_CAPTION - BORDER + 1;
  309. rect = rcBorder;
  310. int r1=245,g1=190,b1=240;
  311. int r2=130,g2=0,b2=0;
  312. int x1=0,y1=0;
  313. int x2=0,y2=0;
  314. CDC dc2;
  315. dc2.CreateCompatibleDC(&dc);
  316. if(m_bitmap.m_hObject)
  317. m_bitmap.DeleteObject();
  318. m_bitmap.CreateCompatibleBitmap(&dc,rect.Width(),
  319. rect.Height());
  320. CBitmap *oldbmap=dc2.SelectObject(&m_bitmap);
  321. while(x1 < rect.Width() && y1 < rect.Height())
  322. {
  323. if(y1 < rect.Height()-1)
  324. y1++;
  325. else
  326. x1++;
  327. if(x2 < rect.Width()-1)
  328. x2++;
  329. else
  330. y2++;
  331. int r,g,b;
  332. int i = x1+y1;
  333. r = r1 + (i * (r2-r1) / (rect.Width()+rect.Height()));
  334. g = g1 + (i * (g2-g1) / (rect.Width()+rect.Height()));
  335. b = b1 + (i * (b2-b1) / (rect.Width()+rect.Height()));
  336. CPen p(PS_SOLID,1,RGB(r,g,b));
  337. CPen *oldpen = dc2.SelectObject(&p);
  338. dc2.MoveTo(x1,y1);
  339. dc2.LineTo(x2,y2);
  340. dc2.SelectObject(oldpen);
  341. }
  342. dc2.SelectObject(oldbmap);
  343. }
  344. */
  345. BOOL CWndEx::OnEraseBkgnd(CDC* pDC)
  346. {
  347. return CWnd::OnEraseBkgnd(pDC);
  348. }