void main(void) { printf "Hello World!"; } |
再次选择File菜单的New,在出现的对话框 中,选择Files栏目(新建文件),并点取其下的C++ Source File项,表示新建一个C++源文件。在右边的File栏中输入“EasyWin”,最后确定让Add to project检查框打上勾 )。单击OK按钮。
在EasyWin.cpp文件中输入以下源程序代码:
//*******************************************************************
// 工程:easywin
// 文件:easywin.cpp
// 内容:一个基本的Win32程序//*******************************************************************
#include
#include
//函数声明
BOOL InitWindow( HINSTANCE hInstance, int nCmdShow );
LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );
//*******************************************************************
//函数:WinMain()
//功能:Win32应用程序入口函数。创建主窗口,处理消息循环
//*******************************************************************
int PASCAL WinMain( HINSTANCE hInstance, //当前实例句柄
HINSTANCE hPrevInstance, //前一个实例句柄
LPSTR lpCmdLine, //命令行字符
int nCmdShow) //窗口显示方式
{
MSG msg;
//创建主窗口
if ( !InitWindow( hInstance, nCmdShow ) )
return FALSE;
//进入消息循环:
//从该应用程序的消息队列中检取消息,送到消息处理过程,
//当检取到WM_QUIT消息时,退出消息循环。
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//程序结束
return msg.wParam;
}
//******************************************************************
//函数:InitWindow()
//功能:创建窗口。
//******************************************************************
static BOOL InitWindow( HINSTANCE hInstance, int nCmdShow )
{
HWND hwnd; //窗口句柄
WNDCLASS wc; //窗口类结构
//填充窗口类结构
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( hInstance, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "EasyWin";
//注册窗口类
RegisterClass( &wc );
//创建主窗口
hwnd = CreateWindow(
"EasyWin", //窗口类名称
"一个基本的Win32程序", //窗口标题
WS_OVERLAPPEDWINDOW, //窗口风格,定义为普通型
100, //窗口位置的x坐标
100, //窗口位置的y坐标
400, //窗口的宽度
300, //窗口的高度
NULL, //父窗口句柄
NULL, //菜单句柄
hInstance, //应用程序实例句柄
NULL ); //窗口创建数据指针
if( !hwnd ) return FALSE;
//显示并更新窗口
ShowWindow( hwnd, nCmdShow );
UpdateWindow( hwnd );
return TRUE;
}
//******************************************************************
//函数:WinProc()
//功能:处理主窗口消息
//******************************************************************
LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch( message )
{
case WM_KEYDOWN://击键消息
switch( wParam )
{
case VK_ESCAPE:
MessageBox(hWnd,"ESC键按下了!","Keyboard",MB_OK);
break;
}
break;
case WM_RBUTTONDOWN://鼠标消息
{
MessageBox(hWnd,"鼠标右键按下了!","Mouse",MB_OK);
break;
}
case WM_PAINT://窗口重画消息
{
char hello[]="你好,我是EasyWin !";
HDC hdc;
PAINTSTRUCT ps;
hdc=BeginPaint( hWnd,&ps ); //取得设备环境句柄
SetTextColor(hdc, #0000ff); //设置文字颜色
TextOut( hdc, 20, 10, hello, strlen(hello) );//输出文字
EndPaint( hWnd, &ps ); //释放资源
break;
}
case WM_DESTROY://退出消息
PostQuitMessage( 0 );//调用退出函数
break;
}
//调用缺省消息处理过程
return DefWindowProc(hWnd, message, wParam, lParam);
}
int PASCAL WinMain( HINSTANCE hInstance, //当前实例句柄 HINSTANCE hPrevInstance, //前一个实例句柄 LPSTR lpCmdLine, //命令行字符 int nCmdShow) //窗口显示方式 |
typedef struct _WNDCLASS { UINT style; //窗口的风格* WNDPROC lpfnWndProc; //指定窗口的消息处理函数的远指针* int cbClsExtra; //指定分配给窗口类结构之后的额外字节数* int cbWndExtra; //指定分配给窗口实例之后的额外字节数 HANDLE hInstance; //指定窗口过程所对应的实例句柄* HICON hIcon; //指定窗口的图标 HCURSOR hCursor; //指定窗口的鼠标 HBRUSH hbrBackground; //指定窗口的背景画刷 LPCTSTR lpszMenuName; //窗口的菜单资源名称 LPCTSTR lpszClassName; //该窗口类的名称* } WNDCLASS; |
typedef struct _WNDCLASSEX { UINT cbSize; //指定WNDCLASSEX结构的大小 UINT style; WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HANDLE hInstance; HICON hIcon; HCURSOR hCursor; HBRUSH hbrBackground; LPCTSTR lpszMenuName; LPCTSTR lpszClassName; HICON hIconSm; //窗口的小图标 } WNDCLASSEX; |
风格 | 含义 |
CS_HREDRAW | 如果窗口客户区宽度发生改变,重绘整个窗口 |
CS_VREDRAW | 如果窗口客户区高度发生改变,重绘整个窗口 |
CS_DBLCLKS | 能感受用户在窗口中的双击消息 |
CS_NOCLOSE | 禁用系统菜单中的“关闭”命令 |
CS_OWNDC | 为该窗口类的各窗口分配各自独立的设备环境 |
CS_CLASSDC | 为该窗口类的各窗口分配一个共享的设备环境 |
CS_PARENTDC | 指定子窗口继承其父窗口的设备环境 |
CS_SAVEBITS | 把被窗口遮掩的屏幕图象部分作为位图保存起来。当该窗口被移动时,Windows使用被保存的位图来重建屏幕图象 |
wc.style = CS_VREDRAW | CS_HREDRAW; wc.lpfnWndProc = (WNDPROC)WinProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon( hInstance, IDI_APPLICATION ); wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground = GetStockObject(WHITE_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = "EasyWin"; |
ATOM RegisterClass( CONST WNDCLASS *lpWndClass ); ATOM RegisterClassEx( CONST WNDCLASSEX *lpwcx ); |
hwnd = CreateWindow( "EasyWin", //创建窗口所用的窗口类的名称* "一个基本的Win32程序", //窗口标题 WS_OVERLAPPEDWINDOW, //窗口风格,定义为普通型* 100, //窗口位置的x坐标 100, //窗口位置的y坐标 400, //窗口的宽度 300, //窗口的高度 NULL, //父窗口句柄 NULL, //菜单句柄 hInstance, //应用程序实例句柄* NULL ); //一般都为NULL |
风格 | 含义 |
WS_OVERLAPPEDWINDOW | 创建一个层叠式窗口,有边框、标题栏、系统菜单、最大最小化按钮,是以下几种风格的集合:WS_OVERLAPPED, WS_CAPTION, WS_SYSMENU, WS_THICKFRAME, WS_MINIMIZEBOX, WS_MAXIMIZEBOX |
WS_POPUPWINDOW | 创建一个弹出式窗口,是以下几种风格的集合: WS_BORDER,WS_POPUP,WS_SYSMENU。WS_CAPTION与WS_POPUPWINDOW风格必须一起使用才能使窗口菜单可见 |
WS_OVERLAPPED | 创建一个层叠式窗口,它有标题栏和边框,与WS_TILED风格一样 |
WS_POPUP | 该窗口为弹出式窗口,不能与WS_CHILD同时使用 |
WS_BORDER | 窗口有单线边框 |
WS_CAPTION | 窗口有标题栏 |
WS_CHILD | 该窗口为子窗口,不能与WS_POPUP同时使用 |
WS_DISABLED | 该窗口为无效,即对用户操作不产生任何反应 |
WS_HSCROLL | 窗口有水平滚动条 |
WS_ICONIC | 窗口初始化为最小化 |
WS_MAXIMIZE | 窗口初始化为最大化 |
WS_MAXIMIZEBOX | 窗口有最大化按钮 |
WS_MINIMIZE | 与WS_MAXIMIZE一样 |
WS_MINIMIZEBOX | 窗口有最小化按钮 |
WS_SIZEBOX | 边框可进行大小控制的窗口 |
WS_SYSMENU | 创建一个有系统菜单的窗口,必须与WS_CAPTION风格同时使用 |
WS_THICKFRAME | 创建一个大小可控制的窗口,与WS_SIZEBOX 风格一样. |
WS_TILED | 创建一个层叠式窗口,有标题栏 |
WS_VISIBLE | 窗口为可见 |
WS_VSCROLL | 窗口有垂直滚动条 |
BOOL ShowWindow( HWND hWnd, int nCmdShow ); |
BOOL UpdateWindow( HWND hWnd ); |
while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } |
BOOL GetMessage( LPMSG lpMsg, //指向一个MSG结构的指针,用来保存消息 HWND hWnd, //指定哪个窗口的消息将被获取 UINT wMsgFilterMin, //指定获取的主消息值的最小值 UINT wMsgFilterMax //指定获取的主消息值的最大值 ); |
typedef struct tagMSG { HWND hwnd; //接收消息的窗口句柄 UINT message; //主消息值 WPARAM wParam; //副消息值,其具体含义依赖于主消息值 LPARAM lParam; //副消息值,其具体含义依赖于主消息值 DWORD time; //消息被投递的时间 POINT pt; //鼠标的位置 } MSG; |
LRESULT CALLBACK WindowProc( HWND hwnd, //接收消息窗口的句柄 UINT uMsg, //主消息值 WPARAM wParam, //副消息值 LPARAM lParam //副消息值 ); |
调用PostQuitMessage()函数以结束消息循环
// Win32_test.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN32_TEST, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WIN32_TEST);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_WIN32_TEST);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_WIN32_TEST;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}