home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / tpw / owldemos / bitbtn.pas < prev    next >
Pascal/Delphi Source File  |  1991-05-20  |  7KB  |  263 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal for Windows                     }
  4. {   Demo library (DLL)                           }
  5. {   Copyright (c) 1991 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. library BitBtn;
  10.  
  11. uses WinTypes, WinProcs;
  12.  
  13. {$R BITBTN.RES}
  14.  
  15. const
  16.   ofState       = 0;
  17.   ofDownBits    = 2;
  18.   ofUpBits      = 4;
  19.   ofFocUpBits   = 6;
  20.   ofSize        = 8; { Amount of window extra bytes to use }
  21.  
  22. const
  23.   bdBorderWidth = 1;
  24.  
  25. const
  26.   bsDisabled    = $0001;
  27.   bsFocus       = $0002;
  28.   bsKeyDown     = $0004;
  29.   bsMouseDown   = $0008;
  30.   bsMouseUpDown = $0010;
  31.   bsDefault     = $0020;
  32.  
  33. function BitButtonWinFn(HWindow: HWnd; Message: Word; wParam: Word;
  34.   lParam: Longint): Longint; export;
  35. var
  36.   DC: HDC;
  37.   BitsNumber: Integer;
  38.   Bitmap: TBitmap;
  39.   Rect: TRect;
  40.   Pt: TPoint;
  41.   PS: TPaintStruct;
  42.  
  43. function Get(Ofs: Integer): Word;
  44. begin
  45.   Get := GetWindowWord(HWindow, Ofs);
  46. end;
  47.  
  48. procedure SetWord(Ofs: Integer; Val: Word);
  49. begin
  50.   SetWindowWord(HWindow, Ofs, Val);
  51. end;
  52.  
  53. function State: Word;
  54. begin
  55.   State := Get(ofState);
  56. end;
  57.  
  58. function DownBits: Word;
  59. begin
  60.   DownBits := Get(ofDownBits);
  61. end;
  62.  
  63. function UpBits: Word;
  64. begin
  65.   UpBits := Get(ofUpBits);
  66. end;
  67.  
  68. function FocUpBits: Word;
  69. begin
  70.   FocUpBits := Get(ofFocUpBits);
  71. end;
  72.  
  73. function GetState(AState: Word): Boolean;
  74. begin
  75.   GetState := (State and AState) = AState;
  76. end;
  77.  
  78. procedure Paint(DC: HDC);
  79. var
  80.   MemDC: HDC;
  81.   Bits, Oldbitmap: HBitmap;
  82.   BorderBrush, OldBrush: HBrush;
  83.   Frame: TRect;
  84.   Height, Width: Integer;
  85. begin
  86.   if (State and (bsMouseDown + bsKeyDown) <> 0) and
  87.       not GetState(bsMouseUpDown) then
  88.     Bits := DownBits
  89.   else
  90.     if GetState(bsFocus) then Bits := FocUpBits
  91.     else Bits := UpBits;
  92.  
  93.   { Draw border }
  94.   GetClientRect(HWindow, Frame);
  95.   Height := Frame.bottom - Frame.top;
  96.   Width := Frame.right - Frame.left;
  97.  
  98.   if GetState(bsDefault) then
  99.     BorderBrush := GetStockObject(Black_Brush)
  100.   else BorderBrush := GetStockObject(White_Brush);
  101.   OldBrush := SelectObject(DC, BorderBrush);
  102.   PatBlt(DC, Frame.left, Frame.top, Width, bdBorderWidth, PatCopy);
  103.   PatBlt(DC, Frame.left, Frame.top, bdBorderWidth, Height, PatCopy);
  104.   PatBlt(DC, Frame.left, Frame.bottom - bdBorderWidth, Width,
  105.     bdBorderWidth, PatCopy);
  106.   PatBlt(DC, Frame.right - bdBorderWidth, Frame.top, bdBorderWidth,
  107.     Height, PatCopy);
  108.   SelectObject(DC, OldBrush);
  109.  
  110.   { Draw bitmap }
  111.   MemDC := CreateCompatibleDC(DC);
  112.   OldBitmap := SelectObject(MemDC, Bits);
  113.   GetObject(Bits, Sizeof(Bitmap), @Bitmap);
  114.   BitBlt(DC, bdBorderWidth, bdBorderWidth, Bitmap.bmWidth, Bitmap.bmHeight,
  115.     MemDC, 0, 0, srcCopy);
  116.   SelectObject(MemDC, OldBitmap);
  117.   DeleteDC(MemDC);
  118. end;
  119.  
  120. procedure Repaint;
  121. var
  122.   DC: HDC;
  123. begin
  124.   DC := GetDC(HWindow);
  125.   Paint(DC);
  126.   ReleaseDC(HWindow, DC);
  127. end;
  128.  
  129. procedure SetState(AState: Word; Enable: Boolean);
  130. var
  131.   OldState: Word;
  132. begin
  133.   OldState := State;
  134.   if Enable then SetWord(ofState, State or AState)
  135.   else SetWord(ofState, State and not AState);
  136.   if State <> OldState then Repaint;
  137. end;
  138.  
  139. function InMe(lPoint: Longint): Boolean;
  140. var
  141.   R: TRect;
  142.   Point: TPoint absolute lPoint;
  143. begin
  144.   GetClientRect(HWindow, R);
  145.   InflateRect(R, -bdBorderWidth, -bdBorderWidth);
  146.   InMe := PtInRect(R, Point);
  147. end;
  148.  
  149. procedure ButtonPressed;
  150. begin
  151.   SetState(bsMouseDown + bsMouseUpDown + bsKeyDown, False);
  152.   SendMessage(GetParent(HWindow), wm_Command, GetDlgCtrlID(HWindow),
  153.     Longint(HWindow));
  154. end;
  155.  
  156. begin
  157.   BitButtonWinFn := 0;
  158.   case Message of
  159.     wm_Create:
  160.       begin
  161.         DC := GetDC(0);
  162.         if (GetSystemMetrics(sm_CYScreen) < 480) or
  163.            (GetDeviceCaps(DC, numColors) < 16) then
  164.           BitsNumber := 2000 + Get(gww_ID)
  165.         else
  166.           BitsNumber := 1000 + Get(gww_ID);
  167.         ReleaseDC(0, DC);
  168.  
  169.         SetWord(ofUpBits, LoadBitmap(hInstance, PChar(BitsNumber)));
  170.         SetWord(ofDownBits, LoadBitmap(hInstance, pChar(BitsNumber + 2000)));
  171.         SetWord(ofFocUpBits, LoadBitmap(hInstance, pChar(BitsNumber + 4000)));
  172.         GetObject(DownBits, SizeOf(Bitmap), @Bitmap);
  173.         GetWindowRect(HWindow, Rect);
  174.         Pt.X := Rect.Left;
  175.         Pt.Y := Rect.Top;
  176.         ScreenToClient(PCreateStruct (lParam)^.hwndParent, Pt);
  177.         MoveWindow(HWindow, Pt.X, Pt.Y,
  178.           Bitmap.bmWidth + bdBorderWidth * 2,
  179.           Bitmap.bmHeight + bdBorderWidth * 2, False);
  180.         if (PCreateStruct(lParam)^.style and $1F) = bs_DefPushButton then
  181.           SetState(bsDefault, True);
  182.       end;
  183.     wm_NCDestroy:
  184.       begin
  185.         BitButtonWinFn := DefWindowProc(HWindow, Message, wParam, lParam);
  186.         DeleteObject(UpBits);
  187.         DeleteObject(DownBits);
  188.         DeleteObject(FocUpBits);
  189.       end;
  190.     wm_Paint:
  191.       begin
  192.         BeginPaint(HWindow, PS);
  193.         Paint(PS.hDC);
  194.         EndPaint(HWindow, PS);
  195.       end;
  196.     wm_EraseBkGnd:
  197.       begin
  198.       end;
  199.     wm_Enable:
  200.       SetState(bsDisabled, wParam <> 0);
  201.     wm_SetFocus:
  202.       SetState(bsFocus, True);
  203.     wm_KillFocus:
  204.       SetState(bsFocus, False);
  205.     wm_KeyDown:
  206.       if (wParam = $20) and not GetState(bsKeyDown) and
  207.           not GetState(bsMouseDown) then
  208.         SetState(bsKeyDown, True);
  209.     wm_KeyUP:
  210.       if (wParam = $20) and GetState(bsKeyDown) then
  211.         ButtonPressed;
  212.     wm_LButtonDblClk, wm_LButtonDown:
  213.       if InMe(lParam) and not GetState(bsKeyDown) then
  214.       begin
  215.         if GetFocus <> HWindow then SetFocus(HWindow);
  216.         SetState(bsMouseDown, True);
  217.         SetCapture(HWindow);
  218.       end;
  219.     wm_MouseMove:
  220.       if GetState(bsMouseDown) then
  221.         SetState(bsMouseUpDown, not InMe(lParam));
  222.     wm_LButtonUp:
  223.       if GetState(bsMouseDown) then
  224.       begin
  225.         ReleaseCapture;
  226.         if not GetState(bsMouseUpDown) then ButtonPressed
  227.         else SetState(bsMouseDown + bsMouseUpDown, False);
  228.       end;
  229.     wm_GetDlgCode:
  230.       if GetState(bsDefault) then
  231.         BitButtonWinFn:= dlgc_DefPushButton
  232.       else
  233.         BitButtonWinFn := dlgc_UndefPushButton;
  234.     bm_SetStyle:
  235.       SetState(bsDefault, wParam = bs_DefPushButton);
  236.   else
  237.     BitButtonWinFn := DefWindowProc(HWindow, Message, wParam, lParam);
  238.   end;
  239. end;
  240.  
  241. exports
  242.   BitButtonWinFn;
  243.  
  244. var
  245.   Class: TWndClass;
  246.  
  247. begin
  248.   with Class do
  249.   begin
  250.     lpszClassName := 'BitButton';
  251.     hCursor       := LoadCursor(0, idc_Arrow);
  252.     lpszMenuName  := nil;
  253.     style         := cs_HRedraw or cs_VRedraw or cs_DblClks or cs_GlobalClass;
  254.     lpfnWndProc   := TFarProc(@BitButtonWinFn);
  255.     hInstance     := System.hInstance;
  256.     hIcon         := 0;
  257.     cbWndExtra    := ofSize;
  258.     cbClsExtra    := 0;
  259.     hbrBackground := 0;
  260.   end;
  261.   RegisterClass(Class);
  262. end.
  263.