home *** CD-ROM | disk | FTP | other *** search
/ PC Administrator / spravce.iso / RunModule / src / CheckBoxImage.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-08-31  |  6.1 KB  |  181 lines

  1. // CheckBoxImage.cpp: implementation of the CCheckBoxImage class.
  2. //
  3. /*
  4. Copyright 2001 Anish Mistry. All rights reserved.
  5.  
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8.  
  9.    1. Redistributions of source code must retain the above copyright notice, 
  10.    this list of conditions and the following disclaimer.
  11.    2. Redistributions in binary form must reproduce the above copyright notice,
  12.    this list of conditions and the following disclaimer in the documentation 
  13.    and/or other materials provided with the distribution.
  14.  
  15. THIS SOFTWARE IS PROVIDED BY ANISH MISTRY ``AS IS'' AND ANY EXPRESS OR IMPLIED 
  16. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
  17. AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS 
  18. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  19. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  20. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  22. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24.  
  25. The views and conclusions contained in the software and documentation are those
  26. of the authors and should not be interpreted as representing official policies,
  27. either expressed or implied, of Anish Mistry or AM Productions.
  28.  
  29. * Variation of the FreeBSD License. http://www.freebsd.org/copyright/freebsd-license.html
  30. */
  31. //////////////////////////////////////////////////////////////////////
  32.  
  33. #include "stdafx.h"
  34. #include "CheckBoxImage.h"
  35.  
  36. //////////////////////////////////////////////////////////////////////
  37. // Construction/Destruction
  38. //////////////////////////////////////////////////////////////////////
  39.  
  40. CCheckBoxImage::CCheckBoxImage()
  41. {
  42.     // store the this pointer for use in a static function
  43.     CButtonImage::CButtonImage();
  44.     textSize.cx = 0;
  45.     textSize.cy = 0;
  46. }
  47.  
  48. CCheckBoxImage::~CCheckBoxImage()
  49. {
  50.     CButtonImage::~CButtonImage();
  51. }
  52.  
  53. void CCheckBoxImage::Create(UINT ID,HWND parent,HINSTANCE parentInstance,BYTE imageFlags,SIZE imgSz)
  54. {
  55.     CButtonImage::Create(ID,parent,parentInstance,imageFlags,imgSz);
  56.     HDC buttonDC = GetWindowDC(ctrlWnd);
  57.     HFONT font = (HFONT)SendMessage(ctrlWnd,WM_GETFONT,0,0);
  58.     HFONT oldFont = (HFONT)SelectObject(buttonDC,font);
  59.     GetTextExtentPoint32(buttonDC,"O",1,&textSize);
  60.     SelectObject(buttonDC,oldFont);
  61.      ReleaseDC(ctrlWnd,buttonDC);
  62. }
  63.  
  64. unsigned int CCheckBoxImage::GetCheckState()
  65. {
  66.     return CButtonImage::OnGetState();
  67. }
  68.  
  69. void CCheckBoxImage::SetCheckState(WPARAM state)
  70. {
  71.     CButtonImage::OnSetState(state);
  72. }
  73.  
  74. void CCheckBoxImage::DrawCheckBox(const HDC buttonDC,const unsigned int styles, POINT *tempPoint)
  75. {
  76.     RECT checkboxRect = {tempPoint->x,tempPoint->y,tempPoint->x+=textSize.cy,tempPoint->y+textSize.cy};
  77.     POINT recenteredPoint = *tempPoint;
  78.     recenteredPoint.y += imageSize.cy/2;
  79.     recenteredPoint.y -= textSize.cy/2;
  80.     checkboxRect.top = recenteredPoint.y;
  81.     checkboxRect.bottom = checkboxRect.top+textSize.cy;
  82.     DrawFrameControl(buttonDC,&checkboxRect,DFC_BUTTON,styles );
  83. }
  84.  
  85.  
  86. bool CCheckBoxImage::Draw(HDC originalButtonDC)
  87. {// begin Draw
  88.  
  89.     // get button info
  90.     RECT buttonRect = {NULL};
  91.     GetClientRect(ctrlWnd,&buttonRect);
  92.  
  93.     if(imageTopLeft.x < 0)
  94.         imageTopLeft.x = 0;
  95.  
  96.     SIZE buttonSize = {buttonRect.right,buttonRect.bottom};
  97.  
  98.     HDC buttonDC = CreateCompatibleDC(originalButtonDC);
  99.     HBITMAP buttonBitmap = CreateCompatibleBitmap(originalButtonDC,buttonRect.right,buttonRect.bottom);
  100.     HGDIOBJ oldButtonBitmap = SelectObject(buttonDC,buttonBitmap);
  101.     if(buttonDC != NULL)
  102.     {// begin draw image
  103.         POINT tempPoint = imageTopLeft;
  104.  
  105.         unsigned long int tempStyles = 0;//buttonStyles;
  106.         tempStyles |= DFCS_BUTTONCHECK;    // set so it shows a check instead of a button
  107.  
  108.         if((m_nStyles & BS_PUSHLIKE) == BS_PUSHLIKE)
  109.             tempStyles |= DFCS_BUTTONPUSH;
  110.         if(IsDown)
  111.             tempStyles |= DFCS_PUSHED;
  112.  
  113.         // select the default button color brush to draw area with
  114.         HBRUSH brush = CreateSolidBrush(buttonColor);
  115.         HGDIOBJ oldBrush = SelectObject(buttonDC,brush);
  116.         // select the default button color pen to draw area with
  117.         HPEN pen = CreatePen(PS_SOLID,1,buttonColor);
  118.         HGDIOBJ oldPen = SelectObject(buttonDC,pen);
  119.         // draw face
  120.         // draw face
  121.         RECT tempRectangle = buttonRect;
  122.         tempRectangle.left -= 1;
  123.         tempRectangle.top -= 1;
  124.         FillRect(buttonDC,&tempRectangle,brush);
  125.  
  126.         if(buttonState == BST_CHECKED)
  127.             tempStyles |= DFCS_CHECKED;
  128.  
  129.         DrawCheckBox(buttonDC,tempStyles,&tempPoint);
  130.         tempPoint.x += borderSize.cx;
  131.  
  132.         // draw image
  133.         tempPoint.x += borderSize.cx*2;
  134.         DrawButtonImage(buttonDC,tempPoint);
  135.         tempPoint.x += borderSize.cx*2;
  136.         buttonRect.left = tempPoint.x;
  137.         // draw the text
  138.         RECT buttonTextRect = DrawButtonText(buttonDC,tempPoint,buttonRect);
  139.         // draw focus rect if the button is active
  140.         if(GetFocus() == ctrlWnd)
  141.         {
  142.             RECT temp = buttonTextRect;
  143.             ExpandRectangle(1,&temp);
  144.             DrawFocusRect(buttonDC,&temp);
  145.         }
  146.         
  147.         // copy to originalButtonDC
  148.         BitBlt(originalButtonDC,buttonTopLeft.x,buttonTopLeft.y,buttonSize.cx,buttonSize.cy,buttonDC,buttonTopLeft.x,buttonTopLeft.y,SRCCOPY);
  149.  
  150.         // clean up
  151.         SelectObject(buttonDC,oldButtonBitmap);
  152.         SelectObject(buttonDC,oldBrush);
  153.         SelectObject(buttonDC,oldPen);
  154.         DeleteObject(pen);
  155.         DeleteObject(brush);
  156.         DeleteObject(buttonBitmap);
  157.         DeleteObject(oldButtonBitmap);
  158.         DeleteDC(buttonDC);
  159.  
  160.         return true;
  161.     }// end draw image
  162.     return false;
  163. }// end Draw
  164.  
  165. RECT CCheckBoxImage::DrawButtonText(const HDC buttonDC, POINT &textPos, const RECT buttonRect)
  166. {
  167.     char buttonText[MAX_PATH] = {NULL};
  168.     GetWindowText(ctrlWnd,buttonText,MAX_PATH-1);
  169.     RECT buttonTextRect = buttonRect;
  170.  
  171.     if((borderSize.cx != 0) || (borderSize.cy != 0))
  172.         ContractRectangle(borderSize,&buttonTextRect);
  173.  
  174.     buttonTextRect.left = textPos.x+imageSize.cx+borderSize.cx;
  175.  
  176.     DrawControlText(buttonDC,buttonText,buttonTextRect);
  177.  
  178.     return buttonTextRect;
  179. }
  180.  
  181.