Thread: MFC, CreateCompatibleDC question.

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    88

    MFC, CreateCompatibleDC question.

    Code:
     	
                    
                    CDC twdc;
                    twdc.CreateCompatibleDC(this->GetDC());
    
    	//=== gen pattern ===//
    	for(	 int x=0 ; x<99 ; x++ ){
    		for( int y=0 ; y<99 ; y++ ){
    			twdc.SetPixelV( x,y, x*x+y*y );
    		}
    	}
    	TRACE( "%d", twdc.GetPixel(4,4) );  //  any number for the (4,4).
    i do this in MFC, why it always TRACE out -1 ?? and alsoo , there is nothing printed when use BitBlt to print it out.

    i think i have problem with the CreateCompatibleDC().

    help me please.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Try creating a bitmap and loading that into the dc first. I think that when you use CreateCompatibleDC (the win32 api function which the mfc is just a wrapper for) the default bitmap created with it is a 1x1 monochrome bitmap.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    88
    Ok, now i know what is wrong in my program. thank you very much!!

    i know that the CDC obj. is 1x1 at first.

    how to resize it ??
    i can use SelectObject( BMP ) ; to resize, but i know this is a dump method.

    i have do this,
    Code:
     
    	twdc.CreateCompatibleDC(this->GetDC());
     
    	CRgn tRgn;	tRgn.CreateRectRgn( 0,0,20,20 );
    	twdc.SelectClipRgn( &tRgn, RGN_COPY );
    but not working.

    how to ??

    P.S.
    Macau has a first SARS case tonight !!!

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    CreateCompatibleBitmap()
    SelectObject() [catch the returned BMP and replace before deleting the DC or a GDI memory leak will result]

    >>twdc.CreateCompatibleDC(this->GetDC());

    this is a GDI memory leak as you never can release the DC returned from GetDC() (and in your code snippet do not DeleteDC() the compatible DC creating another leak)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    88
    O, something new to me.

    what about the
    int AfxMessageBox(......);

    and

    char * itoa( .... .. ) ;

    i always use them without catching the returns.
    will they cause any memory leakage ?

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Its GDI memory created by your app (using a Create....function have to be deleted, those you Get... have to be released).

    GDI objects can not (generally) be deleted while selected into a DC.

    "A DC is a structure that defines a set of graphic objects and their associated attributes, and the graphic modes that affect output."

    "This handle is saved so that the original brush can be selected back into the DC after the last painting operation has been completed with the new brush. After the original brush is selected back into the DC, the new brush is deleted, freeing memory in the GDI heap. "


    As to the returns of
    AfxMessageBox(......)
    isn't that the control pressed by the user? Not a GDI object.
    But if is a dynamically created variable would have to be freed.


    Do you use memory re/allocating functions without checking the return?
    Last edited by novacain; 05-13-2003 at 12:32 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC check box question
    By MyglyMP2 in forum Windows Programming
    Replies: 2
    Last Post: 03-09-2009, 05:47 PM
  2. MFC simple question
    By dole.doug in forum Windows Programming
    Replies: 5
    Last Post: 09-26-2008, 06:11 AM
  3. Window Resize question (MFC)
    By IfYouSaySo in forum Windows Programming
    Replies: 2
    Last Post: 11-16-2005, 12:34 PM
  4. MFC UI question
    By naruto in forum Windows Programming
    Replies: 1
    Last Post: 04-21-2005, 10:10 PM
  5. MFC Dialog App Screen Refresh Question
    By Scotth10 in forum Windows Programming
    Replies: 2
    Last Post: 10-18-2002, 02:07 PM