locked
How to give a C# Windows forms application rounded corners? RRS feed

  • Question

  • Hi,

    As part of our application, one of the windows forms will need to have rounded corners, also must not use the standard windows top-menu bar with the close, minimise buttons etc.

    Basically, I'm looking to create something with a similar design feel to the yousendit express application. See screenshot here:
    http://garrymccarthy.com/yousendit.jpg

    It also has rounded corners and has a large area at the top of the form which allows the user to move it around the screen.

    I'm not an expert with code, I'm dealing with front end design, so if anyone has a link to an example online, I'd greatly appreciate it.

    Cheers,

    Garry.

    Friday, April 30, 2010 10:46 AM

Answers

  • You're welcome! What is wrong with the examples above? I have used this technique many times. Here it is again, copied into a defualt Winforms project... It doesnt get any more working than that...

    Good luck!

     

    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.IO;
    
    namespace SandBox
    {
    
     public partial class Form1 : Form
     {
    
      [System.Runtime.InteropServices.DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
      private static extern System.IntPtr CreateRoundRectRgn
      (
       int nLeftRect, // x-coordinate of upper-left corner
       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
      );
    
      [System.Runtime.InteropServices.DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
      private static extern bool DeleteObject(System.IntPtr hObject);
    
    
      public Form1()
      {
       InitializeComponent();
      }
    
      private void Form1_Paint(object sender, PaintEventArgs e)
      {
       System.IntPtr ptr = CreateRoundRectRgn(0, 0, this.Width, this.Height, 50, 50); // _BoarderRaduis can be adjusted to your needs, try 15 to start.
       this.Region = System.Drawing.Region.FromHrgn(ptr);
       DeleteObject(ptr);
    
      }
     }
    }
    [edit] To further this: The Region property contains methods (such as Union, Compliment, Xor, and a few others) that allow you to "blend" various "shapes" together.. This gives you many more options in your custom form design.
    • Proposed as answer by Helen Zhou Monday, May 3, 2010 9:04 AM
    • Edited by Steven_Schultz Wednesday, May 5, 2010 11:27 AM
    • Marked as answer by Helen Zhou Thursday, May 6, 2010 3:41 AM
    Friday, April 30, 2010 3:31 PM
  • Here are some interesting stuff you can do with forms and controls by Mick Doherty ...

    HTH,
    Vladimir

    • Proposed as answer by Helen Zhou Monday, May 3, 2010 9:04 AM
    • Marked as answer by Helen Zhou Thursday, May 6, 2010 3:41 AM
    Saturday, May 1, 2010 3:09 PM
  • Hi GarryMac,

    Also this may help you : How To Make A Form With Round Corners

    Sincerely,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Thursday, May 6, 2010 3:41 AM
    Monday, May 3, 2010 9:08 AM

All replies

  • Friday, April 30, 2010 11:06 AM
  • The winform has a property REGION..

    You can use the forms drawing REGION in conjunction with CreateRndRectangle() methods in GDI32.dll...

    For example:

     

        [System.Runtime.InteropServices.DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern System.IntPtr CreateRoundRectRgn
        (
          int nLeftRect, // x-coordinate of upper-left corner
          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
        );
        [System.Runtime.InteropServices.DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
        private static extern bool DeleteObject(System.IntPtr hObject);
    
    // Imports needed methods for RoundRect object above.. Below can be placed in the Paint method of form..
    
    
    
          System.IntPtr ptr = CreateRoundRectRgn(0, 0, this.Width, this.Height, _BorderRadius, _BorderRadius); // _BoarderRaduis can be adjusted to your needs, try 15 to start.
    
          this.Region = System.Drawing.Region.FromHrgn(ptr);
          DeleteObject(ptr);
    Friday, April 30, 2010 1:08 PM
  • Thanks for them, guys, I saw those online already though.

    Does anyone have links to working examples?

    Friday, April 30, 2010 2:10 PM
  • You're welcome! What is wrong with the examples above? I have used this technique many times. Here it is again, copied into a defualt Winforms project... It doesnt get any more working than that...

    Good luck!

     

    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.IO;
    
    namespace SandBox
    {
    
     public partial class Form1 : Form
     {
    
      [System.Runtime.InteropServices.DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
      private static extern System.IntPtr CreateRoundRectRgn
      (
       int nLeftRect, // x-coordinate of upper-left corner
       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
      );
    
      [System.Runtime.InteropServices.DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
      private static extern bool DeleteObject(System.IntPtr hObject);
    
    
      public Form1()
      {
       InitializeComponent();
      }
    
      private void Form1_Paint(object sender, PaintEventArgs e)
      {
       System.IntPtr ptr = CreateRoundRectRgn(0, 0, this.Width, this.Height, 50, 50); // _BoarderRaduis can be adjusted to your needs, try 15 to start.
       this.Region = System.Drawing.Region.FromHrgn(ptr);
       DeleteObject(ptr);
    
      }
     }
    }
    [edit] To further this: The Region property contains methods (such as Union, Compliment, Xor, and a few others) that allow you to "blend" various "shapes" together.. This gives you many more options in your custom form design.
    • Proposed as answer by Helen Zhou Monday, May 3, 2010 9:04 AM
    • Edited by Steven_Schultz Wednesday, May 5, 2010 11:27 AM
    • Marked as answer by Helen Zhou Thursday, May 6, 2010 3:41 AM
    Friday, April 30, 2010 3:31 PM
  • Here are some interesting stuff you can do with forms and controls by Mick Doherty ...

    HTH,
    Vladimir

    • Proposed as answer by Helen Zhou Monday, May 3, 2010 9:04 AM
    • Marked as answer by Helen Zhou Thursday, May 6, 2010 3:41 AM
    Saturday, May 1, 2010 3:09 PM
  • Hi GarryMac,

    Also this may help you : How To Make A Form With Round Corners

    Sincerely,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Thursday, May 6, 2010 3:41 AM
    Monday, May 3, 2010 9:08 AM
  • Thanks a million.

    That works perfectly! :)

    Thursday, May 6, 2010 10:09 AM