首页 新闻 会员 周边

Findwindow控制窗体遇到了大问题~~

0
悬赏园豆:30 [待解决问题]

这几天想写一个程序,然后控制这个程序执行一些作业,抱着试试的心情,先试了试控制按钮点击,然后打开的是一个openfileDialog窗口,可是,,这个控制它打开的程序未响应了,,有木有,,头大了半天了,什么return ,全部试过了,可是还是不行,求指点,,菜鸟一个,求大拿。。原代码~:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using System.IO;
11 using System.Text.RegularExpressions;
12 using System.Runtime.InteropServices;
13 
14 namespace 控制程序
15 {
16     public partial class Form1 : Form
17     {
18         [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
19         static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
20         [DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
21         extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
22         [DllImport("User32.dll", EntryPoint = "SendMessage")]
23         private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, string lParam);
24         public Form1()
25         {
26             InitializeComponent();
27         }
28         
29         const int BM_CLICK = 0xF5;
30         const int WM_GETTEXT = 0x000D;
31         const int WM_SETTEXT = 0x000C;
32         const int WM_CLICK = 0x00F5;
33 
34         private void button1_Click(object sender, EventArgs e)
35         {
36             IntPtr maindHwnd = FindWindow(null, "对话框"); //获得照相馆上传照片的句柄 
37             if (maindHwnd != IntPtr.Zero)
38             {
39                 IntPtr childHwnd2 = FindWindowEx(maindHwnd, IntPtr.Zero, null, "打开文件");
40                 if (childHwnd2 != IntPtr.Zero)
41                 {
42                     SendMessage(childHwnd2, WM_CLICK, 0, null);
43                 }
44                 else
45                 {
46                     MessageBox.Show("没有找到子窗口");
47                 }
48             }
49             else
50             {
51                 MessageBox.Show("没有找到窗口");
52             }
53         }
54     }
55 }

然后是另一个程序的代码,这个程序什么都没有。。:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 打开对话框
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = @"C:/";
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }
    }
}

然后是这个问题的图片。。。:

关掉openfilediolog程序就没事了,,够详细了没,,,求解决方案。。。

奔波在大路上的主页 奔波在大路上 | 初学一级 | 园豆:74
提问于:2013-06-15 00:43
< >
分享
所有回答(1)
1

Windows API改用PostMessage应该就没事了,具体你可以看一下这两个API的说明。

I,Robot | 园豆:9783 (大侠五级) | 2013-06-15 08:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册