delphi - Delphi中使用动态按钮的MouseDown事件

标签 delphi delphi-2010

我正在从动态加载的数组动态创建按钮。我正在按钮提示中存储一条所需的信息。

    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/

相关文章:

delphi - Delphi-为初学者使用dll

delphi - 为什么线程在此控制台应用程序中串行运行?

Delphi 报表组件,为最终用户提供可视化控件

delphi - Delphi 2010 中的编码错误

Delphi WideString 和 Delphi 2009+

delphi - 在 Delphi 中按值和按引用调用相同的函数

delphi - 如何从头开始在delphi 7中进行DLL的远程调试

delphi - 如何在客户端仍处于连接状态时更新 DataSnap 服务器?

delphi - WM_COPYDATA 字符串未出现在目标应用程序中

delphi - delphi 2010和IE9