How focus to IE window by API

Hello every one
I create a add-on for IE7.  The focus of my add-on is auto print html page without print dialog
I have 1 problem please help me. This is my code:

Win32Lib.cs
class Win32Lib
    {
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern Int32 FindWindow(string lpClassName, string lpWindowName);
     
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern Int32 PostMessageA(Int32 hwnd, Int32 wMsg, Int32 wParam, Int32 lParam);

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern Int32 BringWindowToTop(Int32 hwnd);

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern Int32 SetForegroundWindow(Int32 hwnd);
       
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern Int32 SetActiveWindow(Int32 hwnd);
    }

file PrintTool.cs
public class Download : IDownload
    {
        /// <summary>
        /// COM exposed Method.
        /// </summary>
        ///
        public const Int32 BM_CLICK = 0xF5;
        public const Int32 VK_MENU = 0x12;
        public const Int32 VK_F = 0x46;
        public const Int32 VK_P = 0x50;
        public void InvokeMethod(string inputTest)
        {
            Int32 hMain, sButton;
            hMain = Win32Lib.FindWindow("IEFrame", "TestPrint - Windows Internet Explorer");            
            if (hMain != 0)
            {                
                //Win32Lip.BringWindowToTop(hMain);
                //hMain = Win32Lib.FindWindowEx(hMain, 0, "TabWindowClass", null);
               // hMain = Win32Lib.FindWindowEx(hMain, 0, "Shell DocObject View", null);
                //hMain = Win32Lib.FindWindowEx(hMain, 0, "Internet Explorer_Server", null);
                Win32Lib.SetForegroundWindow(hMain);
//                Win32Lib.SetActiveWindow(hMain);
               
                //SendKeys.SendWait("^(p)");
                System.Threading.Thread.Sleep(2000);
                SendKeys.SendWait("^{p}");
                hMain = Win32Lib.FindWindow("#32770", "Print");
                sButton = Win32Lib.FindWindowEx(hMain, 0, "Button", "&Print");
               Win32Lib.PostMessageA(sButton, BM_CLICK, 0, 0);
            }
            }
    }

My add-on can run correct when i call another page, example click a button on Page A print Page B
but it can't run correct when print itself , example click a button on Page A and print Page A

Please help me, thank you
globallaboAsked:
Who is Participating?
 
globallaboConnect With a Mentor Author Commented:
Thank you  for all expert who care my problem. Now, i found another solution to send message to IE
I change from SendWait api to keybd_event api. It ok for me now. thank again

Win32Lib.keybd_event(VK_LCONTROL, 0, 0, 0);
Win32Lib.keybd_event(VK_P, 0, 0, 0);
0
 
cookreCommented:
How does it fail?
0
 
globallaboAuthor Commented:
Thank you for your comment.
The function not auto print for me ( print without dialog). I call this function via javascript
But there is a strange. When i call this function, then i click to URL bar. The function run correct :(. I think there is some thing wrong with SetForegroundWindow API window i use
0
 
globallaboAuthor Commented:
No expert can help me :(( . Please.
0
 
SLCCorpCommented:
I am interested if you found the solution or not.
Also I am trying to find url of running IE 7 using similar methods, but failed to retrieve it. I am wondering if you have any suggestion.

Thanks!
0
All Courses

From novice to tech pro — start learning today.