windows - 如何在表单的系统菜单中添加带有图标的项目?

标签 windows delphi

这是我的代码,除了图标之外都有效

procedure TForm1.FormCreate(Sender: TObject);
var item : TMenuItemInfo;
begin
  with item do
  begin
    cbSize := SizeOf(MenuItemInfo);
    fMask := MIIM_TYPE or MIIM_ID;
    fType := MFT_STRING;
    wID := 180;
    dwTypeData := PChar('Test');
    cch := 4;
    hbmpItem := Image1.Picture.Bitmap.Handle;  //Image1 is TImage
  end;
  InsertMenuItem(GetSystemMenu(Handle, FALSE),0,true,item);
end;

最佳答案

几个问题:

  1. 您在使用前没有清除TMenuItemInfo实例。调用时,未分配的字段可能包含无效或错误的数据。

    使用

    ZeroMemory(@item, SizeOf(item));

    在程序开始时。

  2. 您的 fMaskfType 成员的组合不正确。

    使用以下内容代替

    fMask := MIIM_STRING or MIIM_BITMAP or MIIM_ID;
    //  fType := MFT_STRING;
    

    也就是说,不要分配fType

这是测试的示例片段,其中 TImage 保存在橙色背景上描绘数字 2 的图像。这将作为图标添加到新菜单项中。 (这是你的问题)

enter image description here

根据要求添加测试代码:

// Note! Your `Image1` must have a bitmap loaded
procedure TForm39.AddSystemMenuItem;
var
  item : TMenuItemInfo;
begin
  ZeroMemory(@item, SizeOf(item));
  with item do
  begin
    cbSize := SizeOf(MenuItemInfo);
    fMask := MIIM_STRING or MIIM_BITMAP or MIIM_ID;
    // fType := MFT_STRING;
    wID := 180;
    dwTypeData := PChar('Test');
    cch := 4;
    hbmpItem := Image1.Picture.Bitmap.Handle;  //Image1 is TImage
  end;
  if not InsertMenuItem(GetSystemMenu(Handle, FALSE),0,true,item) then
    ShowMessage('Failed');
end;

procedure TForm39.Button1Click(Sender: TObject);
begin
  AddSystemMenuItem;
end;

关于windows - 如何在表单的系统菜单中添加带有图标的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69602957/

相关文章:

linux - 从Fedora Linux主机到Windows目标的交叉编译Rust找不到依赖项

mysql - 备份和恢复数据库

delphi - 在Delphi中,浮点类型最大值的关键字是什么?

delphi - 在运行时更改应用程序的图标

php - 使用 PHP 转换 delphi TColor

delphi - 如何用TIdTCPServer持续发送消息?

windows - 带有命令行选项的 os.execute()

c++ - 相当于 VirtualAlloc() 的 _set_new_handler?

Windows 文件共享 : why sometimes newly created files aren't visible for some period of time?

delphi - 在通话过程中,我的DirectShow筛选器使Skype 5.x崩溃。在4.x,Graph Edit和其他程序中运行良好