As we all know a PEN GDI object has three general attributes -

  • style of the line,
  • width in pixel,
  • color in RGB
When pen style is PS_NULL all other two attributes are not applicable and it is treated as hollow or transparent. GDI APIs which draws shapes in two steps
  • Draw outer lines
  • Fill inner area as per selected brush
With a style PS_NULL style it will draw a shape whose outer lines are invisible.

PS_SOLID vs PS_NULL

For example if this is a rectangle then outer 1 pixel will be transparent as background color and inner rectangle area will be filled with the color and style of selected brush.

Solid Style PEN Drawing:

  case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    hpen = CreatePen(PS_SOLID, 3, RGB(50,50,50));
    logbrush.lbColor = RGB(200,200,200);
    logbrush.lbStyle = BS_SOLID;
    logbrush.lbHatch = 0;
    hbrush = CreateBrushIndirect(&logbrush);
    old_hbrush = (HBRUSH) SelectObject(hdc, (HGDIOBJ)hbrush);
    old_hpen = (HPEN) SelectObject(hdc, (HGDIOBJ)hpen);
    Rectangle(hdc, 10, 10, 100, 100);
    SelectObject(hdc, (HGDIOBJ)old_hbrush);
    SelectObject(hdc, (HGDIOBJ)old_hpen);
    DeleteObject(hbrush);
    DeleteObject(hpen);
    EndPaint(hWnd, &ps);
    break;
NULL Style PEN Drawing:
  case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    hpen = CreatePen(PS_NULL, 3, RGB(50,50,50));
    logbrush.lbColor = RGB(200,200,200);
    logbrush.lbStyle = BS_SOLID;
    logbrush.lbHatch = 0;
    hbrush = CreateBrushIndirect(&logbrush);
    old_hbrush = (HBRUSH) SelectObject(hdc, (HGDIOBJ)hbrush);
    old_hpen = (HPEN) SelectObject(hdc, (HGDIOBJ)hpen);
    Rectangle(hdc, 10, 10, 100, 100);
    SelectObject(hdc, (HGDIOBJ)old_hbrush);
    SelectObject(hdc, (HGDIOBJ)old_hpen);
    DeleteObject(hbrush);
    DeleteObject(hpen);
    EndPaint(hWnd, &ps);
    break;

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#