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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector
  • A Spring Boot App With Half the Startup Time
  • Key Takeaways From Integrating a RAG Application With LangSmith
  • Improving Java Application Reliability with Dynatrace AI Engine

Trending

  • Agentic AI in 2026: How Autonomous AI Agents Are Replacing Manual Dev Work
  • A Practical Pipeline for Identifying Sensitive Columns Before Test Data Masking
  • Containerizing and Testing a Python Backtesting System With Docker and GitHub Actions
  • Five Layers Between Your AI Agent and a Production Outage

C++ and Metro: Basic Application

By 
Toni Petrina user avatar
Toni Petrina
·
Jun. 10, 12 · Interview
Likes (0)
Comment
Save
Tweet
Share
6.7K 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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector
  • A Spring Boot App With Half the Startup Time
  • Key Takeaways From Integrating a RAG Application With LangSmith
  • Improving Java Application Reliability with Dynatrace AI Engine

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook