我有一個 FormBorderStyle 設定為 None 的表單,我還制作了自己的小 GUI 來替換標題欄,我正在嘗試找到一種方法來顯示每當您右鍵單擊標題欄或單擊時顯示的選單標題欄上的圖示

我嘗試過使用這篇文章,但是在呼叫該函式后什么也沒發生,我現在很茫然,我找不到更多關于這個的資源。
uj5u.com熱心網友回復:
當邊框樣式Form設定為 時FormBorderStyle.None,GetSystemMenu()總是回傳一個NULL句柄。
HWND因為您在將邊框樣式設定為 時洗掉了一些重要的視窗樣式None,所以此函式不會回傳HMENU.
可能,它會檢查視窗是否有標題欄。這就是它回傳的原因NULL。
該問題的解決方法是在將 設定為 之前獲取選單FoormBorderStyle句柄None。
using System.Runtime.InteropServices;
public partial class MainForm : Form
{
public MainForm() => InitializeComponent();
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
private static extern int TrackPopupMenu(IntPtr hMenu, uint uFlags, int x, int y,
int nReserved, IntPtr hWnd, IntPtr prcRect);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
private IntPtr hMenu;
private const int WM_SYSCOMMAND = 0x112;
protected override void OnHandleCreated(EventArgs e)
{
// Get the system menu and set the border style after that.
hMenu = GetSystemMenu(Handle, false);
FormBorderStyle = FormBorderStyle.None;
}
protected override void OnMouseUp(MouseEventArgs e)
{
int menuIdentifier = TrackPopupMenu(hMenu, 0x102, Control.MousePosition.X, Control.MousePosition.Y, 0, Handle, IntPtr.Zero);
if(menuIdentifier != 0)
{
PostMessage(Handle, WM_SYSCOMMAND, (IntPtr)menuIdentifier, IntPtr.Zero);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/512749.html
標籤:C#表格温纳皮
下一篇:為什么這兩個部門不同?
