本文介绍了如何在 VB.NET 中激活、移动和调整窗口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口,我只知道该窗口的标题(例如记事本),需要激活、调整大小并将其放置在屏幕的左上角.所以在对 MSDN 和论坛进行了一些研究之后,我发现了一些应该可以实现这一点的功能.我使用 FindWindow 按标题获取句柄,然后我使用 GetWindowPlacement 查看记事本是否最小化(如果没有,那么我只使用 AppActivate,如果它没有最小化,则只需要激活它).但是,如果窗口被最小化,我会尝试使用 SetWindowPlacement 在一个命令中激活、调整大小和移动它.这是我的代码:

I have a window of which I know only the title (e.g. Notepad) that I need to activate, resize and place in the top-left corner of my screen.So after some research on MSDN and forums I found some functions that should achieve this. I use FindWindow to get the handle by title, then I use GetWindowPlacement to see if notepad is minimized or not (if not, then I just use AppActivate, only need to activate it if it's not minimized). If the window is minimized however, I then try to use SetWindowPlacement to activate, resize and move it in one command.Here is my code:

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow( _
 ByVal lpClassName As String, _
 ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function GetWindowPlacement(ByVal hWnd As IntPtr, ByRef lpwndpl As WINDOWPLACEMENT) As Boolean
End Function
<DllImport("user32.dll")> _
Private Shared Function SetWindowPlacement(ByVal hWnd As IntPtr, ByRef lpwndpl As WINDOWPLACEMENT) As Boolean
End Function

Private Structure RECT
    Public Left As Integer
    Public Top As Integer
    Public Right As Integer
    Public Bottom As Integer
    Public Sub New(ByVal X As Integer, ByVal Y As Integer, ByVal X2 As Integer, ByVal Y2 As Integer)
        Me.Left = X
        Me.Top = Y
        Me.Right = X2
        Me.Bottom = Y2
    End Sub
End Structure

Private Structure WINDOWPLACEMENT
    Public Length As Integer
    Public flags As Integer
    Public showCmd As ShowWindowCommands
    Public ptMinPosition As POINTAPI
    Public ptMaxPosition As POINTAPI
    Public rcNormalPosition As RECT
End Structure

Enum ShowWindowCommands As Integer
    Hide = 0
    Normal = 1
    ShowMinimized = 2
    Maximize = 3
    ShowMaximized = 3
    ShowNoActivate = 4
    Show = 5
    Minimize = 6
    ShowMinNoActive = 7
    ShowNA = 8
    Restore = 9
    ShowDefault = 10
    ForceMinimize = 11
End Enum

Public Structure POINTAPI
    Public X As Integer
    Public Y As Integer
    Public Sub New(ByVal X As Integer, ByVal Y As Integer)
        Me.X = X
        Me.Y = Y
    End Sub
End Structure

实际执行在这里:

Dim wp As WINDOWPLACEMENT
wp.Length = Marshal.SizeOf(wp)
GetWindowPlacement(FindWindow(Nothing, "Notepad"), wp)
If wp.showCmd = ShowWindowCommands.ShowMinimized Then
    Dim wp2 As WINDOWPLACEMENT
    wp2.showCmd = ShowWindowCommands.ShowMaximized
    wp2.ptMinPosition = wp.ptMinPosition
    wp2.ptMaxPosition = New POINTAPI(0, 0)
    wp2.rcNormalPosition = New RECT(0, 0, 816, 639) 'this is the size I want
    wp2.flags = wp.flags
    wp2.Length = Marshal.SizeOf(wp2)
    SetWindowPlacement(FindWindow(Nothing, "Notepad"), wp2)
    Else
        AppActivate("Notepad")

所以我尝试运行它,但它只会激活窗口,而矩形也应该调整它的大小.那么我做错了什么?有没有更简单的方法来实现这一切?抱歉,帖子太长了

So I try running this but it just activates the window, while the rectangle is supposed to also resize it. So what am I doing wrong? Is there an easier way to achieve all of this? Sorry for the lengthy post

推荐答案

我不得不做类似的事情,并使用了 user32.dll 中的 SetWindowPos 函数

I had to do a similar thing, and used the SetWindowPos function from user32.dll

这是一个 vb.net 实现:

Here's a vb.net implementation:

Imports System
Imports System.Runtime.InteropServices
Imports System.Diagnostics

Public Class HookUtil

    Public Const SWP_NOMOVE As Short = &H2
    Public Const SWP_NOSIZE As Short = 1
    Public Const SWP_NOZORDER As Short = &H4
    Public Const SWP_SHOWWINDOW As Short = &H40
    Shared ReadOnly HWND_BOTTOM As IntPtr = New IntPtr(1)

    <DllImport("user32.dll", EntryPoint:="SetWindowPos")> _
    Public Shared Function SetWindowPos( _
 hWnd As IntPtr, _
 hWndInsertAfter As IntPtr, _
 x As Int32, y As Int32, cx As Int32, cy As Int32, wFlags As Int32) As IntPtr
    End Function


    Public Shared Sub HookWindow()

        Dim Processes As Process() = Process.GetProcessesByName("Notepad")

        For Each p As Process In Processes

            Dim handle As IntPtr = p.MainWindowHandle
            If handle <> IntPtr.Zero Then
                SetWindowPos(handle, HWND_BOTTOM, 200, 200, 0, 0, SWP_NOZORDER Or SWP_NOSIZE Or SWP_SHOWWINDOW)
            End If
        Next

    End Sub

End Class

你用:

HookUtil.HookWindow()

这是一个 C# 实现:

Here's a C# implementation:

using System;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace csharpsandbox

{
    public class HookUtil
    {
        // hooks window handle and repositions to specified coords

        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        public static extern IntPtr SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

        [DllImport("kernel32.dll")]
        static extern IntPtr LoadLibrary(string lpFileName);

        const short SWP_NOMOVE = 0X2;
        const short SWP_NOSIZE = 1;
        const short SWP_NOZORDER = 0X4;
        const int SWP_SHOWWINDOW = 0x0040;
        static readonly IntPtr HWND_BOTTOM = new IntPtr(1);

        public static void HookWindow()
        {

            Process[] parray = System.Diagnostics.Process.GetProcessesByName("Notepad");
            foreach (Process p in parray)
            {
                IntPtr handle = p.MainWindowHandle;
                if (handle != IntPtr.Zero)
                {
                    SetWindowPos(handle, HWND_BOTTOM, 200, 200, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
                }
            }

        }

    }
}

你用:

HookUtil.HookWindow();

显然这里的值(进程名称、x,y 坐标)是硬编码的.为了使这更有用,您需要将它们设置为可设置的.

Obviously the values here (process name, x,y coordinates) are hard-coded. To make this more useful you'd want to make those settable.

这篇关于如何在 VB.NET 中激活、移动和调整窗口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:30