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)
{
}
}
}
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)
{
}
}
}