为了用API private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);和private static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter, string lpszClass, string lpszWindow);这两个函数。。 开了二三十个页面查资料了,看的我快吐血了,lpszClass说是类名,MD也不说清楚,一开始我还以为是自己写的页面的类名,后来发现不是。貌似是控件的系统类名,用了也不对,那是自己设定的控件名?好像也不对。lpszWindow应该是控件的Text.已经抓狂了!用FindWindow成功找到了主窗体的句柄,但是用FindWindowEx死活找不到窗体上托盘图标的句柄。MMD叼函数参数说明能不能清楚点啊?后面两个查询条件lpszClass和lpszWindow到底是什么啊,控件系统类名Botton?还是自己命的名BottonName?还是控件上的文本BottonText????以上是牢骚,可以掠过。        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd,int Msg, IntPtr wParam, string lParam);         IntPtr defaultWnd = IntPtr.Zero;
        IntPtr notifyIconWnd = IntPtr.Zero;
        
        defaultWnd = FindWindow(null, "主窗体");//这个成功找到了
        //我要找主窗体上的NotifyIcon,NotifyIcon属性如下:
        //Name:notifyIcon1     Text:notifyIconText
        notifyIconWnd = FindWindowEx(defaultWnd, IntPtr.Zero, "NotifyIcon", null);//找不到
        notifyIconWnd = FindWindowEx(defaultWnd, IntPtr.Zero, "NotifyIcon", "notifyIcon1");//找不到
        notifyIconWnd = FindWindowEx(defaultWnd, IntPtr.Zero, "NotifyIcon", "notifyIconText");//找不到
        notifyIconWnd = FindWindowEx(defaultWnd, IntPtr.Zero, null,"notifyIconText");//找不到
        notifyIconWnd = FindWindowEx(defaultWnd, IntPtr.Zero, null,"notifyIcon1");//找不到 
        //死活就是找不到。救命啊!!!!!!!!!!!!!

解决方案 »

  1.   

    NotifyIcon是托盘区的吗?好像托盘区的没有窗口标题。不清楚。纯属个人见解。。不知道是不是。
      

  2.   

    IntPtr vHandle = FindWindow("Shell_TrayWnd", null);
    vHandle = FindWindowEx(vHandle, IntPtr.Zero, "TrayNotifyWnd", null);
    vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SysPager", null);
    vHandle = FindWindowEx(vHandle, IntPtr.Zero, "ToolbarWindow32", null);
    参考:
    http://topic.csdn.net/u/20091018/23/91a416ee-4452-4e6d-ba40-54c9a0f805c0.html?71809
      

  3.   

    NotifyIcon不就是个托盘图标的控件吗,我就是在窗口最小化的时候将窗口隐藏掉,然后让托盘图标显示出来
      

  4.   

    我试了上面的的确能找到,但找出来的是什么东西不知道啊。怎么通过这些找到我自己的托盘图标?
    参考帖子里说“Shell_TrayWnd”就是操作任务栏是这一特殊的窗口的类,包括是托盘。
    那我这么写IntPtr vHandle = FindWindow("Shell_TrayWnd", null);
    IntPtr notifyIconWnd = FindWindowEx(vHandle, IntPtr.Zero, null, "MyNotifyIconName");
    //这么找也找不到啊
      

  5.   


    这样不行,不是标准的windows窗口。用2楼的方法。用spy++查一下就知道了。
      

  6.   

    看到不少地方的都提到了spy++,是用这个查控件所谓的类名?Button的类名我用Button竟然不对我查查看。
    2L的方法我试了啊,在别人的Blog里也看到了,那四行完了最后获取的是个什么句柄啊?而且我这个是标准的windows窗口啊,要找的NotifyIcon也是窗体里的控件啊,和找Button有什么区别吗?
      

  7.   

    试过了,用Spy++查出了窗体中Button的类名是“WindowsForms10.BUTTON.app.0.2004eee”,用这个是对的但我怎么找显示的托盘图标。。不太玩的来
      

  8.   

    新的一天新的一顶!
    想想不对,我要找托盘图标的句柄干什么?就算找到了它的click事件也是写在Form里的。我的目的是要调用已经打开的程序窗口的方法或者属性。
    现在已经成功获得了defaultWnd = FindWindow(null, "主窗体");怎么用这个defaultWnd来实例化Form?或者怎样能直接用defaultWnd去触发Form里的事件?