大多数窗口左上角的图标

时间:2012-01-04 17:06:43

标签: c# windows winforms winapi

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

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

我没有任何实验代码,因为我不知道该图标/点被称为什么,因此我无法研究它。

2 个答案:

答案 0 :(得分:3)

我发现这段代码看起来像你想要的那样:

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

我发现它here。这似乎是你想要的,是吗?

答案 1 :(得分:1)

winform的这个区域称为"Non client area"

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

由于您在问题中包含了“WinApi”,我想您可以获得当前正在运行的进程的窗口句柄,坐标和最顶层属性。