/* -------------------------------------------------------------------
MyWindows.c -- 基本窗口模型
《Windows 程序设计(SDK)》视频教程
--------------------------------------------------------------------*/
#include <windows.h>
#include <strsafe.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
TCHAR str_buf[2048];
int str_buf_cnt = 0; // 统计str_buf的元素个数
int current_location = 0; // 字符串在str_buf中的当前位置
int str_len = 0; // 字符串长度
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MyWindows");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
TEXT("鱼C工作室"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
StringCchPrintf(&str_buf[current_location], 512, L"(%d)WinMain函数中的hwnd是:%d\n", str_buf_cnt++, hwnd);
StringCchLength(&str_buf[current_location], 512, &str_len);
current_location += str_len;
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
StringCchPrintf(&str_buf[current_location], 512, L"(%d)WinMain函数中的msg是:%d\n", str_buf_cnt++, msg);
StringCchLength(&str_buf[current_location], 512, &str_len);
current_location += str_len;
StringCchPrintf(&str_buf[current_location], 512, L"(%d)WinMain函数中的msg中的hwnd是:%d\n", str_buf_cnt++, msg.hwnd);
StringCchLength(&str_buf[current_location], 512, &str_len);
current_location += str_len;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
StringCchPrintf(&str_buf[current_location], 512, L"(%d)WndProc函数中的hwnd是:%d\n", str_buf_cnt++, hwnd);
StringCchLength(&str_buf[current_location], 512, &str_len);
current_location += str_len;
if(str_buf_cnt >= 60) // 小心数组越界
{
HANDLE hFile = CreateFile(L"text.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
//WriteFile(hFile, str_buf, 2048, NULL, NULL); // 这样不行,打开test.txt文件乱码
for(int i = 0; i < 2048; i++)
{
if((*(UINT16 *)(&str_buf[i]) & 0xff00) == 0)
{
WriteFile(hFile, &str_buf[i], 1, NULL, NULL);
}
else
{
WriteFile(hFile, &str_buf[i], 2, NULL, NULL);
}
}
CloseHandle(hFile);
ExitProcess(0);
}
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
DrawText(hdc, TEXT("大家好,这是我的第一个窗口程序!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
这是我运行的一次结果