how to control web components in winform by embeding a browser(ie)
-
:confused:in my windows application,i embeded a web browser,by this method we can show web component.But i want control this component such as draw to a new position.How can i do this.Help :)
-
:confused:in my windows application,i embeded a web browser,by this method we can show web component.But i want control this component such as draw to a new position.How can i do this.Help :)
-
:confused:in my windows application,i embeded a web browser,by this method we can show web component.But i want control this component such as draw to a new position.How can i do this.Help :)
If you're hosting the
IWebBrowser2
interface, you can use the MSHTML hosting interfaces likeIDocHostUIHandler
(see the PSDK for more details). This and several other hosting interfaces give you control over context menus, object placement and drawing, and much more functionality. To implementIDocHostUIHandler
(which pretty much sets everything else up), you must either redefine the interface in .NET (making sure to use the right GUID...or another technique which I'll link to later) and implement that on your class. You can QI (or cast in .NET) the WebBrowser control (or anIHTMLDocument*
interface) toICustomDoc
and callSetUIHandler
, passing a reference to your implementation ofIDocHostUIHandler
(but that only lets you use UI customization methods), or implementIOleClientSite
. The WebBrowser control will QI for this interface on the host (you) and make calls to theIDocHostUIHandler
for all methods. You can read more about hosting the WebBrowser control and MSHTML by looking at http://msdn.microsoft.com/workshop/browser/prog_browser_node_entry.asp[^].-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
:confused:in my windows application,i embeded a web browser,by this method we can show web component.But i want control this component such as draw to a new position.How can i do this.Help :)
Oh, about those other links: rather than manually defining all the interfaces, you can use a technique that described in this article: http://www.codeproject.com/csharp/advhost.asp[^] You create an IDL file (used in COM programming) to forward-define interfaces and compile that to a typelib (TLB). Then you use the tlbimp.exe utility in the .NET SDK to create an interop assembly out of that. Now you're got your interfaces defined correctly and just have to implement them! A word of caution, though: all the methods return
void
. Some methods require that you returnS_FALSE
in COM, which is still a success code. Since the return type isvoid
, you can't return anything and you can't use aCOMException
because that creates an error state. In these cases, you will have to define the interface yourself and for those methods make them return anint
and add thePreserveSignature=true
property statement to theMarshalAsAttribute
that you must add to the method. Also, another helpful article here on CP is a book excerpt: http://www.codeproject.com/books/0764549146_8.asp[^] These are both pretty good, but I still recommend you read that link I gave you to MSDN. It's the best way to learn about hosting the WebBrowser control or MSHTML.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Oh, about those other links: rather than manually defining all the interfaces, you can use a technique that described in this article: http://www.codeproject.com/csharp/advhost.asp[^] You create an IDL file (used in COM programming) to forward-define interfaces and compile that to a typelib (TLB). Then you use the tlbimp.exe utility in the .NET SDK to create an interop assembly out of that. Now you're got your interfaces defined correctly and just have to implement them! A word of caution, though: all the methods return
void
. Some methods require that you returnS_FALSE
in COM, which is still a success code. Since the return type isvoid
, you can't return anything and you can't use aCOMException
because that creates an error state. In these cases, you will have to define the interface yourself and for those methods make them return anint
and add thePreserveSignature=true
property statement to theMarshalAsAttribute
that you must add to the method. Also, another helpful article here on CP is a book excerpt: http://www.codeproject.com/books/0764549146_8.asp[^] These are both pretty good, but I still recommend you read that link I gave you to MSDN. It's the best way to learn about hosting the WebBrowser control or MSHTML.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Heath Stewart wrote: A word of caution, though: all the methods return void. Some methods require that you return S_FALSE in COM, which is still a success code. Since the return type is void, you can't return anything and you can't use a COMException because that creates an error state. Actually, I've successfully done this in the IDocHostUIHandler.TranslateAccelerator method. The way I know I was successful is that it no longer "ate" all keyboad messages that were sent to the WebBrowser control. But yes, it's better to re-define the interface.
throw new System.Runtime.InteropServices.COMException("S_CANCEL - No custom handling",1);
**"Have a heart that never hardens, a temper that never tires, a touch that never hurts." -- Charles Dickens
-
If you're hosting the
IWebBrowser2
interface, you can use the MSHTML hosting interfaces likeIDocHostUIHandler
(see the PSDK for more details). This and several other hosting interfaces give you control over context menus, object placement and drawing, and much more functionality. To implementIDocHostUIHandler
(which pretty much sets everything else up), you must either redefine the interface in .NET (making sure to use the right GUID...or another technique which I'll link to later) and implement that on your class. You can QI (or cast in .NET) the WebBrowser control (or anIHTMLDocument*
interface) toICustomDoc
and callSetUIHandler
, passing a reference to your implementation ofIDocHostUIHandler
(but that only lets you use UI customization methods), or implementIOleClientSite
. The WebBrowser control will QI for this interface on the host (you) and make calls to theIDocHostUIHandler
for all methods. You can read more about hosting the WebBrowser control and MSHTML by looking at http://msdn.microsoft.com/workshop/browser/prog_browser_node_entry.asp[^].-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Heath Stewart wrote: You can QI (or cast in .NET) the WebBrowser control (or an IHTMLDocument* interface) to ICustomDoc and call SetUIHandler, passing a reference to your implementation of IDocHostUIHandler (but that only lets you use UI customization methods) Just to give a quick example:
ICustomDoc cDoc = (ICustomDoc)webBrowser.Document;
cDoc.SetUIHandler((IDocHostUIHandler)this);There's something I'd like to warn people about with the WebBrowser control. I replaced the default context menu for the WebBrowser, but whenever my context menu closed (after an item was clicked), the next item on the default context menu was executed, even though it never appeared. I was just about at my wit's end when I realized that the context menu must be sending something back to the window it was shown on behalf of - the WebBrowser control - and it was executing the default menu's commands. Changing the window passed into the context menu to the form instead of the WebBrowser solved the problem.
**"It is appallingly obvious that our technology exceeds our humanity." -- Albert Einstein