因此,在Google花了两个小时后,我终于找到了解决方案。
第一步:确定RECT结构是这样的:
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
第二步:输入2 user32.dll中的功能:
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
public static extern int TrackPopupMenu(int hMenu, int wFlags, int x, int y, int nReserved, int hwnd, ref RECT lprc);
第三步:添加 '鼠标右键点击标题' 事件处理程序:
private void headerArea_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
switch (e.ChangedButton)
{
case MouseButton.Right:
{
// need to get handle of window
WindowInteropHelper _helper = new WindowInteropHelper(this);
//translate mouse cursor porition to screen coordinates
Point p = PointToScreen(e.GetPosition(this));
//get handler of system menu
IntPtr systemMenuHandle = GetSystemMenu(_helper.Handle, false);
RECT rect = new RECT();
// and calling application menu at mouse position.
int menuItem = TrackPopupMenu(systemMenuHandle.ToInt32(), 1,(int)p.X, (int) p.Y, 0, _helper.Handle.ToInt32(), ref rect);
break;
}
}
}