TPopupMenu 中的專案可以用鍵盤或滑鼠突出顯示/選擇。使用鍵盤選擇時,您可以使用箭頭鍵在選單中移動。
如何在不使用 VK_DOWN 模擬向下箭頭按鍵的情況下將第一個選單項標記為選中(藍色)(參見下面的代碼)?
Popup := TPopupMenu.create(nil);
Popup.OnPopup := PopupClick;
class procedure TTrayMain.PopupClick(Sender: TObject) ;
begin
// Code below activates the first menu entry..
// Now Looking for an alternative solution to replace this hack:
keybd_event( VK_DOWN, MapVirtualKey( VK_DOWN,0), 0, 0);
keybd_event( VK_DOWN, MapVirtualKey( VK_DOWN,0), KEYEVENTF_KEYUP, 0);
end;
uj5u.com熱心網友回復:
您需要將未記錄的MN_SELECITEM訊息發送到彈出視窗。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
Item1: TMenuItem;
Item2: TMenuItem;
Item3: TMenuItem;
private
{ Private declarations }
public
{ Public declarations }
end;
//To override the default PopupList, based on Remy Lebeau's code
TPopupListEx = class(TPopupList)
protected
procedure WndProc(var Message: TMessage); override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const MN_SELECTITEM = $01E5;
{ TPopupListEx }
procedure TPopupListEx.WndProc(var Message: TMessage);
var hm:HMENU;
begin
inherited;
if (Message.Msg = WM_ENTERMENULOOP) and (Message.WParam = 1) then
begin
//When the popupwindow is already created, we can ask it's handle
hm:=FindWindow(PChar('#32768'),nil);
//Send the MN_SELECTITEM message. The third parameter is the desired menuitem's index.
SendMessage(hm,MN_SELECTITEM,0,0);
end;
end;
initialization
Popuplist.Free; //free the "default", "old" list
PopupList := TPopupListEx.Create; //create the new one
// The new PopupList will be freed by
// finalization section of Menus unit.
end.
關于其他(包括提到的 delphipraxis 示例)解決方案的注意事項:
將 MenuItem 設定為高亮狀態 withSetMenuItemInfoW或 withHiliteMenuItem將不起作用,因為它只會影響外觀,但是當您將滑鼠懸停在任何其他專案上時,第一個專案仍然突出顯示。
uj5u.com熱心網友回復:
我相信你被偽造的輸入所困擾。我認為您可以PostMessage將訊息向下/向上鍵入到GetFocus. 這使得它至少是本地黑客而不是全球黑客。您可能需要一個鉤子來捕獲正確的訊息以觸發黑客攻擊。
理想情況下,這應該記錄在這里,但遺憾的是它不是。確定 Microsoft 是如何做到這一點的唯一方法是在 98/2000/XP 中除錯 Explorer。在 Explorer 和 Internet Explorer 中這些系統上的選單實作是從工具列按鈕下拉的背景關系選單,偽裝成選單欄。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/456517.html
