📄 win32.cpp
字号:
// win32.cpp : Defines the entry point for the application.
//
#include "stdafx.h"//包含有windows.h头文件
#include "resource.h"//资源头文件,包含有各个资源的ID号。
#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:
BOOL MyRegisterClass(HINSTANCE hInstance);//注册窗口类
BOOL InitInstance(HINSTANCE, int); //初始化程序
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //主窗口函数
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM); //两个都是CALLBACK(回调)函数
//----------------------------------------------------------------------
//程序的入口点WinMain函数
//----------------------------------------------------------------------
int CALLBACK WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg; //Windows的消息结构
HACCEL hAccelTable; //加速键表句柄(handle);
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN32, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);//注册窗口类
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow)) //实例的初始化
{
return FALSE;
}
//装入加速键表。
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WIN32);
// 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.
//
//----------------------------------------------------------------------
//注册窗口类,在产生窗口之前必须注册窗口类。
//如同开一个公司之前,先要登记注册一样。
//----------------------------------------------------------------------
BOOL MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW; //注册窗口的风格。
wcex.lpfnWndProc = (WNDPROC)WndProc; //注册窗口函数
wcex.cbClsExtra = 0; //为窗口类额外分配的字节(一般为0)
wcex.cbWndExtra = 0; //为窗口实例额外分配的字节(一般为0)
wcex.hInstance = hInstance; //实例句柄
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_WIN32);//大图标
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);//光标
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);//窗口背景颜色
wcex.lpszMenuName = (LPCSTR)IDC_WIN32;//窗口的菜单名称(与类名一致)
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, //窗口位置x,用此参数会??
0,//窗口位置y。为什么为0??
CW_USEDEFAULT,//窗口宽度
0, //窗口高度。为什么为0??
NULL,//窗口的父窗口指针
NULL,//菜单指针
hInstance,//实例指针
NULL);//窗口创建参数
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);//显示窗口
UpdateWindow(hWnd);//更新窗口,即通过发送WM_PAINT消息实现。
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:
//注意:不论什么消息,都必须被处理。
//未被处理的消息由Windows系统缺省的窗口函数调用。
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:
//未被处理的消息由Windows系统缺省的窗口函数调用。
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;//FALSE表示没有处理这个消息。
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -