Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

A Preview of Visual C++ .NET 2003 : Page 6

Visual C++ .NET is not only about .NET. It targets both traditional Windows-based and new .NET-connected applications and components.


advertisement
Using Resource Files
There are no .rc files for Windows Forms. This correctly implies that resources for Windows Forms are not data-based, but rather, are largely code-based. The InitializeComponent() method contains all of the Designer-generated code that instantiates, initializes, and positions all of the controls on the form—manually.

In more advanced scenarios, such as those involving localization or data-centric objects on the form itself (like a bitmap), a resource model is used. .ResX files are XML files that exist for each form type and are compiled into .resource files that are incorporated into the assembly (.dll or .exe) by the compiler. The InitializeComponent() method will instantiate a ResourceManager object to retrieve the values stored in the XML, and is used to initialize the form's properties and sub-objects (rather than hard-coding these values in code).

It is a simple matter to witness this. The default code spit out by the designer for the empty form looks something like this:

void InitializeComponent(void) { // // Form1 // this->AutoScaleBaseSize = System::Drawing::Size(5, 13); this->ClientSize = System::Drawing::Size(292, 266); this->Name = S"Form1"; this->Text = S"Form1"; }

If, in the Property Window for the form, you set the Localizable property to 'True' (default is 'False'), the Designer emits quite a different picture:

void InitializeComponent(void) { System::Resources::ResourceManager * resources = new System::Resources::ResourceManager(__typeof(CoDe::Form1)); // // Form1 // ... (lots of generated code, ommitted here) ... this->ClientSize = (*__try_cast<__box System::Drawing::Size * > (resources->GetObject(S"$this.ClientSize"))); this->Text = resources->GetString(S"$this.Text"); }

You can see that the code is using the ResourceManager to retrieve values defined elsewhere (in the XML resource). In this case, the rationale is to use values that are Localized. The single .ResX file could define values in multiple languages, for example. The ResourceManager object accesses by default the resource values that correspond to the Locale set for the executing thread.

It is generally good practice to NOT edit the code inside IntitializeComponent(). Basic changes you might make to property values WILL be reflected back inside the designer (the designer parses the code it generates for changes), but additional code you place there will in most cases be rejected with an error.

Since this form example doesn't yet do anything, let's add some controls.

Switch back to the Windows Forms Designer (click the View Designer button in the Solution Explorer window), then drag and drop a button from the Toolbox window onto the form. Understand that the designer just generated code "behind the form" in the .h file to implement that button. As you adjust properties on the button it will update the code.

The Properties window serves a number of purposes, and largely supplants the MFC CodeWizard from the 6.0 product. (This is true even for unmanaged C++ code you might write with Visual C++ .NET, although I'm not covering that here.) The primary purpose is to enable the developer to review and set property values on the object, but it also provides an interface to review and implement event handlers for objects.

Set the button's Text property to something like "Click Me". Then click the Events button in the Properties window (it looks like a lightning bolt; I suppose because most would consider a lightning-strike an 'event'). The first event listed is the Click event, and as of yet there is no implemented handler. Double-click the word Click in the list and the designer will do two things: First, implement the event handler. Second, take you to the event handler method so you can write the code that's executed when the event fires.

(You can also simply double-click the button itself on the form. This will also prompt the designer to create the event hander and take you to the code.)

The event handler code is simply a method called when the event occurs. The magic that binds the method to the event source (the button) is called a delegate. Think of delegates as object-oriented and type-safe function pointers. A delegate is a kind of user-definable type that you instantiate and pass to the source of the event. When the source decides to fire, it iterates through the collection of delegates it has amassed and calls the registered event handlers through function pointers. The code generated by the Designer to hook up a delegate event handler looks like this:


this->button1->Click += new System::EventHandler(this, button1_Click);

In this example, Click is the event on the Button object. In C++, the += operator is overloaded to append the delegate to the event's delegate list. You're responsible (or rather, the designer is responsible, in this case) for allocating a new delegate (Called EventHandler) and initializing it with a reference to your handler function on a given object (this).

All Said and Done
The C++ landscape is changing. Compilers are not only becoming more ISO C++ conforming, they're also offering dramatic new optimizations and code-robustness features. Visual C++ is now venturing deep into the realm of managed-code generation—whilst simultaneously keeping a foot firmly planted in the unmanaged world of Windows. Everett is the culmination of several years of effort by the Visual C++ team at Microsoft to facelift the product and deliver exciting new features and capabilities that will have definite impact on your coding. With Everett, you'll be secure in knowing your language and compiler can meet the demands of any software development project.



Nick Hodapp is the Product Manager for Visual C++ in the Developer and Platform Evangelism Division at Microsoft. As product manager, Nick is responsible for evangelizing the benefits of using Visual C++ .NET to develop best-in-class Windows-based and .NET-connected applications and components. Prior to joining Microsoft, Nick worked as a C++ developer specializing in GIS software for telecommunication and power utilities. Reach him via email at nickhod@microsoft.com.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date