如何在MFC的Win32 Console Application生成的窗口中添加按钮
我新建了一个Win32 Console Application工程,注册并新建了一个窗口,也添加了菜单,可以通过点击菜单实现功能,但我想要新建一个按钮,点击按钮实现功能应该怎么做呢,希望各位提供一些思路,散分散分~~~
求大大解决俩个问题:
1.如何在2008显示
2.如何添加ID绑定
程序代码:
#include <windows.h> #include <string.h> #include <stdio.h> #include "resource.h"//菜单IDR_MENU1包含在这里面 LRESULT CALLBACK WindowsFunc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); HWND hwndButton; HWND hWnd; HINSTANCE hInst; TCHAR szWinName[]="MyWin";//窗口类名 TCHAR str[255]="";//保存输出的字符串 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) { //定义一个windows类 hInst=hInstance; WNDCLASSEX wcl; wcl.style=CS_HREDRAW|CS_VREDRAW; wcl.style&=~CS_VREDRAW; wcl.lpfnWndProc=WindowsFunc; wcl.cbClsExtra=0; wcl.cbWndExtra=0; wcl.hInstance=hInstance; wcl.hIcon=LoadIcon(NULL,IDI_APPLICATION); wcl.hIconSm=LoadIcon(NULL,IDI_INFORMATION); wcl.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wcl.hCursor=LoadCursor(NULL,IDC_ARROW); wcl.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1); wcl.lpszClassName=szWinName; wcl.cbSize=sizeof(WNDCLASSEX); //注册这个窗体 if (!RegisterClassEx(&wcl)) return 0; hWnd=CreateWindow( szWinName, " Processing WM_CHAR Message", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_DESKTOP, NULL, hInstance, NULL); //显示这个窗体 ShowWindow(hWnd,SW_SHOWNORMAL); UpdateWindow(hWnd); MSG msg;//定义消息结构体 //创建消息循环 while (GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WindowsFunc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) { HDC hdc; switch (message) { case WM_CHAR://按键消息处理 hdc=GetDC(hWnd);//获得设备上下文 strcpy(str," "); TextOut(hdc,1,1,str,strlen(str));//擦除原有字符 sprintf(str,"%c",(char)wParam);//把字符转换成字符串 TextOut(hdc,1,1,str,strlen(str));//输出字符 ReleaseDC(hWnd,hdc);//释放设备描述表 break; case WM_LBUTTONDOWN: hdc=GetDC(hWnd);//获得设备上下文 strcpy(str," "); TextOut(hdc,1,1,str,strlen(str));//擦除原有字符 sprintf(str,"Mouse");//把字符转换成字符串 TextOut(hdc,1,1,str,strlen(str));//输出字符 ReleaseDC(hWnd,hdc);//释放设备描述表 break; case WM_CREATE://这里创建一个按钮,这里没有用到ID_BUTTON绑定 hwndButton=CreateWindow("Button","OK",WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,100,100,100,100,hWnd,NULL,hInst,NULL); ShowWindow(hwndButton,SW_SHOWNORMAL); break; case WM_COMMAND: //请问如何响应这个按钮?ID_BUTTON这个宏存在定义了,但创建的时候又如何绑定呢? /*switch(LOWORD(wParam)) { case ID_BUTTON: MessageBox("hello"); break; }*/ break; case WM_DESTROY://终止应用程序 PostQuitMessage(0); break; default: return DefWindowProc(hWnd,message,wParam,lParam); } return 0; }