Newest Latest Project

| | Comments (6)

Well, I've always been interested in GDI+, so I've finally bit the bullet, and am porting it into REALbasic code. For those of you who don't know, GDI+ is to GDI what CoreGraphics is to QuickDraw. It's a set of graphics APIs that's been around for quite some time now on Windows (I know it came out with XP, but I think it was out even sooner than that) that really extend what can be done with the old GDI calls. It's really quite a neat set of APIs.

What Microsoft did was make a set of regular APIs that do all of the GDI+ functionality. Then, in their header files, they wrapped all of the flat APIs with a set of C++ classes so that you can use it in an object-oriented manner. This turns out to be a very good thing for REALbasic users since it means that you can do the wrapping in REALbasic as well.

One of the reasons I shunned doing this sooner was because REALbasic, as a language, just wasn't ready for the task of making an elegant set of classes like Microsoft had done. This was back in the early 5.0 days, before there were structures, enumerations, multiple numeric datatypes, etc. A lot of the functionality simply wasn't present in the language to allow it. But the deal-breaker for me was the lack of soft declare support. Without that, no one would want to use the classes since it would cause their entire application to simply not launch.

But now, due to all the awesome new language features that the new compiler allows, REALbasic is ready for the task to start porting. And boy, what a long undertaking this will be. There's something around 600 different APIs to port!

I started this little project last night as a proof of concept, and I've already gotten it to the point of drawing text on the screen.

GDI+ proof of concept

What you are seeing here is a my proof of concept application. It's a Unicode string, drawn with 50% transparency, in blue color, with an MS Sans Serif, 16 pt strikethrough font using font-linking (for an otherwise unprintable set of characters in the MS Sans Serif font).

The code to do this looks like this:[rbcode]
dim gfx as new GdiPlusGraphics( g.Handle( Graphics.HandleTypeHDC ) )
dim myFont as new GdiPlusFont( "MS Sans Serif", 16, FontStyle.Strikeout )
dim origin as new PointF( 10, 10 )
dim brush as new GdiPlusSolidBrush( new ARGB( ARGB.MakeARGB( 128, 0, 0, 255 ) ) )

dim theStr as String = "ABCАБВกขฃ"
dim s as Status
s = gfx.DrawString( theStr, -1, myFont, origin, brush )[/rbcode]
As you can see, the code isn't overly complex -- you simply need to make the font, the location and the brush style, and then draw the text.

The ListBox and PushButton are another proof of concept for displaying all of the font families installed on my system. The code behind the button looks like this:[rbcode] dim fonts as new GdiPlusInstalledFontCollection
dim families( -1 ) as GdiPlusFontFamily
dim count as Integer = fonts.GetFamilyCount

dim numFound as Integer
if fonts.GetFamilies( count, families, numFound ) = Status.Ok then
ListBox1.DeleteAllRows

for each family as GdiPlusFontFamily in families
dim s as String
call family.GetFamilyName( s )
ListBox1.AddRow( s )
next family
end if[/rbcode]
Again, nothing terribly complex -- I'm simply getting the installed font collection (as opposed to a private font collection), getting all of the font families and listing their names. One thing you'll notice is that GDI+ uses ByRef parameters to return information since the vast majority of the APIs return a Status enumeration. This forces you to do better error checking (especially in languages like REALbasic, where you can't ignore the return values).

Like I said, this is an enourmous undertaking, so don't expect to see too many amazing things happening too quickly. So far, the list of classes I have completed is rather small (all of the font related classes, Brush, SolidBrush and the base class), but it's coming along relatively easily. I'll try to post progress updates as interesting things happen.

6 Comments

Dude, don't you ever sleep? :D

Only when forced to.

Cool. I wanted to use GDI+ for some of my projects, but didn't because porting all of it to RB would've been more trouble than it's worth for what I wanted it for.

Nice little addition to the community.

This is pretty cool.

Very nice sample with the Text... although I wouldn't have known that it was 50% transparent without you saying so.

Yeah, if I drew the text over something else, it'd be easier for you to see the transparency part of it.

Leave a comment

Disclaimer

I'm currently an employee of REAL Software. My blog is mine. The opinions represented in this blog are mine as well and may not represent my employer's opinions. All original material is copyrighted and property of the author.

REALbasic® is a registered trademark of REAL Software, Inc. REAL SQL Server™ and Lingua™ are pending trademarks of REAL Software, Inc. All rights reserved.