C# - How to PostMessage to a flash window embedded in a WebBrowser?

后端 未结 4 945
误落风尘
误落风尘 2020-12-18 09:20

I would like to know if there was any way to lock onto a Flash window and post a message to it? Another person here had the answer to it, his name is Spencer K. His question

相关标签:
4条回答
  • 2020-12-18 09:22

    The Flash window will probably not have its own handle, since it is embedded in a webpage. You would want to post the message to the web browser window (that's what Mr. K did).

    If that doesn't work for you, the only other option that I'm aware of is to gain control of the browser via WebDriver or WatiN and interact with the flash object using javascript.

    0 讨论(0)
  • 2020-12-18 09:36

    Actually flash window has its own handle too. To get it you have to get the class names of the controls it is embedded in from Spy++, then you can reach it like this:

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
        public IntPtr Flash()
        {
            IntPtr pControl;
            pControl = FindWindowEx(webBrowser1.Handle, IntPtr.Zero, "Shell Embedding", IntPtr.Zero);
            pControl = FindWindowEx(pControl, IntPtr.Zero, "Shell DocObject View", IntPtr.Zero);
            pControl = FindWindowEx(pControl, IntPtr.Zero, "Internet Explorer_Server", IntPtr.Zero);
            pControl = FindWindowEx(pControl, IntPtr.Zero, "MacromediaFlashPlayerActiveX", IntPtr.Zero);
            return pControl;
        }
    

    When you get the handle, you can post the clicks:

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
        public enum WMessages : int
        {
            WM_LBUTTONDOWN = 0x201,
            WM_LBUTTONUP = 0x202
        }
        private int MAKELPARAM(int p, int p_2)
        {
            return ((p_2 << 16) | (p & 0xFFFF));
        }
        public void DoMouseLeftClick(IntPtr handle, Point x)
        {
            PostMessage(handle, (uint)WMessages.WM_LBUTTONDOWN, 0, MAKELPARAM(x.X, x.Y));
            PostMessage(handle, (uint)WMessages.WM_LBUTTONUP, 0, MAKELPARAM(x.X, x.Y));            
        }
    

    The points will be relative to the client, so when you save them, you should save it like this:

        List<Point> plist = new List<Point>();
        private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.C:                   
                    plist.Add(webBrowser1.PointToClient(Cursor.Position));                    
                    break;
                default:
                    break;
            }
        }
    

    Hope this was helpful

    0 讨论(0)
  • 2020-12-18 09:37

    You can do it via javascript.

    Import this:

    import flash.external.ExternalInterface;
    

    Ad this to your AS code:

    if (ExternalInterface.available) {
       // add external interface
       ExternalInterface.addCallback("jsFunction", asFunction);
    }
    
    public static function asFunction(message:String):void {
    }
    

    On your JS object of the flash object you can call this function:

    jsObject.jsFunction("message");
    

    This is the function to get the js object of the flash object:

    var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
    jsObject = InternetExplorer ? window.jsObjectName: window.document.jsObjectName;
    

    I did not test this code, I just copied it out of a project.

    edit: added js function to get js object

    0 讨论(0)
  • 2020-12-18 09:38

    for calling a function in flash object u can use this code

     swfobject.CallFunction(
                   "<invoke name=\"yourfunction\" returntype=\"xml\">" +
                   " <arguments><number>" yourvalue "</number></arguments> </invoke> ");
    

    for more information follow this link:communicate-betwen-c-and-an-embeded-flash-application

    i try it for a flash object in my form application and it work,but i did not use it for webbrowser

    0 讨论(0)
提交回复
热议问题