Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

.NET CF and Google APIs

0.00/5 (No votes)
24 May 2004 1  
Sample application to access Google APIs from a .NET Compact Framework application

Sample Image - dotNETCF_Google.png

Introduction

This article is primarily for trying to win a Pocket PC! Originally I submitted an article to MSDN Canada for the Between the Lines contest and won for the Month of April. That article was titled "Tips & Tricks & Best Practices with Multithreading and .NET CF" and focused on accessing the Google API using the Compact Framework and multithreading an application as to not lock up the user interface. I have made a few updates to the original application and this article will focus on three new items:

  1. Supporting Landscape and Portrait mode for Square(240x240) or Rectangular(240x320) for Windows Mobile 2003 SE
  2. Creating a "floating" window for the settings and about screen

Background

The source code provided is a continuation of my original code developed for the "Between the Lines" contest held by MSDN Canada and primarily dealt with multithreading a Compact Framework application. To read that article here. Also of note, the Google API for .NET CF does use OpenNETCF.org Smart Device Framework (SDF). These files are included in the Demo Project but are not included in the Source project. To download these files go to http://www.opennetcf.org/sdf. The main SDF libraries that are used are OpenNETCF.Windows.Forms.HTMLViewer to show the Google Search Results and OpenNETCF.Configuration.ConfigurationSettings. The OpenNETCF.org library is a great addition to anyone's development tools currently developing for the Compact Framework. If you don't currently use it I recommend you download the SDF at http://www.opennetcf.org/ and start using it. The SDF adds a lot of functionality that is not included in V1 of the Compact Framework and best of all the source is included!!

To run the code you will have to register at http://www.google.com/api and get a key. Once you get this key you must enter it in the settings within the application and then you will be able to search. The Google API is in beta and I don't know if they will charge a search fee in the future but I am sure they will let us know.

Portrait or Landscape

The first item I will go through is supporting landscape and portrait mode for Windows Mobile 2003 SE. To take advantage of the different screen layouts you must layout all your controls through code and handle the Resize event of the form. If this is not done scrollbars will be automatically added to your form if any controls go out of the screen bounds.

Portrait

Figure 1: Portrait Mode

Landscape

Figure 2: Landscape Mode

The following example shows how the sample application adjusts the controls depending on the screen size.

//
// Wiring up the event to handle resize

//

this.Resize+=new EventHandler(Form1_Resize);
 
//
// Event handler for Resize
// 
private void Form1_Resize(object sender, EventArgs e) 
{ 
   this.ResizeForm(); 
}
 
//
//Adjust the controls approprialty
// 
private void ResizeForm() 
{
   //Setup the buttons for the screen 
   this.btnSearch.Left = this.Width - this.btnSearch.Width - 1; 
 
   //Resize the buttons 
   this.btnNext.Left = this.Width-this.btnNext.Width - 1; 
   this.btnNext.Top = this.Height-this.btnNext.Height-1; 
   this.btnPrevious.Top = this.btnNext.Top;  
   this.btnPrevious.Left = 1;
   this.lblCount.Top = this.btnNext.Top;
   
   //resize the html screen 
   this.htmlGoogleResults.Left = 1; 
   this.htmlGoogleResults.Top = this.btnSearch.Top + this.btnSearch.Height + 1; 
   this.htmlGoogleResults.Height = this.Height- htmlGoogleResults.Top - 
                                  btnNext.Height-2; 
   this.htmlGoogleResults.Width = this.Width -2; 

 
   //Resize the search bar 
   this.textBox1.Width = this.Width-this.btnSearch.Width -3; 

 
   //Resize the result lable 
   this.lblCount.Left = btnPrevious.Left+btnPrevious.Width + 2; 
   this.lblCount.Width = this.Width - this.lblCount.Left - 
                         this.btnPrevious.Width -2; 
  
   //Center the logo 
   this.pictureBox1.Left = (this.Width/2)-(this.pictureBox1.Width/2); 
   this.pictureBox1.Top = (this.Height/2)-(this.pictureBox1.Height/2); 
  
   //Center the progress bar 
   this.progressBar1.Width = (this.Width*75)/100; 
   this.progressBar1.Left = (this.Width/2)-(this.progressBar1.Width/2); 
   this.progressBar1.Top = this.pictureBox1.Top+this.pictureBox1.Height+2; 
}

Visual Studio 2005 and Compact Framework V2 will have support for docking and anchoring of controls. Hopefully when this comes, you shouldn't have to do much manual work in adjusting your controls when the user decides to switch screen orientations. To find out what's in store for Windows Mobile 2003 SE check out http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwm2k3/html/whatsnew2003se.asp

"Floating" Window

There is a lot of info available on creating a "floating" window so I won't go into too much detail. Basically what you have to do is set the following properties of the Form to false:

  • ControlBox
  • MinimizeBox
  • MaximizeBox

Landscape

One other thing you may want to do is center your form on the screen. The following code will center the screen on the device:

//
// Center the screen
//
Rectangle _screen = Screen.PrimaryScreen.WorkingArea; 
this.Location =new Point(((this._screen.Width - this.Width) / 2),
                          ((this._screen.Height - this.Height) / 2));
         
        

Conclusion

Like I stated above, my primary reason for writing this is to try and win a Pocket PC but I also felt it's a pretty cool application. Is there much use for it, I don't know you tell me. I started it because the Google Search APIs are available and thought it would be cool to access them from a PocketPC. What the application does do is go through a few techniques for developing for the Compact Framework and hopefully it will be useful for some to see it all in one application. Don't forget to check out the first article. Also, the Google API exposes "Cache Requests" and "Spelling Requests". I haven't included these in this application and don't intend to (unless I am really bored!) but if someone does include it in this app drop me a line. I think the spelling request would be cool to use in some application. If you have any questions or comments you can contact me at info@markarteaga.com or add comments below.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here