无法获得Notepad的Save As窗体句柄
代码如下,简单的模拟记事本操作,流程为:打开记事本,输入字符串,打开Save As窗体,输入文件名,保存之。现在的问题是,当程序运行到打开Save As窗体后,后台程序就不再调用31行的SaveFile方法了了(Debug不到SaveFile方法,只有关闭Save As窗口后才可以继续运行),请问大家遇到过这种情况吗?该如何解决呢?
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
StartNotepad();
//Get Main Window
IntPtr mwh = GetMainWindowByCaptionName("Untitled - Notepad");
Console.WriteLine("Maximum Notepad window");
MaximumMainWindow(mwh);
System.Threading.Thread.Sleep(2000);
Console.WriteLine("Get edit control and input test string.");
//Get edit control by index
IntPtr childWindow = GetChildWindowByIndex(mwh, 1);
SendString(childWindow, "Just a test!!!");
//Save a new file to harddisk;
Console.WriteLine("Open Save As Dialog.");
OpenSaveAsDialog(mwh);
Console.WriteLine("Save file to hard disk.");
SaveFile("tes");
}
static void OpenSaveAsDialog(IntPtr hwndMain)
{
IntPtr mainMenu = GetMenu(hwndMain);
IntPtr hFile = GetSubMenu(mainMenu, 0);
int iSave = GetMenuItemID(hFile, 2);
uint WM_COMMAND = 0x0111;
SendMessage2(hwndMain, WM_COMMAND, iSave, IntPtr.Zero);
}
static void SaveFile(string fileName)
{
IntPtr hwndSaveAs = GetMainWindowByCaptionName("Save As");
IntPtr hwndCancel = GetChildWindowByCaptionName(hwndSaveAs, "Cancel");
uint WM_LBUTTONDOWN=0x0201;
PostMessage1(hwndCancel,WM_LBUTTONDOWN, 0, 0);
Console.WriteLine(hwndSaveAs);
}
static bool StartNotepad()
{
string path = System.Environment.ExpandEnvironmentVariables("%windir%");
string appname = path+@"\notepad.exe";
Process p = Process.Start(appname);
if (p == null)
{
Console.WriteLine("Process may already exist");
return false;
}
else
{
return true;
}
}
static IntPtr GetMainWindowByCaptionName(string caption)
{
IntPtr mwh = IntPtr.Zero;
bool windowFound = false;
int attempts = 0;
while (!windowFound && attempts < 25)
{
if (mwh == IntPtr.Zero)
{
Console.WriteLine("Window not yet found.");
System.Threading.Thread.Sleep(1000);
attempts++;
mwh = FindWindow(null, caption);
}
else
{
Console.WriteLine("Window has been found.");
windowFound = true;
}
}
return mwh;
}
static void MaximumMainWindow(IntPtr mwh)
{
uint WM_SYSCOMMAND=0x0112;
int SC_MAXIMIZE = 0xf030;
PostMessage1(mwh, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
}
static IntPtr GetChildWindowByCaptionName(IntPtr parent,string caption)
{
IntPtr childhWnd=IntPtr.Zero;
bool windowFound = false;
int attempts = 0;
while (!windowFound && attempts < 25)
{
if (childhWnd == IntPtr.Zero)
{
Console.WriteLine("Window not yet found.");
System.Threading.Thread.Sleep(1000);
attempts++;
childhWnd = FindWindowEx(parent, IntPtr.Zero, null, caption);
}
else
{
Console.Write("Window has been found.");
windowFound = true;
}
}
return childhWnd;
}
static IntPtr GetChildWindowByIndex(IntPtr hwndParent, int index)
{
if (index == 0)
{
return hwndParent;
}
else
{
int ct = 0;
IntPtr result = IntPtr.Zero;
do
{
result = FindWindowEx(hwndParent, result, null, null);
if (result != IntPtr.Zero)
ct++;
} while (ct < index && result != IntPtr.Zero);
return result;
}
}
static void SendChar(IntPtr hControl, char c)
{
uint WM_CHAR = 0x0102;
SendMessage1(hControl, WM_CHAR, c, 0);
}
static void SendString(IntPtr hControl, string s)
{
foreach (char c in s)
{
SendChar(hControl, c);
}
}
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "PostMessage", CharSet = CharSet.Auto)]
static extern bool PostMessage1(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint="FindWindowEx", CharSet=CharSet.Auto)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
static extern void SendMessage1(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll")]
static extern IntPtr GetMenu(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr GetSubMenu(IntPtr hMenu, int nPos);
[DllImport("user32.dll")]
static extern int GetMenuItemID(IntPtr hMenu, int nPos);
[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
static extern void SendMessage2(IntPtr hWnd, uint Msg, int wParam, IntPtr lParam);
}
}
补充:.NET技术 , C#