Thread: color of HPEN and HBRUSH

  1. #1
    xenon
    Guest

    color of HPEN and HBRUSH

    I want to plot moving graphs and show a picture of level tank in real time so I draw lines and pics into bitmap in WM_TIMER and use BitBlt for show on window in WM_PAINT. But when a timer count about 150 times my tank pic and graphs have problem about color.
    A blue line that show a scale on background of graphs had changed to black line like hPen which I use to scale on the frame of graphs and about a tank I use a blue brush fill an ellipse. a blue brush had changed to white brush.
    Please give me some advices.

    Code:
    case WM_TIMER:
    
    
    			j++;     // counter for moving graph
    			t++;
    
    			ClearBitmap(graph1dc);
    			ClearBitmap(graph2dc);
    		  	SetRect(&rect1,30,10,431,261);
    			SetRect(&rect2,30,10,431,261);
    			
    			BlackScale(graph1dc,j);
    			BlackScale(graph2dc,j);
    
    			BlueLine(graph1dc,j);
    			BlueLine(graph2dc,j);
    
    			hBlackbrush = CreateSolidBrush (RGB (0,0,0)) ;
    			FrameRect(graph1dc,&rect1,hBlackbrush);
    			FrameRect(graph2dc,&rect2,hBlackbrush);
    			
    	        
    	 
    	        InvalidateRect(hWnd,NULL,0);
    	
    
    
                return 0;
    
    		case WM_PAINT:
    
    			hdc = BeginPaint(hWnd, &paintstruct); /* get DC */
    						
    			PlotGraph();
    			SelectObject (con1dc, hBitmap1) ;
    			SelectObject (con2dc, hBitmap2) ;
    			SelectObject (con3dc, hBitmap3) ;
    			
    			BitBlt(hdc,560,45,500,290,graph1dc,0,0,SRCCOPY);
    			BitBlt(hdc,560,380,500,290,graph2dc,0,0,SRCCOPY);
    			BitBlt(hdc,55,100,180,500,picdc,0,0,SRCCOPY);
    			BitBlt(hdc,312,400,cxBitmap[0],cyBitmap[0],con1dc,0,0,SRCCOPY);
    			BitBlt(hdc,312,442,cxBitmap[1],cyBitmap[1],con2dc,0,0,SRCCOPY);
    			BitBlt(hdc,308,482,cxBitmap[2],cyBitmap[2],con3dc,0,0,SRCCOPY);
          /* now, copy memory image onto screen */
    			
    			EndPaint(hWnd, &paintstruct); /* release DC */
    			break;
    return 0;
    .....
    ....
    ....
    ....
    
    
    And functions that I use to draw lines 
    
    /*for clear a bitmap
    void ClearBitmap(HDC hdc)
    {
    	HBRUSH hWhitebrush;
    	
    	hWhitebrush = CreateSolidBrush(RGB(255,255,255));
    	PatBlt(hdc,0,0,450,310,PATCOPY);
    	DeleteObject(hWhitebrush);
    }
    
    void BlackScale(HDC hdc,int j)
    {
    	HPEN hBlackpen;
    	int i,k,num,fp;
    	char szBuffer[10];
    	int Xaxis[10][41] = {10,1 ,1 ,1 ,1 ,5 .......};
    
    	hPen = CreatePen(PS_SOLID,1,RGB(0,0,0));
    	SelectObject(hdc,hPen);
    	for (i=0;i<=260;i=i+25)
    	{
    	  MoveToEx(hdc,20,i+10,NULL);
    	  LineTo(hdc,30,i+10);
    	}	
    
                     .......
                     .......
    
    	SelectObject(hdc,hOldpen);
    	DeleteObject(hPen);
    }
    
    void BlueLine(HDC hdc,int j)
    {
        HPEN hBluepen;
    	int i,num,k;
    	int Xaxis[10][41] = {10,1 ,1 ,1 ,1 ,5 ,1 ,1 ,1 ,1 ,10,1 ,1 ,1 ,1 ,5 ,1 ,1 ,1 ,1 ,10,......};
    		
    	hBluepen = CreatePen(PS_DOT,1,RGB(22,80,180));
    	hOldpen = (HPEN) SelectObject(hdc,hBluepen);
    	hPen = CreatePen(PS_DOT,1,RGB(22,80,180));
    	SelectObject(hdc,hBluepen);
    	for (i=0;i<=260;i=i+25)
    	{
    		if (i>0 || i<260 )
    		{
    		  MoveToEx(hdc,30,i+10,NULL);
    		  LineTo(hdc,430,i+10);
    		}
    	}
    
                     .......
                     .......
    
    	SelectObject(hdc,hOldpen);
    	DeleteObject(hBluepen);
    
    }
    
    void PlotGraph()
    {
    			hPen = CreatePen(PS_DASH,1,RGB(0,0,0));
    			hOldpen = (HPEN) SelectObject(graph1dc,hPen);
    			hOldpen = (HPEN) SelectObject(graph2dc,hPen);
    						SelectObject(graph1dc,hPen);
    			SelectObject(graph2dc,hPen);
    			Polyline(graph1dc,Level,t);
    			Polyline(graph2dc,FR,t);
    			SelectObject(graph1dc,hOldpen);
    			SelectObject(graph2dc,hOldpen);
    			DeleteObject(hPen);
    }
    Thank you very much.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    The change in colours is due to yourPC running out of GDI resources due to the many leaks in you code.


    You create a GDI object you must delete it.

    If you use GetXX (ie GetDC() ) you must Release that GDI object.

    If you select a GDI object into a DC you must 'catch' the return and replace it.

    A GDI object can not be deleted while selected into a DC.

    If you do not put the DC back EXACTLY the way you created / get it you will lose memory.

    Specifically some examples (as I do not have the time to fix all the code);
    case WM_TIMER:
    the black brush is not deleted.

    case WM_PAINT
    you select three bmp's into the DC's but do not catch the returns and replace them. What happens to the bitmap currently selected when the next paint msg comes? What happens to the original bmp?
    In the painting function you should have ONE bitblt() as they are very slow. Look into double buffering. That is assembling the screen on a seperate HDC and copying the final image to the buffer. Call for a paint msg and bitBlt the buffer to the hdc in the paint struct. This allows you to draw one frame to the screen while the next is being assembled.

    void BlackScale(HDC hdc,int j)
    looks like an error but as code incompleate can't tell. Check the SelectObject and hOldpen

    void BlueLine(HDC hdc,int j)
    selecting two brushes into same dc.
    Code:
    hOldpen = (HPEN) SelectObject(graph1dc,hPen);
    			hOldpen = (HPEN) SelectObject(graph2dc,hPen);
    same brush in two DC's at same time. AFAIK this is a very bad thing.
    then you select again

    SelectObject(graph2dc,hPen);

    why?

  3. #3
    xenon
    Guest

    Thumbs up

    Thank you very much for help me figured it out.
    Does it work if I use one large bitmap instead of little ones and use BitBlt( ) just one time?

    Actually I don't have any knowledges about C programming before but I have to do this project so I try to read many books and used sourcecodes that can modified to my work.
    Thus I don't know a really meaning of some part of my codes.
    Pardon me if it make you confused.

  4. #4
    xenon
    Guest
    I found a problem again all controls such as button, editbox etc.
    they'll blinking everytime that I call a function InvalidateRect( ).
    How can I fix it?
    PS. I define a bitmap is equal window size.

    Thank you.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Set the last param of InvalidateRect() to FALSE. that is ask that the background is NOT redrawn.

    OR

    return TRUE for the WM_ERASEBKGND msg
    Code:
    case	WM_ERASEBKGND:
    	return TRUE;
    break;//included for syntax
    Does it work if I use one large bitmap instead of little ones and use BitBlt( ) just one time?

    IMHO this would be faster.

    >>Thus I don't know a really meaning of some part of my codes.

    This is a VERY BAD THING!!!!!!!!!
    "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. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. Drawing two or more rectangles on client area.. HELP!
    By csonx_p in forum Windows Programming
    Replies: 2
    Last Post: 05-12-2008, 01:43 AM
  3. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. '=' : cannot convert from 'void *' to 'struct HBRUSH__ *'
    By Bajanine in forum Windows Programming
    Replies: 9
    Last Post: 10-14-2002, 07:54 PM