GDI Drawing a stupid line!

This is a discussion on GDI Drawing a stupid line! within the Windows Programming forums, part of the Platform Specific Boards category; I have reduced my code to try and simplify my problem. This snippet from my windows proceedure draws to the ...

  1. #1
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Angry GDI Drawing a stupid line!

    I have reduced my code to try and simplify my problem.
    This snippet from my windows proceedure draws to the screen as expected.
    Code:
    	case WM_PAINT:	
    		{
    			hDC = BeginPaint( hwnd, &ps );
    			HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255,0,0));
    			SelectObject( hDC, hPen );
    			MoveToEx(hDC,52,52,NULL);
    			LineTo(hDC,100,200);
    			DeleteObject( SelectObject( hDC, GetStockObject( BLACK_PEN ) ) );
    			ReleaseDC( hwnd, hDC );
    			EndPaint( hwnd, &ps );
    			return 0;
    		}
    When I move my code to another function it doesn't draw anything. Here is my most recent attempt to get it up and running:

    Code:
    void DrawSection(HWND hwnd)
    {
    	HDC hDC;
    	hDC = GetDC( hwnd);
    	HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255,0,0));
    	SelectObject( hDC, hPen );
    	MoveToEx(hDC,0,0,NULL);
    	LineTo(hDC,100,200);
    	DeleteObject( SelectObject( hDC, GetStockObject( BLACK_PEN ) ) );
    	ReleaseDC( hwnd, hDC );
    	//	UpdateWindow(hwnd);
    	//	InvalidateRect (hwnd, NULL, TRUE);
    	//	SendMessage(hwnd,WM_PAINT,0,0);
    	return;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Nothing wrong with that code, it's something else. Post a sample app with the modified code you're using.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    Try making hDC static. it might work.

  4. #4
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Calling from callback, bad idea?

    It is drawing the line in my dialog window I use to get input!

    Here is what I had:
    Code:
    BOOL CALLBACK CircleProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    	int nLen = 0;
    	int Decimal =0;
    	LPTSTR pbuffer = NULL;
    
        switch(Message)
        {
            case WM_INITDIALOG:
    			return TRUE;
    
            case WM_COMMAND:
                switch(LOWORD(wParam))
                {
                    case IDOK:
    					subsectionData[SectionNumber].sectionType = circle;
    
    					hdlg = GetDlgItem(hwnd, IDC_CIRCLE_DIAMETER);  //(hOwner, hDialogEditField)
    					nLen = GetWindowTextLength(hdlg);
    					pbuffer = (LPTSTR)malloc(nLen + 1);
    					GetDlgItemText(hwnd,	//(hOwner, hDialogEditField)
    						IDC_CIRCLE_DIAMETER,	// control identifier
    						pbuffer,			// pointer to buffer for text
    						nLen+1				// maximum size of string + NULL
    					);
    					subsectionData[SectionNumber].sectionSpecificData.circleData.diameter 
    						= strtod(pbuffer, NULL);
    					// Validation...Do not allow a ?zero? or ?negative? number.
    					if (subsectionData[SectionNumber].sectionSpecificData.circleData.diameter <= 0)
    					{
    						MessageBox (NULL, TEXT ("Enter a valid floating point number."), 
    							TEXT ("Invalid input!"), MB_ICONINFORMATION) ;
    						return TRUE;	// break out of switch statement.
    					}
    
    					free(pbuffer);
    
    					DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_CENTER_COORDINATES), 
    						hwnd, CenterCoordinates);
    					// since this is a circle subsectionData[].ptX and .ptY = .XBAR and .YBAR respectively.
    					subsectionData[SectionNumber].XBAR = subsectionData[SectionNumber].ptX[0];
    					subsectionData[SectionNumber].YBAR = subsectionData[SectionNumber].ptY[0];
    
    					// Need this here to dismiss CenterCoord dialog before...
    					// ...calling another dialog in case user selects cancel.
    					EndDialog(hwnd, IDOK);
    					
    					// Dont call CalculateData... just do calcs here.
    					subsectionData[SectionNumber].Ixc = (PI/64)*(pow(subsectionData[SectionNumber].sectionSpecificData.circleData.diameter, 4.0));
    					subsectionData[SectionNumber].Iyc = subsectionData[SectionNumber].Ixc;
    					subsectionData[SectionNumber].area = PI * pow(subsectionData[SectionNumber].sectionSpecificData.circleData.diameter/2, 2);
    					DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_ADD_SUBTRACT_SECTION),
    						hwnd, AddSubtractProc);
    					EndDialog(hwnd, IDOK);
    					TransformationEquation();
    					CalculateCompositeData(hwnd);
    					SectionNumber++;  //Count of total number of sections.
    					bFileNeedsSaved = TRUE;	//Data has been modified needs to be saved.
    /**************************************************************/
    //					DrawSection(hwnd); // This is where I originally placed it.
    /**************************************************************/
    				//	SendMessage(hwnd,WM_PAINT,0,0);
    					break;
                    case IDCANCEL:
    					MessageBeep (0) ;
                        EndDialog(hwnd, IDCANCEL);
    					break;
                }
    			break;
    			default:
    				return FALSE;
        }
    	DrawSection(hwnd); // I placed it here just to see if it would paint and it does but in my 
    	// dialog box instead of my main window!
        return TRUE;
    }
    I moved it into the callback for my window proc. like so:
    Code:
    case IDM_SECTIONS_CIRCLE:
    			 DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_CIRCLE_PROPERTIES), hwnd, CircleProc);
    			 DrawSection(hwnd);
    			 return 0;
    and what do you know it works now.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Try using NULL in the GetDC() (or the HWND to the control or dlg to be redrawn.

    I think the line will disappear if the screen is redrawn (use a msgbox to test)

    The ReleaseDC() is not needed as EndPaint() does that.

    Use
    //tell the app where to redraw
    InvalidateRect(hWnd,&Area,bBckGrnd);
    //ask it to draw this dialog
    UpdateWindow(hWnd);

    in the paint function use
    GetUpdateRect() to only draw the invalidated rect
    "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. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  2. Move the Caret to a line
    By TheDan in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2005, 12:59 PM
  3. Line Drawing Algorithm
    By Axpen in forum Game Programming
    Replies: 15
    Last Post: 08-01-2005, 06:30 PM
  4. if is faster than switch?
    By skorman00 in forum C++ Programming
    Replies: 32
    Last Post: 03-06-2004, 12:15 PM
  5. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 05:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21