WinCE 介面開發:使用HTML Browser Control

來源:互聯網
上載者:User

WinCE 介面開發:使用HTML Viewer Control

by cheungmine 2010-1

在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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.