如何使用 c++ 实现多个按钮的代码在 Windows Embedded 的 Silverlight 中

发布于 2024-08-31 08:43:59 字数 2316 浏览 5 评论 0原文

我引用了以下链接:

Silverlight for Windows Embedded

通过引用此链接,我创建了一个演示应用程序,其中包含使用 Microsoft Expression Blend 2 工具创建的两个按钮。然后参考上面的网站写了一段代码。现在我的按钮名称是“浏览器按钮”和“媒体按钮”。单击任一按钮后,我应该能够启动相应的应用程序。我能够为“浏览器按钮”执行操作,但不能为“媒体按钮”执行操作,如果我为“媒体按钮”执行操作,那么我无法为“浏览器按钮”执行操作。我的意思是说我应该如何创建事件两个按钮的处理程序。

这是我应该修改的 C++ 代码,

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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我爱人 2024-09-07 08:43:59

您可以创建两个单独的函数作为每个按钮的处理程序。
如果您希望相同的处理程序识别按下了哪个按钮并采取相应的操作,您可以阅读 下面的 MSDN 文章 证明了这一点。


我没有尝试过,但您也可以使用 IXRDependencyObject::GetName 源对象以了解按下了哪个按钮。

你的处理程序看起来像:

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文