home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / APPLAUNC.PAK / APPBTN.CPP next >
C/C++ Source or Header  |  1995-08-29  |  3KB  |  107 lines

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