Skip to main content

How to draw a rounded control in .Net

Although I enjoy reading a lot, this is my very first attempt at writing (and what? "A Blog"). I don't know the basics of writing, so I take for granted that mistakes are ignored ( Better if you comment your heart out).

This post is related to programming a PictureBox object to rotate around another PictureBox object. This is an idea improved upon by me ( The original one is posted by +Rahul Jayant . Here is the link).

Let's now start the fun:-

We are going to use Visual Studio 2010 and C# ( Although I personally prefer VB.net). Steps are as follows :-

1) Create a new project and name it TestDemo (You can name it whatever you want).
2)  Set the size of the form (should be Form1) to 800 x 800.
3) Insert two PictureBox.
Adjust the Location property
First PictureBox      : 200,200
Second PictureBox : 500,300


Adjust the size property
First PictureBox      : 300,300
Second PictureBox : 100,100

4) Download the image below and save to any location of your choice.


Add the following references


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Drawing2D;




Now in the Form1_Load event add the following code


Bitmap mypicture = new Bitmap("C:\\users\\azad\\desktop\\globe.png");
mypicture.MakeTransparent(Color.Black);
this.pictureBox1.Image = mypicture;
this.pictureBox2.Image = mypicture;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;

The path of the file should be adjusted to the downloaded image's path

Add the following function


void circlepath(int radius, Control ctrl)
        {
            GraphicsPath MyPath = new GraphicsPath();

            for (double i = 0.0; i < 360.0; i += 0.1)
            {
                double angle = i * System.Math.PI / 180;
                int x = (int)(radius + radius * System.Math.Cos(angle));
                int y = (int)(radius + radius * System.Math.Sin(angle));
                MyPath.AddLine(new Point(x, y), new Point(x, y));

            }
            MyPath.CloseFigure();
            ctrl.Region = new Region(MyPath);
            MyPath.Dispose();
        }

5) Add a button and double click on it to open the code editor for the click event of the button and add the following code to it :


double radius = 200;
circlepath(150, this.pictureBox1);
circlepath(50, this.pictureBox2);
for (double i = 0.0; i < 360.0; i += 0.1)
{
double angle = i * System.Math.PI / 180;
int x = (int)(300 + radius * System.Math.Cos(angle));
int y = (int)(300 + radius * System.Math.Sin(angle));
pictureBox4.SetBounds(x, y, 100, 100);
Thread.Sleep(1);



And now we are ready to test our code. Hit F5 to run and see the magic. In case of any difficulty just leave a comment and I shall reply to you at the earliest.

Thank you for reading my blog. Any idea or suggestion is heartily welcome.















Comments

Popular posts from this blog

PDF data extraction using Windows PowerShell

PDF data extraction using Windows PowerShell and pdftotext command line utility provided by xpdftool. In this tutorial we will try to extract some data from a batch of invoices and save it in text file The approach would be:- 1) Get a list of Invoices as PDF files. 2) Convert the 1st PDF to plain text file using pdftotext utility. 3) Find the required information and clean it. 4) Save the data to flat file. 5) Repeat steps 2 to 4 for additional files. Let's start Open Run Dialog Box by pressing Windows Key + R Open Powershell ISE by typing, well powershell_ise.exe in Run Dialog as shown. Tip :- PowerShell Integrated Scripting Environment (ISE) is as an IDE for developing and testing Powershell scripts. You can also use any other text editor (notepad, notepad++) to write the powershell scripts. The IDE looks like this. Now let's dive into the script. A) Set the variables #### Variable setting start ##### # Set the pdftotext.exe location...