## 第一章:WinAPI基础入门
### 1.1 WinAPI概述
Windows API(WinAPI)是微软Windows操作系统的核心应用程序编程接口,为开发者提供了访问系统功能的底层接口。WinAPI主要包含以下几个核心组件:
- **User32.dll**:负责窗口管理、消息传递和用户界面
- **GDI32.dll**:图形设备接口,处理图形绘制
- **Kernel32.dll**:提供核心系统服务如内存管理、进程/线程控制
### 1.2 第一个WinAPI程序
```c
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 1. 注册窗口类
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = {0};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// 2. 创建窗口
HWND hwnd = CreateWindowEx(
0, // 扩展样式
CLASS_NAME, // 窗口类
L"Learn WinAPI", // 窗口标题
WS_OVERLAPPEDWINDOW, // 窗口样式
// 位置和大小
CW_USEDEFAULT, CW_USEDEFAULT,
800, 600,
NULL, // 父窗口
NULL, // 菜单
hInstance, // 实例句柄
NULL // 附加数据
);
if (hwnd == NULL)
{
return 0;
}
// 3. 显示窗口
ShowWindow(hwnd, nCmdShow);
// 4. 消息循环
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// 在此处绘制
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
```
## 第二章:窗口与消息机制
### 2.1 窗口创建流程详解
1. **注册窗口类**:定义窗口的行为和外观特征
2. **创建窗口实例**:根据注册的类创建具体窗口
3. **消息循环**:处理系统发送给窗口的消息
### 2.2 常见消息类型
- **WM_CREATE**:窗口创建时发送
- **WM_SIZE**:窗口大小改变时发送
- **WM_PAINT**:需要重绘窗口内容时发送
- **WM_CLOSE**:窗口即将关闭时发送
- **WM_DESTROY**:窗口销毁前发送
## 第三章:图形绘制与GDI
### 3.1 基本图形绘制
```c
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// 绘制矩形
Rectangle(hdc, 50, 50, 200, 200);
// 绘制文本
TextOut(hdc, 100, 100, L"Hello, WinAPI!", 13);
// 绘制线条
MoveToEx(hdc, 250, 250, NULL);
LineTo(hdc, 400, 400);
EndPaint(hwnd, &ps);
}
return 0;
```
### 3.2 高级图形技术
- 双缓冲技术
- 路径绘制
- 区域操作
- 位图处理
## 第四章:用户界面控件
### 4.1 标准控件创建
```c
// 创建按钮
HWND hButton = CreateWindow(
L"BUTTON", // 预定义类
L"Click Me", // 按钮文本
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
10, 10, 100, 30, // 位置和大小
hwnd, // 父窗口
(HMENU)1, // 控件ID
hInstance,
NULL
);
// 创建编辑框
HWND hEdit = CreateWindow(
L"EDIT",
L"",
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL,
120, 10, 200, 25,
hwnd,
(HMENU)2,
hInstance,
NULL
);
```
### 4.2 控件消息处理
```c
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
switch (wmId)
{
case 1: // 按钮ID
MessageBox(hwnd, L"Button clicked!", L"Info", MB_OK);
break;
}
}
break;
```
## 第五章:文件与系统操作
### 5.1 文件操作API
```c
// 创建文件
HANDLE hFile = CreateFile(
L"example.txt",
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hFile != INVALID_HANDLE_VALUE)
{
const char* data = "Hello, WinAPI File System!";
DWORD bytesWritten;
WriteFile(hFile, data, strlen(data), &bytesWritten, NULL);
CloseHandle(hFile);
}
// 读取文件
hFile = CreateFile(
L"example.txt",
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hFile != INVALID_HANDLE_VALUE)
{
char buffer[256];
DWORD bytesRead;
ReadFile(hFile, buffer, sizeof(buffer), &bytesRead, NULL);
buffer[bytesRead] = '\0';
CloseHandle(hFile);
// 将ANSI转换为Unicode显示
wchar_t wbuffer[256];
MultiByteToWideChar(CP_ACP, 0, buffer, -1, wbuffer, 256);
MessageBox(hwnd, wbuffer, L"File Content", MB_OK);
}
```
### 5.2 注册表操作
```c
// 写入注册表
HKEY hKey;
if (RegCreateKeyEx(HKEY_CURRENT_USER,
L"Software\\MyApp",
0, NULL, 0,
KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
{
DWORD value = 1234;
RegSetValueEx(hKey, L"Setting1", 0, REG_DWORD,
(const BYTE*)&value, sizeof(value));
RegCloseKey(hKey);
}
// 读取注册表
if (RegOpenKeyEx(HKEY_CURRENT_USER,
L"Software\\MyApp", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
DWORD value;
DWORD size = sizeof(value);
DWORD type;
if (RegQueryValueEx(hKey, L"Setting1", NULL, &type,
(LPBYTE)&value, &size) == ERROR_SUCCESS)
{
wchar_t msg[256];
wsprintf(msg, L"Value from registry: %d", value);
MessageBox(hwnd, msg, L"Registry Value", MB_OK);
}
RegCloseKey(hKey);
}
```
## 第六章:多线程编程
### 6.1 线程创建与管理
```c
DWORD WINAPI ThreadFunction(LPVOID lpParam)
{
int threadNum = *(int*)lpParam;
for (int i = 0; i < 5; i++)
{
wchar_t msg[256];
wsprintf(msg, L"Thread %d: Count %d", threadNum, i);
OutputDebugString(msg);
Sleep(1000);
}
return 0;
}
void CreateThreadsExample()
{
int threadData1 = 1;
int threadData2 = 2;
HANDLE hThread1 = CreateThread(
NULL, // 默认安全属性
0, // 默认堆栈大小
ThreadFunction, // 线程函数
&threadData1, // 传递给线程的参数
0, // 默认创建标志
NULL // 不需要线程ID
);
HANDLE hThread2 = CreateThread(
NULL,
0,
ThreadFunction,
&threadData2,
0,
NULL
);
// 等待线程结束
WaitForSingleObject(hThread1, INFINITE);
WaitForSingleObject(hThread2, INFINITE);
CloseHandle(hThread1);
CloseHandle(hThread2);
}
```
### 6.2 线程同步技术
```c
// 使用临界区
CRITICAL_SECTION cs;
int sharedCounter = 0;
DWORD WINAPI ThreadWithSync(LPVOID lpParam)
{
for (int i = 0; i < 1000; i++)
{
EnterCriticalSection(&cs);
sharedCounter++;
LeaveCriticalSection(&cs);
}
return 0;
}
void SyncExample()
{
InitializeCriticalSection(&cs);
HANDLE threads[10];
for (int i = 0; i < 10; i++)
{
threads[i] = CreateThread(NULL, 0, ThreadWithSync, NULL, 0, NULL);
}
WaitForMultipleObjects(10, threads, TRUE, INFINITE);
for (int i = 0; i < 10; i++)
{
CloseHandle(threads[i]);
}
DeleteCriticalSection(&cs);
wchar_t msg[256];
wsprintf(msg, L"Final counter value: %d", sharedCounter);
MessageBox(NULL, msg, L"Thread Sync Result", MB_OK);
}
你们还知道什么winAPI知识?在评论区谈谈吧!