1. 先要有 webbrowser2.cpp/.h 檔
2. 在Webbrowser的Dialog加上下列code
BEGIN_MESSAGE_MAP(CWebDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_SIZE()
ON_COMMAND(ID_DEMO_PRINTPREVIEW, OnPrintpreview)
ON_COMMAND(ID_DEMO_PAGESETUP, OnPagesetup)
END_MESSAGE_MAP()
// CWebDlg 訊息處理常式
BOOL CWebDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CString csAddress("192.168.81.229");
COleVariant vtEmpty;
Web.Navigate(csAddress, &vtEmpty, &vtEmpty, &vtEmpty, &vtEmpty);
ReSize();
return TRUE;
}
void CWebDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if (!((nID & 0xFFF0) == IDM_ABOUTBOX))
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CWebDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CWebDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CWebDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
ReSize();
}
void CWebDlg::ReSize()
{
if (::IsWindow(Web.m_hWnd))
{
CRect rect;
GetClientRect(rect);
Web.MoveWindow(rect);
}
}
void CWebDlg::OnPrintpreview()
{
// Verify the Web Browser control is valid.
LPDISPATCH lpDispApp = Web.GetApplication();
if(lpDispApp)
{
// Get the HTMLDocument interface.
LPDISPATCH lpDispDoc = Web.GetDocument();
if (lpDispDoc != NULL)
{
// Get the IOleCommandTarget interface so that we can dispatch the command.
LPOLECOMMANDTARGET lpTarget = NULL;
if (SUCCEEDED(lpDispDoc->QueryInterface(IID_IOleCommandTarget, (LPVOID*) &lpTarget)))
{
// Execute the print preview command. The control will handle the print preview GUI.
// OLECMDID_PRINTPREVIEW is defined in "docobj.h".
lpTarget->Exec(NULL, OLECMDID_PRINTPREVIEW, 0, NULL, NULL);
lpTarget->Release();
}
lpDispDoc->Release();
}
lpDispApp->Release();
}
}
void CWebDlg::OnPagesetup()
{
Web.ExecWB(OLECMDID_PAGESETUP, OLECMDEXECOPT_PROMPTUSER, NULL, NULL);
}