如何改变你的应用程序之外的一种形式的窗口样式? [英] how to change the window style of a form outside your app?

查看:229
本文介绍了如何改变你的应用程序之外的一种形式的窗口样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何改变窗体的窗口样式您的应用程序之外?很难回答?
我其实想移动窗体女巫是最上面的,没有边界。
我有窗口的句柄(HWND)。
我可以写几千code线,如果保证其正常工作。

how to change the window style of a form outside your app?hard question?
i am actually trying to move a form witch is topmost and has no border.
i have the handle(hWnd) of the window.
i can write thousands of lines of code if guaranteed to work.

推荐答案

假设这个窗口可以从任何类型的基于Win32的运行时所产生的任何应用程序,它看起来像你必须诉诸的P / Invoke的核心的Win32 API进行窗口操作。

Assuming that this window could be from any app produced from any kind of Win32-based runtime, it looks like you'll have to resort to p/invoke of the core Win32 apis for window operations.

例如,您可以使用 SetWindowPos ,它可以从user32.dll中导入。它的签名是这样的:

For example, you could use SetWindowPos, which can be imported from user32.dll. It's signature is this:

BOOL SetWindowPos(HWND hWnd, 
  HWND hWndInsertAfter,
  int X,
  int Y,
  int cx,
  int cy,
  UINT uFlags
);

我不打算假定你已经做之前,AP /援引进口,所以让我们从顶部。让我们只打坏了Windows窗体应用程序:

I'm not going to assume that you've done a p/invoke import before, so let's go from the top. Let's just bash out a windows forms app:

1)创建一个Windows窗体应用程序,然后添加这些声明到Form1类:

1) Create a windows forms app and then add these declarations to the Form1 class:

/* 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);

恼人的事情与P /调用Win32的窗口的方法是,你就必须开始导入各种数字常量等的Win32使用 - 因此所有的gumph事前

The annoying thing with p/invoke of Win32 windows methods is that you then have to start importing various numeric constants etc that Win32 uses - hence all the gumph beforehand.

请参阅MSDN链接的SetWindowPos方法为他们做什么解释。

Refer to the MSDN link for the SetWindowPos method for an explanation of what they do.

2)添加一个按钮叫cmdMakeHidden表格,然后编写处理程序如下:

2) Add a button to the form called cmdMakeHidden and then write the handler as follows:

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

替换this.Handle与您选择隐藏窗口的窗口句柄。

Replace the 'this.Handle' with the window handle of your choice to hide that window.

这个方法实际上是用来申请一次多变化,因此需要使用一些 SWP_NO *的选项。例如,应指定SWP_NOSIZE否则路过0 CX和CY会导致窗口收缩到零的宽度和高度,在同一时间。

This method is actually used to apply multiple changes at once, hence the need to use some of the SWP_NO* options. For example, you should specify SWP_NOSIZE otherwise passing 0 for cx and cy will cause the window to shrink to zero width and height at the same time.

要展示移动窗口,添加另一个按钮,您的窗体调用cmdMove,然后写单击处理程序如下:

To demonstrate moving a window, add another button your form called cmdMove and then write the click handler as follows:

private void cmdMove_Click(object sender, EventArgs e)
{
  SetWindowPos(this.Handle, HWND_TOP, 100, 100, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOREPOSITION);
}

这code将您表格100,100只要你按下按钮。

This code moves your form to 100,100 whenever you hit the button.

再次更换this.Handle你认为合适的。 HWND_TOP这里完全是可选的,因为重新排序已被禁用的SWP_NOZORDER和SWP_NOREPOSITION标志。

Again, replace the this.Handle as you see fit. HWND_TOP here is completely optional, since reordering has been disabled with the SWP_NOZORDER and SWP_NOREPOSITION flags.

希望这有助于让你在正确的轨道!

Hope this helps get you on the right track!

这篇关于如何改变你的应用程序之外的一种形式的窗口样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆