大多数窗口左上角的图标

本文关键字:图标 左上角 窗口 大多数 | 更新日期: 2023-09-27 18:19:37

我在C#中开发了一个WinForms应用程序,通过从下拉列表中选择并切换复选框,可以使任何窗口成为"最顶端"。

但是打开一个应用程序似乎有点傻,所以我想知道是否可以在Windows的左上角图标中添加一个条目,运行我选择的程序?

我没有任何实验代码,因为我不知道那个图标/点叫什么,所以我无法研究它

我发现了这段代码,它似乎可以做你想做的事:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WinFormsSystemMenuTest
{
    public partial class Form1 : Form
    {
        #region Win32 API Stuff
        // Define the Win32 API methods we are going to use
        [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);
        /// Define our Constants we will use
        public const Int32 WM_SYSCOMMAND = 0x112;
        public const Int32 MF_SEPARATOR = 0x800;
        public const Int32 MF_BYPOSITION = 0x400;
        public const Int32 MF_STRING = 0x0;
        #endregion
        // The constants we'll use to identify our custom system menu items
        public const Int32 _SettingsSysMenuID = 1000;
        public const Int32 _AboutSysMenuID = 1001;
        public Form1()
        {
            InitializeComponent();
        }
        protected override void OnHandleCreated(EventArgs e)
        {
            /// Get the Handle for the Forms System Menu
            IntPtr systemMenuHandle = GetSystemMenu(this.Handle, false);
            /// Create our new System Menu items just before the Close menu item
            InsertMenu(systemMenuHandle, 5, MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty); // <-- Add a menu seperator
            InsertMenu(systemMenuHandle, 6, MF_BYPOSITION, _SettingsSysMenuID, "Settings...");
            InsertMenu(systemMenuHandle, 7, MF_BYPOSITION, _AboutSysMenuID, "About...");
            base.OnHandleCreated(e);
        }
        protected override void WndProc(ref Message m)
        {
            // Check if a System Command has been executed
            if (m.Msg == WM_SYSCOMMAND)
            {
                // Execute the appropriate code for the System Menu item that was clicked
                switch (m.WParam.ToInt32())
                {
                    case _SettingsSysMenuID:
                        MessageBox.Show("'"Settings'" was clicked");
                        break;
                    case _AboutSysMenuID:
                        MessageBox.Show("'"About'" was clicked");
                        break;
                }
            }
            base.WndProc(ref m);
        }
    }
}

我在这里找到的。这似乎是你想要的,是吗?

大多数窗口左上角的图标

winform的这个区域被称为"非客户端区域"。

但我认为,如果你的目标是将开关添加到所有外部winform,最简单的解决方案是创建一个流程,将开关放在活动窗口窗体的左上角。你可以尝试将其直接放在外部窗体上,但你会在位置优先级方面遇到问题。

由于您在问题中包含了"WinApi",我想您能够获得当前运行进程的窗口句柄、坐标和最上面的属性。