Process^ currentProcess = Process::GetCurrentProcess();
IntPtr hWndToProcess = currentProcess ->MainWindowHandle; 结果发现hWndToProcess 值为0.请问是怎么回事呀..我是要得到这个窗口的句柄..程序只有一个界面(窗口)请高手指点..谢谢环境是VC++2005
IntPtr hWndToProcess = currentProcess ->MainWindowHandle; 结果发现hWndToProcess 值为0.请问是怎么回事呀..我是要得到这个窗口的句柄..程序只有一个界面(窗口)请高手指点..谢谢环境是VC++2005
解决方案 »
- 怎么将另一个程序的窗口添加到现有程序?
- 谁能给个获取文件crc32的算法啊?
- 请问如何把null值付给整形或数字型,日期型变量?
- c#问题,使用filestream ,老是提示服务器错误,文件被占用,请问怎么解决?
- 如何在安装程序时弹出一个注册窗口?
- C#有没有向vb那样的把API函数直接剪切复制那样的工具吗?
- 请问SharpDevelop是什么东东,除了免费,还有什么优点呢?!
- 如何让datagridview单元格只能输入数字
- 版主::::
- C#调用C写的DLL,Dll中有函数参数是char arr[],C#中如何定义和传递改参数?在线等待
- 请问C#中日历控件的DateChanged事件的问题!
- 边吃边聊——怎样才能学好C#
那我改成C#语法提问....(不过都是.NET嘛 哈哈..高手先别计较C#还是C++好吗?)Process currentProcess = Process.GetCurrentProcess();
IntPtr hWndToProcess = currentProcess .MainWindowHandle;
hWndToProcess 得到的值却是0..试着用GetProcessesByName(devenv),获取VS的主窗口句柄却能正常得到..
我写的程序只有一个窗口..
请看下面程序的窗口的标题,或者单击窗体客户区,你都能看到非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);
}
}
}
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);
}
}
}
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);
}
}
}
GOOGLE了半天..结论大多是说因为程序没有图形界面 .或者说是程序没有加载完..
但是我已经使用了WaitForInputIdle函数.程序有图形界面 .并且已经加载完成..通过GetProcessesByName(),测试了几个例子..最终发现.如果程序没有在任务栏出现,在托盘运行 .则取不出窗口句柄(尽管程序有窗体).但是我恰好是要取的程序是只在托盘运行,而且窗体已经是Hide的...再次请教高手们...在此谢谢了
{
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);
}
{
Console.WriteLine(Handle);
Console.WriteLine(Process.GetCurrentProcess().MainWindowHandle);
}
结贴了