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

前提是你 提交于 2019-11-29 07:42:07

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

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!