WinRT - learning

Post Reply
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

WinRT - learning

Post by Antonio Linares »

Build a WinRT Metro App to Access the Windows 8 File System

http://www.devproconnections.com/articl ... s-8-142173

WinRT GUI controls

http://msdn.microsoft.com/en-us/library ... 27716.aspx
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: WinRT - learning

Post by Antonio Linares »

Programmatically create a control:

Code: Select all

    Private Sub Button_Click_2(sender As Object, e As RoutedEventArgs)
        Dim btn = New Button
        btn.Width = 110
        btn.Height = 50
        btn.Content = "test"
        sender.Parent().Children.Add(btn)

    End Sub
Image
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: WinRT - learning

Post by Antonio Linares »

MsgInfo()

Using VB

Code: Select all

Imports Windows.UI.Popups

        Dim messageDialog = New MessageDialog("click")
        Await messageDialog.ShowAsync()
 
Using C++

Code: Select all

   using namespace Windows::UI::Popups;

MessageDialog ^ dialog = ref new MessageDialog("Hello WinRT");
       
   dialog->ShowAsync();   
 
Available from C:

Code: Select all

extern "C" {
void MsgInfo( Platform::String ^ szMsg  )
{
    MessageDialog ^ dialog = ref new MessageDialog( szMsg );
       
    dialog->ShowAsync();   
}
};
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: WinRT - learning

Post by Antonio Linares »

Convert a char * to Platform::String

Code: Select all

std::wstring stows( std::string s )
{
    std::wstring ws;

    ws.assign( s.begin(), s.end() );

    return ws;
}

std::string wstos( std::wstring ws )
{
    std::string s;

    s.assign( ws.begin(), ws.end() );
    
    return s;
}

Platform::String ^ stops( std::string s )
{
    return ref new Platform::String( stows( s ).c_str() );
}

std::string pstos( Platform::String ^ ps )
{
    return wstos( std::wstring( ps->Data() ) );
}

Platform::String ^ atops( const char * text )
{   
    return stops( std::string( text ) );
}
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: WinRT - learning

Post by Antonio Linares »

void MsgInfo( char * szMsg );

Code: Select all

extern "C" {
void hb_vmInit( BOOL );

std::wstring stows( std::string s )
{
    std::wstring ws;

    ws.assign( s.begin(), s.end() );

    return ws;
}

std::string wstos( std::wstring ws )
{
    std::string s;

    s.assign( ws.begin(), ws.end() );
    
    return s;
}

Platform::String ^ stops( std::string s )
{
    return ref new Platform::String( stows( s ).c_str() );
}

std::string pstos( Platform::String ^ ps )
{
    return wstos( std::wstring( ps->Data() ) );
}

Platform::String ^ atops( const char * text )
{   
    return stops( std::string( text ) );
}

void MsgInfo( char * szMsg  )
{
    MessageDialog ^ dialog = ref new MessageDialog( atops( szMsg ) );
       
    dialog->ShowAsync();   
}

void HB_FUN_MAIN( void );

};
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: WinRT - learning

Post by Antonio Linares »

Platform::String to char *

Code: Select all

    size_t i;
    char buffer[ 300 ];

    wcstombs_s( &i, buffer, (size_t) 300, std::wstring( this->code->Text->Data() ).data(), (size_t) 300 );
 
this->code->Text is a Platform::String
buffer holds the char *

Enhanced version

Code: Select all

    unsigned long ulLen = platformString->Length() * 2;     
    char * buffer = ( char * ) malloc( uiLen );
    size_t i;

    wcstombs_s( &i, buffer, uiLen, std::wstring( platformString->Data() ).data(), uiLen );
    ... (use the text)
    free( ( void * ) buffer );
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: WinRT - learning

Post by Antonio Linares »

Dynamically building a page:

Page ^ page = ref new Page;

Activating it:

this->Frame->Navigate( page->GetType() );

or:

Frame->Navigate( page::typeid );
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: WinRT - learning

Post by Antonio Linares »

Dinamically adding controls:

Code: Select all

Button ^ fivewinrt::BlankPage::AddButton( void )
{
    static Button ^ btn = ref new Button;

    btn->Width = 110;
    btn->Height = 50;
    btn->Content = "Button";

    this->Grid->Children->Append( btn );

    return btn;
}
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: WinRT - learning

Post by Antonio Linares »

Dinamically adding events handlers:

Code: Select all

void btn_Tapped( Object ^ sender, TappedRoutedEventArgs ^ e )
{
    MsgInfo( "btn Click" );
}

Button ^ fivewinrt::BlankPage::AddButton( void )
{
    static Button ^ btn = ref new Button;

    btn->Width = 110;
    btn->Height = 50;
    btn->Content = "Button";

    this->Grid->Children->Append( btn );

    btn->AddHandler( TappedEvent, ref new TappedEventHandler( btn_Tapped ), false );

    return btn;
}
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: WinRT - learning

Post by Enrico Maria Giordano »

Antonio Linares wrote:

Code: Select all

void btn_Tapped( Object ^ sender, TappedRoutedEventArgs ^ e )
What is ^ in the parameters declaration context? I never saw it in C++. Is it C++?

EMG
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: WinRT - learning

Post by Antonio Linares »

Enrico,
C++/CX (Component Extensions) is a language extension for C++ compilers from Microsoft that enables C++ programmers to write programs for the new Windows Runtime platform, or WinRT. It brings a set of syntax and library abstractions that interface with the COM-based WinRT programming model in a way that is natural to native C++-programmers.
http://en.wikipedia.org/wiki/C%2B%2B/CX

Though some C++ purists claim that there is no real need for CX, and all that it offers can be done using standard C++...
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: WinRT - learning

Post by Antonio Linares »

^ is quite similar to * but it means that we are using an object with "reference counting" (it will be destroyed when it is no longer used)
The ref new instead returns a handle, which is a reference to the object rather than the pointer itself. These references are counted so that the object can be automatically deleted when there are no longer any references to it.
http://www.charlespetzold.com/blog/2012 ... ammer.html
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: WinRT - learning

Post by Enrico Maria Giordano »

Thank you. I think that MS still continue with its bad habit of reinventing the wheel to include some custom extension. Something like C++ smart pointers would be enough.

EMG
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: WinRT - learning

Post by Antonio Linares »

Enrico,

Yes, I agree with you. IMO MS should have used C++ only and not implement new languages and/or extensions...

We have been using the Clipper language for years and had no need for changes. Ok, some enhancements are fine, but the core of the language should remain the same unless the language is not robust enough...
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: WinRT - learning

Post by Antonio Linares »

Compiling PRGs from VS2012:

1. Add an existing PRG to the project

Image

2. Right click on it and select properties. Select "Custom Build tool":

Image

3. Right click again on it and configure it this way:

Image

4. Right click again on it and compile it, and add the resulting C file to the project too. Set no to the use of Windows runtime extensions:

Image

Working fine :-)
Image
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply