C# 无边框窗体实现任务栏上的系统菜单

在 Windows Form 开发中我们经常会用到无边框窗体,因为这样我们可以在窗体上随意的定制了。设置成无边框窗体之后后我们也又会遇到另一个的问题,在任务栏上鼠标右键的系统菜单不能用了。

[DllImport ("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
public static extern int GetWindowLong (HandleRef hWnd, int nIndex);

[DllImport ("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowLong (HandleRef hWnd, int nIndex, int dwNewLong);
private void SysMenuClick (object sender, EventArgs e) {
    int WS_SYSMENU = 0x00080000;
    int WS_MINIMIZEBOX = 0x20000;
    this.FormBorderStyle = FormBorderStyle.None;

    int windowLong = (GetWindowLong (new HandleRef (this, this.Handle), -16));
    SetWindowLong (new HandleRef (this, this.Handle), -16, WS_SYSMENU | WS_MINIMIZEBOX);

}
posted @ 2012-04-01 13:30  Charles Zhang  阅读(676)  评论(0编辑  收藏  举报