将编辑框的内容用消息对话框显示出来的问题
#include<windows.h>
#include<stdio.h>
#include<string.h>
char str[80];
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WndProc;
wndclass.lpszClassName = "text1";
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,"窗口注册失败!","text1Win",0);
return 0;
}
hwnd = CreateWindow("text1",
"Application窗口",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,nCmdShow);
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)
{
HDC hdc;
PAINTSTRUCT ps;
static HWND hButton, hwndEdit[1];
switch(message)
{
case WM_CREATE:
hwndEdit[0]= CreateWindow("edit",NULL,WS_CHILD | WS_VISIBLE | WS_BORDER,20,70,100,25,hwnd,NULL,NULL,NULL);
hButton = CreateWindow("button","确定",WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,130,70,80,25,hwnd,NULL,NULL,NULL);
return 0;
case WM_COMMAND:
if(((HWND)lParam == hButton)&&(HIWORD(wParam) == BN_CLICKED))
{
SetWindowText(hwndEdit[1], "edit");
GetWindowText(hwndEdit[1],str,80);
printf(str,str);
MessageBox(NULL,str,"您输入的为:",0);
}
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
TextOut(hdc,10,10," ",50);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
这个是我编写的程序。
题目是在主窗口中创建一个编辑框和一个按钮,单击按钮后,将编辑框的内容用消息对话框显示出来,而且编辑框上的内容是自己随意输入的,不能是固定的
[解决办法]
- C/C++ code
#include<windows.h>#include<stdio.h>#include<string.h>char str[80];LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state ){ HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.hCursor = LoadCursor(NULL,IDC_ARROW); wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION); wndclass.hInstance = hInstance; wndclass.lpfnWndProc = WndProc; wndclass.lpszClassName = "text1"; wndclass.lpszMenuName = NULL; wndclass.style = CS_HREDRAW | CS_VREDRAW; if(!RegisterClass(&wndclass)) { MessageBox(NULL,"窗口注册失败!","text1Win",0); return 0; } hwnd = CreateWindow("text1", "Application窗口", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd,nCmdShow); 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){ HDC hdc; PAINTSTRUCT ps; static HWND hButton, hwndEdit; switch(message) { case WM_CREATE: hwndEdit= CreateWindow("edit",NULL,WS_CHILD | WS_VISIBLE | WS_BORDER,20,70,100,25,hwnd,NULL,NULL,NULL); hButton = CreateWindow("button","确定",WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,130,70,80,25,hwnd,NULL,NULL,NULL); break; case WM_COMMAND: if(((HWND)lParam == hButton)&&(HIWORD(wParam) == BN_CLICKED)) { //SetWindowText(hwndEdit, "edit"); GetWindowText(hwndEdit,str,80); //printf(str,str); MessageBox(NULL,str,"您输入的为:",MB_OK); } break; case WM_PAINT: hdc = BeginPaint(hwnd,&ps); //TextOut(hdc,10,10," ",50); EndPaint(hwnd,&ps); return 0; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProc(hwnd,message,wParam,lParam); }