我正在从动态加载的数组动态创建按钮。我正在按钮提示中存储一条所需的信息。
procedure TForm3.CreateAppButton(sBtnCapt: string);
var
hIcon: THandle;
nIconId: DWORD;
Icon: TIcon;
NewButton: TSpeedButton;
PicConvert: TBitmap;
sPathNew: string;
AppData: TAppDetails;
begin
AppData := TAppDetails.Create(sBtnCapt);
NewButton := TSpeedButton.Create(self);
with NewButton do
begin
Width := 67;
Height := 50;
Left := (Width + 5) * (self.ControlCount - 1);
Top := 5;
Parent := self;
Caption := AppData.Caption;
Name := AppData.ButtonName;
Font.Size := 7;
// extract a 16x16 icon for display on the buttom
sPathNew := '';
sPathNew := sPath + AppData.Exe;
if PrivateExtractIcons(PChar(sPathNew), 0, 16, 16, @hIcon, @nIconId, 1, LR_LOADFROMFILE)
<> 0 then
try
PicConvert := TBitmap.Create;
Icon := TIcon.Create;
try
Icon.Handle := hIcon;
PicConvert.Width := Icon.Width;
PicConvert.Height := Icon.Height;
PicConvert.Canvas.Draw(0, 0, Icon);
Glyph := PicConvert;
finally
Icon.Free;
PicConvert.Free;
end;
finally
DestroyIcon(hIcon);
end;
OnMouseDown := btnMouseDown;
Hint := AppData.Exe;
ShowHint := False;
Layout := blGlyphTop;
AppData.Free;
end;
end;
我的问题出现在鼠标按下事件上。我想以某种方式使用按钮提示属性中存储的信息来触发所需的操作。您可以在左键单击中看到我正在尝试的内容,并在右键单击中看到我来自哪里。设计右键单击方式需要我输入 ~80,还需要我声明表单上的每个按钮,以便我可以构建和编译。
procedure TForm3.btnMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState;
X, Y: Integer);
var
sApp: string;
AppData: TAppDetails;
begin
sApp := '';
if Button = mbLeft then
begin
sApp := btnFoo.Hint;
ShellExecute(self.Handle, 'open', PChar(sApp), nil, nil, SW_SHOWNORMAL);
end
else if Button = mbRight then
begin
if Sender = btnCC3 then
begin
sApp := 'CaseClaims3.exe';
end
else if Sender = btnODBC then
begin
sApp := 'ODBCMgr.exe';
end
else if Sender = btnCredMaint then
begin
sApp := 'CreditorMaint';
end;
AppData := TAppDetails.Create(sApp);
ShellExecute(self.Handle, 'open', PChar(AppData.Wiki), nil, nil, SW_SHOWNORMAL);
AppData.Free;
end;
end;
任何人都可以指出我在不提前声明组件的情况下动态使用 mousedown 事件的方向吗? Appdata 是一个单独的 pas 文件,其中包含每个应用程序所需的所有信息,以防有人想知道。
还请原谅我相当可怕的代码,我知道它需要做很多工作。
最佳答案
我同意您不应使用Hint
属性(property)。尤其是因为您可能在某个时间点希望使用Hint
属性(property)为其他东西。我也对使用 Tag
的解决方案有些怀疑.
我想我会使用 TDictionary<TSpeedButton,TAppPaths>
的实例在按钮和应用程序详细信息之间进行映射。我在这里想象TAppPaths
是一条包含两个字符串的记录,一个用于左键单击,一个用于右键单击,例如
TAppPaths= record
Left: string;
Right: string;
end;
为了便于讨论,我们假设该实例名为 ButtonDetails
.
每当您添加新按钮时,您都会编写如下代码:
AppPaths.Left := AppDetails.Exe;
AppPaths.Right := AppDetails.SomeOtherFunction;
ButtonDetails.Add(NewButton, AppPaths);
然后在表单的鼠标事件处理程序中,您可以这样编写:
AppPaths := ButtonDetails[Sender as TSpeedButton];
现在AppPaths
两条路径都已准备好供您使用。
这个想法可以很容易地扩展到在 TAppPaths
中包含您想要的尽可能多的信息。记录一下。
顺便说一句,我认为你应该处理 MouseUp
而不是MouseDown
因为在 Windows 中,按钮是在释放鼠标按钮时单击的,而不是在按下鼠标按钮时单击的。
关于delphi - Delphi中使用动态按钮的MouseDown事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6950467/