zoukankan      html  css  js  c++  java
  • 制作放两个小图片的按钮 回复 "xhui" 的问题


    问题来源: http://www.cnblogs.com/del/archive/2009/03/12/1409708.html#1475240

    本例效果图:



    自定义的类(TMyButton):
    unit Unit2;
    
    interface
    
    uses
      Windows, Messages, Classes, Graphics, StdCtrls;
    
    type
      TMyButton = class(TButton)
      private
        FBit1,FBit2: TBitmap;
      protected
        procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
      public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
        procedure Click; override;
      end;
    
    implementation
    
    { TMyButton }
    
    procedure TMyButton.Click;
    begin
      inherited;
      MessageBox(0, 'MyButton', 'Hi', MB_OK);
    end;
    
    constructor TMyButton.Create(AOwner: TComponent);
    var
      wh: Integer;
    begin
      inherited;
      FBit1 := TBitmap.Create;
      FBit2 := TBitmap.Create;
      //在此可以载入图片, 为了测试方便, 我随便画两个矩形吧
      wh := Height - 8;
      FBit1.SetSize(wh, wh);
      FBit2.SetSize(wh, wh);
      FBit1.Canvas.Brush.Color := clRed;
      FBit1.Canvas.Rectangle(0, 0, wh, wh);
      FBit2.Canvas.Brush.Color := clLime;
      FBit2.Canvas.Rectangle(0, 0, wh, wh);
    end;
    
    destructor TMyButton.Destroy;
    begin
      FBit1.Free;
      FBit2.Free;
      inherited;
    end;
    
    procedure TMyButton.WMPaint(var Message: TWMPaint);
    var
      cvs: TCanvas;
      DC: HDC;
    begin
      Inherited;
      DC := GetDC(Handle);
      cvs := TCanvas.Create;
      cvs.Handle := DC;
      cvs.Draw(4, (Height - FBit2.Height) div 2, FBit1);
      cvs.Draw(Width - FBit2.Width - 4, (Height - FBit2.Height) div 2, FBit2);
      cvs.Free;
      ReleaseDC(Handle, DC);
    end;
    
    end.
    


    测试代码:
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    uses Unit2;
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      with TMyButton.Create(Self) do begin
        Parent := Self;
        Left := Random(Self.ClientWidth - Width);
        Top := Random(Self.ClientHeight - Height);
      end;
    end;
    
    end.
    

  • 相关阅读:
    破解版sublimeText3重新安装Package Control方法
    javascript中对象的属性的特性
    正则表达式元字符详解
    正则表达式学习笔记
    ExtJs布局大全
    js如何获取select下拉框的value以及文本内容
    ExtJS4之Ext.MessageBox的各种用法
    CODING 代码多仓库实践
    2019 DevOps 必备面试题——容器化和虚拟化
    拥抱微服务,CODING 即将上线单项目多仓库功能
  • 原文地址:https://www.cnblogs.com/del/p/1409890.html
Copyright © 2011-2022 走看看