Sunday, August 15, 2010

C# Round WinForms Helper

While figuring out what values were needed for the call to CreateRoundRectRgn for my tab in the previous post, C# Round WinForms Helper I made this small helper application. 


The source code can be downloaded here RoundedCorners.zip.

It allows you to use the levers to figure out the needed values of the different parameters based on what size the round form is.

Main.cs


using System;
using System.Drawing;
using System.Windows.Forms;

namespace TMData.RoundedCorners
{
 public partial class Main : Form
 {
  private RoundedCorners roundForm;
  public Main()
  {
   InitializeComponent();
  }

  private void timer1_Tick(object sender, EventArgs e)
  {
   formHeightTextBox.Text = roundForm.Height.ToString();
   formWidthTextBox.Text = roundForm.Width.ToString();

   label2.Text = trackBar2.Value.ToString();
   label3.Text = trackBar3.Value.ToString();
   label4.Text = trackBar4.Value.ToString();
   label5.Text = trackBar5.Value.ToString();
   label6.Text = trackBar6.Value.ToString();
   label7.Text = trackBar7.Value.ToString();

   roundForm.Region = Region.FromHrgn(Win32.CreateRoundRectRgn(trackBar2.Value, trackBar3.Value, trackBar4.Value, trackBar5.Value, trackBar6.Value, trackBar7.Value));
   generatedStatmentTextBox.Text = string.Format("CreateRoundRectRgn({0},{1},{2},{3},{4},{5})",trackBar2.Value, trackBar3.Value, trackBar4.Value, trackBar5.Value, trackBar6.Value, trackBar7.Value);
  }
  
  private void RoundedCorners_Load(object sender, EventArgs e)
  {
   roundForm = new RoundedCorners(this);
   roundForm.Show();
   roundForm.UpdateParentTrackBars();
   timer1.Start();
  }
 }
}


RoundedCorners.cs

using System;
using System.Windows.Forms;

namespace TMData.RoundedCorners
{
 public partial class RoundedCorners : Form
 {
  private Main parentForm;
  public RoundedCorners()
  {
   InitializeComponent();
  }

  public RoundedCorners(Main parentForm) : this()
  {
   this.parentForm = parentForm;
  }

  private void RoundedCorners_SizeChanged(object sender, EventArgs e)
  {
   UpdateParentTrackBars();
  }
  public void UpdateParentTrackBars()
  {
   parentForm.trackBar2.Maximum = Width;
   parentForm.trackBar4.Maximum = Width;
   parentForm.trackBar4.Value = Width;
   parentForm.trackBar3.Maximum = Height;
   parentForm.trackBar3.Value = Height;
   parentForm.trackBar5.Maximum = Height;
   parentForm.trackBar6.Maximum = Width;
   parentForm.trackBar7.Maximum = Height;
  }
 }
}


And Wins32.cs

using System;
using System.Runtime.InteropServices;

namespace TMData.RoundedCorners
{ 

 class Win32
 {
  /// <summary>
  /// Creates the round rect RGN.
  /// </summary>
  /// <param name="nLeftRect">The n left rect.</param>
  /// <param name="nTopRect">The n top rect.</param>
  /// <param name="nRightRect">The n right rect.</param>
  /// <param name="nBottomRect">The n bottom rect.</param>
  /// <param name="nWidthEllipse">The n width ellipse.</param>
  /// <param name="nHeightEllipse">The n height ellipse.</param>
  /// <returns></returns>
  [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
  public static extern IntPtr CreateRoundRectRgn
  (
   int nLeftRect,
   int nTopRect, // y-coordinate of upper-left corner
   int nRightRect, // x-coordinate of lower-right corner
   int nBottomRect, // y-coordinate of lower-right corner
   int nWidthEllipse, // height of ellipse
   int nHeightEllipse // width of ellipse
  );
 }
} 


Since each winform runs in a thread there should ideally be some syncronization rather than allowing the RoundedCorners form access to the Main form's controls, However since the point of this application was as to illustrate the use of CreateRoundRectRgn() I will leave it as is.

No comments: