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

  1. // GroupBoxImage.cpp: implementation of the CGroupBoxImage 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 "GroupBoxImage.h"
  35.  
  36. //////////////////////////////////////////////////////////////////////
  37. // Construction/Destruction
  38. //////////////////////////////////////////////////////////////////////
  39.  
  40. CGroupBoxImage::CGroupBoxImage()
  41. {
  42.     CButtonImage::CButtonImage();
  43. }
  44.  
  45. CGroupBoxImage::~CGroupBoxImage()
  46. {
  47.     CButtonImage::~CButtonImage();
  48. }
  49.  
  50. void CGroupBoxImage::Create(UINT ID,HWND parent,HINSTANCE parentInstance,BYTE imageFlags,SIZE imgSz)
  51. {
  52.     // calculate image position
  53. /*    RECT buttonRect;
  54.     GetWindowRect(ctrlWnd,&buttonRect);
  55.     */
  56.     CButtonImage::Create(ID,parent,parentInstance,imageFlags,imgSz);
  57.     HDC buttonDC = GetWindowDC(ctrlWnd);
  58.     HFONT font = (HFONT)SendMessage(ctrlWnd,WM_GETFONT,0,0);
  59.     HFONT oldFont = (HFONT)SelectObject(buttonDC,font);
  60.     char windowText[MAX_PATH] = {NULL};
  61.     GetWindowText(ctrlWnd,windowText,MAX_PATH-1);
  62.     int textLength = lstrlen(windowText);
  63.     GetTextExtentPoint32(buttonDC,windowText,textLength,&textSize);
  64.     SIZE oneCharSize = {NULL};
  65.     GetTextExtentPoint32(buttonDC,"O",1,&oneCharSize);
  66.     averageCharWidth = oneCharSize.cx;
  67.     SelectObject(buttonDC,oldFont);
  68.      ReleaseDC(ctrlWnd,buttonDC);
  69. //    imageTopLeft.x = textSize.cy/2;
  70. }
  71.  
  72. RECT CGroupBoxImage::DrawGroupBox(HDC buttonDC)
  73. {
  74.     RECT buttonRect = {NULL};
  75.     GetClientRect(ctrlWnd,&buttonRect);
  76.     SIZE titleSize = {NULL};
  77.     titleSize.cx = averageCharWidth+borderSize.cx+textSize.cx+imageSize.cx;
  78.     if(textSize.cy > imageSize.cy)
  79.         titleSize.cy = textSize.cy;
  80.     else
  81.         titleSize.cy = imageSize.cy;
  82.  
  83.     RECT textRect = {averageCharWidth,0,titleSize.cx,titleSize.cy};
  84.     RECT hiliteGroupBoxRect = {1,textSize.cy/2+1,buttonRect.right,buttonRect.bottom};
  85.     RECT sunkenGroupBoxRect = {0,textSize.cy/2,buttonRect.right-1,buttonRect.bottom-1};
  86.     
  87.     if(firstDraw)
  88.     {
  89.         RECT groupBoxInterior = sunkenGroupBoxRect;
  90.         groupBoxInterior.right += 1;
  91.         groupBoxInterior.bottom += 1;
  92.         HRGN groupBoxRgn = CreateRectRgnIndirect(&groupBoxInterior);
  93.         HRGN textRgn = CreateRectRgnIndirect(&textRect);
  94.         ContractRectangle(2,&groupBoxInterior);
  95.         HRGN interiorGroupBoxRgn = CreateRectRgnIndirect(&groupBoxInterior);
  96.         CombineRgn(groupBoxRgn,groupBoxRgn,interiorGroupBoxRgn,RGN_DIFF);
  97.         CombineRgn(groupBoxRgn,groupBoxRgn,textRgn,RGN_OR);
  98.         SetWindowRgn(ctrlWnd,groupBoxRgn,true);    // don't delete groupBoxRgn, because a copy is not made
  99.         DeleteObject(textRgn);
  100.         DeleteObject(interiorGroupBoxRgn);
  101.         firstDraw = false;
  102.     }
  103.  
  104. //    COLORREF sunkenColor = RGB(LevelColorBottom(GetRValue(fontColor)+128),LevelColorBottom(GetGValue(fontColor)+128),LevelColorBottom(GetBValue(fontColor)+128));
  105. //    COLORREF hiliteColor = RGB(LevelColorTop(GetRValue(fontColor)+216),LevelColorTop(GetGValue(fontColor)+216),LevelColorTop(GetBValue(fontColor)+216));
  106.     COLORREF sunkenColor = GetShadowColor(buttonColor);
  107.     COLORREF hiliteColor = GetHiliteColor(buttonColor);
  108.     HBRUSH hiliteColorBrush = CreateSolidBrush(hiliteColor);
  109.     HBRUSH sunkenColorBrush = CreateSolidBrush(sunkenColor);
  110.     FrameRect(buttonDC,&hiliteGroupBoxRect,hiliteColorBrush);
  111.     FrameRect(buttonDC,&sunkenGroupBoxRect,sunkenColorBrush);
  112.     // fill in the 4 pixels that were missed
  113.     SetPixel(buttonDC,sunkenGroupBoxRect.left,sunkenGroupBoxRect.bottom,hiliteColor);
  114.     SetPixel(buttonDC,sunkenGroupBoxRect.right,sunkenGroupBoxRect.top,hiliteColor);
  115.     SetPixel(buttonDC,textRect.right,textRect.bottom,buttonColor);
  116.     SetPixel(buttonDC,textRect.right,textRect.top,buttonColor);
  117.  
  118.     DeleteObject(sunkenColorBrush);
  119.     DeleteObject(hiliteColorBrush);
  120.     return textRect;
  121. }
  122.  
  123. bool CGroupBoxImage::Draw(HDC originalButtonDC)
  124. {// begin Draw
  125.  
  126.     // get button info
  127.     RECT buttonRect = {NULL};
  128.     GetClientRect(ctrlWnd,&buttonRect);
  129.  
  130.     if(imageTopLeft.x < 0)
  131.         imageTopLeft.x = 0;
  132.  
  133.     SIZE buttonSize = {buttonRect.right,buttonRect.bottom};
  134.  
  135.     HDC buttonDC = CreateCompatibleDC(originalButtonDC);
  136.     HBITMAP buttonBitmap = CreateCompatibleBitmap(originalButtonDC,buttonRect.right,buttonRect.bottom);
  137.     HGDIOBJ oldButtonBitmap = SelectObject(buttonDC,buttonBitmap);
  138.     if(buttonDC != NULL)
  139.     {// begin draw image
  140.  
  141. /*        // select the default button color brush to draw area with
  142.         HGDIOBJ oldBrush = SelectObject(buttonDC,brush);
  143.         // select the default button color pen to draw area with
  144.         HPEN pen = CreatePen(PS_SOLID,1,buttonColor);
  145.         HGDIOBJ oldPen = SelectObject(buttonDC,pen);*/
  146.  
  147.         RECT buttonTextRect = DrawGroupBox(buttonDC);
  148.         POINT tempPoint = {buttonTextRect.left,buttonTextRect.top};
  149.         // clear area for image and text
  150.         // draw face
  151.         HBRUSH brush = CreateSolidBrush(buttonColor);
  152.         buttonTextRect.right -= 1;
  153.         FillRect(buttonDC,&buttonTextRect,brush);
  154.         buttonTextRect.right += 1;
  155.         DeleteObject(brush);
  156. //        Rectangle(buttonDC,buttonTextRect.left,buttonTextRect.top,buttonTextRect.right+1,buttonTextRect.bottom+1);
  157.         // draw image
  158.         DrawButtonImage(buttonDC,tempPoint);
  159.         // draw the text
  160.         DrawButtonText(buttonDC,tempPoint,buttonTextRect);
  161.         
  162.         // copy to originalButtonDC
  163.         BitBlt(originalButtonDC,buttonTopLeft.x,buttonTopLeft.y,buttonSize.cx,buttonSize.cy,buttonDC,buttonTopLeft.x,buttonTopLeft.y,SRCCOPY);
  164.  
  165.         // clean up
  166.         SelectObject(buttonDC,oldButtonBitmap);
  167. /*        SelectObject(buttonDC,oldBrush);
  168.         SelectObject(buttonDC,oldPen);
  169.         DeleteObject(pen);
  170.         */
  171.         DeleteObject(buttonBitmap);
  172.         DeleteObject(oldButtonBitmap);
  173.         DeleteDC(buttonDC);
  174.  
  175.         return true;
  176.     }// end draw image
  177.     return false;
  178. }// end Draw