WndEx.cpp 9.2 KB

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