Simple WinForms Hello World Example

7:39 PM
Simple WinForms Hello World Example -

HelloWorld

[Also posted on CitrixBlogger. The code is easier to read there.]
There is no longer a team member thought it would be a good idea to show the simplest possible using the Windows Mobile SDK program for applications. It was a great idea as "Hello World" is commonly used to introduce developers of new concepts. The story of "Hello World" comes from the early 1970s with the C language and prevailed even though things have changed so much since.

The first step is to provide a very basic version of "Hello World" using WinForms. Get the code here. It is a very small download about 25K. The download is a full Visual Studio 2010 project and even includes the bin directory with the binary HelloWorld.exe. The bit is not signed, so if you do not trust, you can build your own copy

The code behind this application is short, I will include it in full here

 using System ..; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace HelloWorld {///  /// The HelloWorld form shows how to display "Hello World" with the appropriate font based on the size of the client area. /// Later, it will be transformed to support the SDK for Windows Mobile Apps. ///  public partial class HelloWorld: Form {///  /// /// leading manufacturer HelloWorld form  public HelloWorld () {// initialize the standard designer components InitializeComponent (); // Ensure that this window is maximized WindowState = FormWindowState.Maximized; } ///  /// Called when the form is first loaded. HelloWorldLabel resize to fit the new size of the form ///  ///  Ignored ///  Ignored HelloWorld_Load private void (object sender, EventArgs e) {ResizeHelloWorld (); } ///  /// Called when the form is resized. HelloWorldLabel resize to fit the new size of the form ///  ///  ///  empty HelloWorld_Resize (object sender, EventArgs e) {private ResizeHelloWorld (); } ///  /// Resize Hello world to fill the client area ///  ResizeHelloWorld void () {// change the font to match the size of the client area HelloWorldLabel.Font = GetBestFontFit (ClientSize, HelloWorldLabel.Font, HelloWorldLabel.Text, ClientSize.Height, 8f); ; // Center CenterHelloWorld label (); } ///  /// HelloWorldLabel the Centre in the client area of ​​the HelloWorld form ///  CenterHelloWorld empty () {HelloWorldLabel.Location = new Point ((ClientSize.Width / 2) - (HelloWorldLabel.Width / 2), (ClientSize.Height / 2) - (HelloWorldLabel.Height / 2)); } ///  /// Police Determine the best match for the size / text. The result should be between the maximum and minimum height. /// The size is the fill area. The new policy is based on the old police happened in ///  /// public static GetBestFontFit Police Police (waist size, currFont font, text String, float fontMax, float fontMin) {float fontPixels = fontMax. float = minfontPixels fontMin; // Find the size of the corresponding font to font textbox Font Size = currFont; // Make sure the text is not null or empty before attempting to fit the area. // If it is null or empty, the current font is used because it does not matter. {size if textsize (String.IsNullOrEmpty (text)!); font = new Font (currFont.FontFamily, fontPixels, currFont.Style, GraphicsUnit.Pixel); textsize = TextRenderer.MeasureText (text, font); // Check if the police holds in the area and if not, find a smaller while ((((textsize.Height)> size.Height) || ((textsize.Width)> size.Width)) && (fontPixels > minfontPixels)) {// try to speed up the search process of the size of the font to the right by detecting how it is too big if (textsize.Height> size.Height) {// reduce fontPixels based on how far the font size is fontPixels - = (textsize.Height - size.Height); } Else if (textsize.Width> size.Width) {int = charPixelWidth textsize.Width / text.length; int = targetPixelWidth size.Width / text.length; // If the characters are too wide, cut by fontPixels differently, the result is the desired width if (charPixelWidth> targetPixelWidth) {fontPixels - = (charPixelWidth - targetPixelWidth); } Else {fontPixels - = 1.0F; }} Else {fontPixels - = 1.0F; } // Remove the old because it was not working font.Dispose (); // Get a new policy based on the smallest font size pixel font = new Font (font.FontFamily, fontPixels, font.Style, GraphicsUnit.Pixel); // Recalculate the size according to the new font textsize = TextRenderer.MeasureText (text, font); }} // When all done, return the font to use return (police); }}} 

The most complicated part is finding the right font to use to fill the client area. This code has been changed from the code that is used for previous posts UserInfo. The idea is that loops over attempts to find the correct font from the largest size to the minimum size.

The end result is a program that still displays "Hello world" in its largest possible font to insert the customer area. If you change the window size, the text will adjust to fill it again. This is a good introduction to the concept of having re-sized to fit text to a specified rectangle. The effect is very interesting if you constantly resize and see how fast the text adjusts. It almost seems like a movie.

The next step is to present the SDK for Windows Mobile Apps. This is mostly for fun to match the screen size and orientation.

Previous
Next Post »
0 Komentar