FindWindow找不到窗口

liuqingrong1031 2008-11-28 11:13:41
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;


[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string className, string windowName);

[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern bool IsIconic(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern bool OpenIcon(IntPtr hWnd);
for (int i = 1; i <= number; i++)
{
textBox1.Text = folderBrowserDialog1.SelectedPath+"\\";
string path = textBox1.Text + "nihao(" + i + ").jpg";

Process.Start("mspaint.exe", path);
string cap = "nihao(" + i + ").jpg" + " - 画图";

IntPtr handle = FindWindow(null, cap);
if (handle != IntPtr.Zero)
{
//MessageBox.Show("nihao");
if (IsIconic(handle))
{
OpenIcon(handle);
}
else
{
SetForegroundWindow(handle);
Thread.Sleep(2000);

}
}


//SendKeys.Send("^S");

SendKeys.Send("%{F4}");



}
关掉的是主窗口,而不是画图窗口。
...全文
288 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kimmking 2008-11-28
  • 打赏
  • 举报
回复
SendKeys.Send("%{F4}"); 并没有send到mspaint的窗口上,而是send到你的主窗口了。
要想关掉mapaint,需要postmessage。
gomoku 2008-11-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 liuqingrong1031 的回复:]
还是不行啊。
[/Quote]

不要总是说“还是不行啊”
它没有增加有用的信息。

要学会怎么定位错误。
比如如果handle还是零,那就试试开一个固定的窗口,检测FindWindow是否工作正常...
liuqingrong1031 2008-11-28
  • 打赏
  • 举报
回复
还是不行啊。
gomoku 2008-11-28
  • 打赏
  • 举报
回复
程序刚刚创建,窗口可能还没来得及显示。试试用Process.WaitForInputIdle()等它一下。

Process process = Process.Start("mspaint.exe", path);
process.WaitForInputIdle(); //<---
string cap = "nihao(" + i + ").jpg" + " - 画图";
...
liuqingrong1031 2008-11-28
  • 打赏
  • 举报
回复
handle 为IntPtr.Zero,所以没能激活画图窗口,为什么handle为intptr.zero呢。

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧