win32程序显示网页
2017-08-02 13:25
337 查看
说是win32,实际程序中使用了atl的东西。所以新建win32程序时,记得勾选使用atl:
![](https://img-blog.csdn.net/20170802132455214?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYW5hbG9nb3VzX2xvdmU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
代码如下:
#include "stdafx.h"
#include "Win32Project3.h"
#include <atlbase.h>
CComModule _Module;
#include <atlwin.h>
//#pragma comment(lib,"atl.lib")
#include <exdisp.h>
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
CoInitialize(NULL);
AtlAxWinInit();
// 初始化全局字符串
//LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
//LoadString(hInstance, IDC_IE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
//hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_IE));
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
//if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
CoUninitialize();
return (int)msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
// 注释:
//
// 仅当希望
// 此代码与添加到 Windows 95 中的“RegisterClassEx”
// 函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
// 这样应用程序就可以获得关联的
// “格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, IDC_ARROW);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = _T("IETEst");
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(_T("IETEst"), TEXT("The Hello Program"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static CAxWindow WinContainer;
static CComPtr<IWebBrowser2> WebBrowser;
switch (message)
{
case WM_CREATE:
RECT rc;
GetClientRect(hWnd, &rc);
HRESULT hr;
WinContainer.Create(hWnd, rc, _T("shell.Explorer.2"), WS_CHILD | WS_VISIBLE);
hr = WinContainer.QueryControl(__uuidof(IWebBrowser2), (void**)&WebBrowser);
if (FAILED(hr))
{
return -1L;
}
VARIANT url;
VARIANT v;
url.vt = VT_BSTR;
url.bstrVal = SysAllocString(_T("http://www.baidu.com"));
WebBrowser->Navigate2(&url, &v, &v, &v, &v);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
WebBrowser.Release();
WinContainer.DestroyWindow();
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
参考:http://bbs.csdn.net/topics/330142742
代码如下:
#include "stdafx.h"
#include "Win32Project3.h"
#include <atlbase.h>
CComModule _Module;
#include <atlwin.h>
//#pragma comment(lib,"atl.lib")
#include <exdisp.h>
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
CoInitialize(NULL);
AtlAxWinInit();
// 初始化全局字符串
//LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
//LoadString(hInstance, IDC_IE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
//hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_IE));
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
//if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
CoUninitialize();
return (int)msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
// 注释:
//
// 仅当希望
// 此代码与添加到 Windows 95 中的“RegisterClassEx”
// 函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
// 这样应用程序就可以获得关联的
// “格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, IDC_ARROW);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = _T("IETEst");
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(_T("IETEst"), TEXT("The Hello Program"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static CAxWindow WinContainer;
static CComPtr<IWebBrowser2> WebBrowser;
switch (message)
{
case WM_CREATE:
RECT rc;
GetClientRect(hWnd, &rc);
HRESULT hr;
WinContainer.Create(hWnd, rc, _T("shell.Explorer.2"), WS_CHILD | WS_VISIBLE);
hr = WinContainer.QueryControl(__uuidof(IWebBrowser2), (void**)&WebBrowser);
if (FAILED(hr))
{
return -1L;
}
VARIANT url;
VARIANT v;
url.vt = VT_BSTR;
url.bstrVal = SysAllocString(_T("http://www.baidu.com"));
WebBrowser->Navigate2(&url, &v, &v, &v, &v);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
WebBrowser.Release();
WinContainer.DestroyWindow();
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
参考:http://bbs.csdn.net/topics/330142742
相关文章推荐
- vs2012编译生成的程序在xp系统中显示不是有效的win32程序解决方法
- Win32 SDK中窗口全屏处理 用window api实现程序全屏显示
- 如何改变Win32 Console 程序显示颜色
- Win32 Api(显示程序系统菜单)
- 在Win32程序中显示Dos调试窗口
- 网页中实现一个计算当年还剩多少时间的倒数计时程序,要求网页上实时动态显示"××年还剩××天××时××分××秒"
- Win32窗口程序显示Console
- 在Win32程序中显示Dos调试窗口
- asp.net 读取一个文本文件,并输出到网页显示 通过 一般处理程序实现
- IIS上部署.net 网站,显示信息:此程序无法显示网页
- Win32 Api(显示程序系统菜单)
- 小程序web-view动态显示h5页面,根据不同条件显示不同网页——微信web-view高级用法13
- C访问hadoop程序终端显示运行正确,因为连接参数错误,使得通过网页查看就是没有成功原因分析和解决方案
- 网页中实现一个计算当年还剩多少时间的倒数计时程序,要求网页上实时动态显示“××年还剩××天××时××分××秒”
- vs开发的程序在xp下显示 xxx.exe不是有效的win32程序问题解决
- 关于Unity程序在IOS和Android上显示内嵌网页的方式
- 在gtk程序中显示网页
- 解决fdt5“此程序无法显示网页”问题
- VS2013打包程序后在其他电脑显示(null)不是有效的win32应用程序的问题
- day_6 在sevlet网页程序中显示图片、数据库表格,居中排版,分页算法以及上一页下一页