主题:子窗口重画问题请教
我在一个窗口中建立了2个子窗口,当子窗口被另一子窗口覆盖时,如果移出该子窗口,好像不会重画,该窗口内还保留其被覆盖的部分。不知代码哪里不对,特此请教:
//包含头文件
#include "Windows.h"
#include "stdio.h"
#include "TCHAR.H"
HWND hwndchild1,hwndchild2;//子窗口句柄
TCHAR szChar[100];//在子窗口内显示的文本
int i=0;//响应WM_PAINT消息的次数
//申明窗口回调函数
LRESULT CALLBACK WndFun(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK ChildWndFun(HWND,UINT,WPARAM,LPARAM);
//主程序入口
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE PerhInstance,LPSTR lpszArg,int nShowWin){
//申明要用到的变量
WCHAR szAppName[]=L"我的窗口程序";
WCHAR szChildWinName[]=L"我的子窗口";
WNDCLASSEX wndclass,childwndclass;
HWND hwnd;
MSG msg;
//定义窗口类
wndclass.style=0;
wndclass.cbClsExtra=0;
wndclass.cbSize=sizeof(WNDCLASSEX);
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hIconSm=LoadIcon(NULL,IDI_WINLOGO);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndFun;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
//定义子窗口类
childwndclass.style=0;
childwndclass.cbClsExtra=0;
childwndclass.cbSize=sizeof(WNDCLASSEX);
childwndclass.cbWndExtra=0;
childwndclass.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
childwndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
childwndclass.hIcon=NULL;
childwndclass.hIconSm=NULL;
childwndclass.hInstance=hInstance;
childwndclass.lpfnWndProc=ChildWndFun;
childwndclass.lpszClassName=szChildWinName;
childwndclass.lpszMenuName=NULL;
//注册窗口类
if(!RegisterClassEx(&wndclass)||!RegisterClassEx(&childwndclass)){
MessageBox(NULL,L"不能注册窗口",L"ok",0);
return 0;
}
//建立窗口
hwnd=CreateWindow(szAppName,//窗口类的名称
L"我的窗口",//窗口上的标题
WS_OVERLAPPEDWINDOW,//窗口的式样
CW_USEDEFAULT,//X坐标
CW_USEDEFAULT,//Y坐标
CW_USEDEFAULT,//窗口宽度
CW_USEDEFAULT,//窗口高度
HWND_DESKTOP,//父窗口
NULL,//菜单
hInstance,NULL);
//建立子窗口
hwndchild1=CreateWindow(szChildWinName,//窗口类的名称
L"我的子窗口1",//窗口上的标题
WS_CHILD|WS_OVERLAPPEDWINDOW|CS_HREDRAW|CS_VREDRAW ,//窗口的式样
0,//X坐标
0,//Y坐标
200,//窗口宽度
200,//窗口高度
hwnd,//父窗口
NULL,//菜单
hInstance,NULL);
ShowWindow(hwndchild1,SW_SHOW);
//建立子窗口
hwndchild2=CreateWindow(szChildWinName,//窗口类的名称
L"我的子窗口2",//窗口上的标题
WS_CHILD|WS_OVERLAPPEDWINDOW|CS_HREDRAW|CS_VREDRAW ,//窗口的式样
100,//X坐标
0,//Y坐标
200,//窗口宽度
200,//窗口高度
hwnd,//父窗口
NULL,//菜单
hInstance,NULL);
ShowWindow(hwndchild2,SW_SHOW);
//显示窗口
ShowWindow(hwnd,nShowWin);
UpdateWindow(hwnd);
//消息循环
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//回调函数
LRESULT CALLBACK WndFun(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
switch(message){
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
//子窗口回调函数
LRESULT CALLBACK ChildWndFun(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
PAINTSTRUCT ps;
HDC hdc;
switch(message){
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
i=i+1;
_stprintf(szChar,TEXT("WM_PAINT:%d"),i);
SelectObject(hdc,GetStockObject(BLACK_PEN));
TextOut(hdc,10,10,szChar,_tcslen(szChar));
EndPaint(hwnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
//包含头文件
#include "Windows.h"
#include "stdio.h"
#include "TCHAR.H"
HWND hwndchild1,hwndchild2;//子窗口句柄
TCHAR szChar[100];//在子窗口内显示的文本
int i=0;//响应WM_PAINT消息的次数
//申明窗口回调函数
LRESULT CALLBACK WndFun(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK ChildWndFun(HWND,UINT,WPARAM,LPARAM);
//主程序入口
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE PerhInstance,LPSTR lpszArg,int nShowWin){
//申明要用到的变量
WCHAR szAppName[]=L"我的窗口程序";
WCHAR szChildWinName[]=L"我的子窗口";
WNDCLASSEX wndclass,childwndclass;
HWND hwnd;
MSG msg;
//定义窗口类
wndclass.style=0;
wndclass.cbClsExtra=0;
wndclass.cbSize=sizeof(WNDCLASSEX);
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hIconSm=LoadIcon(NULL,IDI_WINLOGO);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndFun;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
//定义子窗口类
childwndclass.style=0;
childwndclass.cbClsExtra=0;
childwndclass.cbSize=sizeof(WNDCLASSEX);
childwndclass.cbWndExtra=0;
childwndclass.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
childwndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
childwndclass.hIcon=NULL;
childwndclass.hIconSm=NULL;
childwndclass.hInstance=hInstance;
childwndclass.lpfnWndProc=ChildWndFun;
childwndclass.lpszClassName=szChildWinName;
childwndclass.lpszMenuName=NULL;
//注册窗口类
if(!RegisterClassEx(&wndclass)||!RegisterClassEx(&childwndclass)){
MessageBox(NULL,L"不能注册窗口",L"ok",0);
return 0;
}
//建立窗口
hwnd=CreateWindow(szAppName,//窗口类的名称
L"我的窗口",//窗口上的标题
WS_OVERLAPPEDWINDOW,//窗口的式样
CW_USEDEFAULT,//X坐标
CW_USEDEFAULT,//Y坐标
CW_USEDEFAULT,//窗口宽度
CW_USEDEFAULT,//窗口高度
HWND_DESKTOP,//父窗口
NULL,//菜单
hInstance,NULL);
//建立子窗口
hwndchild1=CreateWindow(szChildWinName,//窗口类的名称
L"我的子窗口1",//窗口上的标题
WS_CHILD|WS_OVERLAPPEDWINDOW|CS_HREDRAW|CS_VREDRAW ,//窗口的式样
0,//X坐标
0,//Y坐标
200,//窗口宽度
200,//窗口高度
hwnd,//父窗口
NULL,//菜单
hInstance,NULL);
ShowWindow(hwndchild1,SW_SHOW);
//建立子窗口
hwndchild2=CreateWindow(szChildWinName,//窗口类的名称
L"我的子窗口2",//窗口上的标题
WS_CHILD|WS_OVERLAPPEDWINDOW|CS_HREDRAW|CS_VREDRAW ,//窗口的式样
100,//X坐标
0,//Y坐标
200,//窗口宽度
200,//窗口高度
hwnd,//父窗口
NULL,//菜单
hInstance,NULL);
ShowWindow(hwndchild2,SW_SHOW);
//显示窗口
ShowWindow(hwnd,nShowWin);
UpdateWindow(hwnd);
//消息循环
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//回调函数
LRESULT CALLBACK WndFun(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
switch(message){
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
//子窗口回调函数
LRESULT CALLBACK ChildWndFun(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
PAINTSTRUCT ps;
HDC hdc;
switch(message){
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
i=i+1;
_stprintf(szChar,TEXT("WM_PAINT:%d"),i);
SelectObject(hdc,GetStockObject(BLACK_PEN));
TextOut(hdc,10,10,szChar,_tcslen(szChar));
EndPaint(hwnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}