沒有烘焙的winapi函數來顯示系統菜單。您可以使用pinvoke自行顯示它。 GetSystemMenu()函數返回系統菜單的句柄,通過使用TrackPopupMenu()顯示它,通過調用SendMessage來發送WM_SYSCOMMAND來執行選定的命令。
示例代碼,演示瞭如何做到這一點,包括必要的聲明:
using System.Runtime.InteropServices;
...
private void Window_MouseDown(object sender, MouseButtonEventArgs e) {
if (e.ChangedButton == MouseButton.Right) {
IntPtr hWnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
RECT pos;
GetWindowRect(hWnd, out pos);
IntPtr hMenu = GetSystemMenu(hWnd, false);
int cmd = TrackPopupMenu(hMenu, 0x100, pos.left, pos.top, 0, hWnd, IntPtr.Zero);
if (cmd > 0) SendMessage(hWnd, 0x112, (IntPtr)cmd, IntPtr.Zero);
}
}
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
[DllImport("user32.dll")]
static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
static extern int TrackPopupMenu(IntPtr hMenu, uint uFlags, int x, int y,
int nReserved, IntPtr hWnd, IntPtr prcRect);
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hWnd, out RECT rect);
struct RECT { public int left, top, right, bottom; }
注意,您可以顯示菜單任何你想要的,我只是選擇了窗口的左上角。注意位置值以像素爲單位。
這可能會幫助您: [如何創建WPF中的一個按鈕右鍵單擊快捷菜單] [1] [1]:http://stackoverflow.com/questions/6776911 /如何創建 - 右鍵單擊 - 上下文菜單 - 按鈕在WPF – sam
@sam當然,我知道用自定義項目創建右鍵單擊上下文菜單很容易。但是我需要上面顯示的6個項目的「系統菜單」,包括它們正確的語言相關標籤,啓用/禁用狀態和窗口控制操作。每個標準窗口都有這個菜單,問題是如何以編程方式彈出它,因爲當窗口邊框被隱藏時,你不能再通過鼠標右鍵單擊到達菜單。 – miroxlav