C#怎么用API程序控制开始菜单,帮忙解释一下
用C#调用API的“USER32.DLL”,控制开始菜单的弹出。也就是模拟用户单击键盘上的WINDOWS键。
我找了好久了,百度上出了一大堆。就是下面这段程序。。也找不到原作者是,写的不是很清楚。看不懂。
大致看懂了有以下几点:
User32是他写的一个类。类里有几个结构和方法。
还有几点不明白。
1:第三行 new EnumProc(Enum),这个是什么,请高人解释一下。
2:程序流程也不太清楚。
3:还有API。也不是很清楚。
基本思路为,寻找开始菜单按钮窗口,并模拟用户单击
void pop()
{
User32.EnumWindows(new EnumProc(Enum), 0);
}
private bool Enum(IntPtr hWnd, uint param)
{
User32.STRINGBUFFER buffer=new User32.STRINGBUFFER();
User32.GetClassName(hWnd, ref buffer, 256);
if (buffer.szText == "Shell_TrayWnd")
{
User32.EnumChildWindows(hWnd, new EnumProc(Enum2), 0);
return false;
}
else return true;
}
IntPtr lastForeGroundWindow;
private bool Enum2(IntPtr hWnd, uint param)
{
User32.STRINGBUFFER buffer = new User32.STRINGBUFFER();
User32.GetClassName(hWnd, ref buffer, 256);
if (buffer.szText == "Button")
{
if (User32.SendMessage(hWnd, WndMsg.BM_GETSTATE, 0, 0) ==
User32.BST_PUSHED)
{
User32.SetForegroundWindow(lastForeGroundWindow);
}
else
{
lastForeGroundWindow = User32.GetForegroundWindow();
User32.SetForegroundWindow(hWnd);
User32.PostMessage(hWnd, WndMsg.BM_CLICK, 0, 0);
}
return true;
}
else
return false;
}
using System;
using System.Runtime.InteropServices;
public delegate bool CallBack(int hwnd, int lParam);
public class EnumWindowsApp
{
[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);
public static void Main()
{
CallBack myCallBack = new CallBack(EnumWindowsApp.Report);
EnumWindows(myCallBack, 0);
}
public static bool Report(int hwnd, int lParam)
{
Console.Write("Window handle is :");
Console.WriteLine(hwnd);
return true;
}
}