Home > MFC, Windows API > Using PS_USERSTYLE for creating funky pens!

Using PS_USERSTYLE for creating funky pens!

Using ExtCreatePen and PS_USERSTYLE you can create funky styled pens instead of the old PS_DOT, PS_DASHDOT, PS_DASHDOTDOT style pens. When using PS_USERSTYLE for creating pens you’ve got specify an array of DWORD which holds dash and space length while drawing a user styled pen!

I’ve created a sample application which demostrates marching ants demo, if you’ve worked in photoshop then you’ve already seen it after selecting a portion of an image…

A screenshot of marching ants…

Demo of marching ants

Demo of marching ants

Download Executable (Rename to exe)

Here is the code!

// Add this to "OnPaint" function to have some fun :smiley:
void CDialogTestDlg::DrawMarchingStuff()
   CPaintDC dc(this);
   const int SavePoint = dc.SaveDC();
   // Just for demo, make this a member or whatever but not static !!!
   static CFont fnt;
   if( !fnt.GetSafeHandle() )
      fnt.CreatePointFont( 700, _T( "verdana" ), &dc );
   dc.SelectObject( fnt );
   // Create new ext pen
   CBrush Brush;
   Brush.CreateSolidBrush( RGB( 255, 00, 178 ));
   LOGBRUSH lBrush = { 0 };
   Brush.GetLogBrush( &lBrush );
   dc.SelectStockObject( NULL_BRUSH );
   int DashLen = 1;
   int SpaceLen = 2;
   // Well this is the array that I was talking about, every value at "odd index" denotes dash length and every
   // every value at "even index" denotes space length
   CPen NewPen;
   DWORD Style1[] = { DashLen, SpaceLen };
   DWORD Style2[] = { DashLen, SpaceLen + 1 }; // Secret to animation
   static int Decider = 0;
   int StyleLength = 0;
   DWORD* pStyle = NULL;
   if( !Decider )
      StyleLength = sizeof(Style1)/sizeof(Style1[0]);
      pStyle = Style1;
      Decider = 1;
      StyleLength = sizeof(Style2)/sizeof(Style2[0]);
      pStyle = Style2;
      Decider = 0;
   NewPen.CreatePen( PS_GEOMETRIC | PS_USERSTYLE, 1, &lBrush, StyleLength, pStyle );
   // Hey Mr DC use MY pen
   dc.SelectObject( &NewPen );
   // This text is not drawn on screen, but instead each action is being  
   // recorded and stored internally as a path, since we called BeginPath  
   dc.TextOut( 20, 20, _T( "Hello Nibu" ));  
   // Stop path  
   // Draw outlined text
   // Draw a marching rectangle wrapping above text
   dc.Rectangle( 20, 30, 500, 130 );
   // Restore previous stuff
   dc.RestoreDC( SavePoint );
  1. bruce
    March 19, 2009 at 3:09 am

    Just what I needed. Thanks!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: