#include <stdio.h> #include <Windows.h> void __cdecl OutputDebugPrintf(const char* format, ...) { va_list vlArgs; char* strBuffer = (char*)GlobalAlloc(GPTR, 4096); va_start(vlArgs, format); //_vsnprintf(strBuffer, 4096 - 1, format, vlArgs); int size = strlen(strBuffer) - 1; _vsnprintf_s(strBuffer, size, 4096 - 1, format, vlArgs); va_end(vlArgs); //strcat(strBuffer, "\n"); strcat_s(strBuffer, size, "\n");//这里字符串必须以 '\n' 结尾 OutputDebugStringA(strBuffer); GlobalFree(strBuffer); return; } //自定义的窗口过程 LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { switch (Msg) { case WM_DESTROY: OutputDebugPrintf("WM_DESTROY"); PostQuitMessage(0); return 0; default: return DefWindowProc(hWnd, Msg, wParam, lParam); } return 0; } int WINAPI WinMain( _In_ HINSTANCE hInstance, // 窗口句柄 _In_opt_ HINSTANCE hPrevInstance, // 上一个窗口类的句柄 _In_ LPSTR lpCmdLine, // 命令行参数 _In_ int nShowCmd // 窗口的显示状态 ) { WNDCLASSW wnd = { CS_HREDRAW, MyWindowProc, 0,0, hInstance, LoadIcon(NULL, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW), (HBRUSH)(GetStockObject(WHITE_BRUSH)), NULL,L"MyWindow" }; RegisterClass(&wnd); HWND hWnd = CreateWindow(L"MyWindow", L"newWindow", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, nShowCmd); UpdateWindow(hWnd); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); //翻译消息 DispatchMessage(&msg); //分发消息到窗口过程 } OutputDebugPrintf("exit"); exit(0); return 0; }
创建出来:
添加按钮:
CreateWindow(TEXT("button"),//window预定义窗口类名 TEXT("Button"),//名称 WS_CHILD | WS_VISIBLE | WS_TABSTOP,//属性 0, 0, 100, 30, //left、top、width、height (位置、大小) hwnd,//父窗口句柄 (HMENU)1,//id hInstance,//程序句柄 NULL);