首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

FindWindow始终回到0

2012-11-04 
FindWindow始终返回0代码如下,我建立了一个test.txt文档并打开,然后运行程序,控制台始终返回0,,,,,Google

FindWindow始终返回0
代码如下,我建立了一个test.txt文档并打开,然后运行程序,控制台始终返回0,,,,,Google Baidu找了半天都没有解决。

我的系统是win7,IDE是 MonoDevelop

C# code
using System;using System.Runtime.InteropServices;namespace SendMsgToWindow{    class MainClass    {        //[DllImport("USER32.DLL")]        [DllImport("user32.dll", EntryPoint = "FindWindow")]        public static extern IntPtr FindWindow (string lpClassName, string lpWindowName);            //静态方法必须在静态方法中调用?        private static void MyFindWindow (string wndName)        {            IntPtr ip = FindWindow (null, wndName);            Console.WriteLine(ip.ToString());            if (ip != IntPtr.Zero) {                //找到句柄后,你就可以做你想做的事了,发送信息你可以用SendKeys;                Console.WriteLine ("yes");            }        }        public static void Main (string[] args)        {            MyFindWindow ("test.txt - 记事本");//        }    }}


[解决办法]
在VISTA/WIN 7下 FindWindow或FindWindowEX无法获取到窗体句柄
http://www.cnblogs.com/wuhenke/archive/2010/06/20/1761534.html
[解决办法]
你给的参数不对,最安全的方式是用Visual Studio带的工具spy++找到你所需要的Window,看看对应的class和name是什么,对应更改你的调用代码就可以了。

静态方法无需在静态方法中调用,其他方法也可以的。
[解决办法]
用SPY+确定标题对不对

[解决办法]


你试一下只设置类名,或者只设置窗口名,或2个都设置看。
或者换换其他窗口试一试看呢。有时候和系统关系,出一些奇怪的问题。

热点排行