c# - 将菜单选项插入 ApplicationIcon 菜单

标签 c# winforms menu titlebar

Windows 应用程序的标题栏左上角有一个图标,在应用程序名称的左侧?如果你点击它,它有像Restore, Minimize, Maximize..等选项

在许多程序中,它们都有额外的菜单选项(除了 Windows 提供的默认菜单选项)。我如何在 C# Winforms 中实现它?

最佳答案

“在 Windows 窗体应用程序中自定义系统菜单”教程:

http://www.codeproject.com/KB/dotnet/CustomWinFormSysMenu.aspx

http://www.codeguru.com/csharp/csharp/cs_misc/userinterface/article.php/c9327

片段:

导入 user32.dll 以访问更改系统菜单所需的功能。

[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
private static extern bool InsertMenu (IntPtr hMenu, 
    Int32 wPosition, Int32 wFlags, Int32 wIDNewItem, 
    string lpNewItem);

获取当前系统菜单,并向其中添加项目:

IntPtr sysMenuHandle = GetSystemMenu(this.Handle, false);
//It would be better to find the position at run time of the 'Close' item, but...

InsertMenu(sysMenuHandle, 5, MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty);
InsertMenu(sysMenuHandle, 6, MF_BYPOSITION , IDM_CUSTOMITEM1, "Item 1");
InsertMenu(sysMenuHandle, 7, MF_BYPOSITION , IDM_CUSTOMITEM2, "Item 2");

public const Int32 WM_SYSCOMMAND = 0x112;
public const Int32 MF_SEPARATOR = 0x800;
public const Int32 MF_BYPOSITION = 0x400;
public const Int32 MF_STRING = 0x0;
public const Int32 IDM_CUSTOMITEM1  = 1000;
public const Int32 IDM_CUSTOMITEM2 = 1001;

捕获新自定义项的选择,以便为它们分配方法:

protected override void WndProc(ref Message m)
{
    if(m.Msg == WM_SYSCOMMAND)
    {
        switch(m.WParam.ToInt32())
        {
            case IDM_CUSTOMITEM1 : 
                MessageBox.Show("Clicked 'Item 1'");
                return;
            case IDM_CUSTOMITEM1 :
                MessageBox.Show("Clicked 'item 2'");
                return;
            default:
                break;
        } 
    }
    base.WndProc(ref m);
}

关于c# - 将菜单选项插入 ApplicationIcon 菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4165087/

相关文章:

c# - SQL Server 'ROUND' 与 C# 'Math.Round'

c# - 在 C# 中应用 DeMorgan 定理手动优化条件语句中的 bool 表达式是否有用(例如 if 条件)

javascript - MENU 手机版不显示

c - 循环输出我的 C 程序的菜单两次

c# - KeyDown KeySuppress 是否取消 KeyUp 事件?

html - 我的下拉列表中的第一项已突出显示

c# - 如何将 IQueryable 转换为 DataTable

c# - 为什么在 C# 中使用 -3==~2

c# - Panel 在继承的 Windows 窗体上的行为不正确?

C# 创建 "wireframe"/3D "map"