Process^ currentProcess = Process::GetCurrentProcess();
IntPtr hWndToProcess = currentProcess ->MainWindowHandle; 结果发现hWndToProcess 值为0.请问是怎么回事呀..我是要得到这个窗口的句柄..程序只有一个界面(窗口)请高手指点..谢谢环境是VC++2005

解决方案 »

  1.   


    那我改成C#语法提问....(不过都是.NET嘛    哈哈..高手先别计较C#还是C++好吗?)Process currentProcess = Process.GetCurrentProcess(); 
    IntPtr hWndToProcess = currentProcess .MainWindowHandle; 
    hWndToProcess 得到的值却是0..试着用GetProcessesByName(devenv),获取VS的主窗口句柄却能正常得到..
    我写的程序只有一个窗口..
      

  2.   

    窗体显示完成后,才能得到窗口句柄。
    请看下面程序的窗口的标题,或者单击窗体客户区,你都能看到非0的窗口句柄。using System;
    using System.Diagnostics;
    using System.Windows.Forms;namespace GUI1
    {
      class Prog : Form
      {
        [STAThread]
        static void Main()
        {
          Application.Run(new Prog());
        }    // 窗体是首次显示时
        protected override void OnShown(EventArgs e)
        {
          Text = Process.GetCurrentProcess().MainWindowHandle.ToString();
          base.OnShown(e);
        }    // 单击窗体客户区时
        protected override void OnClick(EventArgs e)
        {
          MessageBox.Show(Process.GetCurrentProcess().MainWindowHandle.ToString());
          base.OnClick(e);
        }
      }
    }
      

  3.   

    如果改成这样,你将会发现窗口的标题为0,而弹出的消息框显示非0的窗口句柄。using System;
    using System.Diagnostics;
    using System.Windows.Forms;namespace GUI1
    {
      class Prog : Form
      {
        [STAThread]
        static void Main()
        {
          Application.Run(new Prog());
        }    // 构造函数,此时窗口尚未显示。
        Prog()
        {
          Text = Process.GetCurrentProcess().MainWindowHandle.ToString();
        }
        
        // 窗体是首次显示时
        protected override void OnShown(EventArgs e)
        {
          MessageBox.Show(Process.GetCurrentProcess().MainWindowHandle.ToString());
          base.OnShown(e);
        }
      }
    }
      

  4.   

    这样也行,直接看窗体标题好了:using System;
    using System.Diagnostics;
    using System.Windows.Forms;namespace GUI1
    {
      class Prog : Form
      {
        [STAThread]
        static void Main()
        {
          Application.Run(new Prog());
        }    // 构造函数
        Prog()
        {
          Text = Process.GetCurrentProcess().MainWindowHandle.ToString();
        }
        
        // 窗体是首次显示时
        protected override void OnShown(EventArgs e)
        {
          Text += " -> " + Process.GetCurrentProcess().MainWindowHandle.ToString();
          base.OnShown(e);
        }
      }
    }
      

  5.   

    谢谢wuyi8808
    GOOGLE了半天..结论大多是说因为程序没有图形界面 .或者说是程序没有加载完..
    但是我已经使用了WaitForInputIdle函数.程序有图形界面 .并且已经加载完成..通过GetProcessesByName(),测试了几个例子..最终发现.如果程序没有在任务栏出现,在托盘运行 .则取不出窗口句柄(尽管程序有窗体).但是我恰好是要取的程序是只在托盘运行,而且窗体已经是Hide的...再次请教高手们...在此谢谢了
      

  6.   

    唉...无解贴....我用其他API函数算了..
      

  7.   

    public Form1()
    {
        Console.WriteLine(Process.GetCurrentProcess().MainWindowHandle); // 0
        InitializeComponent();
    }private void Form1_Load(object sender, EventArgs e)
    {
        Console.WriteLine(Process.GetCurrentProcess().MainWindowHandle); // 0
    }private void button1_Click(object sender, EventArgs e)
    {
        Console.WriteLine(Process.GetCurrentProcess().MainWindowHandle); // 输出非零
    }通过测试,是获取“时机”的问题。看来是构造和载入完毕不能获取。可以放到窗体第一次显示的事件中:
    private void Form1_Shown(object sender, EventArgs e)
    {
        Console.WriteLine(Process.GetCurrentProcess().MainWindowHandle);
    }
      

  8.   

    另外,通常当前进程主窗体的句柄,其实就是Form1的实例句柄private void Form1_Shown(object sender, EventArgs e)
    {
        Console.WriteLine(Handle);
        Console.WriteLine(Process.GetCurrentProcess().MainWindowHandle);
    }
      

  9.   

    谢谢 清洁工 大哥...但是我已经说过了..不是因为窗体没有加载完的问题..而是Process类的MainWindowHandle方法不能取没有在任务栏上面显示的窗口句柄..也就是说.我要获取的窗口..必须是在任务上面..如果只在托盘处.就不能取.我试一下FindWindow但是如果有多个标题相同的窗体..应该怎么办..这是我现在正在知道的..
      

  10.   

    用FindWindow 勉强把问题解决了...唉...
    结贴了
      

  11.   

    和楼主遇到同样的问题,但是你FindWindow需要calssname啊