2015-03-18 5 views
5

Ich habe ein Fenster, von dem ich nur den Titel (z. B. Notepad) kenne, den ich aktivieren, Größe ändern und in der oberen linken Ecke meines Bildschirms platzieren muss. Also nach einigen Recherchen auf MSDN und Foren habe ich einige Funktionen gefunden, die dies erreichen sollten. Ich benutze FindWindow, um das Handle nach Titel zu bekommen, dann benutze GetWindowPlacement, um zu sehen, ob Notepad minimiert ist oder nicht (wenn nicht, dann benutze ich nur AppActivate, muss es nur aktivieren, wenn es nicht minimiert ist). Wenn das Fenster jedoch minimiert ist, versuche ich SetWindowPlacement zu verwenden, um es in einem Befehl zu aktivieren, zu skalieren und zu verschieben. Hier ist mein Code:Wie aktiviert, verschiebe und skaliere ich ein Fenster in VB.NET

<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 

die tatsächliche Ausführung, hier zu sein:

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") 

Also das ich versuche laufe, aber es aktiviert nur das Fenster, während das Rechteck auch sollte es ändern. Was mache ich falsch? Gibt es einen einfacheren Weg, all das zu erreichen? Sorry für den langen Pfosten

+0

Mit Ihrem Code werden die Effekte des Rechtecks ​​ausgeführt, nachdem Sie das Fenster von 'maximized' auf' normal' zurückgesetzt haben. Ändern Sie "wp2.showCmd = ShowWindowCommands.ShowMaximized" in "wp2.showCmd = ShowWindowCommands.Normal". Sie möchten nicht, dass das Fenster maximiert wird. –

+0

Sie vernachlässigen die Fehlerprüfung –

Antwort

1

Ich hatte ein ähnliches, was zu tun, und verwendet, um die SetWindowPos-Funktion von user32.dll

Hier ist eine vb.net Implementierung:

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 

Sie nennen es mit:

HookUtil.HookWindow() 

Hier ist eine C# -Implementierung:

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

     } 

    } 
} 

Sie nennen es mit:

HookUtil.HookWindow(); 

Offensichtlich sind die Werte hier (Prozessname, x, y-Koordinaten) sind hartcodiert. Um dies nützlicher zu machen, sollten Sie diese Einstellungen vornehmen.

Verwandte Themen