我有以下代码,将项目添加到系统菜单。我的问题是 DeleteMenu(SysMenu, cLANGMENU, MF_BYCOMMAND) 没有删除添加到系统菜单的弹出菜单。我无法理解的是为什么为添加的项目(扫描组件)工作,而不是为“程序语言”工作
procedure TForm1.Button1Click(Sender: TObject);
const aBaseItem : string = 'Programm-Sprache';
cSepSYSTEM_MENU = wm_user + 100;
cScanCompSystemMenu = wm_user + 101;
cLANGMENU = wm_user + 102;
SC_ITEM = $FF00;
var
MenuItemInfo: TMenuItemInfo;
PopupMenu: HMENU;
Result: Boolean;
SysMenu: HMenu;
iPos, NumItems : Integer;
wTemp1, wtemp2 : string;
Buffer: array[0..79] of Char;
begin
{Create the popup menu}
PopupMenu := CreatePopupMenu;
{Insert an item into it}
for iPos := 5 downto 0 do
begin
FillChar(MenuItemInfo, SizeOf(MenuItemInfo), iPos);
with MenuItemInfo do
begin
cbSize := SizeOf(MenuItemInfo);
fMask := MIIM_TYPE or MIIM_ID;
fType := MFT_STRING;
wID := SC_ITEM + iPos;
dwTypeData := PChar(inttostr(iPos));
cch := Length(dwTypeData) * 2 + 2; //4; {'Item' is 4 chars}
end;
Result := InsertMenuItem(PopupMenu, 0, True, MenuItemInfo);
Assert(Result, 'InsertMenuItem failed');
end;
SysMenu := GetSystemMenu(Handle, False);
//why this isn't working???
if not DeleteMenu(SysMenu, cLANGMENU, MF_BYCOMMAND) then
begin
ShowMessage('System Error Message: '+ SysErrorMessage(GetLastError)) ;
//error message is - operation completed succesfully
end;
{Insert the popup into the system menu}
FillChar(MenuItemInfo, SizeOf(MenuItemInfo), 0);
with MenuItemInfo do
begin
cbSize := SizeOf(MenuItemInfo);
fMask := MIIM_SUBMENU or MIIM_TYPE or MIIM_ID;
fType := MFT_STRING;
hSubMenu := PopupMenu;
wId := cLANGMENU;
dwTypeData := PChar('Test');//Before editing the question it was Program Language
cch := Length(dwTypeData) * 2 + 2; //7; {'SubMenu' is 7 chars}
end;
Assert(SysMenu <> 0);
Result := InsertMenuItem(SysMenu, GetMenuItemCount(SysMenu), True, MenuItemInfo);
Assert(Result, 'InsertMenuItem failed');
SysMenu := GetSystemMenu(Handle, False);
//delete menu if exists - this is working
DeleteMenu(SysMenu,cSepSYSTEM_MENU,MF_SEPARATOR);
DeleteMenu(SysMenu,cScanCompSystemMenu,MFT_STRING);
//add new ones
InsertMenu(SysMenu, Word(-1), MF_SEPARATOR, cSepSYSTEM_MENU, '');
FillChar(MenuItemInfo, SizeOf(TMenuItemInfo), #0);
with MenuItemInfo do
begin
cbSize := SizeOf(TMenuItemInfo);
fMask := MIIM_TYPE or MIIM_ID or MIIM_STATE;
fType := MFT_STRING;
wId := cScanCompSystemMenu;
dwTypeData := PChar('Scan components');
cch := Length('Scan components');
end;
Result := InsertMenuItem(SysMenu, GetMenuItemCount(SysMenu), True, MenuItemInfo);
Assert(Result, 'InsertMenuItem failed');
end;
LE:这段代码运行了好几次,所以我需要删除每次添加的项目。LE2:即使这段代码是用Delphi(Object Pascal)编写的,它也从Windows 调用WinApi 函数。也许有人在这方面进行了大量工作并且是 C/C++ 程序员可以提供帮助。