程序控制开始菜单的弹出 C#

基本思路为,寻找开始菜单按钮窗口,并模拟用户单击

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

posted on 2008-10-21 09:12  踏燕归来  阅读(183)  评论(0编辑  收藏  举报