大家好 如何在C#中检测到用户点击了外部程序(例如记事本)的最小化按钮? 感谢
答案 0 :(得分:2)
这应该有效:
public class myClass
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
const UInt32 SW_HIDE = 0;
const UInt32 SW_SHOWNORMAL = 1;
const UInt32 SW_NORMAL = 1;
const UInt32 SW_SHOWMINIMIZED = 2;
const UInt32 SW_SHOWMAXIMIZED = 3;
const UInt32 SW_MAXIMIZE = 3;
const UInt32 SW_SHOWNOACTIVATE = 4;
const UInt32 SW_SHOW = 5;
const UInt32 SW_MINIMIZE = 6;
const UInt32 SW_SHOWMINNOACTIVE = 7;
const UInt32 SW_SHOWNA = 8;
const UInt32 SW_RESTORE = 9;
public myClass()
{
var proc = Process.GetProcessesByName("notepad");
if (proc.Length > 0)
{
bool isNotepadMinimized = myClass.GetMinimized(proc[0].MainWindowHandle);
if (isNotepadMinimized)
Console.WriteLine("Notepad is Minimized!");
}
}
private struct WINDOWPLACEMENT
{
public int length;
public int flags;
public int showCmd;
public System.Drawing.Point ptMinPosition;
public System.Drawing.Point ptMaxPosition;
public System.Drawing.Rectangle rcNormalPosition;
}
public static bool GetMinimized(IntPtr handle)
{
WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
placement.length = Marshal.SizeOf(placement);
GetWindowPlacement(handle, ref placement);
return placement.flags == SW_SHOWMINIMIZED;
}
}
编辑:只需重新阅读您的问题,并注意到您希望在记事本最小化时收到通知。那么你可以在计时器中使用上面的代码来轮询状态变化。
答案 1 :(得分:0)
正如Hans Passant所说,你无法将事件降到最低限度。
虽然,我相信你可以存储窗口的状态,看看它们是否在稍后的时间间隔内被最小化。 使用GetWindowPlacement Function。
答案 2 :(得分:0)
以上答案有一个错误。
您需要检查placement.showCMD
,而不是placement.flags
WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
placement.length = Marshal.SizeOf(placement);
GetWindowPlacement(_hwnd, ref placement);
return placement.showCmd == SW_SHOWMINIMIZED;
请参阅https://msdn.microsoft.com/en-us/library/windows/desktop/ms632611(v=vs.85).aspx