将菜单选项插入 ApplicationIcon 菜单

发布于 2024-10-01 14:22:29 字数 176 浏览 5 评论 0原文

Windows 应用程序的标题栏左上角、应用程序名称左侧有一个图标吗?如果您单击它,它会提供诸如恢复最小化最大化等选项。

在许多程序中,它们还有其他菜单选项(除了Windows 提供的默认值)。我如何在 C# Winforms 中实现这个?

Windows applications have an icon in the top left of the titlebar, to the left of the application name? If you click it, it has options like Restore, Minimize, Maximize.. etc.

In many programs they have additional menu options there (beyond the default one's provided by Windows). How can I implement this in C# Winforms?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世晨晓 2024-10-08 14:22:29

“在 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);
}

Tutorials for "Customizing the System Menu in a Windows Forms Application":

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

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

Snippet:

Import user32.dll to access the functions required to alter the system menu.

[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);

Get the current system menu, and add items to it:

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;

Capture the selection of the new custom items in order to assign methods to them:

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文