C# 大多数窗口左上角的图标
我在C#中开发了一个WinForms应用程序,它可以通过从下拉列表中选择任何窗口并切换复选框,使其成为“最顶端” 但是打开一个应用程序似乎有点傻,所以我想知道是否有可能在Windows中运行我选择的程序的左上角图标中添加一个条目C# 大多数窗口左上角的图标,c#,windows,winforms,winapi,C#,Windows,Winforms,Winapi,我在C#中开发了一个WinForms应用程序,它可以通过从下拉列表中选择任何窗口并切换复选框,使其成为“最顶端” 但是打开一个应用程序似乎有点傻,所以我想知道是否有可能在Windows中运行我选择的程序的左上角图标中添加一个条目 我没有任何实验性代码,因为我不知道该图标/点的名称,因此我无法对其进行研究。我发现该代码似乎可以满足您的要求: using System; using System.Windows.Forms; using System.Runtime.InteropServices;
我没有任何实验性代码,因为我不知道该图标/点的名称,因此我无法对其进行研究。我发现该代码似乎可以满足您的要求:
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);
}
}
}
使用系统;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
命名空间WinFormsSystemMenuTest
{
公共部分类Form1:Form
{
#区域Win32 API
//定义我们将要使用的Win32 API方法
[DllImport(“user32.dll”)]
私有静态外部IntPtr GetSystemMenu(IntPtr hWnd,bool bRevert);
[DllImport(“user32.dll”)]
私有静态外部bool InsertMenu(IntPtr-humenu、Int32-wPosition、Int32-wFlags、Int32-wIDNewItem、string-lpNewItem);
///定义我们将使用的常量
public const Int32 WM_SYSCOMMAND=0x112;
公共常量Int32 MF_分隔符=0x800;
公共常数Int32 MF_BYPOSITION=0x400;
public const Int32 MF_STRING=0x0;
#端区
//用于标识自定义系统菜单项的常量
公共常数Int32 _SettingsSymenuid=1000;
公共常数Int32 _关于Symenuid=1001;
公共表格1()
{
初始化组件();
}
已创建受保护的重写无效OnHandleCreated(EventArgs e)
{
///获取窗体系统菜单的句柄
IntPtr systemMenuHandle=GetSystemMenu(this.Handle,false);
///在关闭菜单项之前创建新的系统菜单项
插入菜单(systemMenuHandle,5,MF_BYPOSITION | MF_SEPARATOR,0,string.Empty);//我发现这段代码似乎可以执行您想要的操作:
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);
}
}
}
使用系统;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
命名空间WinFormsSystemMenuTest
{
公共部分类Form1:Form
{
#区域Win32 API
//定义我们将要使用的Win32 API方法
[DllImport(“user32.dll”)]
私有静态外部IntPtr GetSystemMenu(IntPtr hWnd,bool bRevert);
[DllImport(“user32.dll”)]
私有静态外部bool InsertMenu(IntPtr-humenu、Int32-wPosition、Int32-wFlags、Int32-wIDNewItem、string-lpNewItem);
///定义我们将使用的常量
public const Int32 WM_SYSCOMMAND=0x112;
公共常量Int32 MF_分隔符=0x800;
公共常数Int32 MF_BYPOSITION=0x400;
public const Int32 MF_STRING=0x0;
#端区
//用于标识自定义系统菜单项的常量
公共常数Int32 _SettingsSymenuid=1000;
公共常数Int32 _关于Symenuid=1001;
公共表格1()
{
初始化组件();
}
已创建受保护的重写无效OnHandleCreated(EventArgs e)
{
///获取窗体系统菜单的句柄
IntPtr systemMenuHandle=GetSystemMenu(this.Handle,false);
///在关闭菜单项之前创建新的系统菜单项
插入菜单(systemMenuHandle,5,MF_BYPOSITION | MF_SEPARATOR,0,string.Empty);//winform的此区域称为
但我认为,如果您的目标是将开关添加到所有外部winforms,那么最简单的解决方案是创建一个进程,该进程将在活动窗口窗体的左上角放置一个带有开/关开关的小窗体。您可以尝试将其直接放在外部窗体上,但在位置优先级方面会有问题
由于您的问题中包含了“WinApi”,我想您可以获得当前运行进程的窗口句柄、坐标和最顶层属性。winform的此区域称为
但我认为,如果您的目标是将开关添加到所有外部winforms,那么最简单的解决方案是创建一个进程,该进程将在活动窗口窗体的左上角放置一个带有开/关开关的小窗体。您可以尝试将其直接放在外部窗体上,但在位置优先级方面会有问题
因为你包括了“WinApi”在您的问题中,我想您能够获得当前正在运行的进程的窗口句柄、坐标和最顶层属性。屏幕截图可能有助于此操作。显示的菜单是窗口的系统菜单,也可以通过右键单击标题栏或任务栏上的窗口来触发。屏幕截图可能有助于此操作ne.出现的菜单是窗口的系统菜单,也可以通过右键单击标题栏或任务栏上的窗口来触发。加载事件处理程序中的代码属于OnHandleCreated()的覆盖.Wow,这看起来比预期的要简单!将在半小时左右进行尝试。从链接上看,它似乎正是我想要的:)将这一行添加到加载事件处理程序以了解原因:ShowInTaskbar=false;
有许多属性会导致重新创建本机窗口。当@HansPassant谢谢!我已经修改了代码。加载事件处理程序中的代码属于OnHandleCreated()的重写.Wow,这看起来比预期的要简单!将在半小时左右进行尝试。从链接上看,它似乎正是我想要的:)将这一行添加到加载事件处理程序以了解原因:ShowInTaskbar=false;
有许多属性会导致重新创建本机窗口。当谢谢!我已经修改了代码。