Sign in to follow this  

NULL_BRUSH not working

This topic is 2668 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im doing my own painting of a window, and so do not require the its background to be erased. So Ive set the window background to a NULLBRUSH, and I return 0 from WM_ERASEBACKGROUND. Yet the windows backround initially still starts off white, which causes flicker when I overpaint it.

And when I resize the window, what should be painted NULL, allowing me to see through the window to my desktop, is black. Im sure Ive got this to work before.

Ive set up a quick example from one of Charles Petzolds code demos, and it happens in this too.


#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
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(NULL_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;s
}

hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // 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 ;
RECT rect ;

switch (message)
{
case WM_ERASEBKGND:
return 0;
case WM_PAINT:
return 0;

case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

Share this post


Link to post
Share on other sites
This is exactly what NULL_BRUSH is supposed to do: it simply doesn't draw anything. Note that not drawing anything is not equivalent to having a transparent window that shows the contents of other windows below it in the Z-order.

Share this post


Link to post
Share on other sites
But it is drawing something. Its drawing all white even with hbrBackground set to NULL. So that when I draw over it, you see the white flicker. This only happens when the application is first lauched.

Is there no way around this?

Im sure Ive had it before where what was intially behind the window before it was open gets seen when you set the NULL brush, and that gets painted over, rather than this white background that I seem to get at the moment

Share this post


Link to post
Share on other sites
Yes, setting the visual quality to "Adjust for best performance", does produce the results I was trying to get.

Thats kindof annoying. Good to know I guess though.

Share this post


Link to post
Share on other sites
Quote:
Original post by hick18
I return 0 from WM_ERASEBACKGROUND


Quote:
From MSDN Docs
Return Value

An application should return nonzero if it erases the background; otherwise, it should return zero.


Return 1 from WM_ERASEBKGDN.

Share this post


Link to post
Share on other sites
Also, if your WM_PAINT handler really looks like this you'll have an endless stream of WM_PAINTs. If you return 0 you need to either call BeginPaint/EndPaint or ValidateRect.

If you don't your window is kept being marked as dirty.

Share this post


Link to post
Share on other sites

This topic is 2668 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this