The Power of Reflection


This morning, I received my standard 8 A.M. phone call from Chet, who apparently thinks that if he s up, everyone should be up. Fortunately, I was. I told him about the Internet Explorer solution and was kind of whining because it seems like a cheat. In the course of the conversation I told him about all the options I had learned about, including hooking up to the COM object. That got me thinking. I went back to microsoft.public.whatever and found that someone had said that if I would hook up to the COM object, I could drag it into a .NET Windows Form. I decided it would do no harm to try it and decided to go ahead.

I made a new solution and project. Then I clicked the Visual Studio Toolbox and chose Customize. This brings up, in practically no time, a huge list of COM objects that you could add to the box. I searched for a while and found Microsoft Web Browser, so I added it. Visual Studio asks no more questions; it just plunks Explorer into the Toolbox.

Then I created a Windows Forms project and dragged the Explorer into it. Sure enough, it displayed a little rectangle representing where the pane would be. I looked around a bit and then double-clicked it. Visual Studio put a new method in my source code called Form1_Load. I guess that s the main event for the pane. (I have yet to find any really useful documentation on it.) I surfed MSDN and found some example C++ code that looks like this:

 CRect rect; 
GetClientRect (&rect);
// Create the control.
m_pBrowser = new CWebBrowser;
ASSERT (m_pBrowser);
if (!m_pBrowser->Create(NULL,NULL,WS_VISIBLE,rect,this,NULL))
{
TRACE("failed to create browser\n");
delete m_pBrowser;
m_pBrowser = NULL;
return 0;
}
// Initialize the first URL.
COleVariant noArg;
m_pBrowser->Navigate("www.microsoft.com",&noArg,&noArg,&noArg,&noArg);
return 0;

That was pretty cryptic, being in C++ and all, but the Navigate method looked pretty interesting. So I decided to try it. Into the Form1_Load, I type

 private void Form1_Load(object sender, System.EventArgs e) { 
axWebBrowser1.Navigate("http://www.xprogramming.com");
}

That won t compile. Navigate() wants five arguments. The IntelliSense says: string URL, ref object flags, ref object targetFrameName, ref object postData, ref object headers . I have no idea what any of those are. I type

 private void Form1_Load(object sender, System.EventArgs e) { 
axWebBrowser1.Navigate("http://www.xprogramming.com" , 0, 0, 0, 0 );
}

The compiler doesn t like that; it can t convert from int to ref object. Now I see in the code above that they are declaring a COleVariant noArg. There is no COleVariant listed in the Visual Studio Help. I try

 private void Form1_Load(object sender, System.EventArgs e) { 
object noArgs;
axWebBrowser1.Navigate("http://www.xprogramming.com", & noArgs, & noArgs,
& noArgs, & noArgs );
}

I was thinking that the noArgs would initialize to null and that & might be the way to do a reference in a call. (Remember, I really am new to C#!) Wrong on both counts. So I try

 private void Form1_Load(object sender, System.EventArgs e) { 
object noArgs = null;
axWebBrowser1.Navigate("http://www.xprogramming.com", ref noArgs, ref noArgs,
ref noArgs, ref noArgs );
}

This compiles; I run it. It takes a long time to start up, and then a window comes up with my little Internet Explorer pane in it. Nothing is inside the pane; I m about to give up. Then an hourglass appears. Then it goes away. Then the little pane has my Web site s home page in it!




Extreme Programming Adventures in C#
Javaв„ў EE 5 Tutorial, The (3rd Edition)
ISBN: 735619492
EAN: 2147483647
Year: 2006
Pages: 291

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net