EDN Admin
Well-known member
Hi,
I want to control the SaveAsDialog with the WebBrowser component. As there is no direct access to the saveas capability I am trying to control the dialog box. On my Win7 test workstation I have worked out that I can use a thread to monitor me starting ShowSaveAsDialog() and then populate the file name textbox using the following code (ControlID found using Spy++ (64bit):
[DllImport("User32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string className, string lpszWindow);
private static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, string lParam);
WHandle = (IntPtr)FindWindow("#32770", "Save Webpage");
if (WHandle != null && (int)WHandle != 0)
{
SetForegroundWindow(WHandle);
child = FindWindowEx(WHandle, IntPtr.Zero, @"DUIViewWndClassName", string.Empty);
if (child != null && (int)child != 0)
{
child = FindWindowEx(child, IntPtr.Zero, "DirectUIHWND", string.Empty);
if (child != null && (int)child != 0)
{
child = FindWindowEx(child, IntPtr.Zero, "FloatNotifySink", string.Empty);
if (child != null && (int)child != 0)
{
child = FindWindowEx(child, IntPtr.Zero, "ComboBox", string.Empty);
if (child != null && (int)child != 0)
{
child = FindWindowEx(child, IntPtr.Zero, "Edit", string.Empty);
if (child != null && (int)child != 0)
{
//Should have edit box here
SetWindowText(child, FileName);
RequestStop();
}
}
}
}
}
.....
This works fine but reading about GetDlgItem I thought I could write it in less code and be more generic. I tried the code construct but the GetDlgItem never returns a non zero result.
Anyone help please?
.....
[DllImport("user32.dll", SetLastError = false)]
public static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
int CID_EDIT = 0x3E9;
uint WM_SETTEXT = 0x000C;
.....
WHandle = (IntPtr)FindWindow("#32770", "Save Webpage");
if (WHandle != null && (int)WHandle != 0)
{
found = true;
firstloop = false;
IntPtr iptrHWndControl = GetDlgItem(WHandle, CID_EDIT);
HandleRef hrefHWndTarget = new HandleRef(null, iptrHWndControl);
if (iptrHWndControl != null && (int)iptrHWndControl != 0)
{
SendMessage(hrefHWndTarget, WM_SETTEXT, IntPtr.Zero, FileName);
RequestStop();
}
View the full article
I want to control the SaveAsDialog with the WebBrowser component. As there is no direct access to the saveas capability I am trying to control the dialog box. On my Win7 test workstation I have worked out that I can use a thread to monitor me starting ShowSaveAsDialog() and then populate the file name textbox using the following code (ControlID found using Spy++ (64bit):
[DllImport("User32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string className, string lpszWindow);
private static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, string lParam);
WHandle = (IntPtr)FindWindow("#32770", "Save Webpage");
if (WHandle != null && (int)WHandle != 0)
{
SetForegroundWindow(WHandle);
child = FindWindowEx(WHandle, IntPtr.Zero, @"DUIViewWndClassName", string.Empty);
if (child != null && (int)child != 0)
{
child = FindWindowEx(child, IntPtr.Zero, "DirectUIHWND", string.Empty);
if (child != null && (int)child != 0)
{
child = FindWindowEx(child, IntPtr.Zero, "FloatNotifySink", string.Empty);
if (child != null && (int)child != 0)
{
child = FindWindowEx(child, IntPtr.Zero, "ComboBox", string.Empty);
if (child != null && (int)child != 0)
{
child = FindWindowEx(child, IntPtr.Zero, "Edit", string.Empty);
if (child != null && (int)child != 0)
{
//Should have edit box here
SetWindowText(child, FileName);
RequestStop();
}
}
}
}
}
.....
This works fine but reading about GetDlgItem I thought I could write it in less code and be more generic. I tried the code construct but the GetDlgItem never returns a non zero result.
Anyone help please?
.....
[DllImport("user32.dll", SetLastError = false)]
public static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
int CID_EDIT = 0x3E9;
uint WM_SETTEXT = 0x000C;
.....
WHandle = (IntPtr)FindWindow("#32770", "Save Webpage");
if (WHandle != null && (int)WHandle != 0)
{
found = true;
firstloop = false;
IntPtr iptrHWndControl = GetDlgItem(WHandle, CID_EDIT);
HandleRef hrefHWndTarget = new HandleRef(null, iptrHWndControl);
if (iptrHWndControl != null && (int)iptrHWndControl != 0)
{
SendMessage(hrefHWndTarget, WM_SETTEXT, IntPtr.Zero, FileName);
RequestStop();
}
View the full article