左搞右搞,怎么也找不到托盘里程序的句柄。
据说托盘里的程序是没有句柄的。
那该怎么打开呢

解决方案 »

  1.   

    通过进程获取句柄不行吗?通过调用Win32 API实现。Code Snippet
    public class User32API
    {
        private static Hashtable processWnd = null;    public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);    static User32API()
        {
            if (processWnd == null)
            {
                processWnd = new Hashtable();
            }
        }    [DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
        public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);    [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
        public static extern IntPtr GetParent(IntPtr hWnd);    [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
        public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);    [DllImport("user32.dll", EntryPoint = "IsWindow")]
        public static extern bool IsWindow(IntPtr hWnd);    [DllImport("kernel32.dll", EntryPoint = "SetLastError")]
        public static extern void SetLastError(uint dwErrCode);    public static IntPtr GetCurrentWindowHandle()
        {
            IntPtr ptrWnd = IntPtr.Zero;
            uint uiPid = (uint)Process.GetCurrentProcess().Id;  // 当前进程 ID
            object objWnd = processWnd[uiPid];        if (objWnd != null)
            {
                ptrWnd = (IntPtr)objWnd;
                if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd))  // 从缓存中获取句柄
                {
                    return ptrWnd;
                }
                else
                {
                    ptrWnd = IntPtr.Zero;
                }
            }        bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
            // 枚举窗口返回 false 并且没有错误号时表明获取成功
            if (!bResult && Marshal.GetLastWin32Error() == 0)
            {
                objWnd = processWnd[uiPid];
                if (objWnd != null)
                {
                    ptrWnd = (IntPtr)objWnd;
                }
            }        return ptrWnd;
        }    private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
        {
            uint uiPid = 0;        if (GetParent(hwnd) == IntPtr.Zero)
            {
                GetWindowThreadProcessId(hwnd, ref uiPid);
                if (uiPid == lParam)    // 找到进程对应的主窗口句柄
                {
                    processWnd[uiPid] = hwnd;   // 把句柄缓存起来
                    SetLastError(0);    // 设置无错误
                    return false;   // 返回 false 以终止枚举窗口
                }
            }        return true;
        }
      

  2.   

    这个代码我试过了,如果程序在托盘里,就找不到句柄了。如果程序只是最小化到任务栏,还可以。据说托盘里的程序是没有句柄的。
    上面的代码跟process.MainWindowHandle结果差不多,只不过在托盘里process.MainWindowHandle为0了,上面代码还是返回什么IME什么的
      

  3.   

    可以给QQ设置快捷键嘛。利用快捷键弹出。QQ窗体
      

  4.   

    这是一段vb的代码,据说是可以取QQ的句柄
    CreateToolhelp32Snapshot API

    PROCESSENTRY32 的定义,百度一下Dim uSnapShot As Long '系统快照返回值
      Dim uResult As Long '遍历进程返回值
      Dim uProcess As PROCESSENTRY32 '定义进程结构变量
      Dim meHandle As Long '进程句柄
      
      uSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&) '建立系统快照
      uProcess.dwSize = Len(uProcess) '初始化进程信息长度
      
      If uSnapShot Then
        uResult = Process32First(uSnapShot, uProcess) '取得第一个进程
        Do While uResult
         
         If InStr(Left(uProcess.szexeFile, InStr(uProcess.szexeFile, Chr(0)) - 1), "QQ.exe") > 0 Then
          meHandle = OpenProcess(PROCESS_ALL_ACCESS, True, uProcess.th32ProcessID)
          'meHandle中就是你想要的QQ的句柄
         End If
        uResult = Process32Next(uSnapShot, uProcess) '取得快照中的下一个进程
        Loop
      End If 
      

  5.   


     //if   (!::EnumWindows((WNDENUMPROC)enumProc,   0))       //  AfxMessageBox("Error");  BOOL CALLBACK enumProc(HWND   hwnd,   LPARAM   lParam)   {       if   (hwnd   ==   NULL)          return   FALSE;       if   (::IsWindow(hwnd)   )//&&   ::IsWindowVisible(hwnd))       {          TCHAR   szCap[255]   =   {0};          ::GetWindowText(hwnd,   szCap,   255);          if   (strlen(szCap)   ==   0)              return   TRUE;         CString str ;       str.Format(_T("%s"),szCap);         if(str.Find(_T("ubbles")) != -1)           ShowWindow(hwnd,SW_SHOWNORMAL);     }       return   TRUE;   }据说这个可以,可是怎么用C#写出来呢?
      

  6.   

    这个代码可以的,但是根据GetWindowThreadProcessId搞出来的不对,根据getwindowtext搞出来的是对的,不知道怎么回事。大概QQ的进程还有其他顶层窗体?
      

  7.   

    如果windowtext不固定的话就枚举不到了,如果已知窗口标题,那又何必用EnumWindows来枚举呢?