Abhineet
Abhineet

Reputation: 6617

How to implement code for multiple buttons using c++ in Silverlight for Windows Embedded

I have referred the following link:

Silverlight for Windows Embedded

By referring this link i created a demo application which consist of two buttons created using Microsoft expression blend 2 tools. And then written a code referring the above site. Now my button names are "Browser Button" and "Media Button". On click of any one of the button i should able to launch the respective application. I was able to do for "Browser Button" but not for "Media Button" and if i do for "Media Button" then i am not able to do for "Browser Button".. I mean to say that how should i create event handler for both the buttons.

This is the code in c++ which i should modify

class BtnEventHandler
{
public:
    HRESULT OnClick(IXRDependencyObject* source,XRMouseButtonEventArgs* args)
    {
        RETAILMSG(1,(L"Browser event"));
        Execute(L"\\Windows\\iesample.exe",L"");
        return S_OK;
    }
};



// entry point for the application.
INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
 LPWSTR lpCmdLine,int nCmdShow)
{
    PrintMessage();
    int      exitCode = -1;
    HRESULT  hr = S_OK;

if (!XamlRuntimeInitialize())
    return -1;

HRESULT retcode;
IXRApplicationPtr app;

if (FAILED(retcode=GetXRApplicationInstance(&app)))
    return -1;

if (FAILED(retcode=app->AddResourceModule(hInstance)))
    return -1;

XRWindowCreateParams wp;

ZeroMemory(&wp, sizeof(XRWindowCreateParams));

wp.Style       = WS_OVERLAPPED;
wp.pTitle      = L"Bounce Test";
wp.Left        = 0;
wp.Top         = 0;

XRXamlSource xamlsrc;

xamlsrc.SetResource(hInstance,TEXT("XAML"),MAKEINTRESOURCE(IDR_XAML1));


IXRVisualHostPtr vhost;
if (FAILED(retcode=app->CreateHostFromXaml(&xamlsrc, &wp, &vhost)))
    return -1;  

IXRFrameworkElementPtr root;    
if (FAILED(retcode=vhost->GetRootElement(&root)))
    return -1;  

IXRButtonBasePtr btn;   
if (FAILED(retcode=root->FindName(TEXT("BrowserButton"), &btn)))
    return -1;      

IXRDelegate<XRMouseButtonEventArgs>* clickdelegate;
BtnEventHandler handler;    

if(FAILED(retcode=CreateDelegate
    (&handler,&BtnEventHandler::OnClick,&clickdelegate)))
    return -1;
if (FAILED(retcode=btn->AddClickEventHandler(clickdelegate)))
    return -1;

UINT exitcode;
if (FAILED(retcode=vhost->StartDialog(&exitcode)))
    return -1;

return exitCode;
}

I have to add event handler for both the button so that on emulator whenever i click on any one of the button i should be able to launch the respective applications.

Thanks in advance

Upvotes: 0

Views: 1030

Answers (1)

Shaihi
Shaihi

Reputation: 3974

You can create two seperate functions to be the handlers for each button.
If you want the same handler to identify which button was pressed and act accordingly you can read the following MSDN article that demonstrates that.


I have not tried this, but you can also use IXRDependencyObject::GetName of the source object to know which button was pressed.

Your handler would look like:

HRESULT OnClick(IXRDependencyObject* source,XRMouseButtonEventArgs* args)
{
    BSTR pName[50];

    source->GetName(pName);
    if (_tcscmp(L"BrowserEvent", LPCWSTR(pName)) == 0)
    {
            RETAILMSG(1,(L"Browser event"));
            Execute(L"\\Windows\\iesample.exe",L"");

    }
    else if (_tcscmp(L"BrowserEvent", LPCWSTR(pName)) == 0)
    {
            /* Handle another button or element */
    }
    return S_OK;
}

Upvotes: 2

Related Questions