2689
- 收藏
- 点赞
- 分享
- 举报
创建Windows Mobile的IE浏览器
本实例实现创建一个windows mobile上的IE浏览器,可以连接到因特网的主站,还包含前进或是后退功能。程序主要框架为ATL窗口,其中还调用了CAxWindow类,该类是一个浏览器的窗口类。
运行环境:visual studio 2008
SDK:windows mobile 5.0 SDK
程序主要代码:
// MainWindow.cpp: Defines main window for this application.
#include "stdafx.h"
#include "resource.h"
#include "MainWindow.h"
CMainWindow::CMainWindow()
{
memset(&m_sai, 0, sizeof(m_sai));
m_sai.cbSize = sizeof(m_sai);
}
CMainWindow::~CMainWindow()
{
}
// **************************************************************************
//
// WM_xxx handlers
//
// **************************************************************************
LRESULT CMainWindow::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
HRESULT hr = S_OK;
SHMENUBARINFO mbi = { sizeof(mbi), 0 };
RECT rcMainWindow = { 0 };
RECT rcMenuBar = { 0 };
SIPINFO si = { sizeof(si), 0 };
// In one step, create an "AtlAxWin" window for the PIEWebBrowser control,
// and also create the control itself. (AtlAxWin is a window class that
// ATL uses to support containment of controls in windows.)
m_browser.Create(m_hWnd,
CWindow::rcDefault, // proper sizing is done in CMainWindow::OnSize
TEXT("Microsoft.PIEDocView"), // ProgID of the control
WS_CHILD | WS_VISIBLE | WS_BORDER, 0,
ID_BROWSER);
CBR(m_browser.m_hWnd != NULL);
// cache IWebBrowser2 interface pointer
hr = m_browser.QueryControl(&m_spIWebBrowser2);
CHR(hr);
// set up connection point
hr = AtlAdviseSinkMap(this, true);
CHR(hr);
// set initial properties for the control
m_spIWebBrowser2->put_AddressBar(VARIANT_TRUE);
// Create a menubar
// (mbi was initialized above)
mbi.hwndParent = m_hWnd;
mbi.nToolBarId = IDR_MAIN_MENUBAR; // ID of toolbar resource
mbi.hInstRes = _AtlBaseModule.GetResourceInstance();
CBR(SHCreateMenuBar(&mbi));
m_menuBar = mbi.hwndMB; // save menu bar HWND
// Compute RECT for initial size and position.
// The following code should compute RECT appropriately
// on both Pocket PC and Smartphone. It should function correctly
// whether SIP is on or off, and
// whether device is in portrait or landscape mode.
// (rcMainWindow was initialized above)
VERIFY(SystemParametersInfo(SPI_GETWORKAREA, 0, &rcMainWindow, 0));
// (rcMenuBar was initialized above)
m_menuBar.GetWindowRect(&rcMenuBar);
rcMainWindow.bottom = rcMenuBar.top;
// SIP state
// (si was initialized above)
if (SHSipInfo(SPI_GETSIPINFO, 0, &si, 0) &&
(si.fdwFlags & SIPF_ON) && (si.fdwFlags & SIPF_DOCKED))
{
rcMainWindow.bottom = si.rcVisibleDesktop.bottom;
}
MoveWindow(&rcMainWindow);
ASSERT(SUCCEEDED(hr));
Error:
return SUCCEEDED(hr) ? 0 : -1;
}
LRESULT CMainWindow::OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
m_menuBar = NULL;
// Tear down connection points while controls are still alive.
VERIFY(SUCCEEDED(AtlAdviseSinkMap(this, false)));
m_spIWebBrowser2 = NULL;
m_browser = NULL;
PostQuitMessage(0);
bHandled = FALSE; // Allow ATL's default processing (e.g. NULLing m_hWnd)
return 0;
}
LRESULT CMainWindow::OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
m_browser.MoveWindow(0, 0, LOWORD(lParam), HIWORD(lParam));
return 0;
}
LRESULT CMainWindow::OnActivate(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
// Notify shell of our WM_ACTIVATE message
SHHandleWMActivate(m_hWnd, wParam, lParam, &m_sai, 0);
return 0;
}
LRESULT CMainWindow::OnSettingChange(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
// Notify shell of our WM_SETTINGCHANGE message
SHHandleWMSettingChange(m_hWnd, wParam, lParam, &m_sai);
return 0;
}
// **************************************************************************
//
// WM_COMMAND handlers
//
// **************************************************************************
LRESULT CMainWindow::OnExitCommand(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
SendMessage(WM_CLOSE);
return 0;
}
LRESULT CMainWindow::OnBackCommand(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
m_spIWebBrowser2->GoBack();
return 0;
}
LRESULT CMainWindow::OnForwardCommand(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
m_spIWebBrowser2->GoForward();
return 0;
}
LRESULT CMainWindow::OnHomeCommand(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
m_spIWebBrowser2->GoHome();
return 0;
}
LRESULT CMainWindow::OnOpenURLCommand(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CGetURLDialog getURLDialog;
int nRetCode = getURLDialog.DoModal(m_hWnd); // display the 'Get URL' dialog box
if (IDOK == nRetCode)
{
m_spIWebBrowser2->Navigate(getURLDialog.GetURL(), NULL, NULL, NULL, NULL);
}
return 0;
}
LRESULT CMainWindow::OnRefreshCommand(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
m_spIWebBrowser2->Refresh();
return 0;
}
LRESULT CMainWindow::OnStopCommand(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
m_spIWebBrowser2->Stop();
return 0;
}
// **************************************************************************
//
// event handlers
//
// **************************************************************************
void __stdcall CMainWindow::OnBeforeNavigate2(IDispatch* pDisp, VARIANT * pvtURL,
VARIANT * /*pvtFlags*/, VARIANT * pvtTargetFrameName,
VARIANT * /*pvtPostData*/, VARIANT * /*pvtHeaders*/,
VARIANT_BOOL * /*pvbCancel*/)
{
USES_CONVERSION;
TCHAR szOutput[128];
StringCchPrintf(szOutput, ARRAYSIZE(szOutput),
TEXT("0x%08p %s %s"), pDisp, OLE2CT(V_BSTR(pvtURL)),
VT_ERROR != V_VT(pvtTargetFrameName) ? OLE2CT(V_BSTR(pvtTargetFrameName)) : TEXT("(error)"));
OutputDebugString(szOutput);
SetWindowText(TEXT("Untitled"));
VERIFY(SetEnabledState(IDM_STOP, TRUE));
}
void __stdcall CMainWindow::OnBrowserTitleChange(BSTR bstrTitleText)
{
USES_CONVERSION;
TCHAR szOutput[128];
StringCchPrintf(szOutput, ARRAYSIZE(szOutput),
TEXT("%s"), OLE2CT(bstrTitleText));
OutputDebugString(szOutput);
SetWindowText(OLE2CT(bstrTitleText));
}
void __stdcall CMainWindow::OnNavigateComplete2(IDispatch *pDisp, VARIANT *pvtURL)
{
USES_CONVERSION;
TCHAR szOutput[128];
StringCchPrintf(szOutput, ARRAYSIZE(szOutput),
TEXT("0x%08p %s"), pDisp, OLE2CT(V_BSTR(pvtURL)));
OutputDebugString(szOutput);
}
void __stdcall CMainWindow::OnDocumentComplete(IDispatch *pDisp, VARIANT *pvtURL)
{
USES_CONVERSION;
TCHAR szOutput[128];
StringCchPrintf(szOutput, ARRAYSIZE(szOutput),
TEXT("0x%08p %s"), pDisp, OLE2CT(V_BSTR(pvtURL)));
OutputDebugString(szOutput);
VERIFY(SetEnabledState(IDM_STOP, FALSE));
}
void __stdcall CMainWindow::OnCommandStateChange(long lCommand, BOOL bEnable)
{
if (CSC_NAVIGATEBACK == lCommand)
{
VERIFY(SetEnabledState(IDM_BACK, bEnable));
}
else if (CSC_NAVIGATEFORWARD == lCommand)
{
VERIFY(SetEnabledState(IDM_FORWARD, bEnable));
}
}
// **************************************************************************
//
// utility function
//
// **************************************************************************
BOOL CMainWindow::SetEnabledState(UINT uMenuItemID, BOOL bEnable)
{
HMENU hMenu = (HMENU)m_menuBar.SendMessage(SHCMBM_GETSUBMENU, 0, IDM_SK2_MENU);
BOOL bRetVal = EnableMenuItem(hMenu, uMenuItemID, bEnable ? MF_ENABLED : MF_GRAYED);
return (bRetVal != 0xFFFFFFFF);
}
// **************************************************************************
//
// CMainWindow::TranslateAccelerator
//
// Required to forward messages to the PIEWebBrowser control (and any other
// ActiveX controls that may be added to the main window's design).
//
// **************************************************************************
BOOL CMainWindow::TranslateAccelerator(MSG* pMsg)
{
// Accelerators are only keyboard or mouse messages
UINT uMsg = pMsg->message;
if (!(WM_KEYFIRST <= uMsg && uMsg <= WM_KEYLAST) &&
!(WM_MOUSEFIRST <= uMsg && uMsg <= WM_MOUSELAST))
{
return FALSE;
}
if (NULL == m_hWnd)
{
return FALSE;
}
// Find a direct child of this window from the window that has focus.
// This will be AtlAxWin window for the hosted control.
CWindow control = ::GetFocus();
if (IsChild(control) && m_hWnd != control.GetParent())
{
do
{
control = control.GetParent();
} while (m_hWnd != control.GetParent());
}
// Give the control (via the AtlAxWin) a chance to translate this message
if (control.m_hWnd && control.SendMessage(WM_FORWARDMSG, 0, (LPARAM)pMsg))
{
return TRUE;
}
// If the main window used accelerators, we could have called the global
// ::TranslateAccelerator() function here, instead of simply returning FALSE.
return FALSE;
}
运行环境:visual studio 2008
SDK:windows mobile 5.0 SDK
程序主要代码:
// MainWindow.cpp: Defines main window for this application.
#include "stdafx.h"
#include "resource.h"
#include "MainWindow.h"
CMainWindow::CMainWindow()
{
memset(&m_sai, 0, sizeof(m_sai));
m_sai.cbSize = sizeof(m_sai);
}
CMainWindow::~CMainWindow()
{
}
// **************************************************************************
//
// WM_xxx handlers
//
// **************************************************************************
LRESULT CMainWindow::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
HRESULT hr = S_OK;
SHMENUBARINFO mbi = { sizeof(mbi), 0 };
RECT rcMainWindow = { 0 };
RECT rcMenuBar = { 0 };
SIPINFO si = { sizeof(si), 0 };
// In one step, create an "AtlAxWin" window for the PIEWebBrowser control,
// and also create the control itself. (AtlAxWin is a window class that
// ATL uses to support containment of controls in windows.)
m_browser.Create(m_hWnd,
CWindow::rcDefault, // proper sizing is done in CMainWindow::OnSize
TEXT("Microsoft.PIEDocView"), // ProgID of the control
WS_CHILD | WS_VISIBLE | WS_BORDER, 0,
ID_BROWSER);
CBR(m_browser.m_hWnd != NULL);
// cache IWebBrowser2 interface pointer
hr = m_browser.QueryControl(&m_spIWebBrowser2);
CHR(hr);
// set up connection point
hr = AtlAdviseSinkMap(this, true);
CHR(hr);
// set initial properties for the control
m_spIWebBrowser2->put_AddressBar(VARIANT_TRUE);
// Create a menubar
// (mbi was initialized above)
mbi.hwndParent = m_hWnd;
mbi.nToolBarId = IDR_MAIN_MENUBAR; // ID of toolbar resource
mbi.hInstRes = _AtlBaseModule.GetResourceInstance();
CBR(SHCreateMenuBar(&mbi));
m_menuBar = mbi.hwndMB; // save menu bar HWND
// Compute RECT for initial size and position.
// The following code should compute RECT appropriately
// on both Pocket PC and Smartphone. It should function correctly
// whether SIP is on or off, and
// whether device is in portrait or landscape mode.
// (rcMainWindow was initialized above)
VERIFY(SystemParametersInfo(SPI_GETWORKAREA, 0, &rcMainWindow, 0));
// (rcMenuBar was initialized above)
m_menuBar.GetWindowRect(&rcMenuBar);
rcMainWindow.bottom = rcMenuBar.top;
// SIP state
// (si was initialized above)
if (SHSipInfo(SPI_GETSIPINFO, 0, &si, 0) &&
(si.fdwFlags & SIPF_ON) && (si.fdwFlags & SIPF_DOCKED))
{
rcMainWindow.bottom = si.rcVisibleDesktop.bottom;
}
MoveWindow(&rcMainWindow);
ASSERT(SUCCEEDED(hr));
Error:
return SUCCEEDED(hr) ? 0 : -1;
}
LRESULT CMainWindow::OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
m_menuBar = NULL;
// Tear down connection points while controls are still alive.
VERIFY(SUCCEEDED(AtlAdviseSinkMap(this, false)));
m_spIWebBrowser2 = NULL;
m_browser = NULL;
PostQuitMessage(0);
bHandled = FALSE; // Allow ATL's default processing (e.g. NULLing m_hWnd)
return 0;
}
LRESULT CMainWindow::OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
m_browser.MoveWindow(0, 0, LOWORD(lParam), HIWORD(lParam));
return 0;
}
LRESULT CMainWindow::OnActivate(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
// Notify shell of our WM_ACTIVATE message
SHHandleWMActivate(m_hWnd, wParam, lParam, &m_sai, 0);
return 0;
}
LRESULT CMainWindow::OnSettingChange(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
// Notify shell of our WM_SETTINGCHANGE message
SHHandleWMSettingChange(m_hWnd, wParam, lParam, &m_sai);
return 0;
}
// **************************************************************************
//
// WM_COMMAND handlers
//
// **************************************************************************
LRESULT CMainWindow::OnExitCommand(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
SendMessage(WM_CLOSE);
return 0;
}
LRESULT CMainWindow::OnBackCommand(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
m_spIWebBrowser2->GoBack();
return 0;
}
LRESULT CMainWindow::OnForwardCommand(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
m_spIWebBrowser2->GoForward();
return 0;
}
LRESULT CMainWindow::OnHomeCommand(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
m_spIWebBrowser2->GoHome();
return 0;
}
LRESULT CMainWindow::OnOpenURLCommand(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CGetURLDialog getURLDialog;
int nRetCode = getURLDialog.DoModal(m_hWnd); // display the 'Get URL' dialog box
if (IDOK == nRetCode)
{
m_spIWebBrowser2->Navigate(getURLDialog.GetURL(), NULL, NULL, NULL, NULL);
}
return 0;
}
LRESULT CMainWindow::OnRefreshCommand(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
m_spIWebBrowser2->Refresh();
return 0;
}
LRESULT CMainWindow::OnStopCommand(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
m_spIWebBrowser2->Stop();
return 0;
}
// **************************************************************************
//
// event handlers
//
// **************************************************************************
void __stdcall CMainWindow::OnBeforeNavigate2(IDispatch* pDisp, VARIANT * pvtURL,
VARIANT * /*pvtFlags*/, VARIANT * pvtTargetFrameName,
VARIANT * /*pvtPostData*/, VARIANT * /*pvtHeaders*/,
VARIANT_BOOL * /*pvbCancel*/)
{
USES_CONVERSION;
TCHAR szOutput[128];
StringCchPrintf(szOutput, ARRAYSIZE(szOutput),
TEXT("0x%08p %s %s"), pDisp, OLE2CT(V_BSTR(pvtURL)),
VT_ERROR != V_VT(pvtTargetFrameName) ? OLE2CT(V_BSTR(pvtTargetFrameName)) : TEXT("(error)"));
OutputDebugString(szOutput);
SetWindowText(TEXT("Untitled"));
VERIFY(SetEnabledState(IDM_STOP, TRUE));
}
void __stdcall CMainWindow::OnBrowserTitleChange(BSTR bstrTitleText)
{
USES_CONVERSION;
TCHAR szOutput[128];
StringCchPrintf(szOutput, ARRAYSIZE(szOutput),
TEXT("%s"), OLE2CT(bstrTitleText));
OutputDebugString(szOutput);
SetWindowText(OLE2CT(bstrTitleText));
}
void __stdcall CMainWindow::OnNavigateComplete2(IDispatch *pDisp, VARIANT *pvtURL)
{
USES_CONVERSION;
TCHAR szOutput[128];
StringCchPrintf(szOutput, ARRAYSIZE(szOutput),
TEXT("0x%08p %s"), pDisp, OLE2CT(V_BSTR(pvtURL)));
OutputDebugString(szOutput);
}
void __stdcall CMainWindow::OnDocumentComplete(IDispatch *pDisp, VARIANT *pvtURL)
{
USES_CONVERSION;
TCHAR szOutput[128];
StringCchPrintf(szOutput, ARRAYSIZE(szOutput),
TEXT("0x%08p %s"), pDisp, OLE2CT(V_BSTR(pvtURL)));
OutputDebugString(szOutput);
VERIFY(SetEnabledState(IDM_STOP, FALSE));
}
void __stdcall CMainWindow::OnCommandStateChange(long lCommand, BOOL bEnable)
{
if (CSC_NAVIGATEBACK == lCommand)
{
VERIFY(SetEnabledState(IDM_BACK, bEnable));
}
else if (CSC_NAVIGATEFORWARD == lCommand)
{
VERIFY(SetEnabledState(IDM_FORWARD, bEnable));
}
}
// **************************************************************************
//
// utility function
//
// **************************************************************************
BOOL CMainWindow::SetEnabledState(UINT uMenuItemID, BOOL bEnable)
{
HMENU hMenu = (HMENU)m_menuBar.SendMessage(SHCMBM_GETSUBMENU, 0, IDM_SK2_MENU);
BOOL bRetVal = EnableMenuItem(hMenu, uMenuItemID, bEnable ? MF_ENABLED : MF_GRAYED);
return (bRetVal != 0xFFFFFFFF);
}
// **************************************************************************
//
// CMainWindow::TranslateAccelerator
//
// Required to forward messages to the PIEWebBrowser control (and any other
// ActiveX controls that may be added to the main window's design).
//
// **************************************************************************
BOOL CMainWindow::TranslateAccelerator(MSG* pMsg)
{
// Accelerators are only keyboard or mouse messages
UINT uMsg = pMsg->message;
if (!(WM_KEYFIRST <= uMsg && uMsg <= WM_KEYLAST) &&
!(WM_MOUSEFIRST <= uMsg && uMsg <= WM_MOUSELAST))
{
return FALSE;
}
if (NULL == m_hWnd)
{
return FALSE;
}
// Find a direct child of this window from the window that has focus.
// This will be AtlAxWin window for the hosted control.
CWindow control = ::GetFocus();
if (IsChild(control) && m_hWnd != control.GetParent())
{
do
{
control = control.GetParent();
} while (m_hWnd != control.GetParent());
}
// Give the control (via the AtlAxWin) a chance to translate this message
if (control.m_hWnd && control.SendMessage(WM_FORWARDMSG, 0, (LPARAM)pMsg))
{
return TRUE;
}
// If the main window used accelerators, we could have called the global
// ::TranslateAccelerator() function here, instead of simply returning FALSE.
return FALSE;
}
我来回答
回答0个
时间排序
认可量排序
暂无数据
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币
Markdown 语法
- 加粗**内容**
- 斜体*内容*
- 删除线~~内容~~
- 引用> 引用内容
- 代码`代码`
- 代码块```编程语言↵代码```
- 链接[链接标题](url)
- 无序列表- 内容
- 有序列表1. 内容
- 缩进内容
- 图片![alt](url)
相关问答
-
2012-12-04 14:13:12
-
2012-12-05 14:39:10
-
2018-07-26 15:48:32
-
2017-08-18 10:58:11
-
2016-10-19 00:01:15
-
2016-10-12 21:41:00
-
2021-01-04 16:42:18
-
2020-11-21 17:16:33
-
2012-12-04 11:37:28
-
2008-07-06 21:15:38
-
2008-08-14 01:01:56
-
2012-12-04 13:07:48
-
2012-12-04 12:57:16
-
2012-12-04 11:57:10
-
2012-12-04 11:51:25
-
2010-07-19 15:15:27
-
2012-12-04 11:57:50
-
2020-12-31 15:02:41
-
2021-04-16 17:56:12
无更多相似问答 去提问
点击登录
-- 积分
-- E币
提问
—
收益
—
被采纳
—
我要提问
切换马甲
上一页
下一页
悬赏问答
-
5Hi3516CV610 如何使用SD卡升级固件
-
5cat /dev/logmpp 报错 <3>[ vi] [func]:vi_send_frame_node [line]:99 [info]:vi pic queue is full!
-
50如何获取vpss chn的图像修改后发送至vo
-
5FPGA通过Bt1120传YUV422数据过来,vi接收不到数据——3516dv500
-
50SS928 运行PQtools 拼接 推到设备里有一半画面会异常
-
53536AV100的sample_vdec输出到CVBS显示
-
10海思板子mpp怎么在vi阶段改变视频数据尺寸
-
10HI3559AV100 多摄像头同步模式
-
9海思ss928单路摄像头vio中加入opencv处理并显示
-
10EB-RV1126-BC-191板子运行自己编码的程序
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
提醒
你的问题还没有最佳答案,是否结题,结题后将扣除20%的悬赏金
取消
确认
提醒
你的问题还没有最佳答案,是否结题,结题后将根据回答情况扣除相应悬赏金(1回答=1E币)
取消
确认