C# 如何更改应用程序外部窗体的窗口样式?
如何更改应用程序外部窗体的窗口样式?一个难题?C# 如何更改应用程序外部窗体的窗口样式?,c#,.net,windows,handles,C#,.net,Windows,Handles,如何更改应用程序外部窗体的窗口样式?一个难题? 我实际上是想移动一个没有边界的女巫 我有窗户的把手。 如果保证可以工作的话,我可以编写数千行代码。假设此窗口可以来自任何基于Win32的运行时生成的任何应用程序,看起来您将不得不求助于核心Win32 API的p/invoke来进行窗口操作 例如,您可以使用,它可以从user32.dll导入。签名如下: BOOL SetWindowPos(HWND hWnd, HWND hWndInsertAfter, int X, int Y,
我实际上是想移动一个没有边界的女巫 我有窗户的把手。
如果保证可以工作的话,我可以编写数千行代码。假设此窗口可以来自任何基于Win32的运行时生成的任何应用程序,看起来您将不得不求助于核心Win32 API的p/invoke来进行窗口操作 例如,您可以使用,它可以从user32.dll导入。签名如下:
BOOL SetWindowPos(HWND hWnd,
HWND hWndInsertAfter,
int X,
int Y,
int cx,
int cy,
UINT uFlags
);
我不会假设您以前做过p/invoke导入,所以让我们从顶部开始。让我们来一个windows窗体应用程序:
1) 创建windows窗体应用程序,然后将以下声明添加到Form1类:
/* hWndInsertAfter constants. Lifted from WinUser.h,
* lines 4189 onwards depending on Platform SDK version */
public static IntPtr HWND_TOP = (IntPtr)0;
public static IntPtr HWND_BOTTOM = (IntPtr)1;
public static IntPtr HWND_TOPMOST = (IntPtr)(-1);
public static IntPtr HWND_NOTOPMOST = (IntPtr)(-2);
/* uFlags constants. Lifted again from WinUser.h,
* lines 4168 onwards depending on Platform SDK version */
/* these can be |'d together to combine behaviours */
public const int SWP_NOSIZE = 0x0001;
public const int SWP_NOMOVE = 0x0002;
public const int SWP_NOZORDER = 0x0004;
public const int SWP_NOREDRAW = 0x0008;
public const int SWP_NOACTIVATE = 0x0010;
public const int SWP_FRAMECHANGED = 0x0020;
public const int SWP_SHOWWINDOW = 0x0040;
public const int SWP_HIDEWINDOW = 0x0080;
public const int SWP_NOCOPYBITS = 0x0100;
public const int SWP_NOOWNERZORDER = 0x0200; /* Don't do owner Z ordering */
public const int SWP_NOSENDCHANGING = 0x0400; /* Don't send WM_WINDOWPOSCHANGING */
public const int SWP_DRAWFRAME = SWP_FRAMECHANGED;
public const int SWP_NOREPOSITION = SWP_NOOWNERZORDER;
public const int SWP_DEFERERASE = 0x2000;
public const int SWP_ASYNCWINDOWPOS = 0x4000;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SetWindowsPos(IntPtr hWnd,
IntPtr hWndInsertAfter,
int x,
int y,
int cx,
int cy,
UInt32 uFlags);
Win32 windows方法的p/invoke的烦人之处在于,您随后必须开始导入Win32使用的各种数值常量等,因此需要事先准备好所有的gumph
请参阅SetWindowPos方法的MSDN链接,以了解其功能的说明
2) 将一个名为cmdMakeHidden的按钮添加到表单中,然后按如下方式编写处理程序:
private void cmdMakeHidden_Click(object sender, EventArgs e)
{
//also causes the icon in the start bar to disappear
//SWP_HIDEWINDOW is the 'kill -9' of the windows world without actually killing!
SetWindowPos(this.Handle, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_HIDEWINDOW);
}
private void cmdMove_Click(object sender, EventArgs e)
{
SetWindowPos(this.Handle, HWND_TOP, 100, 100, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOREPOSITION);
}
将“this.Handle”替换为您选择的窗口句柄以隐藏该窗口
此方法实际上用于一次应用多个更改,因此需要使用一些SWP\u NO*
选项。例如,您应该指定SWP_NOSIZE,否则为cx和cy传递0将导致窗口同时收缩为零宽度和高度
要演示如何移动窗口,请在窗体中添加另一个名为cmdMove的按钮,然后按如下所示编写单击处理程序:
private void cmdMakeHidden_Click(object sender, EventArgs e)
{
//also causes the icon in the start bar to disappear
//SWP_HIDEWINDOW is the 'kill -9' of the windows world without actually killing!
SetWindowPos(this.Handle, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_HIDEWINDOW);
}
private void cmdMove_Click(object sender, EventArgs e)
{
SetWindowPos(this.Handle, HWND_TOP, 100, 100, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOREPOSITION);
}
这段代码会在您点击按钮时将表单移动到100100
再次,更换此手柄,如您认为合适。这里的HWND_TOP是完全可选的,因为已使用SWP_NOZORDER和SWP_NOREPOSITION标志禁用了重新排序
希望这能帮助你走上正轨 假设此窗口可以来自任何类型的基于Win32的运行时生成的任何应用程序,看起来您将不得不求助于核心Win32 API的p/invoke来进行窗口操作 例如,您可以使用,它可以从user32.dll导入。签名如下:
BOOL SetWindowPos(HWND hWnd,
HWND hWndInsertAfter,
int X,
int Y,
int cx,
int cy,
UINT uFlags
);
我不会假设您以前做过p/invoke导入,所以让我们从顶部开始。让我们来一个windows窗体应用程序:
1) 创建windows窗体应用程序,然后将以下声明添加到Form1类:
/* hWndInsertAfter constants. Lifted from WinUser.h,
* lines 4189 onwards depending on Platform SDK version */
public static IntPtr HWND_TOP = (IntPtr)0;
public static IntPtr HWND_BOTTOM = (IntPtr)1;
public static IntPtr HWND_TOPMOST = (IntPtr)(-1);
public static IntPtr HWND_NOTOPMOST = (IntPtr)(-2);
/* uFlags constants. Lifted again from WinUser.h,
* lines 4168 onwards depending on Platform SDK version */
/* these can be |'d together to combine behaviours */
public const int SWP_NOSIZE = 0x0001;
public const int SWP_NOMOVE = 0x0002;
public const int SWP_NOZORDER = 0x0004;
public const int SWP_NOREDRAW = 0x0008;
public const int SWP_NOACTIVATE = 0x0010;
public const int SWP_FRAMECHANGED = 0x0020;
public const int SWP_SHOWWINDOW = 0x0040;
public const int SWP_HIDEWINDOW = 0x0080;
public const int SWP_NOCOPYBITS = 0x0100;
public const int SWP_NOOWNERZORDER = 0x0200; /* Don't do owner Z ordering */
public const int SWP_NOSENDCHANGING = 0x0400; /* Don't send WM_WINDOWPOSCHANGING */
public const int SWP_DRAWFRAME = SWP_FRAMECHANGED;
public const int SWP_NOREPOSITION = SWP_NOOWNERZORDER;
public const int SWP_DEFERERASE = 0x2000;
public const int SWP_ASYNCWINDOWPOS = 0x4000;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SetWindowsPos(IntPtr hWnd,
IntPtr hWndInsertAfter,
int x,
int y,
int cx,
int cy,
UInt32 uFlags);
Win32 windows方法的p/invoke的烦人之处在于,您随后必须开始导入Win32使用的各种数值常量等,因此需要事先准备好所有的gumph
请参阅SetWindowPos方法的MSDN链接,以了解其功能的说明
2) 将一个名为cmdMakeHidden的按钮添加到表单中,然后按如下方式编写处理程序:
private void cmdMakeHidden_Click(object sender, EventArgs e)
{
//also causes the icon in the start bar to disappear
//SWP_HIDEWINDOW is the 'kill -9' of the windows world without actually killing!
SetWindowPos(this.Handle, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_HIDEWINDOW);
}
private void cmdMove_Click(object sender, EventArgs e)
{
SetWindowPos(this.Handle, HWND_TOP, 100, 100, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOREPOSITION);
}
将“this.Handle”替换为您选择的窗口句柄以隐藏该窗口
此方法实际上用于一次应用多个更改,因此需要使用一些SWP\u NO*
选项。例如,您应该指定SWP_NOSIZE,否则为cx和cy传递0将导致窗口同时收缩为零宽度和高度
要演示如何移动窗口,请在窗体中添加另一个名为cmdMove的按钮,然后按如下所示编写单击处理程序:
private void cmdMakeHidden_Click(object sender, EventArgs e)
{
//also causes the icon in the start bar to disappear
//SWP_HIDEWINDOW is the 'kill -9' of the windows world without actually killing!
SetWindowPos(this.Handle, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_HIDEWINDOW);
}
private void cmdMove_Click(object sender, EventArgs e)
{
SetWindowPos(this.Handle, HWND_TOP, 100, 100, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOREPOSITION);
}
这段代码会在您点击按钮时将表单移动到100100
再次,更换此手柄,如您认为合适。这里的HWND_TOP是完全可选的,因为已使用SWP_NOZORDER和SWP_NOREPOSITION标志禁用了重新排序
希望这能帮助你走上正轨 应该注意的是,您可以使用相同的方法,然后使用其他Win32方法修改窗口的实际窗口样式。SetWindowLong方法()用于修改一个或多个窗口样式。通常,在修改样式后必须调用SetWindowPos,如果样式更改导致窗口框架发生更改,则会出现这种情况。这将是丑陋的-但它会工作!应该注意的是,您可以使用相同的方法,然后使用其他Win32方法修改窗口的实际窗口样式。SetWindowLong方法()用于修改一个或多个窗口样式。通常,在修改样式后必须调用SetWindowPos,如果样式更改导致窗口框架发生更改,则会出现这种情况。这将是丑陋的-但它会工作!