是否可以对WinForms / C#执行以下操作?
参见例如(黑色形状将是WinForm窗口):
本质上,我需要为程序创建一个工具栏,并且该工具栏应在同一位置“捕捉”到该程序,而不管窗口的位置或大小如何。
首先找到记事本窗口的句柄:
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
只需为第一个参数传递null,然后将窗口的标题(“记事本”?)作为第二个参数。
一种替代方法是枚举所有窗口并根据标题选择最佳匹配:
using System.Runtime.InteropServices;
public delegate bool CallBackPtr(int hwnd, int lParam);
private CallBackPtr callBackPtr;
public class EnumReport
{
[DllImport("user32.dll")]
private static extern int EnumWindows(CallBackPtr callPtr, int lPar);
public static bool Report(int hwnd, int lParam)
{
Console.WriteLine("Window handle is "+hwnd);
return true;
}
}
static void Main()
{
// note in other situations, it is important to keep
// callBackPtr as a member variable so it doesnt GC while you're calling EnumWindows
callBackPtr = new CallBackPtr(EnumReport.Report);
EnumReport.EnumWindows(callBackPtr, 0);
}
然后将WndProc附加到它:
HwndSource src = HwndSource.FromHwnd(windowHandle);
src.AddHook(new HwndSourceHook(WndProc));
在WndProc中,响应窗口的大小调整和移动。
我不确定是否将工具栏设置为记事本窗口的子级。当记事本尝试对其进行管理并对其z深度进行排序时,可能会产生意想不到的效果。
同时,我怀疑这是一件好事。用户将能够在叠加层“下方”输入内容并丢失其光标/文本。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句