创建按钮一般是在 WM_CREATE消息的响应中创建子窗口 创建控件 创建按钮,创建单选框,复选框 等等
按钮的创建:
创建按钮第一个参数(类名)必须是 button 不区分大小写
case WM_CREATE: { CreateWindow("button", "窗口标题", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 15, 5, 100, 50, hwnd, (HMENU)111, ((LPCREATESTRUCT)LParam)->hInstance, NULL); CreateWindow("button", "窗口标题", WS_CHILD | WS_VISIBLE | BS_FLAT, 15, 100, 100, 50, hwnd, (HMENU)111, ((LPCREATESTRUCT)LParam)->hInstance, NULL); CreateWindow("button", "窗口标题", WS_CHILD | WS_VISIBLE | BS_PUSHLIKE, 15, 200, 100, 50, hwnd, (HMENU)111, ((LPCREATESTRUCT)LParam)->hInstance, NULL); break; }
事件的响应:
按钮控件会向父窗口发送命令消息WM_COMMAND:
BN_CLICKED:用户在按钮上单击鼠标时会向父窗口发送BN_CLICKED消息
BN_DOUBLECLICKED:双击鼠标时会向父窗口发送BN_DOUBLECLICKED消息
//以下是窗口过程其中一个分支 case WM_COMMAND: { WORD id = LOWORD(wParam);//控件id WORD code = HIWORD(wParam);//事件,是双击还是单击还是.... HWND hCtrl = (HWND)LParam; if (id==1001 && code==BN_CLICKED) //控件id=1001 且 事件=单击 { MessageBox(hwnd, "按钮1被单击", "提示", MB_OK); } else if (id == 1002 && code == BN_CLICKED) { MessageBox(hwnd, "按钮2被单击", "提示", MB_OK); } else if (id == 1003) { if (SendMessage(hCtrl, BM_GETCHECK, 0, 0)==BST_CHECKED) { MessageBox(hwnd, "复选框被选中", "提示", MB_OK); } else { MessageBox(hwnd, "复选框未选中", "提示", MB_OK); } } break; }