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;
}
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.