Page 1 of 1

WinRT - learning

Posted: Sat Sep 29, 2012 4:41 am
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

Re: WinRT - learning

Posted: Sat Sep 29, 2012 5:22 am
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

Re: WinRT - learning

Posted: Sat Sep 29, 2012 8:54 pm
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();   
}
};
 

Re: WinRT - learning

Posted: Sun Sep 30, 2012 10:25 am
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 ) );
}

Re: WinRT - learning

Posted: Sun Sep 30, 2012 10:30 am
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 );

};

Re: WinRT - learning

Posted: Sun Sep 30, 2012 6:39 pm
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 );
 

Re: WinRT - learning

Posted: Wed Oct 03, 2012 10:41 am
by Antonio Linares
Dynamically building a page:

Page ^ page = ref new Page;

Activating it:

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

or:

Frame->Navigate( page::typeid );

Re: WinRT - learning

Posted: Tue Oct 09, 2012 4:01 pm
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;
}

Re: WinRT - learning

Posted: Tue Oct 09, 2012 4:20 pm
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;
}

Re: WinRT - learning

Posted: Tue Oct 09, 2012 6:05 pm
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

Re: WinRT - learning

Posted: Wed Oct 10, 2012 10:02 am
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++...

Re: WinRT - learning

Posted: Wed Oct 10, 2012 10:06 am
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

Re: WinRT - learning

Posted: Wed Oct 10, 2012 10:24 am
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

Re: WinRT - learning

Posted: Wed Oct 10, 2012 10:46 am
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...

Re: WinRT - learning

Posted: Wed Oct 10, 2012 9:53 pm
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