⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmbmpbtn.pas

📁 一套及时通讯的原码
💻 PAS
📖 第 1 页 / 共 3 页
字号:
      if (FTimer = nil) then
      begin
         FTimer := TTimer.Create(Self);
         FTimer.Interval := 50;
         FTimer.OnTimer := DoMouseTimer;
         Perform(CM_MOUSEENTER, 0, 0);
      end;
   end;
   {$ENDIF}

   inherited MouseMove(Shift,X,Y);

   if FMouseDown and not FStayDown then
   begin
      if not InBtn(X,Y) then
      begin
         if FState = bsDown then { mouse has slid off, so release }
         begin
            FState := bsUp;
            RedrawButton;
         end;
      end
      else
      begin
         if FState = bsUp then { mouse has slid back on, so push }
         begin
            FState := bsDown;
            RedrawButton;
         end;
      end;
   end;
end;

{-- TMMBitmapButton -----------------------------------------------------------}
procedure TMMBitmapButton.RedrawButton;
begin
   if not Visible then exit;

   if (csDesigning in ComponentState) then Repaint
   else
   begin
      if (FSaveBitmap <> nil) then
      begin
         { first copy the background to our bitmap }
         FSaveBitmap.Canvas.CopyRect(Rect(0,0,Width,Height),
                                     FSaveBitmap.Canvas,
                                     Rect(0,Height,Width,2*Height));
         { now draw the button to the bitmap }
         PaintButton(FSaveBitmap.Canvas);
         { copy to screen }
         Canvas.Draw(0,0,FSaveBitmap);
      end
      else PaintButton(Canvas);
   end;
end;

{-- TMMBitmapButton -----------------------------------------------------------}
procedure TMMBitmapButton.DrawTheText(Canvas: TCanvas; aRect: TRect);
var
   Flags,MidX,MidY: Integer;
   DC: THandle;
   TmpRect:TRect;

begin
   Canvas.Font := Self.Font;
   DC := Canvas.Handle; { reduce calls to GetHandle }

   if FWordWrap then
      Flags := DT_WORDBREAK or DT_END_ELLIPSIS
   else
      Flags := DT_SINGLELINE or DT_END_ELLIPSIS;

   TmpRect := Rect(0,0,Width,Height);

   { calculate width and height of text: }
   DrawText(DC, PChar(FCaption), Length(FCaption), TmpRect, Flags or DT_CALCRECT);
   MidY := TmpRect.Bottom - TmpRect.Top;
   MidX := TmpRect.Right-TmpRect.Left;
   Flags := DT_CENTER or DT_END_ELLIPSIS;

   case TextAlign of
      ttaTop        : OffsetRect(TmpRect,Width div 2-MidX div 2,aRect.Top - MidY - Spacing);
      ttaTopLeft    : OffsetRect(TmpRect,Spacing,aRect.Top - MidY - Spacing);
      ttaTopRight   : OffsetRect(TmpRect,Width - TmpRect.Right - Spacing,aRect.Top - MidY - Spacing);
      ttaBottom     : OffsetRect(TmpRect,Width div 2-MidX div 2,aRect.Bottom + Spacing);
      ttaBottomLeft : OffsetRect(TmpRect,Spacing,aRect.Bottom + Spacing);
      ttaBottomRight: OffsetRect(TmpRect,Width - MidX - Spacing,aRect.Bottom + Spacing);
      ttaCenter     : OffsetRect(TmpRect,Width div 2 - MidX div 2,Height div 2 - MidY div 2);
      ttaRight      : OffsetRect(TmpRect,Width  - MidX - Spacing,Height div 2 - MidY div 2);
      ttaLeft       : OffsetRect(TmpRect,Spacing,Height div 2 - MidY div 2);
   end;

   if FWordWrap then
      Flags := Flags or DT_WORDBREAK or DT_NOCLIP
   else
      Flags := Flags or DT_SINGLELINE or DT_NOCLIP;

   if ((FState = bsDown) and FShowPressed) then
       OffsetRect(TmpRect,FDownIndentH,FDownIndentV);

   SetBkMode(DC,Windows.TRANSPARENT);

   if not Enabled then
   begin
      { draw disabled text }
      SetTextColor(DC,ColorToRGB(clBtnHighLight));
      OffsetRect(TmpRect,1,1);
      DrawText(DC, PChar(FCaption), Length(FCaption), TmpRect, Flags);
      OffsetRect(TmpRect,-1,-1);
      SetTextColor(DC,ColorToRGB(clBtnShadow));
   end
   else SetTextColor(DC,Self.Font.Color);
   DrawText(DC, PChar(FCaption), Length(FCaption), TmpRect, Flags);
end;

{-- TMMBitmapButton -----------------------------------------------------------}
procedure TMMBitmapButton.DrawTheBitmap(Canvas: TCanvas; aRect:TRect);
var
   index: integer;
   SrcRect: TRect;

begin
   if BitmapValid and (FTempGlyph <> nil) then
   begin
     Index := -1;

     if assigned(FOnGetGlyphIndex) then
     begin
        FOnGetGlyphIndex(Self, FState = bsDown, Index);
     end;

     if (Index = -1) then
     begin
        Index := 0;
        case FNumGlyphs of   {normal,disabled,down,down }
           2: if not Enabled then Index := 1;
           3: if not Enabled then
                 Index := 1
              else if (FState = bsDown) then
                 Index := 2;
           4: if not Enabled then
                 Index := 1
              else if FStayDown then
                 Index := 3
              else if (FState = bsDown) then
                 Index := 2;
        end;

        { do we need the grayed bitmap ? }
        if ((ButtonStyle = bsExplorer) or (ButtonStyle = bsHighLight)) and Enabled then
        begin
           if not FStayDown and not FInButton and FShowDisabled then
           begin
              if FAutoGray then
                 Index := FNumGlyphs
              else if (FNumGlyphs > 1) then
                 Index := 1;
           end;
        end;

        { do we need the disabled bitmap ? }
        if not Enabled and (FNumGlyphs = 1) then Index := FNumGlyphs+1;
     end;

     SrcRect := GetSrcRect(index);

     if ((FState = bsDown) and FShowPressed) then
          OffsetRect(aRect,FDownIndentH,FDownIndentV);

     if Transparent then
     begin
        DrawTransparentBitmapEx(Canvas.Handle, FTempGlyph.Handle,
                                aRect.Left, aRect.Top,
                                SrcRect,
                                GetTransparentColor);
     end
     else
     begin
        DrawTransparentBitmapEx(Canvas.Handle, FTempGlyph.Handle,
                                aRect.Left, aRect.Top,
                                SrcRect,
                                GetTransparentColorEx(FTempGlyph.Handle,Point(Index*Bitmap.Width div FNumGlyphs,FTempGlyph.Height)));
     end;
   end;
end;

{-- TMMBitmapButton -----------------------------------------------------------}
procedure TMMBitmapButton.DrawTheButton(Canvas: TCanvas);
var
   Dest: TRect;
   TmpWidth,TmpHeight: integer;

begin
   with Canvas do
   begin
      TmpWidth := 0;
      TmpHeight := 0;

      { find glyph bounding rect - adjust according to textalignment}
      if BitmapValid then
      begin
         TmpWidth := Bitmap.Width div NumGlyphs;
         if TmpWidth <= 0 then TmpWidth := Bitmap.Width;
         TmpHeight := Bitmap.Height;
      end;

      { do top }
      if TextAlign in [ttaBottomLeft,ttaBottom,ttaBottomRight] then
         Dest.Top := Spacing
      else if TextAlign in [ttaTopLeft,ttaTop,ttaTopRight] then
         Dest.Top := Height - TmpHeight - Spacing
      else
         Dest.Top :=  (Height - TmpHeight) div 2;

      if (TextAlign = ttaLeft) then               { left }
          Dest.Left := Width - TmpWidth- Spacing
      else if TextAlign = ttaRight then           { right }
          Dest.Left := Spacing
      else                                        { center }
          Dest.Left := (Width - TmpWidth) div 2;

      Dest.Bottom:= Dest.Top + TmpHeight;
      Dest.Right := Dest.Left + TmpWidth;

      if BitmapValid then DrawTheBitmap(Canvas,Dest);

      { finally, do the caption }
      if Length(FCaption) > 0 then DrawTheText(Canvas,Dest);
   end;
end;

{-- TMMBitmapButton -----------------------------------------------------------}
procedure TMMBitmapButton.PaintButton(Canvas: TCanvas);
var
   TmpRect: TRect;

begin
   TmpRect := Rect(0,0,Width,Height);

   { draw the outline }
   with Canvas do
   begin
      Brush.Color := Color;
      Pen.Color   := clBlack;
      Pen.Width   := BorderWidth;

      case ButtonStyle of
        bsNone,
        bsHighLight:
        begin
           if not Transparent then
              FillRect(Rect(0,0,Width,Height));
           if (csDesigning in ComponentState) then
           begin
              Brush.Style := bsClear;
              Pen.Style   := psDot;
              Pen.Width   := 1;
              Rectangle(TmpRect.Left,TmpRect.Top,TmpRect.Right,TmpRect.Bottom);
              Pen.Style := psSolid;
              Brush.Style := bsSolid;
           end;
        end;

        bsExplorer:
        begin
           if not Transparent then
              FillRect(Rect(0,0,Width,Height));
           if (csDesigning in ComponentState) then
              Frame3D(Canvas,TmpRect,clBtnHighLight,clBtnShadow,1);
        end;

        bsRegular:
        begin
           { draw outline }
           Pen.Color := clBlack;
           if not Transparent then
              Rectangle(1,1,Width,Height)
           else
           begin
              TmpRect := Rect(1,1,Width,Height);
              Frame3D(Canvas,TmpRect,clBlack,clBlack,BorderWidth);
           end;
        end;

        bsIndent:
        begin
           { draw outline }
           Pen.Color := clBtnShadow;
           if not Transparent then
              Rectangle(0,0,Width-1,Height-1)
           else
           begin
              TmpRect := Rect(0,0,Width-1,Height-1);
              Frame3D(Canvas,TmpRect,clBtnShadow,clBtnShadow,BorderWidth)
           end;
           TmpRect := Rect(1,1,Width,Height);
           Frame3D(Canvas,TmpRect,clBtnHighLight,clBtnHighLight,BorderWidth);
        end;

        bsLight:
        begin
           if not Transparent then
              FillRect(Rect(0,0,Width,Height));
           if (csDesigning in ComponentState) then
              Frame3D(Canvas,TmpRect,clBtnHighLight,clBtnShadow,1);
        end;

        bsDark:
        begin
           if not Transparent then
              FillRect(Rect(0,0,Width,Height));
           if (csDesigning in ComponentState) then
              Frame3D(Canvas,TmpRect,clBtnFace,cl3DDkShadow,1);
        end;

        bsMono:
        begin
           if not Transparent then
              FillRect(Rect(0,0,Width,Height));
           if (csDesigning in ComponentState) then
              Frame3D(Canvas,TmpRect,clBtnHighLight,cl3DDkShadow,1);
        end;
      end;

      TmpRect := Rect(1,1,Width-1,Height-1);

      if (FState = bsDown) then
      begin
         if not (ButtonStyle = bsNone) or (ButtonStyle = bsHighLight) then
         begin
            InflateRect(TmpRect,1,1);
            case ButtonStyle of
                bsRegular : if ShowPressed then
                            begin
                               Frame3D(Canvas,TmpRect,clBlack,clBtnHighLight,BorderWidth);
                               Frame3D(Canvas,TmpRect,clBtnShadow,clBtnFace,BorderWidth);
                            end;

                bsExplorer: if FInButton or FStayDown then
                            begin
                               if ShowPressed then
                                  Frame3D(Canvas,TmpRect,clBtnShadow,clBtnHighLight,BorderWidth)
                               else
                                  Frame3D(Canvas,TmpRect,clBtnHighLight,clBtnShadow,BorderWidth);
                            end;

                bsIndent  : if ShowPressed then
                            begin
                               Frame3D(Canvas,TmpRect,clBlack,clBtnHighLight,BorderWidth);
                               Frame3D(Canvas,TmpRect,clBtnShadow,clBtnFace,BorderWidth);
                            end;

                bsLight   : if ShowPressed then
                               Frame3D(Canvas,TmpRect,clBtnShadow,clBtnHighLight,1);

                bsDark    : if ShowPressed then
                               Frame3D(Canvas,TmpRect,cl3DDkShadow,clBtnFace,1);

                bsMono    : if ShowPressed then
                               Frame3D(Canvas,TmpRect,cl3DDkShadow,clBtnHighLight,1);
            end;
         end;
      end;

      if (FState = bsUp) then
      begin
         InflateRect(TmpRect,1,1);

         case ButtonStyle of
             bsRegular :
             begin
                Frame3D(Canvas,TmpRect,clBtnHighLight,clBlack,BorderWidth);
                Frame3D(Canvas,TmpRect,clBtnFace,clBtnShadow,BorderWidth);
             end;

             bsExplorer: if FInButton then
                            Frame3D(Canvas,TmpRect,clBtnHighLight,clBtnShadow,BorderWidth);

             bsIndent  : Frame3D(Canvas,TmpRect,clBtnShadow,clBtnHighLight,BorderWidth);

             bsLight   : Frame3D(Canvas,TmpRect,clBtnHighLight,clBtnShadow,1);

             bsDark    : Frame3D(Canvas,TmpRect,clBtnFace,cl3DDkShadow,1);

             bsMono    : Frame3D(Canvas,TmpRect,clBtnHighLight,cl3DDkShadow,1);
         end;
      end;
   end;
   { repaint rest }
   IntersectClipRect(Canvas.Handle,0,0,Width,Height);
   DrawTheButton(Canvas);
   //ExcludeClipRect(Canvas.Handle,0,0,Width,Height);
end;

{-- TMMBitmapButton -----------------------------------------------------------}
procedure TMMBitmapButton.Paint;
begin
   if not (csDesigning in ComponentState) and (FSaveBitmap <> nil) then
   begin
      { save the actual background }
      FSaveBitmap.Canvas.CopyRect(Rect(0,Height,Width,2*Height),Canvas,ClientRect);
   end;
   PaintButton(Canvas);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -