home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / APPLAUNC.PAK / APPBTN.CPP next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  2.7 KB  |  108 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1995 by Borland International, All Rights Reserved
  4. //----------------------------------------------------------------------------
  5. #include <owl/pch.h>
  6. #include <owl/gdiobjec.h>
  7. #include <owl/dc.h>
  8. #include "appbtn.h"
  9. #include "applaunc.rh"
  10.  
  11. int TAppButton::ButtonPixelSize = 32;
  12.  
  13. //
  14. // Constructor.  Construct from a bitmap and id.
  15. //
  16. TAppButton::TAppButton(const TBitmap& bitmap, int id)
  17.   : TButtonGadget(0, CM_DUMMY), RealId(id)
  18. {
  19.   CreateGlyph(bitmap);
  20. }
  21.  
  22. //
  23. // Constructor.  Construct given path to icon file and id.  An HINSTANCE
  24. // is passed for use in extracting the icon from the given file.
  25. //
  26. TAppButton::TAppButton(HINSTANCE hInst, const string& iconPath, int id)
  27.   : TButtonGadget(0, CM_DUMMY, Command, true), RealId(id), Glyph(0)
  28. {
  29.   if (hInst)
  30.     HInst = hInst;
  31.  
  32.   TBitmap   bitmap(TScreenDC(), TAppButton::ButtonPixelSize,
  33.                                 TAppButton::ButtonPixelSize);
  34.   BitmapFromIconPath(iconPath, bitmap);
  35.   CreateGlyph(bitmap);
  36. }
  37.  
  38. //
  39. // Destructor.  Delete the glyph that was created in ctor.
  40. //
  41. TAppButton::~TAppButton()
  42. {
  43.   delete Glyph;
  44. }
  45.  
  46. //
  47. // GetGlyphDib().  OWL calls this virtual to get the bitmap to be drawn on
  48. // the button.
  49. //
  50. TDib*
  51. TAppButton::GetGlyphDib()
  52. {
  53.   return Glyph;
  54. }
  55.  
  56. //
  57. // ReleaseGlyphDib().  Called by OWL to delete the dib returned by
  58. // GetGlyphDib().  Since the dib is created in the constructor it
  59. // is not deleted here.
  60. //
  61. void
  62. TAppButton::ReleaseGlyphDib(TDib*)
  63. {
  64. }
  65.  
  66. //
  67. // CreateGlyph().  Create and return a dib, created from given bitmap.
  68. //
  69. void
  70. TAppButton::CreateGlyph(const TBitmap& bitmap)
  71. {
  72.   delete Glyph;
  73.   Glyph = new TDib(bitmap);
  74. }
  75.  
  76. //
  77. // BitmapFromIconPath().  Create a TBitmap from an icon, specified by
  78. // 'iconPath'.
  79. //
  80. void
  81. TAppButton::BitmapFromIconPath(const string& iconPath, TBitmap& bitmap)
  82. {
  83.   int freeIcon = 0;
  84.  
  85.   // extract icon from source.
  86.   //
  87.   HICON icon = ExtractIcon(HInst, iconPath.c_str(), 0);
  88.  
  89.   // Use question icon if icon could not be found.
  90.   //
  91.   if (!icon || (int)icon == 1)
  92.     icon = LoadIcon(0, IDI_QUESTION);
  93.   else
  94.     freeIcon = 1;
  95.  
  96.   // create bitmap from icon to be used with button gadget.
  97.   //
  98.   TMemoryDC memDC;              // screen capatable DC.
  99.  
  100.   memDC.SelectObject(bitmap);   // Use given bitmap to draw into.
  101.                                 // Initialize bitmap with button-face color.
  102.   memDC.TextRect(0, 0, TAppButton::ButtonPixelSize, TAppButton::ButtonPixelSize,
  103.                  GetSysColor(COLOR_WINDOW));
  104.   memDC.DrawIcon(0, 0, TIcon(icon));
  105.   if (freeIcon)
  106.     ::DestroyIcon(icon);
  107. }
  108.