the problem with below program is i want to erase background when i press the button. how it is possible and what is wrong with the program. is there anything wrong with programming stratagy. i am preparing for a big leap ?? Code: #include <windows.h> HINSTANCE ghins; HWND ghwnd,btnHwnd; LRESULT __stdcall WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam); int __stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrev,LPSTR lpCmdline,int nShowCmd) { MSG msg; WNDCLASS wc; ghins = hInstance; wc.cbClsExtra=0; wc.cbWndExtra=0; wc.hbrBackground=GetStockObject(WHITE_BRUSH); wc.hCursor=LoadCursor(NULL,IDC_ARROW); wc.hIcon=NULL; wc.hInstance=ghins; wc.lpfnWndProc=WndProc; wc.lpszClassName=TEXT("WM_PAINTAPPS"); wc.lpszMenuName=NULL; wc.style=0; RegisterClass(&wc); ghwnd=CreateWindow( wc.lpszClassName, TEXT("Test Prog"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, ghins, NULL); ShowWindow(ghwnd,nShowCmd); UpdateWindow(ghwnd); while(GetMessage(&msg,0,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (INT)msg.wParam; } LRESULT __stdcall WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) { HDC hdc; static TCHAR pStr[100]; PAINTSTRUCT ps; switch(msg) { case WM_CREATE: CreateWindow(TEXT("BUTTON"),TEXT("GetDC"),WS_VISIBLE|WS_CHILD, 200,300,150,40,hWnd,(HMENU)3001,ghins,NULL); CreateWindow(TEXT("BUTTON"),TEXT("BeginPaint"),WS_VISIBLE|WS_CHILD, 355,300,150,40,hWnd,(HMENU)3002,ghins,NULL); lstrcpy(pStr,TEXT("Hello World")); return 0; case WM_PAINT: hdc = GetDC(hWnd); TextOut (hdc, 0, 0,pStr,lstrlen(pStr)) ; DeleteDC(hdc); break; case WM_COMMAND: switch(LOWORD(wParam)) { case 3001: lstrcpy(pStr,TEXT("What I")); break; case 3002: lstrcpy(pStr,TEXT("Know")); break; } return 0; case WM_CLOSE: PostQuitMessage(0); return 0; } return DefWindowProc(hWnd,msg,wParam,lParam); }
InvalidateRect is your friend. Also see UpdateWindow if you want the changes to take place immediately, rather than waiting for Windows to get round to validating the window (which it will eventually, but for snappy updates then you need to force it to happen).
thanks, but if my memory serves well i can say that InvalidateRect will erase Buttons from the main window too.
It shouldn't unless you specify that the invalidated area needs blanking first. Look at the bErase parameter. If specifying that it shouldn't be blanked first still causes the buttons to flicker, try invalidating a region (InvalidateRgn perhaps) that excludes those buttons instead of the whole lot.