mirror of
https://github.com/mirror/processhacker
synced 2026-06-08 16:03:24 +00:00
refactoring in WindowHandle
git-svn-id: svn://svn.code.sf.net/p/processhacker/code@1577 21ef857c-d57f-4fe0-8362-d861dc6d29cd
This commit is contained in:
@@ -2251,7 +2251,7 @@ namespace ProcessHacker.Native.Api
|
||||
);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
public static extern int EnumWindows(
|
||||
public static extern bool EnumWindows(
|
||||
[In] [MarshalAs(UnmanagedType.FunctionPtr)] EnumWindowsProc Callback,
|
||||
[In] int param
|
||||
);
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using ProcessHacker.Native.Api;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ProcessHacker.Native.Objects
|
||||
{
|
||||
public struct WindowHandle
|
||||
public delegate bool EnumerateWindowsDelegate(WindowHandle windowHandle);
|
||||
|
||||
public struct WindowHandle : IEquatable<WindowHandle>, IEquatable<IntPtr>
|
||||
{
|
||||
private static WindowHandle _zero = new WindowHandle(IntPtr.Zero);
|
||||
|
||||
@@ -15,6 +17,16 @@ namespace ProcessHacker.Native.Objects
|
||||
get { return _zero; }
|
||||
}
|
||||
|
||||
public static bool Enumerate(EnumerateWindowsDelegate callback)
|
||||
{
|
||||
return Win32.EnumWindows((hWnd, param) => callback(new WindowHandle(hWnd)), 0);
|
||||
}
|
||||
|
||||
public static bool EnumerateByThreadId(int tid, EnumerateWindowsDelegate callback)
|
||||
{
|
||||
return Win32.EnumThreadWindows(tid, (hWnd, param) => callback(new WindowHandle(hWnd)), 0);
|
||||
}
|
||||
|
||||
public static WindowHandle Find(string className, string windowName)
|
||||
{
|
||||
IntPtr handle = Win32.FindWindow(className, windowName);
|
||||
@@ -54,6 +66,11 @@ namespace ProcessHacker.Native.Objects
|
||||
get { return _handle; }
|
||||
}
|
||||
|
||||
public bool IsInvalid
|
||||
{
|
||||
get { return _handle == IntPtr.Zero; }
|
||||
}
|
||||
|
||||
public bool BringToTop()
|
||||
{
|
||||
return Win32.BringWindowToTop(this);
|
||||
@@ -69,6 +86,21 @@ namespace ProcessHacker.Native.Objects
|
||||
return Win32.DestroyWindow(this);
|
||||
}
|
||||
|
||||
public bool EnumerateChildren(EnumerateWindowsDelegate callback)
|
||||
{
|
||||
return Win32.EnumChildWindows(this, (hWnd, param) => callback(new WindowHandle(hWnd)), 0);
|
||||
}
|
||||
|
||||
public bool Equals(WindowHandle other)
|
||||
{
|
||||
return this.Handle.Equals(other.Handle);
|
||||
}
|
||||
|
||||
public bool Equals(IntPtr other)
|
||||
{
|
||||
return this.Handle.Equals(other);
|
||||
}
|
||||
|
||||
public ClientId GetClientId()
|
||||
{
|
||||
int tid, pid;
|
||||
@@ -83,11 +115,13 @@ namespace ProcessHacker.Native.Objects
|
||||
return new WindowHandle(Win32.GetParent(this));
|
||||
}
|
||||
|
||||
public WindowPlacement GetWindowPlacement()
|
||||
public WindowPlacement GetPlacement()
|
||||
{
|
||||
WindowPlacement placement = new WindowPlacement();
|
||||
|
||||
placement.Length = Marshal.SizeOf(placement);
|
||||
Win32.GetWindowPlacement(this, ref placement);
|
||||
|
||||
return placement;
|
||||
}
|
||||
|
||||
@@ -130,5 +164,25 @@ namespace ProcessHacker.Native.Objects
|
||||
{
|
||||
return Win32.IsWindowVisible(this);
|
||||
}
|
||||
|
||||
public bool PostMessage(WindowMessage message, int wParam, int lParam)
|
||||
{
|
||||
return Win32.PostMessage(this, message, wParam, lParam);
|
||||
}
|
||||
|
||||
public IntPtr SendMessage(WindowMessage message, int wParam, int lParam)
|
||||
{
|
||||
return Win32.SendMessage(this, message, wParam, lParam);
|
||||
}
|
||||
|
||||
public bool SetForeground()
|
||||
{
|
||||
return Win32.SetForegroundWindow(this);
|
||||
}
|
||||
|
||||
public bool Show(ShowWindowType flags)
|
||||
{
|
||||
return Win32.ShowWindow(this, flags);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -977,16 +977,15 @@ namespace ProcessHacker
|
||||
|
||||
// Find the process' window (if any).
|
||||
windowHandle = WindowHandle.Zero;
|
||||
Win32.EnumWindows(
|
||||
(hwnd, param) =>
|
||||
WindowHandle.Enumerate(
|
||||
(handle) =>
|
||||
{
|
||||
WindowHandle handle = new WindowHandle(hwnd);
|
||||
// GetWindowLong
|
||||
// Shell_TrayWnd
|
||||
// GetWindowLong
|
||||
// Shell_TrayWnd
|
||||
if (handle.IsWindow() && handle.IsVisible() && handle.IsParent())
|
||||
{
|
||||
int pid;
|
||||
Win32.GetWindowThreadProcessId(hwnd, out pid);
|
||||
Win32.GetWindowThreadProcessId(handle, out pid);
|
||||
|
||||
if (pid == processSelectedPid)
|
||||
{
|
||||
@@ -995,11 +994,11 @@ namespace ProcessHacker
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}, 0);
|
||||
});
|
||||
|
||||
// Enable the Window submenu if we found window owned
|
||||
// by the process. Otherwise, disable the submenu.
|
||||
if (windowHandle.Equals(WindowHandle.Zero))
|
||||
if (windowHandle.IsInvalid)
|
||||
{
|
||||
windowProcessMenuItem.Enabled = false;
|
||||
}
|
||||
@@ -1007,7 +1006,8 @@ namespace ProcessHacker
|
||||
{
|
||||
windowProcessMenuItem.Enabled = true;
|
||||
windowProcessMenuItem.EnableAll();
|
||||
switch (windowHandle.GetWindowPlacement().ShowState)
|
||||
|
||||
switch (windowHandle.GetPlacement().ShowState)
|
||||
{
|
||||
case ShowWindowType.ShowMinimized:
|
||||
minimizeProcessMenuItem.Enabled = false;
|
||||
@@ -1020,7 +1020,7 @@ namespace ProcessHacker
|
||||
case ShowWindowType.ShowNormal:
|
||||
restoreProcessMenuItem.Enabled = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -1409,56 +1409,6 @@ namespace ProcessHacker
|
||||
w.ShowDialog();
|
||||
}
|
||||
|
||||
#region Window
|
||||
|
||||
private void bringToFrontProcessMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!windowHandle.Equals(WindowHandle.Zero) && windowHandle.IsWindow())
|
||||
{
|
||||
WindowPlacement placement = windowHandle.GetWindowPlacement();
|
||||
|
||||
if (placement.ShowState == ShowWindowType.ShowMinimized)
|
||||
Win32.ShowWindow(windowHandle, ShowWindowType.Restore);
|
||||
else
|
||||
Win32.SetForegroundWindow(windowHandle);
|
||||
}
|
||||
}
|
||||
|
||||
private void restoreProcessMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!windowHandle.Equals(WindowHandle.Zero) && windowHandle.IsWindow())
|
||||
{
|
||||
Win32.ShowWindow(windowHandle, ShowWindowType.Restore);
|
||||
}
|
||||
}
|
||||
|
||||
private void minimizeProcessMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!windowHandle.Equals(WindowHandle.Zero) && windowHandle.IsWindow())
|
||||
{
|
||||
Win32.ShowWindow(windowHandle, ShowWindowType.ShowMinimized);
|
||||
}
|
||||
}
|
||||
|
||||
private void maximizeProcessMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!windowHandle.Equals(WindowHandle.Zero) && windowHandle.IsWindow())
|
||||
{
|
||||
Win32.ShowWindow(windowHandle, ShowWindowType.ShowMaximized);
|
||||
}
|
||||
}
|
||||
|
||||
private void closeProcessMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!windowHandle.Equals(WindowHandle.Zero) && windowHandle.IsWindow())
|
||||
{
|
||||
Win32.PostMessage(windowHandle,WindowMessage.Close,0,0);
|
||||
//windowHandle.Close();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Run As
|
||||
|
||||
private void launchAsUserProcessMenuItem_Click(object sender, EventArgs e)
|
||||
@@ -1722,6 +1672,56 @@ namespace ProcessHacker
|
||||
|
||||
#endregion
|
||||
|
||||
#region Window
|
||||
|
||||
private void bringToFrontProcessMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!windowHandle.IsInvalid && windowHandle.IsWindow())
|
||||
{
|
||||
WindowPlacement placement = windowHandle.GetPlacement();
|
||||
|
||||
if (placement.ShowState == ShowWindowType.ShowMinimized)
|
||||
windowHandle.Show(ShowWindowType.Restore);
|
||||
else
|
||||
windowHandle.SetForeground();
|
||||
}
|
||||
}
|
||||
|
||||
private void restoreProcessMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!windowHandle.IsInvalid && windowHandle.IsWindow())
|
||||
{
|
||||
windowHandle.Show(ShowWindowType.Restore);
|
||||
}
|
||||
}
|
||||
|
||||
private void minimizeProcessMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!windowHandle.IsInvalid && windowHandle.IsWindow())
|
||||
{
|
||||
windowHandle.Show(ShowWindowType.ShowMinimized);
|
||||
}
|
||||
}
|
||||
|
||||
private void maximizeProcessMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!windowHandle.IsInvalid && windowHandle.IsWindow())
|
||||
{
|
||||
windowHandle.Show(ShowWindowType.ShowMaximized);
|
||||
}
|
||||
}
|
||||
|
||||
private void closeProcessMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!windowHandle.IsInvalid && windowHandle.IsWindow())
|
||||
{
|
||||
windowHandle.PostMessage(WindowMessage.Close, 0, 0);
|
||||
//windowHandle.Close();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private void searchProcessMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (treeProcesses.SelectedNodes.Count != 1)
|
||||
|
||||
Reference in New Issue
Block a user