Saturday, November 5, 2011

Types of Pens of .Graphics Under Windows in C programming

In the previous program we have used the default solid black pen of thickness 1 pixel. We can create pens of different style, color and thickness to do our drawing. The following OnPaint( ) handler shows how this can be achieved.
void OnPaint ( HWND hWnd )
{
HDC hdc ;
PAINTSTRUCT ps ;
HPEN hpen ;
HGDIOBJ holdpen ;
hdc = BeginPaint ( hWnd, &ps ) ;
hpen = CreatePen ( PS_DASH, 1, RGB ( 255, 0, 0 ) ) ;
holdpen = SelectObject ( hdc, hpen ) ;
MoveToEx ( hdc, 10, 10, NULL ) ;
LineTo ( hdc, 500, 10 ) ;
SelectObject ( hdc, holdpen ) ;
DeleteObject ( hpen ) ;
hpen = CreatePen ( PS_DOT, 1, RGB ( 255, 0, 0 ) ) ;
holdpen = SelectObject ( hdc, hpen ) ;
MoveToEx ( hdc, 10, 60, NULL ) ;
LineTo ( hdc, 500, 60 ) ;
SelectObject ( hdc, holdpen ) ;
DeleteObject ( hpen ) ;
hpen = CreatePen ( PS_DASHDOT, 1, RGB ( 255, 0, 0 ) ) ;
holdpen = SelectObject ( hdc, hpen ) ;
MoveToEx ( hdc, 10, 110, NULL ) ;
LineTo ( hdc, 500, 110 ) ;
SelectObject ( hdc, holdpen ) ;
DeleteObject ( hpen ) ;
hpen = CreatePen ( PS_DASHDOTDOT, 1, RGB ( 255, 0, 0 ) ) ;
holdpen = SelectObject ( hdc, hpen ) ;
MoveToEx ( hdc, 10, 160, NULL ) ;
LineTo ( hdc, 500, 160 ) ;
SelectObject ( hdc, holdpen ) ;
DeleteObject ( hpen ) ;
hpen = CreatePen ( PS_SOLID, 10, RGB ( 255, 0, 0 ) ) ;
holdpen = SelectObject ( hdc, hpen ) ;
MoveToEx ( hdc, 10, 210, NULL ) ;
LineTo ( hdc, 500, 210 ) ;
SelectObject ( hdc, holdpen ) ;
DeleteObject ( hpen ) ;
EndPaint ( hWnd, &ps ) ;
}
On execution of this program the window shown in Figure 18.3 appears.










A new pen can be created using the CreatePen( ) API function. This function needs three parameters—pen style, pen thickness and pen color. Different macros like PS_SOLID, PS_DOT, etc. have been defined in ‘windows.h’ to represent different pen styles. Note that for pen styles other than PS_SOLID the pen thickness has to be 1 pixel.

No comments:

Post a Comment