I want to draw vertical and horizontal straight lines in WM_PAINT, How can I do it useing CDC?
Printable View
I want to draw vertical and horizontal straight lines in WM_PAINT, How can I do it useing CDC?
CDC is for MFC if I am not wrong, and WM_PAINT is for programs that don't use MFC, if I am not wrong.
Here is a solution, not for MFC:
Code:int paint_line(HWND hwnd, HDC hdc, int x0, int y0, int x9, int y9, int thickness, COLORREF rgb)
{
HBRUSH hbr; HPEN hpen, hpen_sav;
// int rop2_sav;
// rop2_sav = SetROP2(hdc, R2_NOTXORPEN);
hpen = CreatePen(PS_SOLID, thickness, rgb);
hpen_sav = (HPEN) SelectObject(hdc, hpen);
hbr = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH));
MoveToEx(hdc, x0, y0, NULL);
LineTo(hdc, x9, y9);
DeleteObject(SelectObject(hdc, hbr));
SelectObject(hdc, hpen_sav);
DeleteObject(hpen);
return 0;
}
Sorry I ean OnDraw. The way you can draw Rectangle and Ellipse, I want to draw lines
MoveTo(x,y) moves the pen to position and LineTo(x,y) draws the line from MoveTo position to LineTo position
this code draws grey lines,horizontal and vertical in center of screen
Code:OnDraw()
{
CRect rect;
GetClientRect(rect);
CPen penGrey; // Construct it, then initialize
if( penGrey.CreatePen( PS_SOLID, 0, RGB(200,200,200) ) )
{
// Select it into the device context
// Save the old pen at the same time
CPen* pOldPen = pDC->SelectObject( &penGrey );
// Draw with the pen
//draws axis
pDC->MoveTo(rect.right/2,rect.top);
pDC->LineTo(rect.right/2,rect.bottom);
pDC->MoveTo(rect.right,rect.bottom/2);
pDC->LineTo(rect.left,rect.bottom/2);
// Restore the old pen to the device context
pDC->SelectObject( pOldPen );
}
else
{
MessageBox("Resources are running low\nPlease close one or more applications");
}
}
MSDN knows everything about the CDC object.