Click to See Complete Forum and Search --> : GDI+ programming - Altimeter


wilso_s
March 10th, 2005, 08:07 AM
In my attempt build this altimeter, I have the following code in C#. My question is really with the GDI+ interface. My question is : How do I get the numbers to stand right side up next to their major tick marker counterpart when the form is resized? In this example, they are at all differenct angles. I'm new to C# and graphics programming and I've been trying hard and am very slowly making progress but I'm stuck with the numbers not displaying right side up. I'm hoping someone familiar with GDI+ can make a suggestion on how to get the numbers right side up and to stick close to their major tick mark as the form is resized. I thank everyone who can suggest something.



using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Windows.Forms;

namespace tmpTicks
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(416, 349);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.Resize += new System.EventHandler(this.Form1_Resize);
}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;

g.SmoothingMode = SmoothingMode.AntiAlias;

Font f = new Font("Times New Roman", 12,System.Drawing.FontStyle.Bold);

g.TranslateTransform(this.Width /2,this.Height/2);
g.FillEllipse(Brushes.Black,this.Height/-2,this.Height/-2,this.Height, this.Height);


//20's tick markers
for(int x = 0; x<50;x++)
{
g.FillRectangle(Brushes.White,-2,(this.Height/2)* -1,2,15);
g.RotateTransform(7.2F);
}
g.ResetTransform();

//100' tick markers
g.TranslateTransform(this.Width /2,this.Height/2);

int sp = 0;
for(int x = 0; x<10;x++)
{
g.FillRectangle(Brushes.White,-3,(this.Height/2 )* -1,3,25);
g.DrawString(sp.ToString(),f,Brushes.White,(sp.ToString().Length)*(-6),(this.Height/-2) + 25);
g.RotateTransform(36);
sp += 1;
}
g.ResetTransform();

}

private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}


private void Form1_Load(object sender, System.EventArgs e)
{

}
}
}

Norfy
March 10th, 2005, 10:28 AM
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle bounds = this.ClientRectangle;
int x = bounds.Width / 2;
int y = bounds.Height / 2;
int radius = y;

g.SmoothingMode = SmoothingMode.AntiAlias;

g.TranslateTransform(x, y);
g.FillEllipse(Brushes.Black, -radius, -radius, radius * 2, radius * 2);

// draw ticks
for(int i = 0; i < 50; i++)
{
// large tick every 5
if (i % 5 == 0)
{
g.FillRectangle(Brushes.White, -3, -radius, 3, 25);
}
else
{
g.FillRectangle(Brushes.White, -2, -radius, 2, 15);
}
g.RotateTransform(7.2F);
}

// draw numbers
using (StringFormat format = new StringFormat())
{
Font font = new Font("Times New Roman", 12, System.Drawing.FontStyle.Bold);

format.LineAlignment = StringAlignment.Center;
format.Alignment = StringAlignment.Center;

for(int i = 0; i < 10; i++)
{
g.ResetTransform();
g.TranslateTransform(x, y);
g.RotateTransform(36f * i);
g.TranslateTransform(0, (float)(-radius + 35));
g.RotateTransform(-36f * i);

g.DrawString(i.ToString(), font, Brushes.White, new Rectangle(-10, -10, 20, 20), format);
}
font.Dispose();
}

}

wilso_s
March 10th, 2005, 11:28 AM
Norfy,

Thankyou very much for taking your time to help me with this. You even fixed the cutoff of the ellipse at the bottom of the form, and simplified the code too. This is much more than what I was looking for and I appreciate it a lot.

Norfy
March 11th, 2005, 02:13 AM
No problem, I enjoyed solving the problem.