IntPtr hwndDialog;
string pathFile;
EnumBrowserFileSaveType saveType;
[DllImport(
"user32.dll"
, CharSet = CharSet.Auto)]
public
static
extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
[DllImport(
"user32.dll"
, CharSet = CharSet.Auto)]
public
static
extern IntPtr GetDlgItem(IntPtr hWnd, int nIDDlgItem);
[DllImport(
"user32.dll"
, CharSet = CharSet.Auto)]
static
extern
private
bool SetWindowText(IntPtr hWnd, string lpString);
[DllImport(
"user32.dll"
)]
[
return
: MarshalAs(UnmanagedType.Bool)]
static
extern bool IsWindowVisible(IntPtr hWnd);
[DllImport(
"user32.dll"
)]
static
extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport(
"user32.dll"
)]
static
extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfeter, string lpszClass, string lpszWindow);
[DllImport(
"user32.dll"
)]
static
extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, String lParam);
[DllImport(
"user32.dll"
)]
static
extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
const
uint WM_SETTEXT = 0x000c;
const
uint WM_IME_KEYDOWN = 0x0290;
const
uint WM_LBUTTONDOWN = 0x0201;
const
uint WM_LBUTTONUP = 0x0202;
public
void ThreadProc()
{
int maxRetry = 10;
int retry = 0;
IntPtr hWndPrint = FindWindow(
"#32770"
,
"打印"
);
IntPtr hWnd = FindWindow(
"#32770"
,
"文件另存为"
);
if
(hWnd != IntPtr.Zero)
{
log.InfoFormat(
"got saveas dialog handle. Printer Dialog skipped."
);
}
else
{
Thread.Sleep(200);
hWndPrint = FindWindow(
"#32770"
,
"打印"
);
while
(hWndPrint == IntPtr.Zero && retry < maxRetry)
{
Thread.Sleep(200);
log.InfoFormat(
"retry get Print dialog handle.retry:{0}"
, retry);
hWndPrint = FindWindow(
"#32770"
,
"打印"
);
retry++;
}
if
(hWndPrint == IntPtr.Zero)
{
Thread.Sleep(1000);
hWndPrint = FindWindow(
"#32770"
,
"打印"
);
}
if
(hWndPrint == IntPtr.Zero)
{
log.InfoFormat(
"Did not get Print dialog handle.retry:{0}"
, retry);
return
;
}
log.InfoFormat(
"got Print dialog handle.retry:{0}"
, retry);
IntPtr hChildP;
hChildP = IntPtr.Zero;
hChildP = FindWindowEx(hWndPrint, IntPtr.Zero,
"Button"
,
"打印(&P)"
);
PostMessage(hChildP, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
PostMessage(hChildP, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
Application.DoEvents();
}
hWnd = FindWindow(
"#32770"
,
"文件另存为"
);
retry = 0;
while
((!IsWindowVisible(hWnd) || hWnd == IntPtr.Zero) && retry < maxRetry)
{
Thread.Sleep(200);
log.InfoFormat(
"retry get saveas dialog handle.retry:{0}"
, retry);
hWnd = FindWindow(
"#32770"
, null);
retry++;
Application.DoEvents();
}
log.InfoFormat(
"got saveas dialog handle.retry:{0}"
, retry);
if
(hWnd == IntPtr.Zero)
{
Thread.Sleep(1000);
hWnd = FindWindow(
"#32770"
,
"文件另存为"
);
}
if
(hWnd == IntPtr.Zero)
{
return
;
}
Application.DoEvents();
IntPtr hChild;
hChild = FindWindowEx(hWnd, IntPtr.Zero,
"DUIViewWndClassName"
, String.
Empty
);
hChild = FindWindowEx(hChild, IntPtr.Zero,
"DirectUIHWND"
, String.
Empty
);
hChild = FindWindowEx(hChild, IntPtr.Zero,
"FloatNotifySink"
, String.
Empty
);
hChild = FindWindowEx(hChild, IntPtr.Zero,
"ComboBox"
, String.
Empty
);
hChild = FindWindowEx(hChild, IntPtr.Zero,
"Edit"
, String.
Empty
);
SendMessage(hChild, WM_SETTEXT, IntPtr.Zero, pathFile);
System.Threading.Thread.Sleep(1000);
hChild = IntPtr.Zero;
hChild = FindWindowEx(hWnd, IntPtr.Zero,
"Button"
,
"保存(&S)"
);
PostMessage(hChild, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
PostMessage(hChild, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
Application.DoEvents();
return
;
}