这几天想写一个程序,然后控制这个程序执行一些作业,抱着试试的心情,先试了试控制按钮点击,然后打开的是一个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程序就没事了,,够详细了没,,,求解决方案。。。
Windows API改用PostMessage应该就没事了,具体你可以看一下这两个API的说明。