home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / CheckMark.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-11  |  2.8 KB  |  96 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : checkmark.cpp                                                         //
  10. //  Description: Using DDB as menu checkmarks                                        //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16.  
  17. #include <windows.h>
  18. #include <assert.h>
  19. #include <tchar.h>
  20.  
  21. #include "CheckMark.h"
  22.  
  23.  
  24. void KCheckMark::AddBitmap(int id, HBITMAP hBmp)
  25. {
  26.     if ( m_nUsed < MAXSUBIMAGES )
  27.     {
  28.         m_nSubImageId[m_nUsed]   = id;
  29.         m_hSubImage  [m_nUsed++] = hBmp;
  30.     }
  31. }
  32.  
  33.  
  34. void KCheckMark::LoadToolbar(HMODULE hModule, int resid, bool transparent)
  35. {
  36.     m_hBmp = (HBITMAP) ::LoadImage(hModule, MAKEINTRESOURCE(resid), IMAGE_BITMAP, 0, 0, 
  37.         transparent ? LR_LOADTRANSPARENT : 0);
  38.     AddBitmap((int) hModule + resid, m_hBmp);    // not to be reused as subimage
  39. }
  40.  
  41.  
  42. KCheckMark::~KCheckMark()
  43. {
  44.     for (int i=0; i<m_nUsed; i++)
  45.         DeleteObject(m_hSubImage[i]);
  46. }
  47.  
  48.  
  49. HBITMAP KCheckMark::GetSubImage(int id)
  50. {
  51.     if ( id < 0 )
  52.         return NULL;
  53.  
  54.     for (int i=0; i<m_nUsed; i++)
  55.         if ( m_nSubImageId[i]==id )
  56.             return m_hSubImage[i];
  57.  
  58.     BITMAP bmp;
  59.  
  60.     if ( ! GetObject(m_hBmp, sizeof(bmp), & bmp) )
  61.         return NULL;
  62.  
  63.     if ( id *bmp.bmHeight >= bmp.bmWidth )
  64.         return NULL;
  65.  
  66.     HDC hMemDCS = CreateCompatibleDC(NULL);
  67.     HDC hMemDCD = CreateCompatibleDC(NULL);
  68.  
  69.     SelectObject(hMemDCS, m_hBmp);
  70.  
  71.     int w = GetSystemMetrics(SM_CXMENUCHECK);
  72.     int h = GetSystemMetrics(SM_CYMENUCHECK);
  73.     
  74.     HBITMAP hRslt = CreateCompatibleBitmap(hMemDCS, w, h);
  75.  
  76.     if  ( hRslt )
  77.     {
  78.         HGDIOBJ hOld = SelectObject(hMemDCD, hRslt);
  79.         StretchBlt(hMemDCD, 0, 0, w, h, hMemDCS, id*bmp.bmHeight, 0, bmp.bmHeight, bmp.bmHeight, SRCCOPY);
  80.         SelectObject(hMemDCD, hOld);
  81.         AddBitmap(id, hRslt);
  82.     }
  83.  
  84.     DeleteObject(hMemDCS);
  85.     DeleteObject(hMemDCD);
  86.  
  87.     return hRslt;
  88. }
  89.  
  90.  
  91. BOOL KCheckMark::SetCheckMarks(HMENU hMenu, UINT uPos, UINT uFlags, int unchecked, int checked)
  92. {
  93.     return SetMenuItemBitmaps(hMenu, uPos, uFlags, GetSubImage(unchecked), GetSubImage(checked));
  94. }
  95.  
  96.