Link to home
Start Free TrialLog in
Avatar of globallabo
globallabo

asked on

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
Avatar of cookre
cookre
Flag of United States of America image

How does it fail?
Avatar of globallabo
globallabo

ASKER

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
No expert can help me :(( . Please.
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!
ASKER CERTIFIED SOLUTION
Avatar of globallabo
globallabo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial