Thread: Gdi questions(how much is available on object into a device contex?)

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    71

    Gdi questions(how much is available on object into a device contex?)

    1.how much is available on object into a device contex?
    i create a pen and select into a device contex(in WM_PANIT) but it draw a line red, only in wm_paint.why this pen is not remain select?i must select evry time,in echy message when i want to draw a line?
    2.i'm going to ask later.
    L.E.:i draw a line.When i draw another one(superimposed on the first) the R2_NOTXORPEN it delete the "superimposed(overlap) on the first"(it set to white).When WM_LBUTTONUP disappear the white of the overlap of the two lines.I want that the user of my program to see te changes immediately(when the mouse move) not only when left button is up.

    Code:
    #include <windows.h>
    
    HDC hdc;
    const char g_szClassName[] = "myWindowClass";
    HPEN hPen,hPenOld;
    // Step 4: the Window Procedure
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	static int linie=0;
    	//static i=0;
    	PAINTSTRUCT ps;
    	static POINTS ptsIN,ptsOUT,ptsANT;
    	static COLORREF crRosu;
    	switch(msg)
    	{
    		
    		case WM_PAINT:
    			hdc=BeginPaint(hwnd,&ps);
    			
    			hPen=CreatePen(PS_SOLID,0,RGB(255,0,0));
    			hPenOld=SelectObject(hdc,hPen);
    			MoveToEx(hdc,20,20,NULL);
    			LineTo(hdc,100,100);
    			EndPaint(hwnd,&ps);
    		break;
    		case WM_LBUTTONDOWN:
    			
    				ptsIN=MAKEPOINTS(lParam);
    		break;
    		case WM_MOUSEMOVE:
    			
    			if (wParam & MK_LBUTTON )
    			{
    				
    				ptsOUT=MAKEPOINTS(lParam);
    				hdc=GetDC(hwnd);
    				//SetROP2(hdc, R2_NOTXORPEN);
    				if (linie==1)
    				//MessageBox(hwnd,"da","daaa",MB_OK);
    				//ptsIN=MAKEPOINTS(lParam);
    				{
    					SetROP2(hdc, R2_NOTXORPEN);
    					MoveToEx(hdc,ptsIN.x,ptsIN.y,NULL);
    					LineTo(hdc,ptsANT.x , ptsANT.y);//sterge linia pt a face alta
    				}
    				ptsOUT=MAKEPOINTS(lParam);
    				//SetROP2(hdc,R2_COPYPEN);
    				MoveToEx(hdc,ptsIN.x,ptsIN.y,NULL);
    				LineTo(hdc,ptsOUT.x , ptsOUT.y);
    				
    				ptsANT=ptsOUT;
    				ReleaseDC(hwnd,hdc);
    				linie=1;
    			}
    		break;
    		case WM_LBUTTONUP:
    			hdc=GetDC(hwnd);
    			//SetROP2(hdc,R2_NOTXORPEN);
    			//MoveToEx(hdc,ptsIN.x,ptsIN.y,NULL);
    			//LineTo(hdc,ptsOUT.x , ptsOUT.y);
    			
    			//SetROP2(hdc, R2_COPYPEN);
    			MoveToEx(hdc,ptsIN.x,ptsIN.y,NULL);
    			LineTo(hdc,ptsOUT.x , ptsOUT.y);
    
    			linie=0;
    			ReleaseDC(hwnd,hdc);
    		break;
    		
    		case WM_CLOSE:
    			DestroyWindow(hwnd);
    		break;
    		case WM_DESTROY: 
    			hdc=GetDC(hwnd);
    			SelectObject(hdc,hPenOld);
    			DeleteObject(hPen);
    			ReleaseDC(hwnd,hdc);
                		PostQuitMessage(0);
    		break;
            		default:
                		return DefWindowProc(hwnd, msg, wParam, lParam);
        	}
        	return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
    
        //Step 1: Registering the Window Class
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "The title of my window",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 440, 320,
            NULL, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        // Step 3: The Message Loop
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }
    sorry for my english
    Last edited by nutzu2010; 02-16-2012 at 11:25 AM.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    When you asked something very similar to this 6 months ago I gave you a pattern to use.

    It did not have the GDI leaks your code does and the user would see the updates as the mouse moved.

    If you had taken my advice then, you could have avoided this issue.

    How do i draw something(a ellipse) when mouse is over a point?
    "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

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    you mean this?
    "I would write functions and put all the code in it.
    Pass in any params you need (HWND, LPARAM, WPARAM etc)"
    Yes,I keep in mind that but this is a small exemple.
    "It did not have the GDI leaks your code does"
    yes ,I was wrong,but did not know that you must always select the pen into device context(This confused me).Now is good?
    Code:
    		case WM_CREATE:
    			hPen=CreatePen(PS_SOLID,0,RGB(255,0,0));	
    		break;
    		case WM_PAINT:
    			hdc=BeginPaint(hwnd,&ps);
    			
    			SelectObject(hdc,hPen);
    			MoveToEx(hdc,20,20,NULL);
    			LineTo(hdc,100,100);
    			EndPaint(hwnd,&ps);
    		break;
    		case WM_DESTROY: 
    			DeleteObject(hPen);
                		PostQuitMessage(0);
    		break;
    "...and the user would see the updates as the mouse moved."
    but in my program i see the updates as the mouse moved.I still have problem 2.
    Look on the picture.When mouse move and left button is down(the xor delete the overlap of the two lines).
    When left button is up then the lines are okay.
    Last edited by nutzu2010; 02-17-2012 at 02:47 AM.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>why this pen is not remain select?i must select evry time,in echy message when i want to draw a line?

    Because you do not get the same HDC each time from BeginPaint(). HDCs do not have to be the same for the life of your program.

    That is why you create a DC of your own and copy it to the HDC from BeginPaint().

    Then you can have the pen etc remain the same. It also means any drawing will remain even if the app is minimised, another window passes over it etc.

    The pattern I gave you before looks harder and is more code, but it avoids many mistakes you are making. (I know because I made the same mistakes)

    Your next issue will probably be the screen flickering.

    >>I still have problem 2.

    Could you explain the issue another way? Is it that the line should have been erased?


    You are losing a GDI pen each paint msg after the first.

    Code:
    create new pen
    select new pen into hdc (catch old pen)
    draw
    select old pen into hdc
    delete new pen
    Last edited by novacain; 02-24-2012 at 05:00 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. More than 1 object on same Device Context (DC)
    By csonx_p in forum Windows Programming
    Replies: 8
    Last Post: 04-14-2008, 10:13 AM
  2. Replies: 4
    Last Post: 11-14-2006, 11:52 AM
  3. Object oriented job questions
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-28-2004, 05:06 AM
  4. Replies: 4
    Last Post: 06-30-2004, 03:11 PM
  5. 2 mathematical / class object questions
    By Lurker in forum C++ Programming
    Replies: 20
    Last Post: 12-28-2003, 03:21 PM