WinCE 界面开发:使用HTML Viewer Control

 

在Win32平台上用 C++做界面开发确实是很吃力的活。无论使用MFC还是ATL或者WTL都是很痛苦的。通常引入HTML控件,可以做出类似网页效果的精美布局。在传统的Windows应用程序中使用Web控件已是大势所趋,大大简化工作量。但是在WinCE平台上,做同样的事情,往往遇到很多困难。我自己摸索了半天,总结出下面的方法,效果还是很好的。

 


首先,使用WTL(WTL8.0 with VS2005)创建Windows Mobile 6的程序,选择新建项目ceWtl->WTL Mobile Application Wizard。

Platforms选择:Windows Mobile 6 Professional SDK。

Application Type:默认。

User Interface Features:勾选Use a view window,view type 选择Html Browser Control。

按Finish。

 

我仅修改默认生成的CeWtlFrame.h文件如下:

//cheungmine{{

我增加的部分

//cheungmine}}

 

// CeWtlFrame.h : interface of the CCeWtlFrame class
//
/

#pragma once

//cheungmine{{
/**
  OLEMethod
    OLEMethod receives variable parameters. I.e., you can pass any number of parameters
    depending on the property/method. Following is a summary of the OLEMethod() parameters,
    * nType – Type of call to make, which can be any of the following values:
           DISPATCH_PROPERTYPUT - Set property value
           DISPATCH_PROPERTYGET - Get property value
           DISPATCH_METHOD - Call a method
    * pvResult – Return value for the call made; it can be another IDispatch object,
                  or an integer value, or a boolean, or so on..
    * pDisp – IDispatch interface object for which the call is to be made.
    * ptName – Property or method name.
    * cArgs – Number of arguments followed after this parameter.
    * … parameters in reverse order for the call (it can be values of a property, or
         parameters of a method for the IDispatch object).
 
  See also:
    MS Office OLE Automation Using C++  By Naren Neelamegam
    A simple guide to automate MS Word and MS Excel using C++.
   
    http://www.codeproject.com/KB/office/MSOfficeAuto.aspx
*/
static HRESULT OLEMethod(int       nType,
                        VARIANT   *pvResult,
                        IDispatch *pDisp,
                        LPOLESTR   ptName,
                        int        cArgs...)
{
    if(!pDisp) return E_FAIL;

    va_list marker;
    va_start(marker, cArgs);

    DISPPARAMS dp = { NULL, NULL, 0, 0 };
    DISPID dispidNamed = DISPID_PROPERTYPUT;
    DISPID dispID;
    char szName[200];

    // Convert down to ANSI
    WideCharToMultiByte(CP_ACP, 0, ptName, -1, szName, 256, NULL, NULL);

    // Get DISPID for name passed...
    HRESULT hr= pDisp->GetIDsOfNames(IID_NULL, &ptName, 1,
                             LOCALE_USER_DEFAULT, &dispID);
    if(FAILED(hr)) {
        return hr;
    }
    // Allocate memory for arguments...
    VARIANT *pArgs = new VARIANT[cArgs+1];
    // Extract arguments...
    for(int i=0; i<cArgs; i++) {
        pArgs[i] = va_arg(marker, VARIANT);
    }

    // Build DISPPARAMS
    dp.cArgs = cArgs;
    dp.rgvarg = pArgs;

    // Handle special-case for property-puts!
    if(nType & DISPATCH_PROPERTYPUT) {
        dp.cNamedArgs = 1;
        dp.rgdispidNamedArgs = &dispidNamed;
    }

    // Make the call!
    hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT,
                       nType, &dp, pvResult, NULL, NULL);
    if(FAILED(hr)) {
        return hr;
    }

    // End variable-argument section...
    va_end(marker);
    delete [] pArgs;
    return hr;
}
//cheungmine}}

class CCeWtlFrame :
    public CFrameWindowImpl<CCeWtlFrame>,
    public CUpdateUI<CCeWtlFrame>,
    public CAppWindow<CCeWtlFrame>,
    public CMessageFilter, public CIdleHandler
{
public:
    DECLARE_APP_FRAME_CLASS(NULL, IDR_MAINFRAME, L"Software//WTL//CeWtl")

    CCeWtlView m_view;

    virtual BOOL PreTranslateMessage(MSG* pMsg)
    {
        if(CFrameWindowImpl<CCeWtlFrame>::PreTranslateMessage(pMsg))
            return TRUE;

        return m_view.IsWindow() ? m_view.PreTranslateMessage(pMsg) : FALSE;
    }

// CAppWindow operations
    bool AppHibernate( bool bHibernate)
    {
        // Insert your code here or delete member if not relevant
        return bHibernate;
    }

    bool AppNewInstance( LPCTSTR lpstrCmdLine)
    {
        // Insert your code here or delete member if not relevant
        return false;
    }

    void AppSave()
    {
        CAppInfo info;
        // Insert your code here
    }

    virtual BOOL OnIdle()
    {
        UIUpdateToolBar();
        return FALSE;
    }

    BEGIN_UPDATE_UI_MAP(CCeWtlFrame)
    END_UPDATE_UI_MAP()

    BEGIN_MSG_MAP(CCeWtlFrame)
        MESSAGE_HANDLER(WM_CREATE, OnCreate)
        COMMAND_ID_HANDLER(ID_APP_EXIT, OnFileExit)
        COMMAND_ID_HANDLER(ID_FILE_NEW, OnFileNew)
        COMMAND_ID_HANDLER(ID_ACTION, OnAction)
        COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
        CHAIN_MSG_MAP(CAppWindow<CCeWtlFrame>)
        CHAIN_MSG_MAP(CUpdateUI<CCeWtlFrame>)
        CHAIN_MSG_MAP(CFrameWindowImpl<CCeWtlFrame>)
    END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
//    LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//    LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//    LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

    LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
    {
        CAppInfo info;

        CreateSimpleCEMenuBar();
        UIAddToolBar(m_hWndCECommandBar);

        m_hWndClient = m_view.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN  | HS_CONTEXTMENU);

        // register object for message filtering and idle updates
        CMessageLoop* pLoop = _Module.GetMessageLoop();
        ATLASSERT(pLoop != NULL);
        pLoop->AddMessageFilter(this);
        pLoop->AddIdleHandler(this);

        return 0;
    }

    LRESULT OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    {
        PostMessage(WM_CLOSE);
        return 0;
    }

    LRESULT OnFileNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    {
        // TODO: add code to initialize document
       
        //cheungmine{{
        // 文件选择对话框
        m_view.AddHTML(L"<input id=/"PathFile/" type=/"file/" />");
        //cheungmine}}

        return 0;
    }

    LRESULT OnAction(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    {
        //cheungmine{{
        HRESULT     hr;
        VARIANT     varOut;
        CComVariant varEmpty;

        // 取得 IWebBrowser2 接口
        CComPtr<IDispatch>  spDispDoc;
        m_view.GetDocumentDispatch(&spDispDoc);
        ATLASSERT(spDispDoc);

        // 取得 IHTMLElementCollection 接口
        varOut.vt = VT_DISPATCH;
        varOut.pdispVal = 0;
        hr = OLEMethod(DISPATCH_PROPERTYGET, &varOut, spDispDoc, L"all", 0);
        ATLASSERT(hr==S_OK);

        CComPtr<IDispatch> spElemColl;
        spElemColl.Attach(varOut.pdispVal);

        // 取得指定ID的 IHTMLElement
        varOut.vt = VT_DISPATCH;
        varOut.pdispVal = 0;
        CComBSTR  bstrPathFile(L"PathFile");
        VARIANT  name;
        name.vt=VT_BSTR;
        name.bstrVal=bstrPathFile.m_str;
        hr = OLEMethod(DISPATCH_METHOD, &varOut, spElemColl, L"item", 1, name);
        ATLASSERT(hr==S_OK);

        CComPtr<IDispatch> spElem;
        spElem.Attach(varOut.pdispVal);

        // 取得value属性, 存放到bstrPathFile
        varOut.vt=VT_BSTR;
        varOut.bstrVal=0;
        hr = OLEMethod(DISPATCH_PROPERTYGET, &varOut, spElem, L"value", 0);
        ATLASSERT(hr==S_OK && varOut.vt==VT_BSTR);
        bstrPathFile.Empty();
        bstrPathFile.Attach(varOut.bstrVal);

        MessageBox( bstrPathFile );
        //cheungmine}}

        return 0;
    }

    LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    {
        CAboutDlg dlg;
        dlg.DoModal();
        return 0;
    }
};

 

编译部署到设备,按Menu->New显示界面。选择一个文件,按Action,跟踪调试,可以看到一起如我们期望那样。

例子代码下载地址:

 

http://download.csdn.net/source/1984878

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cheungmine/archive/2010/01/11/5176124.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值