caption.cpp
上传用户:songshun
上传日期:2007-01-04
资源大小:51k
文件大小:9k
源码类别:

按钮控件

开发平台:

Visual C++

  1. //===========================================================================
  2. //
  3. // HomeWork from Belgium Not licensed software  
  4. // 1999 - 2000 No rights reserved
  5. //
  6. //===========================================================================
  7. //
  8. // Project/Product : Iconizer DLL
  9. //  FileName : caption.cpp
  10. // Author(s) : Bart Gysens
  11. //
  12. // Description : Implementation of caption related functionality
  13. //
  14. // Classes : CCaptionRect
  15. //
  16. // Information :
  17. //   Compiler(s) : Visual C++ 6.0
  18. //   Target(s) : Windows 95/98 and Windows NT (x86)
  19. //   Editor : Visual C++ 6.0 internal editor
  20. //
  21. // History
  22. // Vers.  Date      Aut.  Type     Description
  23. //  -----  --------  ----  -------  -----------------------------------------
  24. // 1.00   22 05 99  BG    Create   Original
  25. //
  26. //===========================================================================
  27. #include "stdafx.h"
  28. #include "hooklist.h"
  29. #include "trayicon.h"
  30. #include "caption.h"
  31. //===========================================================================
  32. // Macros and typedefs
  33. //===========================================================================
  34. extern CTrayIcon g_TrayIcon;
  35. //===========================================================================
  36. // 
  37. // Class : CCaptionRect
  38. // Author(s) : Bart Gysens
  39. //
  40. // Description : Implementation of the CCaptionRect class
  41. //
  42. // Comments : none
  43. //
  44. // History : 1.00  : Create
  45. //
  46. //===========================================================================
  47. CCaption::CCaption()
  48. {
  49. }
  50. void CCaption::CalcCaptionRect( HWND hWnd, RECT& Rect )
  51. {
  52. DWORD dStyle ;
  53. SIZE  sFrame ;
  54. int   Icon ;
  55. // Get frame size of window
  56. dStyle    = GetWindowLong( hWnd, GWL_STYLE );
  57. sFrame.cx = GetSystemMetrics( ( dStyle & WS_THICKFRAME ) ? SM_CXSIZEFRAME : SM_CXFIXEDFRAME );
  58. sFrame.cy = GetSystemMetrics( ( dStyle & WS_THICKFRAME ) ? SM_CYSIZEFRAME : SM_CYFIXEDFRAME );
  59. // Get width of icon/button in caption
  60. Icon = GetSystemMetrics( SM_CXSIZE );
  61. // Calculate rectangle dimensions
  62. GetWindowRect( hWnd, &Rect );
  63. Rect.bottom -= Rect.top;
  64. Rect.right  -= Rect.left;
  65. Rect.top     = 0;
  66. Rect.left    = 0;
  67. Rect.left   += sFrame.cx;
  68. Rect.right  -= sFrame.cx;
  69. Rect.top    += sFrame.cy;
  70. Rect.bottom  = Rect.top + GetSystemMetrics( SM_CYCAPTION )
  71.                         - GetSystemMetrics( SM_CYBORDER );
  72. }
  73. void CCaption::DrawIconize( HDC hDc, int x, int y, int off )
  74. {
  75. //
  76. // TODO:
  77. // This should be done flikker free by means
  78. // of a memory DC
  79. //
  80. HPEN Pen, oldPen;
  81. int  cxBtn = GetSystemMetrics( SM_CXSIZE ) - 5;
  82. int  cyBtn = GetSystemMetrics( SM_CYSIZE ) - 5;
  83. Pen = CreatePen( PS_SOLID, 1, RGB( 0, 0, 0 ) );
  84. oldPen = (HPEN)   SelectObject( hDc, Pen );
  85. MoveToEx( hDc, x + 1 + cxBtn/4 + off, y + 1 + cyBtn/4, NULL );
  86. LineTo( hDc, x + 1 + cxBtn*3/4 + off, y + 1 + cyBtn/4 );
  87. LineTo( hDc, x + 1 + cxBtn*3/4 + off, y + 1 + cyBtn/4 );
  88. LineTo( hDc, x + 1 + cxBtn/2 + off, y + 1 + cyBtn*3/4 );
  89. LineTo( hDc, x + 1 + cxBtn/4 + off, y + 1 + cyBtn/ 4 );
  90. SelectObject( hDc, oldPen );
  91. DeleteObject( Pen );
  92. }
  93. void CCaption::DrawButtons( HWND hWnd )
  94. {
  95. HDC   hDc ;
  96. RECT  rCap ;
  97. DWORD dStyle ;
  98. DWORD dExStyle ;
  99. int   cxBtn ;
  100. int   cyBtn ;
  101. RECT  rPos ;
  102. // Get window device context
  103. if ( ( hDc = GetWindowDC( hWnd ) ) != NULL )
  104. {
  105. // Get caption coordinates
  106. CCaption::CalcCaptionRect( hWnd, rCap );
  107. // Get window style
  108. dStyle   = GetWindowLong( hWnd, GWL_STYLE );
  109. dExStyle = GetWindowLong( hWnd, GWL_EXSTYLE );
  110. // Check if we have a caption
  111. if ( ( dStyle & WS_CAPTION ) == WS_CAPTION )
  112. {
  113. // Get button dimensions
  114. cxBtn = GetSystemMetrics( SM_CXSIZE );
  115. cyBtn = GetSystemMetrics( SM_CYSIZE );
  116. // Calc position and draw close button
  117. rPos.top    = rCap.top + 2;
  118. rPos.bottom = rCap.bottom - 2;
  119. rPos.right  = rCap.right - 2;
  120. rPos.left   = rCap.right - 2 - ( cxBtn - 2);
  121. DrawFrameControl( hDc, &rPos, DFC_CAPTION, DFCS_CAPTIONCLOSE );
  122. // Calc position and draw maximize<->restore/help button
  123. if ( ( dStyle & WS_MAXIMIZEBOX ) == WS_MAXIMIZEBOX )
  124. {
  125. rPos.right -= cxBtn;
  126. rPos.left  -= cxBtn;
  127. DrawFrameControl( hDc, &rPos, DFC_CAPTION, IsZoomed( hWnd ) ? DFCS_CAPTIONRESTORE 
  128.                                                             : DFCS_CAPTIONMAX );
  129. }
  130. else if ( ( dExStyle & WS_EX_CONTEXTHELP ) == WS_EX_CONTEXTHELP )
  131. {
  132. rPos.right -= cxBtn;
  133. rPos.left  -= cxBtn;
  134. DrawFrameControl( hDc, &rPos, DFC_CAPTION, DFCS_CAPTIONHELP );
  135. }
  136. // Calc position and draw minimize and iconize button
  137. if ( ( dStyle & WS_MINIMIZEBOX ) == WS_MINIMIZEBOX )
  138. {
  139. // Minimize button
  140. rPos.right -= ( cxBtn - 2 );
  141. rPos.left  -= ( cxBtn - 2 );
  142. DrawFrameControl( hDc, &rPos, DFC_CAPTION, DFCS_CAPTIONMIN );
  143. // Iconize button
  144. rPos.right -= ( cxBtn - 2 );
  145. rPos.left  -= ( cxBtn - 2 );
  146. DrawFrameControl( hDc, &rPos, DFC_BUTTON, DFCS_BUTTONPUSH );
  147. // Draw a btnface
  148. DrawIconize( hDc, rPos.left, rPos.top, 0 );
  149. }
  150. }
  151. // Release device context
  152. ReleaseDC( hWnd, hDc );
  153. }
  154. }
  155. void CCaption::OnNcActivate( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, CHookItem * pItem )
  156. {
  157. // Call default window procedure
  158. CallWindowProc( pItem->m_DefWndProc, hWnd, Msg, wParam, lParam );
  159. // Send WM_NCPAINT message
  160. SendMessage( hWnd, WM_NCPAINT, 1, 0 );
  161. }
  162. void CCaption::OnNcPaint( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, CHookItem * pItem )
  163. {
  164. // Call default window procedure
  165. CallWindowProc( pItem->m_DefWndProc, hWnd, Msg, wParam, lParam );
  166. // Add iconize button to caption
  167. CCaption::DrawButtons( hWnd );
  168. }
  169. void CCaption::OnSetText( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, CHookItem * pItem )
  170. {
  171. // Call default window procedure
  172. CallWindowProc( pItem->m_DefWndProc, hWnd, Msg, wParam, lParam );
  173. // Redraw caption buttons
  174. CCaption::DrawButtons( hWnd );
  175. }
  176. void CCaption::OnNcLButtonDown( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, CHookItem * pItem )
  177. {
  178. HDC hDc ;
  179. POINT Pnt ;
  180. RECT rPos ;
  181. RECT rWin ;
  182. // Redraw caption buttons
  183. if ( ( hDc = GetWindowDC( hWnd ) ) != NULL )
  184. {
  185. // Convert mouse postion relative to caption rectangle
  186. GetWindowRect( hWnd, &rWin );
  187. Pnt.x = MAKEPOINTS( lParam ).x - rWin.left;
  188. Pnt.y = MAKEPOINTS( lParam ).y - rWin.top;
  189. // Calculate rectangle position of iconize button
  190. CCaption::CalcCaptionRect( hWnd, rPos );
  191. rPos.top    += 2;
  192. rPos.bottom -= 2;
  193. rPos.right  -= ( ( GetSystemMetrics( SM_CXSIZE ) * 3 ) - 2 );
  194. rPos.left    = rPos.right - ( GetSystemMetrics( SM_CXSIZE ) - 2 );
  195. // Check if mouse position is in rectangle
  196. if ( PtInRect( &rPos, Pnt ) )
  197. {
  198. pItem->m_LBtnDown = TRUE;
  199. DrawFrameControl( hDc, &rPos, DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_PUSHED );
  200. DrawIconize( hDc, rPos.left, rPos.top, 1 );
  201. }
  202. else
  203. CallWindowProc( pItem->m_DefWndProc, hWnd, Msg, wParam, lParam );
  204. DeleteObject( hDc );
  205. }
  206. }
  207. void CCaption::OnNcLButtonUp( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, CHookItem * pItem )
  208. {
  209. HDC hDc ;
  210. POINT Pnt ;
  211. RECT rPos ;
  212. RECT rWin ;
  213. // Redraw caption buttons
  214. if ( ( hDc = GetWindowDC( hWnd ) ) != NULL )
  215. {
  216. // Convert mouse postion relative to caption rectangle
  217. GetWindowRect( hWnd, &rWin );
  218. Pnt.x = MAKEPOINTS( lParam ).x - rWin.left;
  219. Pnt.y = MAKEPOINTS( lParam ).y - rWin.top;
  220. // Calculate rectangle position of iconize button
  221. CCaption::CalcCaptionRect( hWnd, rPos );
  222. rPos.top    += 2;
  223. rPos.bottom -= 2;
  224. rPos.right  -= ( ( GetSystemMetrics( SM_CXSIZE ) * 3 ) - 2 );
  225. rPos.left    = rPos.right - ( GetSystemMetrics( SM_CXSIZE ) - 2 );
  226. // Check if mouse position is in rectangle
  227. if ( PtInRect( &rPos, Pnt ) )
  228. {
  229. SendMessage( hWnd, WM_SYSCOMMAND, SC_MINIMIZE, 0 );
  230. ShowWindow( hWnd, SW_HIDE );
  231. g_TrayIcon.AddTrayIcon( hWnd );
  232. }
  233. else
  234. CallWindowProc( pItem->m_DefWndProc, hWnd, Msg, wParam, lParam );
  235. DrawFrameControl( hDc, &rPos, DFC_BUTTON, DFCS_BUTTONPUSH );
  236. DrawIconize( hDc, rPos.left, rPos.top, 0 );
  237. pItem->m_LBtnDown = FALSE;
  238. DeleteObject( hDc );
  239. }
  240. }
  241. void CCaption::OnNcMouseMove( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam, CHookItem * pItem )
  242. {
  243. HDC hDc ;
  244. POINT Pnt ;
  245. RECT rPos ;
  246. RECT rWin ;
  247. // Redraw caption buttons
  248. if ( ( hDc = GetWindowDC( hWnd ) ) != NULL )
  249. {
  250. // Convert mouse postion relative to caption rectangle
  251. GetWindowRect( hWnd, &rWin );
  252. Pnt.x = MAKEPOINTS( lParam ).x - rWin.left;
  253. Pnt.y = MAKEPOINTS( lParam ).y - rWin.top;
  254. // Calculate rectangle position of iconize button
  255. CCaption::CalcCaptionRect( hWnd, rPos );
  256. rPos.top    += 2;
  257. rPos.bottom -= 2;
  258. rPos.right  -= ( ( GetSystemMetrics( SM_CXSIZE ) * 3 ) - 2 );
  259. rPos.left    = rPos.right - ( GetSystemMetrics( SM_CXSIZE ) - 2 );
  260. // Check if mouse position is in rectangle
  261. if ( PtInRect( &rPos, Pnt ) )
  262. {
  263. DrawFrameControl( hDc, &rPos, DFC_BUTTON, ( pItem ->m_LBtnDown ) ? DFCS_BUTTONPUSH | DFCS_PUSHED : DFCS_BUTTONPUSH );
  264. DrawIconize( hDc, rPos.left, rPos.top, ( pItem ->m_LBtnDown ) ? 1 : 0 );
  265. }
  266. else
  267. {
  268. pItem->m_LBtnDown = FALSE;
  269. DrawFrameControl( hDc, &rPos, DFC_BUTTON, DFCS_BUTTONPUSH );
  270. DrawIconize( hDc, rPos.left, rPos.top, 0 );
  271. CallWindowProc( pItem->m_DefWndProc, hWnd, Msg, wParam, lParam );
  272. }
  273. DeleteObject( hDc );
  274. }
  275. }