Showing posts with label Code.Visual Cpp. Show all posts
Showing posts with label Code.Visual Cpp. Show all posts

Saturday, April 28, 2012

Hello Metro application using C++, with Button click event handler.

Continuous work from the last post "Create Metro style Hello World in C++ using Microsoft Visual Studio 11 Express for Windows 8 Beta".

- Add a button
With BlankPage.xaml opened. Drag a Button from ToolBox on the Design Panel, place anywhere you want.
With the Button selected, edit in Properties panel to change its Name to "myButton", and change Content under Common to "Click Me".
It can be noticed that <Button> in BlankPage.xaml will be updated accordingly.

- Name the TextBlock
With the TextBlock selected, edit in Properties panel to change its Name to "myText".

- Insert Button click event handler
Double click on the Button in Design panel. It will insert Button click event handler, helloMetroC::BlankPage::myButton_Click(), in BlankPage.xaml.cpp automatically.

And Click="myButton_Click" will be added in <Button> in BlankPage.xaml.
    <Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
        <TextBlock x:Name="myText" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="50" Text="Hello Metro" />
        <Button x:Name="myButton" Content="Click Me" HorizontalAlignment="Left" Margin="552,519,0,0" VerticalAlignment="Top" Width="263" Click="myButton_Click"/>

    </Grid>

- Add code in Button click event handler 
Switch to BlankPage.xaml.cpp, and insert the code in the function helloMetroC::BlankPage::myButton_Click().
void helloMetroC::BlankPage::myButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
 myText->Text = "Thanks";
}


- Build and Run
When the application running, click on the Button will make the TextBlock change. That's.




Friday, April 27, 2012

Create Metro style Hello World in C++ using Microsoft Visual Studio 11 Express for Windows 8 Beta

- New a Project in Microsoft Visual Studio 11 Express for Windows 8 Beta, using template of Visual C++, Windows Metro style, Blank Application, name it helloMetroC.

- The wizard will generate a dummy application for you.

- Double click on the BlankPage.xaml from the Solution Explorer, to start edit it.

- Click TOOLBOX on the left side border, to expend the Toolbox panel. Click to expend Common XAML Controls, drag a TextBlock on the Design Pane.

- Now, you can see a TextBlock in the design view, and a new element of <TextBlock> in XAML view.


- We can edit the XAML directly.
Modify the content of <TextBlock> element like it:
    <Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="50" Text="Hello Metro" />

    </Grid>


- Select Save All from File menu, to save the solution.

- To build the solution, Build Solution from Build menu (or press F7).

- Run it by clicking on the Play button, or Start Debugging from Debug menu, or press F5 directly.


Next:
- Hello Metro application using C++, with Button click event handler.

Thursday, January 26, 2012

Basic usage of enum class in CLR C++

// testENUM_CLR.cpp : main project file.

#include "stdafx.h"

using namespace System;

enum class Day {SUN, MON, TUE, WED, THU, FRI, SAT};

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"http://dev-microsoft.blogspot.com/");

 Console::WriteLine("");
 Console::WriteLine(L"Day {0} have value of {1}.", Day::SUN, safe_cast<int>(Day::SUN));
 Console::WriteLine(L"Day {0} have value of {1}.", Day::MON, safe_cast<int>(Day::MON));
 Console::WriteLine(L"Day {0} have value of {1}.", Day::TUE, safe_cast<int>(Day::TUE));
 Console::WriteLine(L"Day {0} have value of {1}.", Day::WED, safe_cast<int>(Day::WED));
 Console::WriteLine(L"Day {0} have value of {1}.", Day::THU, safe_cast<int>(Day::THU));
 Console::WriteLine(L"Day {0} have value of {1}.", Day::FRI, safe_cast<int>(Day::FRI));
 Console::WriteLine(L"Day {0} have value of {1}.", Day::SAT, safe_cast<int>(Day::SAT));

 Console::ReadLine();
    return 0;
}

Basic usage of enum class in CLR C++

Compare to Basic usage of enum in C++

Wednesday, January 25, 2012

Console::ReadKey() - obtain a single from Console

  • ReadKey(): Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.
  • ReadKey(true): Obtains the next character or function key pressed by the user, NOT display the pressed key in the console window.
  • ReadKey(false): Obtains the next character or function key pressed by the user, display the pressed key in the console window.


Example:
Obtain a single from Console using Console::ReadKey()

// CLRConsole.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
 ConsoleKeyInfo keyPressed;

 do{
  Console::WriteLine(L"press and key, or Q to quit");
  keyPressed = Console::ReadKey();
  Console::WriteLine();
  Console::WriteLine(keyPressed.KeyChar);
 }while(keyPressed.KeyChar != 'Q' && keyPressed.KeyChar != 'q');

 return 0;
}

Monday, January 23, 2012

Get user input from Console, ReadLine()

The function Console::ReadLine() reads a complete line from user inpt from console, terminated when ENTER key pressed.

Example:
Example of using Console::ReadLine()
// CLRConsole.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{

 Console::WriteLine(L"type in something, then press ENTER:");
 String^ userInput = Console::ReadLine();

 Console::WriteLine("Thanks! you enter: " + userInput);

 Console::ReadLine();
 return 0;
}


Please note that the data returned from Console::ReadLine() is in type of String^. It's a handle that reference a String object. To know more about "^", please refer the post ^ (Handle to Object on Managed Heap).

Sunday, January 22, 2012

More output format for Console::Write()/WriteLine()

Example:
// CLRConsole.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{

 Console::WriteLine(L"More output format for Console::Write()/WriteLine()");
 Console::WriteLine(L"C/c: in Currency Format: {0:C4}", 123.456789);
 Console::WriteLine(L"D/d: output integer as decimal: {0:D4}", 123);
 Console::WriteLine(L"E/e: output floating point in scientific format: {0:E4}", 123.456789);
 Console::WriteLine(L"F/f: output floating point in fixed-point format: {0:F4}", 123.456789);
 Console::WriteLine(L"G/g: output in compact form: {0:G5}", 123.456789);
 Console::WriteLine(L"N/n: output with comma if needed: {0:N}", 12345.6789);
 Console::WriteLine(L"X/x: output integer as hexadecimal: {0:D4}", 123);

 Console::ReadLine();
    return 0;
}

output format for Console::Write()/WriteLine()

Console::Write()/WriteLine() with arguments

Modify from the exercise in Hello Visual C++ CLR Console Application using Microsoft Visual Studio 11 Developer Preview to demonstrate the use of Console::Write()/WriteLine() functions with arguments.

Console::Write()/WriteLine() with arguments

// CLRConsole.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
int number1 = 2;
float number2 = 1.23456f;

Console::WriteLine(L"Use Write()/WriteLine() without argument!");
Console::Write(L"Do you know? ");
Console::Write(number1);
Console::Write(L" + ");
Console::Write(number2);
Console::Write(L" = ");
Console::WriteLine(number1 + number2);
//The follow line have Error!
Console::WriteLine(L"Do you know? " + number1 + " + " + number2 + " = " + number1 + number2);

Console::WriteLine(L"");
Console::WriteLine(L"With arguments!");
Console::WriteLine(L"Do you know? {0} + {1} = {2}", number1, number2, number1 + number2);
Console::WriteLine(L"Do you know? {1} + {0} = {2}", number1, number2, number1 + number2);

Console::WriteLine(L"");
Console::WriteLine(L"You can also specify how much digits after decimal point:");
Console::WriteLine(L"Do you know? {0:F2} + {1:F2} = {2:F2}", number1, number2, number1 + number2);
Console::ReadLine();
return 0;
}


Related:
- More output format for Console::Write()/WriteLine()