按钮
#include<windows.h> #include <stdio.h> #include <stdarg.h> #include <ctype.h> HINSTANCE hAppInstance; LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void __cdecl OutputDebugStringF(const char* format, ...) { va_list vlArgs; char* strBuffer = (char*)GlobalAlloc(GPTR, 4096); va_start(vlArgs, format); _vsnprintf(strBuffer, 4096 - 1, format, vlArgs); va_end(vlArgs); strcat(strBuffer, "\n"); OutputDebugStringA(strBuffer); GlobalFree(strBuffer); return; } void CreateButton(HWND hwnd) { HWND hwndPushButton; HWND hwndCheckBox; HWND hwndRadio; hwndPushButton = CreateWindow( TEXT("button"), TEXT("普通按钮"), //WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_DEFPUSHBUTTON, WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_DEFPUSHBUTTON, 10, 10, 80, 20, hwnd, (HMENU)1001, //子窗口ID hAppInstance, NULL); TCHAR szBuffer[0x20]; GetClassName(hwndPushButton, szBuffer, 0x20); WNDCLASS wc; GetClassInfo(hAppInstance, szBuffer, &wc); OutputDebugStringF("-->%s\n", wc.lpszClassName); OutputDebugStringF("-->%x\n", wc.lpfnWndProc); hwndCheckBox = CreateWindow( TEXT("button"), TEXT("复选框"), //WS_CHILD | WS_VISIBLE | BS_CHECKBOX | BS_AUTOCHECKBOX, WS_CHILD | WS_VISIBLE | BS_CHECKBOX | BS_AUTOCHECKBOX, 10, 40, 80, 20, hwnd, (HMENU)1002, //子窗口ID hAppInstance, NULL); hwndRadio = CreateWindow( TEXT("button"), TEXT("单选按钮"), //WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON | BS_AUTORADIOBUTTON, WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON , 10, 70, 80, 20, hwnd, (HMENU)1003, //子窗口ID hAppInstance, NULL); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { HWND hwnd; static TCHAR szAppName[] = TEXT("Window Base App"); MSG msg; WNDCLASS wndclass; hAppInstance = hInstance; 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("This program requires Windows NT!"), szAppName, MB_ICONERROR); return 0; } hwnd = CreateWindow( szAppName, TEXT("My Window App!"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL ); CreateButton(hwnd); ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_COMMAND: { switch (LOWORD(wParam)) { case 1001: MessageBox(hwnd, TEXT("Hello Button 1"), TEXT("Demo"), MB_OK); return 0; case 1002: MessageBox(hwnd, TEXT("Hello Button 2"), TEXT("Demo"), MB_OK); return 0; case 1003: MessageBox(hwnd, TEXT("Hello Button 3"), TEXT("Demo"), MB_OK); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); } } return DefWindowProc(hwnd, message, wParam, lParam); }
按钮是系统已经预定义的,所以可以直接创建。并且