views:

356

answers:

4

I have a need to do some drawing using win32/GDI (Native, not .NET), and I've run into the following problem:

I need to draw lines that are "styled." For example, in the attached image, the line marked "A" is a straight line as far as my application data is concerned, it just needs to be drawn with the additional zigzag as a style. Of course, this is easy to do programatically, but it gets more complicated when the line can be at any angle ("B") or even a bezier curve ("C").

Now, I could do this all programatically, painstakingly doing the math to put a zigzag around each line possibility, but that will take a lot of time and, more importantly, be rather error prone.

Is it possible to just give windows/GDI a "style" to apply to the line, perhaps a bitmap like the one marked "D", and have it use that as a pen to draw the lines? If not, is there a more flexible and less error-prone way of doing this than writing a bunch of specific drawing code for each of the "styled" lines?

*Apparently first timers can't post images. Examples can be found at http://i.imgur.com/IC0T2.png

A: 

ExtCreatePen(), maybe? I don't know for a fact if it supports zigzagging...

Seva Alekseyev
+2  A: 

This is not possible in Win32 GDI. You will need to do the math yourself.

It should be noted however, that you can obtain the points used to make up the line or curve which should make it substantially easier.

See this "Hit-Testing" tutorial for an example.

For a bezier curve you would use Path Functions:

For straight lines you could use:

LineDDA

+1  A: 

As far as I know there's nothing in GDI or even GDI+ that would support this. The only line options you have are dash-patterns, compound-pens, dash caps, end caps, and fill brushes.

You might be able to trick one of those functions into drawing something vaguely akin to your wiggles for straight splines, but it definitely won't work for curved splines.

It shouldn't be too hard to do this however. Sure, it will take a day or so, but all you have to do is write a line and bezier interpolator, divide the curves into equal segments, find the tangents at all those segments and alternate left and right. You'll end up with an array of points which can be drawn very quickly as a polyline.

David Rutten
A: 

There's nothing that'll do this automatically. You'll have to write some code. You might want to look at the LineDDA API in GDI. It might simplify the math your code will need.

Adrian McCarthy