Thread: Strange problem

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Strange problem

    Hallo, I am trying to remove some strange things in my program and I found something which im not sure why is not working.

    Why does not the first example of setClassLongPrt work?

    Code:
    HBITMAP img = (HBITMAP)LoadImage(NULL, TEXT(imgPath.c_str()), IMAGE_BITMAP,640,480,LR_LOADFROMFILE);
    HBRUSH  brush = CreatePatternBrush(img);
    
    // this does not work, is simply loads a white image
    SetClassLongPtr(overlayWindow,GCLP_HBRBACKGROUND,(LONG)brush);
    
    
    // This works, but here I get problems after the function have been called several times. Memory leak?
    // SetClassLongPtr(overlayWindow,GCLP_HBRBACKGROUND,(LONG)CreatePatternBrush(img));

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Memory leak?
    Yes, specifically GDI resources. You must DeleteObject any GDI object when you are done with it(caveat: shared system resources don't require deletion). In the case of the patterned brush, DeleteObject will not affect the bitmap used with CreatePatternBrush so you would have to use DeleteObject on that bitmap handle, too, once you were finished with it.
    Why does not ... work
    There is a delay when using SetClassLongPtr to change the background brush as the new brush is not drawn immediately. It also affects all windows of that particular window class which may not always be what you want.

    Alternatively, to change the background of a single window, you can handle the window's WM_ERASEBKGND message.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Thanks for your reply.

    But im still not sure why the code I posted does not work in the first case. I cant see why first loading data into a variable before passing it to a function makes any difference from just loading it into the function.

    EDIT:
    The problem was that I was deleting the brush while it still was in use by the window.
    Last edited by h3ro; 09-02-2008 at 01:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem with GETLINE
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 07-07-2008, 09:57 AM
  2. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. Strange problem
    By ~Kyo~ in forum Game Programming
    Replies: 0
    Last Post: 02-14-2006, 10:35 PM