home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / button.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-12  |  2.6 KB  |  103 lines

  1. #pragma once
  2.  
  3. //-----------------------------------------------------------------------------------//
  4. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  5. //                             ISBN  0-13-086985-6                                   //
  6. //                                                                                   //
  7. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  8. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  9. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  10. //                                                                                   //
  11. //  FileName   : button.h                                                             //
  12. //  Description: Clickable button without window                                     //
  13. //  Version    : 1.00.000, May 31, 2000                                              //
  14. //-----------------------------------------------------------------------------------//
  15.  
  16. class KButton
  17. {
  18. protected:
  19.     HRGN m_hRegion; 
  20.     bool m_bOn;
  21.     int  m_x, m_y, m_w, m_h;
  22.  
  23. public:
  24.  
  25.     KButton()
  26.     {
  27.         m_hRegion = NULL;
  28.         m_bOn     = false;
  29.     }
  30.  
  31.     virtual ~KButton()
  32.     {
  33.     }
  34.  
  35.     void DefineButton(int x, int y, int w, int h)
  36.     {
  37.         m_x = x; m_y = y; m_w = w; m_h = h;
  38.     }
  39.  
  40.     virtual void DrawButton(HDC hDC)
  41.     {
  42.     }
  43.  
  44.     void UpdateButton(HDC hDC, LPARAM xy)
  45.     {
  46.         if ( m_bOn != IsOnButton(xy) )
  47.         {
  48.             m_bOn = ! m_bOn;
  49.             DrawButton(hDC);
  50.         }
  51.     }
  52.     
  53.     bool IsOnButton(LPARAM xy) const
  54.     {
  55.         return PtInRegion(m_hRegion, LOWORD(xy), HIWORD(xy)) != 0;
  56.     }
  57. };
  58.  
  59.  
  60. class KRectButton : public KButton
  61. {
  62. public:
  63.     void DrawButton(HDC hDC)
  64.     {
  65.         RECT rect = { m_x, m_y, m_x+m_w, m_y+m_h };
  66.  
  67.         if ( m_hRegion == NULL )
  68.             m_hRegion = CreateRectRgnIndirect(& rect);
  69.  
  70.         InflateRect(&rect, 2, 2);
  71.         FillRect(hDC, & rect, GetSysColorBrush(COLOR_BTNFACE));
  72.         InflateRect(&rect, -2, -2);
  73.  
  74.         DrawFrameControl(hDC, &rect, DFC_CAPTION, DFCS_CAPTIONHELP | (m_bOn ? 0 : DFCS_INACTIVE));
  75.     }
  76. };
  77.  
  78.  
  79. class KEllipseButton : public KButton
  80. {
  81. public:
  82.     void DrawButton(HDC hDC)
  83.     {
  84.         RECT rect = { m_x, m_y, m_x+m_w, m_y+m_h };
  85.  
  86.         if ( m_hRegion == NULL )
  87.             m_hRegion = CreateEllipticRgnIndirect(& rect);
  88.  
  89.         if ( m_bOn )
  90.         {
  91.             FillRgn(hDC, m_hRegion, GetSysColorBrush(COLOR_CAPTIONTEXT));
  92.             FrameRgn(hDC, m_hRegion, GetSysColorBrush(COLOR_ACTIVEBORDER), 2, 2);
  93.         }
  94.         else
  95.         {
  96.             FillRgn(hDC, m_hRegion, GetSysColorBrush(COLOR_INACTIVECAPTIONTEXT));
  97.             FrameRgn(hDC, m_hRegion, GetSysColorBrush(COLOR_INACTIVEBORDER), 2, 2);
  98.         }
  99.     }
  100. };
  101.  
  102.  
  103.