VC win32 Application SDK创建窗口Demo

win32.h

#ifndef _X_WIN32_H_
#define _X_WIN32_H_
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE,
int);
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
#endif

  

win32.cpp

#include <windows.h>
#include
"win32.h"
HINSTANCE g_hInst;
HWND g_hWnd;
char g_szAppName[]="WIN32Sample";
char g_szTitle[]="Win32 Sample Application";
int CALLBACK WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd )
{
MSG msg;
UNREFERENCED_PARAMETER(lpCmdLine);
//to avoid compile warnning
if(!hPrevInstance)
{
if (!InitApplication(hInstance))
{
return FALSE;
}
}
if(!InitInstance(hInstance,nShowCmd))
{
return FALSE;
}
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(
&msg);
DispatchMessage(
&msg);
}
return msg.wParam;//PostQuitMessage's wParam
}
BOOL InitApplication( HINSTANCE hInstance )
{
WNDCLASS wc;
wc.cbClsExtra
=0;
wc.cbWndExtra
=0;
wc.hbrBackground
=(HBRUSH)GetStockObject(WHITE_BRUSH);;
wc.hCursor
=LoadCursor(NULL,IDC_ARROW);
wc.hIcon
=LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance
=hInstance;
wc.lpfnWndProc
=(WNDPROC)WndProc;
wc.lpszClassName
=g_szAppName;
wc.lpszMenuName
=NULL;
wc.style
=0;
return RegisterClass(&wc);
}
BOOL InitInstance( HINSTANCE hInstance,
int nShowCmd)
{
g_hInst
=hInstance;

g_hWnd
= CreateWindow(g_szAppName, g_szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

if(!g_hWnd)
{
return FALSE;
}
ShowWindow(g_hWnd,nShowCmd);
//show window
UpdateWindow(g_hWnd);//send WM_PAINT to msgQue
return TRUE;
}
LRESULT CALLBACK WndProc( HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(
0);//wnd destoryed,application will quit soon
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);//pass to default wndproc
}
return 0;
}

  

更多请参考:

http://www.ibm.com/developerworks/cn/linux/l-cn-guimigrt/index4.html?ca=drs-cn#N1019A

转载于:https://www.cnblogs.com/oyjj/archive/2011/06/06/2132860.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在VC2010中编写Win32窗口应用程序的基本步骤: 1. 打开VC2010,选择“新建项目”。 2. 在“新建项目”对话框中,选择“Win32控制台应用程序”并命名您的项目。 3. 在“应用程序类型”对话框中,选择“Windows应用程序”并勾选“空项目”选项。 4. 单击“确定”按钮创建项目。 5. 在“解决方案资源管理器”中,右键单击“源文件”文件夹并选择“添加”>“新建项”。 6. 在“添加新项”对话框中,选择“C++文件”并命名您的文件。 7. 在您的源文件中,编写WinMain函数和窗口过程函数。 8. 在您的WinMain函数中,调用CreateWindow函数创建窗口。 9. 在您的窗口过程函数中,处理您需要处理的窗口消息。 10. 在您的窗口过程函数中,处理WM_DESTROY消息并调用PostQuitMessage函数以退出应用程序。 11. 编译并运行您的应用程序。 以下是一个简单的Win32窗口应用程序示例: ```c++ #include <Windows.h> LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // 注册窗口类 WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = "MyWindowClass"; RegisterClass(&wc); // 创建窗口 HWND hWnd = CreateWindow("MyWindowClass", "Win32 Window Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL); // 显示窗口 ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); // 消息循环 MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); TextOut(hdc, 10, 10, "Hello, Win32 Window Application!", strlen("Hello, Win32 Window Application!")); EndPaint(hWnd, &ps); break; } case WM_DESTROY: { PostQuitMessage(0); break; } default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } ``` 这个示例创建一个简单的窗口,并在窗口中绘制一些文本。请注意,这只是一个简单的示例,您可以根据您的需要进行更改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值