Thread: Why CS_OWNDC doesnt work?

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    133

    Why CS_OWNDC doesnt work?

    Code:
    HDC hDC;
    	HBITMAP hbitmap;
    
    	// create bitmap brush	
        hbitmap = LoadBitmap(hinst,MAKEINTRESOURCE(IDB_BACKGROUND));
    	hbrush = CreatePatternBrush(hbitmap);
    	DeleteBitmap(hbitmap);
    	hDC = GetDC(hwnd);
    	SelectBrush(hDC,hbrush);
    	ReleaseDC(hwnd,hDC);
    	SelectBrush(hDC,GetStockBrush(BLACK_BRUSH));
    Hi, I have the above code right after i created a certain window. However, the window was not painted with the bitmap brush i created. Can someone explan why? I thought the new brush will takes effect for the rest of the program life if I use the style CS_OWNDC.

    If I set the hbrBackground field to the bitmap brush during the window class registration, the window is painted properly. I find this very strange.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    CS_OWNDC Just gives each window its own device context.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    I have only made one window. It has its own DC and it is suppose to retain any changes in its DC attributes right? So if I change the brush after creating the window, it should be able to paint the window with the new bitmap brush or at least that's what I know.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah ha! It looks like you need to select the brush to the HDC before deleting the original bitmap.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    I am quite sure i can delete the bitmap before i select the brush. Anyway i tried to select it in without deleting the bitmap. It still didnt work.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Code:
    	ReleaseDC(hwnd,hDC);
    	SelectBrush(hDC,GetStockBrush(BLACK_BRUSH));
    Why do you select a brush after you've released the DC?

    The problem comes from there. Since the DC is an OWNDC, ReleaseDC has no effect on it. Thus the handle is still valid after the ReleaseDC call (though it's poor coding to still use it), and the SelectBrush with the BLACK_BRUSH has an effect. In other words, you're resetting the brush back to black.

    But to set the brush in the WNDCLASS would be a better method anyway. Especially since you don't rely on CS_OWNDC that way.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I didn't notice that he did that. Yeah that would be a major problem. There are some memory issues to consider when using CS_OWNDC depending on what you are doing too. CS_OWNDC is great for a window that serves as a canvas (i.e. a paint program), but in an app that is heavy on controls it tends to waste memory.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    ack...sorry i was doing cut and paste to replicate the problem and the SelectBrush() wasn't there at first.

    Here is the actual problem:

    Code:
    hDC = GetDC(hwnd); 
    		
    
    	HBITMAP hbitmap;
    
    	// create bitmap brush	
        hbitmap = LoadBitmap(hinst,MAKEINTRESOURCE(IDB_BACKGROUND));
    	hbrush = CreatePatternBrush(hbitmap);
    	DeleteBitmap(hbitmap);
    
    	SelectBrush(hDC,hbrush);
    	ReleaseDC(hwnd,hDC);
    My background brush was initially (COLOR_WINDOW+1) for the hbrBackground field.

    list of things i tried AFTER CREATING THE WINDOW and didnt work
    :
    1. Delete bitmap after selecting brush
    Code:
    	hDC = GetDC(hwnd); 
    		
    
    	HBITMAP hbitmap;
    
    	// create bitmap brush	
        hbitmap = LoadBitmap(hinst,MAKEINTRESOURCE(IDB_BACKGROUND));
    	hbrush = CreatePatternBrush(hbitmap);
    	SelectBrush(hDC,hbrush);
    	DeleteBitmap(hbitmap);
    	ReleaseDC(hwnd,hDC);
    2. select black brush into hDC, the background remains white instead of black

    Code:
    	hDC = GetDC(hwnd); 
    	
    	HBITMAP hbitmap;
    
    	// create bitmap brush	
        hbitmap = LoadBitmap(hinst,MAKEINTRESOURCE(IDB_BACKGROUND));
    	hbrush = CreatePatternBrush(hbitmap);
    	SelectBrush(hDC,hbrush);
    	DeleteBitmap(hbitmap);
            SelectBrush(hDC,GetStockBrush(BLACK_BRUSH));
    	ReleaseDC(hwnd,hDC);
    BTW, I am using private DCs because i will be making two windows base on the same window class. But their background will be of different bitmap brush.
    Last edited by Raison; 09-17-2004 at 03:25 AM.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It might be that, contrary to the documentation, ReleaseDC resets the DC or GetDC does.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    Removed all the ReleaseDC()s and EndPaint()...it still didnt work.
    The strangest thing is that i checked the brush in the hDC using GetCurrentObject() and OBJ_BRUSH during every WM_PAINT msg. It has the right bitmap brush, but it just doesnt paint the window with that brush.

    I can just fill the brush field with the bitmap brush before creating the window and make it work. But i really want to know what is wrong with the other method.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Perhaps the DC used to draw the background is different?
    Catch the WM_ERASEBKGND message. wParam is a device context, you could check that.
    Besides, the way I read the WM_ERASEBKGND docs, DefWindowProc ignores the currently selected brush and automatically selects the one from the WNDCLASS. So, either you change it in the WNDCLASS or you intercept the message and erase the background yourself.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    I managed to solve the problem by adding PatBlt while handling WM_PAINT msg.

    Code:
    void mainFrame::mainWindow_onPaint(HWND hwnd)
    {
    	PAINTSTRUCT ps;
    
    	HDC hDC = BeginPaint(hwnd,&ps);
    	PatBlt(hDC,0,0,512,512,PATCOPY);
    	EndPaint(hwnd,&ps);
    	
    }
    However, if i fill the hbrBackground field with the bitmap brush and do a PatBlt. I get a white screen again. Strange!

  13. #13
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    Hmm, i have encountered yet another problem, so i'm gonna post it here.
    Code:
    	// setup common controls
    	icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
    	icc.dwICC = 0x00004000;//ICC_STANDARD_CLASSES
    	InitCommonControlsEx(&icc);
    Visual Studio .NET 2003 doesnt recognize ICC_STANDARD_CLASSES and complains it's a undeclared identifier. It is definitely found in <commctrl.h> and i have included that header file.

    My edit control works without calling InitCommonControlsEx() or if i hardcode the value of dwICC. But Microsoft says i must call it to initialize controls and i dont want to expect any strange problem later.

    Can someone help? Thanks alot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM