WndEx.cpp 8.6 KB

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