[转载自MSDN:http://msdn.microsoft.com/en-us/library/ms838684.aspx]
Microsoft Internet Explorer for the Pocket PC lets you browse the wireless Web and view offline Web files. However the HTML Viewer Control gives these abilities and more to any application.
Applies to:
Microsoft Windows Powered Pocket PC 2000
Microsoft eMbedded Visual C++ version 3.0
VOImage classes (optional) available for free from Virtual Office Systems, Inc
GIF, JPG, and other types of files
Languages Supported
English
Before you can create a control you need a project with a main window. The following steps walk you through creating both your project and the HTML Viewer Control.
Figure 1: Select the target platform.
#include <sipapi.h> #include <htmlctrl.h> #define IDC_HTMLVIEW 10 #define MAX_LOADSTRING 100
HWND hwndCB; // The command bar handle HWND hwndHTML; // HTML Control static SHACTIVATEINFO s_sai;
HWND CreateHTMLControl(HWND hwndParent) { RECT rc; DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_CLIPSIBLINGS; if(!InitHTMLControl( hInst )) MessageBox(hwndParent, TEXT("Unable to initialize HTML control"), TEXT("Error"), MB_OK); GetClientRect (hwndParent, &rc); return CreateWindow (DISPLAYCLASS, NULL, dwStyle, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, hwndParent, (HMENU)IDC_HTMLVIEW, hInst, NULL); }
case WM_CREATE: hwndCB = CreateRpCommandBar(hWnd); hwndHTML = CreateHTMLControl(hWnd); SetFocus (hwndHTML); break;
case WM_SIZE: if(IsWindow(hwndHTML)) SetWindowPos(hwndHTML, 0, 0, 0, LOWORD(lParam), HIWORD(lParam) - MENU_HEIGHT, SWP_NOZORDER | SWP_NOMOVE); break;
Before populating the list control with HTML code, you should decide if you want to work with Unicode or single-byte text. Depending on which one you choose, you will send either the DTM_ADDTEXT or the DTM_ADDTEXTW message to the control. The one you use determines the format of text parameters in notification messages sent from the control to your application. I'll assume you want to use Unicode, because that is the default character set for most Pocket PC character strings.
To set the contents of the HTML Viewer Control, you need to:
The HTML code can come from a file, your code, or the Internet. To keep it simple, let's just generate the code in your sample project. Add the following code to your WM_CREATE message handler, after the SetFocus() line and before the break (the optional DTM_ENABLESHRINK message tells the control to attempt to fit the contents of the HTML page to the control width if possible):
BOOL fFitToPage = TRUE; PostMessage(hwndHTML, DTM_ENABLESHRINK, 0, fFitToPage); SendMessage(hwndHTML, WM_SETTEXT, 0, (LPARAM)""); SendMessage(hwndHTML, DTM_ADDTEXTW, FALSE, (LPARAM)TEXT("<HTML><TITLE>Test</TITLE>")); SendMessage(hwndHTML, DTM_ADDTEXTW, FALSE, (LPARAM)TEXT("<BODY><P>")); SendMessage(hwndHTML, DTM_ADDTEXTW, FALSE, (LPARAM)TEXT("<h1>Heading</h1>Normal Text<BR>")); SendMessage(hwndHTML, DTM_ADDTEXTW, FALSE, (LPARAM)TEXT("<A HREF=\"www.voscorp.com\"><IMG SRC=\"\\Pic.gif\"></A>")); SendMessage(hwndHTML, DTM_ADDTEXTW, FALSE, (LPARAM)TEXT("</BODY></HTML>")); SendMessage(hwndHTML, DTM_ENDOFSOURCE, 0, (LPARAM)NULL);
You will now have a control that contains the text (including the header style) from your HTML code, but if you run the application at this point you will notice that the image (\PIC.GIF ) is not displayed (as shown in Figure 2). To display images, your application needs to load the images in response to a notification event from the HTML Viewer Control.
Figure 2: HTML View Control without images.
There are several notification messages from the HTML Viewer Control that you may wish to process. If you have no need for images, you may choose not to process the NM_INLINE_IMAGE message. Likewise, you may not want to process sounds, so you may choose not to process the NM_INLINE_SOUND message. Of course, anything you choose not to process will not be available to your users via the control.
To avoid having the (rather lengthy) code to handle the loading of GIF, JPG, BMP, and other code using IMGDECMP, this code assumes you are using the free VOImage class libraries from Virtual Office Systems, Inc to do the dirty work. To add image processing to your code, follow these steps:
Note that SHLoadImageFile and SHLoadImageResource should be used for Windows Mobile-based devices.
HWND hwndHTML; // HTML Control CVOImage img; // Interface to IMGDECMP.DLL static SHACTIVATEINFO s_sai;
case WM_NOTIFY: switch(wParam) { case IDC_HTMLVIEW: { NM_HTMLVIEW* pnm = (NM_HTMLVIEW*)lParam; switch(pnm->hdr.code) { case NM_INLINE_IMAGE: { HDC dc = GetDC(hWnd); if(img.Load(dc, pnm->szTarget)) { INLINEIMAGEINFO imgInfo; imgInfo.dwCookie = pnm->dwCookie; imgInfo.iOrigHeight = img.GetHeight(); imgInfo.iOrigWidth = img.GetWidth(); imgInfo.hbm = img.Copy(); imgInfo.bOwnBitmap = FALSE; SendMessage(hwndHTML, DTM_SETIMAGE, 0, (LPARAM)&imgInfo); } else SendMessage(hwndHTML, DTM_IMAGEFAIL, 0, pnm->dwCookie); ReleaseDC(hWnd, dc); break; } // end case NM_INLINE_IMAGE: } // end switch(pnm->hdr.code) } // End case IDC_HTMLVIEW: } // End switch(wParam) break;
Note that we pass a copy of the HBITMAP to the DTM_SETIMAGE message by using the Copy() method of the VOImage object and setting bOwnBitmap to TRUE. This allows us to reuse the same VOImage object for multiple image files, and instructs the control to clean up the HBITMAP objects we have passed to it instead of relying on the calling code to do the cleanup.
The HTML Viewer Control allows you to include the rich interfaces to your application that users are used to seeing in Web browsers. What you do with the control is of course up to you, but as you can see in Figure 3, you can have quite an impressive interface with very little coding.
Figure 3: HTML View Control with images.