C++ 在C+中托管Silverlight+;
我在这里有点不知所措,想听听关于如何进行的建议 基本上,我想做的是能够在我的C++应用程序中渲染和控制Silverlight。我想要的是:C++ 在C+中托管Silverlight+;,c++,silverlight,com,atl,C++,Silverlight,Com,Atl,我在这里有点不知所措,想听听关于如何进行的建议 基本上,我想做的是能够在我的C++应用程序中渲染和控制Silverlight。我想要的是: class silverlight_host { public: // Prio 1 silverlight_host(const std::string& filename); // Load a xap file void draw(void* dest); // Draw with alph
class silverlight_host
{
public:
// Prio 1
silverlight_host(const std::string& filename); // Load a xap file
void draw(void* dest); // Draw with alpha to dest
std::pair<size_t, size_t> get_size(); // Need to know required size of dest
// Prio 2
bool is_dirty() const; // Check for dirty rect since last call to draw
void send_param(const std::string& param); // Send data to silverlight control or call functions. Alternative name maybe, call_function
void set_size(size_t width, size_t height); // Set the size of drawing.
// Prio 3
// Some more nice to have functions, although not as important as those above
double get_desired_frame_rate(); // The desired frame rate which is specified in xap
std::pair<size_t, size_t> get_desired_size(); // The desired size which is specified in xap
void tick(); // Tick a synchronous timeline, disable internal asynchronous timer
void set_param_callback(const std::function<void(const std::string&)>& callback); // Let the xap file call the application
};
我使用CoInitialize在线程内创建XcpControlHost:
void run()
{
struct co_init
{
co_init(){CoInitialize(nullptr);}
~co_init(){CoUninitialize();}
} co;
if(FAILED(CComObject<XcpControlHost>::CreateInstance(&host_)))
throw std::exception("Failed to create XcpControlHost");
if(FAILED(host_->CreateXcpControl()))
throw std::exception("Failed to create XcpControl");
while(GetMessage(&msg, 0, 0, 0))
{
if(!is_running_)
PostQuitMessage(0);
if(msg.message == WM_USER + 1) // Sent from app
OleDraw(host_->m_pUnKnown, DVASPECT_CONTENT, targetDC, 0);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
我已经测试了这里可用的代码项目:在VisualStudio2010下。以下是使其工作所需的:
- 将其加载到VS 2010下,并将其转换为VS 2010(不关心备份日志等)
- 删除注册生成后事件(“$(TargetPath)”/RegServer),它仅用于注册projet内的COM组件。如果愿意,您也可以让该事件发生,然后忘记错误
- 我不得不在XcpControlHost.cpp中做一个小改动
HRESULT CXcpControlHost::CreateXcpControl(HWND hWnd)
{
AtlAxWinInit();
CoInitialize(NULL); // add this line to initialize COM
...
}
它可以工作(我正在运行Windows7和Silverlight4)
这才是真正的出路
不过有一点值得注意:在示例中,作者没有使用此处提供的官方文件:。相反,他重新定义了所有接口和GUID,这是不需要的。您只需将xcpctrl.idl添加到Visual Studio 2010并进行编译,这将触发MIDL编译器,该编译器将创建以下3个文件:xcpctrl_h.h、xcpctrl_i.c、xcpctrl_p.c。编译完成后,您可以将它们添加到项目中。+1对于这个好的主题。我还需要知道这件事……当我试图做这些事情时:。。。Silverlight的哪个版本?最好是最新版本。干得好。我已经添加了一个指向文件的链接,根据您的建议,我已经在该文件中修复了“TestProject”项目。此示例的一个问题是它无法向Silverlight主机控件发送键盘消息。如何解决这个问题?@IslamYahiatene-发布另一个问题
hr = m_spOleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, spClientSite, 0, NULL, &m_rcPos);
HRESULT CXcpControlHost::CreateXcpControl(HWND hWnd)
{
AtlAxWinInit();
CoInitialize(NULL); // add this line to initialize COM
...
}