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:
- Supporting Landscape and Portrait mode for Square(240x240) or Rectangular(240x320) for Windows Mobile 2003 SE
- Creating a "floating" window for the settings and about screen
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.
Figure 1: Portrait Mode
Figure 2: Landscape Mode
The following example shows how the sample application adjusts the controls depending on the screen size.
private void Form1_Resize(object sender, EventArgs e)
private void ResizeForm()
this.btnSearch.Left = this.Width - this.btnSearch.Width - 1;
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;
this.htmlGoogleResults.Left = 1;
this.htmlGoogleResults.Top = this.btnSearch.Top + this.btnSearch.Height + 1;
this.htmlGoogleResults.Height = this.Height- htmlGoogleResults.Top -
this.htmlGoogleResults.Width = this.Width -2;
this.textBox1.Width = this.Width-this.btnSearch.Width -3;
this.lblCount.Left = btnPrevious.Left+btnPrevious.Width + 2;
this.lblCount.Width = this.Width - this.lblCount.Left -
this.pictureBox1.Left = (this.Width/2)-(this.pictureBox1.Width/2);
this.pictureBox1.Top = (this.Height/2)-(this.pictureBox1.Height/2);
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
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:
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:
Rectangle _screen = Screen.PrimaryScreen.WorkingArea;
this.Location =new Point(((this._screen.Width - this.Width) / 2),
((this._screen.Height - this.Height) / 2));
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 email@example.com or add comments below.