使用VB.NET删除另一个程序的框架/窗口/边框(Aero位) [英] Remove another program's frame/window/border (the Aero bit) using VB.NET

查看:109
本文介绍了使用VB.NET删除另一个程序的框架/窗口/边框(Aero位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从VB.NET程序中删除另一个应用程序窗口(例如记事本)的边框/框架(Aero位)?

Is there a way to remove the border/frame (the Aero bit) of another application's window (say notepad) from a VB.NET program?

推荐答案

您可以使用P-Invoke来做到这一点.下面是一些使用 SetWindowLong (在User32.dll)来更改记事本主窗口的边框. (此代码假定您有一个运行记事本的实例.)您可以尝试使用不同的窗口样式来获得所需的结果.

You can do that using P-Invoke. Below is some code which uses SetWindowLong (in User32.dll) to change the border of the main window of notepad. (This code assumes that you have an instance of notepad running.) You can experiment with different window styles to achieve the result you want.

GWL_STYLE用于基本的窗口样式.您可以在此处上阅读.
GWL_EXSTYLE用于扩展窗口样式.您可以在此处上阅读.. >

GWL_STYLE is for basic window styles. You can read about them here.
GWL_EXSTYLE is for extended window styles. You can read about them here.

Imports System.Diagnostics
Imports System.Runtime.InteropServices

Module Module1

    Sub Main()
       Dim notepad As Process = Process.GetProcessesByName("notepad")(0)

       Dim GWL_STYLE As Int32 = -16
       Dim GWL_EXSTYLE As Int32 = -20

       ' MainWindowHandle happens to be the handle of the window you want for notepad.

       ' It may not be the handle you want if you try this on a different process.

       Dim hWnd As IntPtr = notepad.MainWindowHandle

       ' You can examine the current styles using GetWindowLong.
       Dim styles As WindowStyles = GetWindowLong(hWnd, GWL_STYLE)
       Dim exStyles As WindowStyles = GetWindowLong(hWnd, GWL_EXSTYLE)

       ' WS_VISIBLE must be used for the window to be selectable.
       Dim newStyles As WindowStyles = WindowStyles.WS_VISIBLE Or WindowStyles.WS_BORDER

       SetWindowLong(hWnd, GWL_STYLE, newStyles)

       ' If you want to modify the extended styles, use GWL_EXSTYLE

       SetWindowLong(hWnd, GWL_EXSTYLE, exStyles)


    End Sub

     _
 Private Function GetWindowLong( _
      ByVal hWnd As IntPtr, _
      ByVal nIndex As Integer) As Integer
    End Function

     _
    Private Function SetWindowLong( _
    ByVal hWnd As IntPtr, _
    ByVal nIndex As Integer, _
    ByVal dwNewLong As IntPtr) As Integer
    End Function

End Module

 _
Public Enum WindowStyles As Long

    WS_OVERLAPPED = 0
    WS_POPUP = 2147483648
    WS_CHILD = 1073741824
    WS_MINIMIZE = 536870912
    WS_VISIBLE = 268435456
    WS_DISABLED = 134217728
    WS_CLIPSIBLINGS = 67108864
    WS_CLIPCHILDREN = 33554432
    WS_MAXIMIZE = 16777216
    WS_BORDER = 8388608
    WS_DLGFRAME = 4194304
    WS_VSCROLL = 2097152
    WS_HSCROLL = 1048576
    WS_SYSMENU = 524288
    WS_THICKFRAME = 262144
    WS_GROUP = 131072
    WS_TABSTOP = 65536

    WS_MINIMIZEBOX = 131072
    WS_MAXIMIZEBOX = 65536

    WS_CAPTION = WS_BORDER Or WS_DLGFRAME
    WS_TILED = WS_OVERLAPPED
    WS_ICONIC = WS_MINIMIZE
    WS_SIZEBOX = WS_THICKFRAME
    WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW

    WS_OVERLAPPEDWINDOW = WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or _
              WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX
    WS_POPUPWINDOW = WS_POPUP Or WS_BORDER Or WS_SYSMENU
    WS_CHILDWINDOW = WS_CHILD

    WS_EX_DLGMODALFRAME = 1
    WS_EX_NOPARENTNOTIFY = 4
    WS_EX_TOPMOST = 8
    WS_EX_ACCEPTFILES = 16
    WS_EX_TRANSPARENT = 32

    '#If (WINVER >= 400) Then
    WS_EX_MDICHILD = 64
    WS_EX_TOOLWINDOW = 128
    WS_EX_WINDOWEDGE = 256
    WS_EX_CLIENTEDGE = 512
    WS_EX_CONTEXTHELP = 1024

    WS_EX_RIGHT = 4096
    WS_EX_LEFT = 0
    WS_EX_RTLREADING = 8192
    WS_EX_LTRREADING = 0
    WS_EX_LEFTSCROLLBAR = 16384
    WS_EX_RIGHTSCROLLBAR = 0

    WS_EX_CONTROLPARENT = 65536
    WS_EX_STATICEDGE = 131072
    WS_EX_APPWINDOW = 262144

    WS_EX_OVERLAPPEDWINDOW = WS_EX_WINDOWEDGE Or WS_EX_CLIENTEDGE
    WS_EX_PALETTEWINDOW = WS_EX_WINDOWEDGE Or WS_EX_TOOLWINDOW Or WS_EX_TOPMOST
    '#End If

    '#If (WIN32WINNT >= 500) Then
    WS_EX_LAYERED = 524288
    '#End If

    '#If (WINVER >= 500) Then
    WS_EX_NOINHERITLAYOUT = 1048576 ' Disable inheritence of mirroring by children
    WS_EX_LAYOUTRTL = 4194304 ' Right to left mirroring
    '#End If

    '#If (WIN32WINNT >= 500) Then
    WS_EX_COMPOSITED = 33554432
    WS_EX_NOACTIVATE = 67108864
    '#End If

End Enum

代码说明

我不确定您在开发GUI应用程序方面有多少经验,所以我将为您介绍窗口的工作原理.窗口具有唯一标识的号码,称为句柄".与该窗口关联的还有一个窗口过程,该过程处理该窗口的消息(标识事件和命令的整数).创建窗口后,您可以指定窗口要具有的样式等.Windows应用程序的复杂性要高得多,但是为了避免陷入细节,我们将继续进行下去.

I'm not sure how much experience you have in developing GUI applications, so I'll give a little background on how a window works. A window has a uniquely identifying number called a handle. Also associated with the window is a window procedure, which handles messages (integer numbers that identify events and commands) for that window. When a window is created, you specify what styles you want the window to have, etc. There's a lot more complexity to windows applications, but to avoid getting bogged down in the details we'll move on.

非常幸运的是,.NET Winforms使我们不必与Windows API进行交互并直接处理消息(大部分情况下),从而使创建功能性GUI应用程序变得非常容易. Windows API的强大功能是大多数.NET开发人员通常不需要担心的.

Thankfully, .NET Winforms isolates us from having to interact with the Windows API and handle messages directly (for the most part) and makes it very easy to create functional GUI applications. There is a lot more power under the hood in the Windows API that most .NET developers don't usually need to worry about.

现在有了这样的背景,代码应该更容易理解了.

Now with that background, the code should be a little easier to understand.

首先,我们需要获取第一个名为"notepad"的进程.

First off we need to get the first process named "notepad".

Dim notepad As Process = Process.GetProcessesByName("notepad")(0)

然后我们定义两个整数GWL_STYLEGWL_EXSTYLE.在SetWindowLong函数的上下文中,这两个整数将具有特定的含义.它们的值(以及许多其他常量的值)可以在Winuser.h和Windows SDK的其余头文件中找到.

Then we define two integers GWL_STYLE and GWL_EXSTYLE. These two integers will have a specific meaning in context of the SetWindowLong function. Their value (and the value of many other constants) can be found in Winuser.h and the rest of the header files in the Windows SDK.

Dim GWL_STYLE As Int32 = -16
Dim GWL_EXSTYLE As Int32 = -20

接下来,我们获得记事本主窗口的句柄.

Next we get the handle of the notepad's main window.

Dim hWnd As IntPtr = notepad.MainWindowHandle

此后,我们遇到了 GetWindowLong 功能.从MSDN:

After that we encounter the GetWindowLong function. From MSDN:

GetWindowLong函数检索有关指定窗口的信息.

The GetWindowLong function retrieves information about the specified window.

GetWindowLong带有窗口句柄和一个值,该值指示要检索的信息,并返回指定的信息.

GetWindowLong takes the window handle and a value indicating what information to retrieve and returns the specified information.

Dim styles As WindowStyles = GetWindowLong(hWnd, GWL_STYLE)
Dim exStyles As WindowStyles = GetWindowLong(hWnd, GWL_EXSTYLE)

包含了这些样式,因此您可以查看对窗口应用了哪些样式,从而可以确定要保留的样式.

These were included so you could see what styles were applied to the window, so you could determine which styles to leave out.

接下来,我们定义要应用于窗口的样式.您可以在此处阅读有关各种样式及其含义的信息.

Next we define what we styles we want to apply to the window. You can read about the various styles and their meanings here.

Dim newStyles As WindowStyles = WindowStyles.WS_VISIBLE Or WindowStyles.WS_BORDER

然后,我们使用 SetWindowLong将这些样式应用于窗口.从MSDN:

Then we apply those styles to the window using SetWindowLong. From MSDN:

SetWindowLong函数更改指定窗口的属性.

The SetWindowLong function changes an attribute of the specified window.

SetWindowLong获取窗口句柄,一个指示要更改的属性的值以及该属性的新值,然后更改该属性.

SetWindowLong takes the window handle, a value indicating what attribute to change, and the new value of the attribute, and changes the attribute.

SetWindowLong(hWnd, GWL_STYLE, newStyles)

基本上,这就是代码的作用.为避免重复,我将不重复GWL_EXSTYLE,因为它的使用方式与GWL_STYLE完全相同.其余代码只是为了使我们能够使用SetWindowLongGetWindowLong.

That's basically what the code does. To avoid repetition, I will not go over GWL_EXSTYLE since it is used exactly the same way as GWL_STYLE. The rest of the code is just logistics to allow us to use SetWindowLong and GetWindowLong.

这篇关于使用VB.NET删除另一个程序的框架/窗口/边框(Aero位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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