CGContextSetLineDash : Need a Windows Equivalent

The declare in the title can be used in COCOA to set line patterns (dotted, dashed etc)…
Does anyone have a snippet that does the same thing for WINDOWS?

Here is how I use the declare in OSX

  Dim lengths(-1) As Double
  Dim x As Integer
  Dim lengthArray As MemoryBlock
  Declare Sub CGContextSetLineDash Lib "Cocoa" ( context As Integer, phase As Single, lengths As Ptr, count As UInt32)
  
  x=g.penwidth
  Select Case pattern_id
  Case BorderDotted
    lengths=Array(Ceil(x/2.),x*2.) ' dotted line [.......]
  Case borderDashed
    lengths=Array(x*8.,x*4.) ' dashed line [- - - -]
  Case Else
    pattern_id=BorderSolid
  End Select
  //
  If pattern_id<>BorderSolid Then
    lengthArray= zz_CFloatArray(lengths)
    If lengthArray Is Nil Then Return
    CGContextSetLineDash g.handle( g.HandleTypeCGContextRef ),start_at,lengthArray,lengths.Ubound+1
  Else
    CGContextSetLineDash g.handle( g.HandleTypeCGContextRef ),0,Nil,0 ' solid line
  End If

My current project has ONE declare keeping it from being “x-plat”, and this is the one

I would use a GDI plus pen and set DashStyle on it

Here is .NET example.

http://www.openwinforms.com/line_pattern_in_gdi_plus.html

Though same functions apply for non managed GDI+. So you can do same in C++, which means you should be able to declare into GDI+.

Thanks… but .Net code doesn’t help … since that simply shows what I WANT to do, not HOW to do it

What I need is an example of the proper DECLARE to use when XOJO is compiled for Windows

[quote=165571:@Dave S]Thanks… but .Net code doesn’t help … since that simply shows what I WANT to do, not HOW to do it

What I need is an example of the proper DECLARE to use when XOJO is compiled for Windows[/quote]

The Win32 toolbox is far from being identical to the Cocoa one. I am not sure a declare is available.

Why not use a good’ol Xojo Canvas and a few Xojo code lines ?

I am using a Canvas… the declare in my first post creates a “brush” in OSX, and I know (and confirmed by above response) that something most likey exists for windows.

Drawing a dotted or dashed line in “pure xojo code” is VERY VERY SLOW (compared)

With the declare I am using I say

myLineStyle(g,BorderDotted)
g.drawline 0,0,100,100

and its a done deal

otherwise I would have to implement a Bresham (sic) line algorithm with is time consuming… if I can get the OS to do the work for me…

there is this
https://forum.xojo.com/10126-simple-way-to-draw-a-dotted-line

but no mention of how to implement the GDIPlus object

[quote=165580:@Dave S]I am using a Canvas… the declare in my first post creates a “brush” in OSX, and I know (and confirmed by above response) that something most likey exists for windows.

Drawing a dotted or dashed line in “pure xojo code” is VERY VERY SLOW (compared)

With the declare I am using I say

myLineStyle(g,BorderDotted)
g.drawline 0,0,100,100

and its a done deal

otherwise I would have to implement a Bresham (sic) line algorithm with is time consuming… if I can get the OS to do the work for me…

there is this
https://forum.xojo.com/10126-simple-way-to-draw-a-dotted-line

but no mention of how to implement the GDIPlus object[/quote]

Looks like Alain Bailleul posted a method.

where?

if you mean

 dim gfx as new GdiPlusGraphics( g.Handle( Graphics.HandleTypeHDC ) )
  dim brush as new GdiPlusSolidBrush(new GdiPlusColor( Alpha, col.Red, col.Green, col.Blue ))

that is the “no mention of how to implement the GDIPLUS objects”

Xojo uses flat GDI objects. Meaning their basically just C calls. GDI plus both comes as C++ object API as well as flat API

So you just need to check the MSDN for their flat names.

Declaring into the flat API would just be like declaring into any other function I would imagine.

I’m not a windows guy… but thanks for trying to help…

Some time ago, there was some code floating around for faking a dashed line… I realize that a declare is the best solution, but you may end up having to resort to faking it…

The biggest problem with x-plat nowadays, is feature parity. Mobile developers have the same issue, where Apple has pumped up the iOS toolbox far quicker than Google, making it even less rewarding to create an Android version of your application.

Way WAY back (as in over a decade) there was a class named DotLine by Søren Olin. Is that what you are referring to?

I have some code for drawing patterned lines with pure xojo that I need to clean up that I am considering open sourcing… No fancy math just an empirical solution.

I sort of have a web page about it, but I never seem to get around to getting the code ready to upload and finishing documentation… What It does can be seen here:

http://katkosoft.xojonews.com/StyledLine/Styledlines.html

Yikes Karen! You took dash lines to the extreme!

There is a Windows API that seems to be the equivalent to CGContextSetLineDash
and I believe it is SETPENDASH or something like that (part of GDI+)

and I guess in plain GDI there is/was

Declare Function CreatePen Lib “gdi32” (ByVal fnPenStyle As Long, ByVal w As Long, ByVal crColor As Long) As Long

but can’t find anything to tell me exactly how to implement it… seeing as the OSX versionis related to a specific graphics object… and all.

and for now I need 3 line styles … DOTTED, DASHED and SOLID (obviously solid is easy)

and YES I know I could do

for x=x1 to x2 step 8
g.drawline x,y,x+4,y
next x

but I would prefer to do it “right” (and of course as fast as possible)

[quote=165949:@Dave S]There is a Windows API that seems to be the equivalent to CGContextSetLineDash
and I believe it is SETPENDASH or something like that (part of GDI+)

and I guess in plain GDI there is/was

Declare Function CreatePen Lib “gdi32” (ByVal fnPenStyle As Long, ByVal w As Long, ByVal crColor As Long) As Long

but can’t find anything to tell me exactly how to implement it… seeing as the OSX versionis related to a specific graphics object… and all.

and for now I need 3 line styles … DOTTED, DASHED and SOLID (obviously solid is easy)

and YES I know I could do

for x=x1 to x2 step 8
g.drawline x,y,x+4,y
next x

but I would prefer to do it “right” (and of course as fast as possible)[/quote]

The other thing… with the OSX call… I don’t have to alter any of my drawing code at all… regardless of the line pattern I call the same line drawing routine. resorting to the above method would cause me to have to alter tons of existing code

LOL!

I wrote that originally back in 2009 she I was out of work for an extended period (1.5 years!!!).

It was during that same period that I also wrote my listbox subclass.

I had to do something to keep my mind off of being out of work in the worst recession since the great depression while in my 50s!

  • karen

[quote=165949:@Dave S]Declare Function CreatePen Lib “gdi32” (ByVal fnPenStyle As Long, ByVal w As Long, ByVal crColor As Long) As Long

[/quote]

Check the Windows Functionality Suit?

Aaron also wrote some Advanced Graphics Classes for Windows … I have not looked at in ages but I have a copy, I can send it to you if you want.

Aaron Ballman’s Advanced Graphics for Windows has the declares and examples of using them… is that hosted anywhere these days?

I think I originally got it from his website when he worked for REALSoftware.

In any case in this day and age Xojo really should directly support pattered lines and gradients at least, along other graphics goodies that are supported by both Windows and OSX . Xojo’s built-in graphics capabilities are really very behind the times!!!

My Bezo class draws solid or dashed bezier curves of any number of control points. The demo starts with 5 control points which you can drag around, context-click a point to delete it or context-click elsewhere to append a point. original post

It doesn’t automatically support curve chaining, which would also need supplying an initial dash pattern phase. Should be easy to add.

[quote=165958:@Karen Atkocius]Aaron Ballman’s Advanced Graphics for Windows has the declares and examples of using them… is that hosted anywhere these days?
[/quote]

https://forum.xojo.com/10893-problems-with-declaring-a-function/p1#p78264

In GDI+ you pass a pen object to each drawing primitive. There is no way to set a dashed style globally that subsequent drawing commands will just use. Here is an example of drawing a dashed line. You must enable UseGDIPlus to use it.

Sub DrawDash(g as graphics, x1 as integer, y1 as integer, x2 as integer, y2 as integer)
  const DashStyleSolid = 0
  const DashStyleDash = 1
  const DashStyleDot = 2
  const DashStyleDashDot = 3
  const DashStyleDashDotDot = 4
  const DashStyleCustom = 5
  
  declare function GdipCreatePen1 lib "gdiplus" (c as integer, width as single, unit as integer, byref p as integer) as integer
  declare function GdipSetPenDashStyle lib "gdiplus" (pen as integer, style as integer) as integer
  declare function GdipCreateFromHDC lib "gdiplus" (hdc as integer, byref g as integer) as integer
  declare function GdipDrawLine lib "gdiplus" (g as integer, pen as integer, x1 as single, y1 as single, x2 as single, y2 as single) as integer
  declare function GdipDeletePen lib "gdiplus" (pen as integer) as integer
  declare function GdipDeleteGraphics lib "gdiplus" (g as integer) as integer
  
  dim pen, n as integer
  dim c as color
  dim mb as new MemoryBlock(4)
  c = g.ForeColor
  mb.byte(0) = c.blue
  mb.byte(1) = c.green
  mb.byte(2) = c.red
  mb.byte(3) = 255    // alpha = opaque
  n = mb.Int32Value(0)
  call GdipCreatePen1(n, g.PenWidth, 2, pen)
  call GdipSetPenDashStyle(pen, DashStyleDash)
  
  dim gpx as integer
  call GdipCreateFromHDC(g.handle(Graphics.HandleTypeHDC), gpx)
  
  call GdipDrawLine(gpx, pen, x1, y1, x2, y2)
  
  call GdipDeletePen(pen)
  call GdipDeleteGraphics(gpx)
  
End Sub