DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

C++ and Metro: Basic Application

Toni Petrina user avatar by
Toni Petrina
·
Jun. 10, 12 · · Interview
Like (0)
Save
Tweet
5.35K Views

Join the DZone community and get the full member experience.

Join For Free
Visual Studio 11 Consumer Preview enables creating Metro styled applications using C++ which is great news for any native developer. Let’s create a simple Metro application. Start up Visual Studio and create new project using the Visual C++>Windows Metro Style>Blank Application template.


“Hello world”

Open the BlankPage.xaml file and start poking around with XAML. For those who haven’t used it yet, it is similar to HTML development. Scroll the code down until you find the Grid element and insert a TextBlock inside it, the final result should be:

<Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
    <TextBlock Text="Hello world" Margin="12,20" Style="{StaticResource SubheaderTextStyle}"/>
</Grid>

The Margin property offsets the element from the default top left corner for 12 pixels from the left and 20 pixels from the top. I have used the style that comes native with all Metro applications and it is primarily used to increase the size of the text. If you want, you can set size via the FontSize attribute.

If you run the application now, you should get a black screen with the familiar “Hello world” text in the upper left corner. You define how the UI will look like in the BlankPage.xaml file, but the logic is placed in the regular BlankPage.xaml.h and BlankPage.xaml.cpp which are placed as child items for the BlankPage.xaml file in Solution Explorer.

This is a regular C++ class but something looks different. The code does not look native at all with all those strange hats around the code and ref new syntax. Although these are reminiscent of the C++/CLI syntax, these extensions are called C++/CX, which is short for Component extensions. Pure C++ is not used for developing Metro applications and using the pure WinRT would be cumbersome since everything in Metro world is actually a COM object. C++/CX extensions are supposed to cut the burden for C++ programmer with this nonstandard extension. It litters the code with AddRef and Release calls and essentially all hat pointers are nothing more than shared_ptr variant.

C++ to XAML

You can “name” the previously added TextBlock XAML element by adding the x:Name="txtHello" attribute and value. By doing this, you can refer to it from the code behind files using the following code (add it to the BlankPage::OnNavigatedTo method):

txtHello->Text = "Hello world!!!";

Run application and you should see different text like on the image below.



Although not very exciting and super-simple, think about how it is done in Win32, MFC or WTL, this is clearly easier. In the next post we will look at how to handle simple events.




application

Published at DZone with permission of Toni Petrina, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Synchronization Methods for Many-To-Many Associations
  • OpenTelemetry in Action: Identifying Database Dependencies
  • Memory Debugging and Watch Annotations
  • Modern REST API Design Principles and Rules

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo