Sign in to follow this  
Guest CWR Weather Radar

Using GDI+ to Draw Needles

Recommended Posts

Hello, Experts!Once again, I'm in need of your guidance! I'm working on an EICAS gauge, using GDI+. I'm not 100% certain the best way to animate the needles (N1, EGT) using GDI+. Also, it would have to be in such a way that I could fill the area between 0%N1 and whatever the current setting is. I've thought of using a closed curve but haven't had too much luck making them turn out just right. A do...until loop has also crossed my mind... Basically I'm wondering how to draw the needles to be manipulated by the token variable. Thanks!Matt

Share this post


Link to post
Share on other sites
Help AVSIM continue to serve you!
Please donate today!

I draw them making use of the matrix features of GDI+ which makes it quite simple.My steps would be:Prep. work:Work out what angle of rotation the needle needs to be at 0% and 100%. Write code to generate an angle given the current % value of N1 etc.Write code to draw the needle at a position of 0,0 and an angle of 0.To draw the needle:Get current angle of the needleSet the current rotation matrix in GDI+ to that angleSet the current translation matrix to the x,y position of the needleDraw the needle.Reset rotation and translations.This would result in a needle rotated by the correct amount and in the correct position.To fill areas on a curve take a look at the FillArc function.

Share this post


Link to post
Share on other sites

Okay, I think I understand what you're saying but I'm completely new to GDI+ and I'm not sure how to do all that. I mean, academically, yes, I know this is basically what I'd need to do. Would you be willing to make a generic example? Thanks.

Share this post


Link to post
Share on other sites

Yes I can. It will be tonight after work before I can start on it though.

Share this post


Link to post
Share on other sites

Thanks, I appreciate it. I can pretend to be patient since there's a lot of other stuff to work on! I'll be eagerly awaiting your input.

Share this post


Link to post
Share on other sites

I haven't had chance to test this as I use my own abstract graphics engine inside all my gauges now and don't have the time to build another pure GDI+ gauge at the moment. This should work though:This assumes that:'g' is a pointer to your GDI+ graphics engine,If the needle is drawn at an angle of 0 it will point straight upwards.The needle is being drawn at a position of 50,75 and an angle of 45 degrees.A pen has been created with the desired colour and width.The needle is 20 pixels long.REAL angle = 45;// reset any previously set transformsg->ResetTransform();// set the correct needle rotationg->RotateTransform(angle, MatrixOrderAppend);// position the needle in the correct placeg->TranslateTransform(50, 75, MatrixOrderAppend);// draw the needleg->DrawLine(pen, 0, 0, 0, -20);// reset transforms ready for the next drawingg->ResetTransform();

Share this post


Link to post
Share on other sites

Okay, thanks. I'll give this a shot. I appreciate your time and effort.

Share this post


Link to post
Share on other sites

Okay, I've gotten some results with that, thanks. I couldn't find any documentation on any matrix other than ColorMatrix (which I don't think will work for this, right?) I'm getting the needle to rotate properly and the DrawLine length is the right length. But it's not rotating the full range. I've messed with some of the elements here, but couldn't get the desired results. Here's what I have:REAL n1l = val_n1l + 90.0f;graphics.ResetTransform();graphics.RotateTransform(n1l, MatrixOrderAppend);graphics.TranslateTransform(88, 88, MatrixOrderAppend);graphics.DrawLine(&witPen, 0, 0, 0, -40);graphics.ResetTransform();The range of the rotation is 240 degrees.

Share this post


Link to post
Share on other sites

It looks like you just need to scale the value you are using for angle n1l.I don't know what val1_n1l is, if it is 0 to 1 then the line should be:REAL n1l = (val_n1l*240) + 90;If val_n1l is 0 to 100 try:REAL nl1 = ((val_n1l*240)/100) + 90;If you go to the MSDN library at:http://msdn.microsoft.com/library/default.aspYou can find a full GDI+ reference, look for the transformation section. The correct combination of rotations and translations makes what might seem a difficult drawing problem quite simple a lot of the time!

Share this post


Link to post
Share on other sites

Thanks. That's given me a much better result. I used the second solution after trying the first.val_n1l = N1% for the #1 engine (N1 left). I had tacked the "+ 90" to put the needle at 90 degrees as the starting position (Engine off). Apparently I need to subtract that 90 degrees from my travel area, but I'll mess with it. I'm relatively new to gauges in the first place (I had done quite a few of the older Macro-style gauges before. They recently ran "down the curtain and joined the choir invisible" as John Cleese puts it). I'm totally new to GDI+; a lot of the concepts I learned to write the previous gauges don't apply to these! I guess that's the long way of saying, in violation of Strunk & White's Rule 8, I appreciate your help immensely.Thanks.

Share this post


Link to post
Share on other sites

Why dont you draw a filled pie? i think thats a quite simple and fast solution ?Florian

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this