以下是代码:经过调试发现执行GetWindowText(hwndEdit ,szBuffer,100);后,szBuffer中没有内容。
#include <windows.h>
#include<stdio.h>LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK ChildCardProc(HWND, UINT, WPARAM, LPARAM) ;TCHAR szChildClass[] = TEXT("card_child");
#define ID_BUTCARD 0
#define ID_CARDNEW 1
#define ID_TEXT 2
#define ID_EDIT 3
#define ID_BUTBACK 4
#define ID_BUTSAVE 5static HWND hwndcard;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("card") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ; 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 ;
}
wndclass.lpfnWndProc = ChildCardProc ;
// wndclass.cbWndExtra = sizeof(long);
wndclass.hIcon = 0 ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszClassName = szChildClass ; RegisterClass(&wndclass);
hwnd = CreateWindow (szAppName, // window class name
TEXT ("申请"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
400, // initial x size
600, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
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)
{
HDC hdc ;
PAINTSTRUCT ps ;
HWND cardnew;
switch (message)
{
case WM_CREATE:
hwndcard=CreateWindow(TEXT("button"),TEXT("新帐户"),
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
20,
20,
100,
30,
hwnd,
(HMENU)ID_BUTCARD,
((LPCREATESTRUCT)lParam)->hInstance,
NULL);
return 0 ; case WM_COMMAND:
cardnew=CreateWindow(szChildClass,NULL, WS_CHILDWINDOW|WS_VISIBLE,
0,0,400,570,
hwnd,
(HMENU)ID_CARDNEW ,
(HINSTANCE) GetWindowLong( hwnd,GWL_HINSTANCE),
NULL);
ShowWindow (hwndcard, SW_HIDE) ;
return 0;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}LRESULT CALLBACK ChildCardProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
HWND hwndText,hwndEdit,hButback;
static TCHAR szBuffer[100];
int iLength;
FILE *fp; switch(message)
{
case WM_CREATE:
hwndText = CreateWindow (TEXT ("static"), TEXT("姓名"),
WS_CHILD | WS_VISIBLE | SS_CENTER ,
20, 20,
50, 20,
hwnd, (HMENU) ID_TEXT,
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;
hwndEdit = CreateWindow (TEXT ("edit"), NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT ,
100, 20,
150, 20,
hwnd, (HMENU) ID_EDIT,
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;
hButback=CreateWindow(TEXT("button"),TEXT("back"),
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
300,
540,
100,
30,
hwnd,
(HMENU) ID_BUTBACK,
((LPCREATESTRUCT)lParam)->hInstance,
NULL); hButback=CreateWindow(TEXT("button"),TEXT("save"),
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
0,
540,
100,
30,
hwnd,
(HMENU) ID_BUTSAVE,
((LPCREATESTRUCT)lParam)->hInstance,
NULL);
return 0; case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_BUTSAVE:
GetWindowText(hwndEdit ,szBuffer,100);
fp = fopen("E:\\STUDY\\WIN\\CARD\\card.dat","w+");
fwrite(szBuffer,sizeof(szBuffer),1,fp);
return 0;
case ID_BUTBACK:
ShowWindow(hwnd,SW_HIDE);
ShowWindow(hwndcard,SW_SHOW);
return 0;
} return 0; case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
// TextOut(hdc,100,100,szBuffer,iLength);
// SetBkColor(hdc,COLOR_WINDOW);
EndPaint(hwnd,&ps);
return 0; }
return DefWindowProc(hwnd,message,wParam,lParam);}
#include <windows.h>
#include<stdio.h>LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK ChildCardProc(HWND, UINT, WPARAM, LPARAM) ;TCHAR szChildClass[] = TEXT("card_child");
#define ID_BUTCARD 0
#define ID_CARDNEW 1
#define ID_TEXT 2
#define ID_EDIT 3
#define ID_BUTBACK 4
#define ID_BUTSAVE 5static HWND hwndcard;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("card") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ; 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 ;
}
wndclass.lpfnWndProc = ChildCardProc ;
// wndclass.cbWndExtra = sizeof(long);
wndclass.hIcon = 0 ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszClassName = szChildClass ; RegisterClass(&wndclass);
hwnd = CreateWindow (szAppName, // window class name
TEXT ("申请"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
400, // initial x size
600, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
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)
{
HDC hdc ;
PAINTSTRUCT ps ;
HWND cardnew;
switch (message)
{
case WM_CREATE:
hwndcard=CreateWindow(TEXT("button"),TEXT("新帐户"),
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
20,
20,
100,
30,
hwnd,
(HMENU)ID_BUTCARD,
((LPCREATESTRUCT)lParam)->hInstance,
NULL);
return 0 ; case WM_COMMAND:
cardnew=CreateWindow(szChildClass,NULL, WS_CHILDWINDOW|WS_VISIBLE,
0,0,400,570,
hwnd,
(HMENU)ID_CARDNEW ,
(HINSTANCE) GetWindowLong( hwnd,GWL_HINSTANCE),
NULL);
ShowWindow (hwndcard, SW_HIDE) ;
return 0;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}LRESULT CALLBACK ChildCardProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
HWND hwndText,hwndEdit,hButback;
static TCHAR szBuffer[100];
int iLength;
FILE *fp; switch(message)
{
case WM_CREATE:
hwndText = CreateWindow (TEXT ("static"), TEXT("姓名"),
WS_CHILD | WS_VISIBLE | SS_CENTER ,
20, 20,
50, 20,
hwnd, (HMENU) ID_TEXT,
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;
hwndEdit = CreateWindow (TEXT ("edit"), NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT ,
100, 20,
150, 20,
hwnd, (HMENU) ID_EDIT,
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;
hButback=CreateWindow(TEXT("button"),TEXT("back"),
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
300,
540,
100,
30,
hwnd,
(HMENU) ID_BUTBACK,
((LPCREATESTRUCT)lParam)->hInstance,
NULL); hButback=CreateWindow(TEXT("button"),TEXT("save"),
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
0,
540,
100,
30,
hwnd,
(HMENU) ID_BUTSAVE,
((LPCREATESTRUCT)lParam)->hInstance,
NULL);
return 0; case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_BUTSAVE:
GetWindowText(hwndEdit ,szBuffer,100);
fp = fopen("E:\\STUDY\\WIN\\CARD\\card.dat","w+");
fwrite(szBuffer,sizeof(szBuffer),1,fp);
return 0;
case ID_BUTBACK:
ShowWindow(hwnd,SW_HIDE);
ShowWindow(hwndcard,SW_SHOW);
return 0;
} return 0; case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
// TextOut(hdc,100,100,szBuffer,iLength);
// SetBkColor(hdc,COLOR_WINDOW);
EndPaint(hwnd,&ps);
return 0; }
return DefWindowProc(hwnd,message,wParam,lParam);}
设成static的变量static HWND hwndText,hwndEdit,hButback;
这样呢