5,379
社区成员
发帖
与我相关
我的任务
分享
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ColorGrd, ImgList, Menus, ADODB, Provider,
DB, DBClient, ComCtrls, ToolWin;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
//两个过程的定义:
procedure ButtonClick(Sender: TObject);
procedure MenuClick(Sender:TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var ColorBox:TColorBox;
MainMenu:TMainMenu;
Colorlist,Coloritem:TMenuItem;
Button:TButton;
Panel:TPanel;
Lab:TLabel;
StatusBar:TStatusBar;
{$R *.dfm}
//新建一个工程、双击窗体、输入以下代码:
procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.Width:=330;
Form1.Height:=280;
Form1.Caption:='将ColorBox的颜色添加MainMenu';
Form1.Position:=poScreenCenter;
StatusBar:=TStatusBar.Create(self);
StatusBar.Parent:=form1;
StatusBar.Panels.Add;
StatusBar.Panels[0].Text:='中国软件研发联盟QQ群122058606 广州佬';
Button:=TButton.Create(self);
Button.Parent:=form1;
Button.Caption:='建立主菜单';
Button.Top:=Form1.Height div 2-24;
Button.Left:=Form1.Width div 2 - Button.Width div 2;
Button.OnClick:=ButtonClick;
end;
//增加以下两过程:
procedure TForm1.ButtonClick(Sender: TObject);
var i,r,g,b,x,y:integer;
bmp:TBitmap;
ImageList:TImageList;
PRGB:pRGBTriple;
mc:TColor;
begin
bmp:=TBitmap.Create;
ImageList:=TImageList.Create(self);
ColorBox:=TColorBox.Create(self);
ColorBox.Parent:=form1;
ColorBox.Left:=-300;
MainMenu:=TMainMenu.Create(self);
MainMenu.AutoHotkeys:=maManual;
MainMenu.Images:=ImageList;
Colorlist:=TMenuItem.Create(self);
Form1.Menu:=MainMenu;
Colorlist.Caption:='颜色选择';
Colorlist.Name:='Colorlist';
MainMenu.items.insert(MainMenu.items.count,Colorlist);
for i:=0 to ColorBox.Items.Count-1 do
begin
Coloritem:=TMenuItem.Create(self);
Coloritem.Caption:=ColorBox.Items.Strings[i];
Coloritem.Name:='N'+inttostr(i);
Colorlist.Insert(Colorlist.Count,Coloritem);
Colorlist.Items[Colorlist.Count-1].ImageIndex:=i;
Colorlist.Items[Colorlist.Count-1].OnClick:=MenuClick;
mc:=ColorBox.Colors[i];
if mc<0 then mc:=GetSysColor(mc and $000000FF);
r:=GetRValue(mc);
g:=GetGValue(mc);
b:=GetBValue(mc);
bmp.PixelFormat:=pf24bit;
bmp.width:=16;
bmp.height:=16;
for y:=0 to bmp.Height-1 do
begin
pRGB:=bmp.ScanLine[Y];
for x:=0 to bmp.Width-1 do
begin
pRGB^.rgbtRed:=r;
pRGB^.rgbtGreen:=g;
pRGB^.rgbtBlue:=b;
inc(pRGB);
end;
end;
ImageList.Add(bmp,nil);
end;
bmp.Free;
ColorBox.Free;
Panel:=TPanel.Create(self);
Panel.Visible:=false;
Panel.BevelInner:=bvLowered;
Panel.Parent:=form1;
Panel.Height:=Button.Height;
Panel.Width:=Button.Width;
Panel.Top:=Button.Top;
Panel.Left:=Button.Left;
Lab:=TLabel.Create(self);
Lab.Visible:=false;
Lab.Parent:=form1;
Lab.Left:=Panel.Left;
Lab.Top:=Panel.Top-16;
Lab.Caption:='选择的颜色:';
Button.Visible:=false;
end;
procedure TForm1.MenuClick(Sender:TObject);
var mc:Tcolor;
begin
Lab.Visible:=true;
Panel.Visible:=true;
IdentToColor(TMenuItem(Sender).Caption,integer(mc));
if mc<0 then mc:=GetSysColor(mc and $000000FF);
Panel.Color:=mc;
end;
end.