Controls 属性与继承 TShape 类的小练习
生活随笔
收集整理的這篇文章主要介紹了
Controls 属性与继承 TShape 类的小练习
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本例效果圖:
代碼文件:
unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, ExtCtrls;typeTMyShape = class(TShape)protectedprocedure CMMouseenter(var Message: TMessage); message CM_MOUSEENTER;procedure CMMouseleave(var Message: TMessage); message CM_MOUSELEAVE;end;TForm1 = class(TForm)Panel1: TPanel;Button1: TButton;Button2: TButton;procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); constW = 50;H = 50; varshape: TMyShape; beginshape := TMyShape.Create(Self);shape.Parent := Panel1;shape.Width := W;shape.Height := H;Randomize;shape.Left := Random(Panel1.ClientWidth - W);shape.Top := Random(Panel1.ClientHeight - H);shape.Brush.Color := Random($FFFFFF); end;procedure TForm1.Button2Click(Sender: TObject); vari: Integer; beginif Panel1.ControlCount = 0 then Exit;Randomize;i := Random(Panel1.ControlCount - 1);Panel1.Controls[i].Free; end;{ TMyShape }procedure TMyShape.CMMouseenter(var Message: TMessage); consts = '當(dāng)前 %s 的顏色值是: %.6x'; varWCtrl: TWinControl; beginWCtrl := Parent;while WCtrl.HasParent do WCtrl := WCtrl.Parent;if WCtrl is TForm then TForm(WCtrl).Caption := Format(s, [ClassName,Brush.Color]);inherited; end;procedure TMyShape.CMMouseleave(var Message: TMessage); consts = 'Form1'; varWCtrl: TWinControl; beginWCtrl := Parent;while WCtrl.HasParent do WCtrl := WCtrl.Parent;if WCtrl is TForm then TForm(WCtrl).Caption := s;inherited; end;end. 窗體文件:
object Form1: TForm1Left = 0Top = 0Caption = 'Form1'ClientHeight = 212ClientWidth = 395Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont.Height = -11Font.Name = 'Tahoma'Font.Style = []OldCreateOrder = FalsePixelsPerInch = 96TextHeight = 13object Panel1: TPanelLeft = 8Top = 8Width = 297Height = 193Caption = 'Panel1'TabOrder = 0endobject Button1: TButtonLeft = 311Top = 40Width = 75Height = 25Caption = #28155#21152TabOrder = 1OnClick = Button1Clickendobject Button2: TButtonLeft = 311Top = 85Width = 75Height = 25Caption = #38543#26426#21024#38500TabOrder = 2OnClick = Button2Clickend end
代碼文件:
unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, ExtCtrls;typeTMyShape = class(TShape)protectedprocedure CMMouseenter(var Message: TMessage); message CM_MOUSEENTER;procedure CMMouseleave(var Message: TMessage); message CM_MOUSELEAVE;end;TForm1 = class(TForm)Panel1: TPanel;Button1: TButton;Button2: TButton;procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); constW = 50;H = 50; varshape: TMyShape; beginshape := TMyShape.Create(Self);shape.Parent := Panel1;shape.Width := W;shape.Height := H;Randomize;shape.Left := Random(Panel1.ClientWidth - W);shape.Top := Random(Panel1.ClientHeight - H);shape.Brush.Color := Random($FFFFFF); end;procedure TForm1.Button2Click(Sender: TObject); vari: Integer; beginif Panel1.ControlCount = 0 then Exit;Randomize;i := Random(Panel1.ControlCount - 1);Panel1.Controls[i].Free; end;{ TMyShape }procedure TMyShape.CMMouseenter(var Message: TMessage); consts = '當(dāng)前 %s 的顏色值是: %.6x'; varWCtrl: TWinControl; beginWCtrl := Parent;while WCtrl.HasParent do WCtrl := WCtrl.Parent;if WCtrl is TForm then TForm(WCtrl).Caption := Format(s, [ClassName,Brush.Color]);inherited; end;procedure TMyShape.CMMouseleave(var Message: TMessage); consts = 'Form1'; varWCtrl: TWinControl; beginWCtrl := Parent;while WCtrl.HasParent do WCtrl := WCtrl.Parent;if WCtrl is TForm then TForm(WCtrl).Caption := s;inherited; end;end. 窗體文件:
object Form1: TForm1Left = 0Top = 0Caption = 'Form1'ClientHeight = 212ClientWidth = 395Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont.Height = -11Font.Name = 'Tahoma'Font.Style = []OldCreateOrder = FalsePixelsPerInch = 96TextHeight = 13object Panel1: TPanelLeft = 8Top = 8Width = 297Height = 193Caption = 'Panel1'TabOrder = 0endobject Button1: TButtonLeft = 311Top = 40Width = 75Height = 25Caption = #28155#21152TabOrder = 1OnClick = Button1Clickendobject Button2: TButtonLeft = 311Top = 85Width = 75Height = 25Caption = #38543#26426#21024#38500TabOrder = 2OnClick = Button2Clickend end
轉(zhuǎn)載于:https://www.cnblogs.com/del/archive/2008/10/23/1317926.html
總結(jié)
以上是生活随笔為你收集整理的Controls 属性与继承 TShape 类的小练习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何实现自己的机器做服务器
- 下一篇: 现在已经到了考虑云计算的时候