以下是代码,写的比较乱,还请各位赐教,点了BUTTON(CARD)后出现一个新的窗口(将上个窗口覆盖,但为什么我点BUTTON的位置,还可以显示这个按钮?
#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_TEXT 1
#define ID_EDIT 2
#define ID_BUTBACK 3static 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 ("desktop"), // 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)
{
HWND cardnew;
HDC hdc ;
PAINTSTRUCT ps ;
static int i=1;
switch (message)
{
case WM_CREATE:
hwndcard=CreateWindow(TEXT("button"),TEXT("card"),
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
20,
20,
100,
30,
hwnd,
(HMENU)i,
((LPCREATESTRUCT)lParam)->hInstance,
NULL); return 0 ; case WM_COMMAND:
cardnew=CreateWindow(szChildClass,TEXT("CARD"), WS_CHILDWINDOW|WS_VISIBLE,
0,0,400,570,
hwnd,
(HMENU)(i++),
(HINSTANCE) GetWindowLong( hwnd,GWL_HINSTANCE),
NULL);
return 0; case WM_SETFOCUS:
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];
static int iLength;
FILE *fp;
// int cxChar,cyChar; switch(message)
{
case WM_CREATE:
//cxChar=LOWORD(GetDialogBaseUnits());
//cyChar=HIWORD(GetDialogBaseUnits());
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) ;
SetFocus(hwndEdit); hButback=CreateWindow(TEXT("button"),TEXT("back"),
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
300,
540,
100,
30,
hwnd,
(HMENU) ID_BUTBACK,
((LPCREATESTRUCT)lParam)->hInstance,
NULL);
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_TEXT 1
#define ID_EDIT 2
#define ID_BUTBACK 3static 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 ("desktop"), // 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)
{
HWND cardnew;
HDC hdc ;
PAINTSTRUCT ps ;
static int i=1;
switch (message)
{
case WM_CREATE:
hwndcard=CreateWindow(TEXT("button"),TEXT("card"),
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
20,
20,
100,
30,
hwnd,
(HMENU)i,
((LPCREATESTRUCT)lParam)->hInstance,
NULL); return 0 ; case WM_COMMAND:
cardnew=CreateWindow(szChildClass,TEXT("CARD"), WS_CHILDWINDOW|WS_VISIBLE,
0,0,400,570,
hwnd,
(HMENU)(i++),
(HINSTANCE) GetWindowLong( hwnd,GWL_HINSTANCE),
NULL);
return 0; case WM_SETFOCUS:
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];
static int iLength;
FILE *fp;
// int cxChar,cyChar; switch(message)
{
case WM_CREATE:
//cxChar=LOWORD(GetDialogBaseUnits());
//cyChar=HIWORD(GetDialogBaseUnits());
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) ;
SetFocus(hwndEdit); hButback=CreateWindow(TEXT("button"),TEXT("back"),
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
300,
540,
100,
30,
hwnd,
(HMENU) ID_BUTBACK,
((LPCREATESTRUCT)lParam)->hInstance,
NULL);
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);}
case WM_COMMAND:
cardnew=CreateWindow(szChildClass,TEXT("CARD"), WS_CHILDWINDOW|WS_VISIBLE,
0,0,400,570,
hwnd,
(HMENU)(i++),
(HINSTANCE) GetWindowLong( hwnd,GWL_HINSTANCE),
NULL);
return 0;
参考:
HWND CreateWindow(
LPCTSTR lpClassName, // pointer to registered class name
LPCTSTR lpWindowName, // pointer to window name
DWORD dwStyle, // window style
int x, // horizontal position of window
int y, // vertical position of window
int nWidth, // window width
int nHeight, // window height
HWND hWndParent, // handle to parent or owner window
HMENU hMenu, // handle to menu or child-window identifier
HANDLE hInstance, // handle to application instance
LPVOID lpParam // pointer to window-creation data
);因为父窗口句柄相同,所以同样创建在同一个窗口中!
在case WM_COMMAND:
的cardnew=CreateWindow…………的后面加上ShowWindow (hwndcard, SW_HIDE) ;把这个按钮隐藏了,然后等到需要的时候,在把这个按钮显示出来
——————————————————————————————这个可以了,谢谢毛毛。
switch(LOWORD(wParam))
{
case ID_BUTBACK:
ShowWindow(hwnd,SW_HIDE);
return 0;
}
return 0;结果没有将那个BUTTON显示出来,
你要调用这个,才能把这个button显出来呵呵,
switch(LOWORD(wParam))
{
case ID_BUTBACK:
ShowWindow(hwnd,SW_HIDE);
ShowWindow(hwndcard,SW_SHOW);
return 0;
} return 0;
这样就对了,
还有其他的方法可以实现么? dirdirdir3(风)说的child window本来就不能覆盖原来的window里面的控件的,最好把childwindow生成在一个静态控件里面。是什么意思?