Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to Draw Circles on GUI?

How to Draw Circles on GUI?

Scheduled Pinned Locked Moved C#
questioncsharpdesigntutorial
22 Posts 5 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Stuck At Zero

    I guess my only options may be to see if I can investigate if I can place 40+ buttons onto the form that I can disable but have visible and see if I can make them circular buttons with the ability for me to change the color of the button. My other way I suppose would be to draw a circle on something like MS Paint and somehow save it with a transparent background. 50+ pictures on a form. The idea would be to either forcibly change the color of the circle on the picture or to use a different picture of the circle of a different color when an event happens. that I can simply change the color of the circle in the picture as needed (or have varying pictures of the same circle). I actually thought this was going to be rather easy, but it seems making simple shapes on the GUI is not as easy as I had thought for C#. Am I better off doing this on Visual C++ or Visual Basic?

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #12

    Stuck At Zero wrote:

    Am I better off doing this on Visual C++ or Visual Basic?

    Only if you have more experience with either of those languages, although the issue is exactly the same whether you use VB.NET or C#. My original suggestion to use the graphics class is still your best option.

    Use the best guess

    L 1 Reply Last reply
    0
    • L Lost User

      Gerry Schmitz wrote:

      You're simply making things harder for yourself with "older" technology.

      Not true, there is nothing in this question that cannot be solved quite easily in VS 2005.

      Use the best guess

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #13

      True. I should have said "generally"; or "moving forward". I assumed that at some point, he would be doing something other than "drawing circles". But I could be wrong.

      S 1 Reply Last reply
      0
      • L Lost User

        Stuck At Zero wrote:

        Am I better off doing this on Visual C++ or Visual Basic?

        Only if you have more experience with either of those languages, although the issue is exactly the same whether you use VB.NET or C#. My original suggestion to use the graphics class is still your best option.

        Use the best guess

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #14

        Why is that "the best option"? I see no reason why it is better than say WPF. Or flipping picture images as the OP is thinking of.

        L 1 Reply Last reply
        0
        • L Lost User

          Why is that "the best option"? I see no reason why it is better than say WPF. Or flipping picture images as the OP is thinking of.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #15

          Gerry Schmitz wrote:

          why it is better than say WPF.

          In the original question the OP states: "I'm a total noob at C# 2005", which suggests that WPF would be an even bigger mountain to climb.

          Use the best guess

          L 1 Reply Last reply
          0
          • L Lost User

            Gerry Schmitz wrote:

            why it is better than say WPF.

            In the original question the OP states: "I'm a total noob at C# 2005", which suggests that WPF would be an even bigger mountain to climb.

            Use the best guess

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #16

            You assume too much. If the OP was prepared to use WPF, I would have posted a solution that any "noob" would have understood. Any solution (in this case) that is at least object-oriented is superior to one that simply "draws". The OP is creating a UI (not some scribbles); and creating "leds" implies managing "state" (i.e. on/off); image controls (the OP's thinking), "shapes", etc. are a lot more useful in this case than simply "drawing" (as you are suggesting).

            L 1 Reply Last reply
            0
            • L Lost User

              You assume too much. If the OP was prepared to use WPF, I would have posted a solution that any "noob" would have understood. Any solution (in this case) that is at least object-oriented is superior to one that simply "draws". The OP is creating a UI (not some scribbles); and creating "leds" implies managing "state" (i.e. on/off); image controls (the OP's thinking), "shapes", etc. are a lot more useful in this case than simply "drawing" (as you are suggesting).

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #17

              Gerry Schmitz wrote:

              You assume too much

              Not at all; if someone states that they are completely new to C# then it's a fair bet that they would not find WPF easy.

              Gerry Schmitz wrote:

              Any solution (in this case) that is at least object-oriented is superior to one that simply "draws".

              I never claimed that it wasn't.

              Gerry Schmitz wrote:

              more useful in this case than simply "drawing" (as you are suggesting).

              I made no such suggestion; I merely pointed the OP at the .NET Graphics Class[^] which can be used to draw circles. I left the actual implementation to him/her to figure out.

              Use the best guess

              1 Reply Last reply
              0
              • S Stuck At Zero

                All, I'm trying to replicate a design of a GUI which has numerous circles scattered across the form to represent the equivalent of an LED status light. I'm a total noob at C# 2005, but my million dollar question is how can I do this?

                C Offline
                C Offline
                cjb110
                wrote on last edited by
                #18

                I don't know what VisualStudio it came with but there is a VisualBasic PowerPack that added the basic graphics objects to the toolbox for C# WinForm usage. There's also multiple articles about creating LED controls on this site. Search If you want to do it yourself, then you need to start looking into UserControls, and basically you create a class inheriting from UserControl and then you override the OnPaint event. This is the OnPaint I did, its not great but it looks like a little led (with fade from colour to colour) which is all I wanted.

                    protected override void OnPaint(PaintEventArgs e)
                    {
                        base.OnPaint(e);
                
                        Pen borderPen = new Pen(Color.Black);
                        borderPen.Width = ClientSize.Width <= 16 ? 2 : 4;
                
                        if (this.IsError)
                        {
                            using (Graphics g = e.Graphics)
                            {
                                SolidBrush brushError = new SolidBrush(ColorError);
                                g.FillEllipse(brushError, 0, 0, ClientSize.Width, ClientSize.Height);
                
                                g.DrawEllipse(borderPen, 0, 0, ClientSize.Width, ClientSize.Height);
                
                                brushError.Dispose();
                            }
                        }
                        else
                        {
                            Pen pen = new Pen(PercentageColor);
                
                            SolidBrush brushInside = new SolidBrush(PercentageColor);
                
                            int colorStepGradient = 1;
                            byte colorGradient = 255 / 10;
                
                            using (Graphics g = e.Graphics)
                            {
                                g.SmoothingMode = SmoothingMode.AntiAlias;
                
                                int x = 0, y = 0;
                                Color origPenColor = pen.Color;
                                Color origBrushColor = brushInside.Color;
                                int width = ClientSize.Width, height = ClientSize.Height;
                
                                //draw ellipses at smaller and smaller sizes, stepping by \_colorStepGradient
                                for (; x <= width && y <= height; x += colorStepGradient, y += colorStepGradient, width -= 2 \* colorStepGradient, height -= 2 \* colorStepGradient)
                                {
                                    g.DrawEllipse(pen, x, y, width, height);
                                    g.FillEllipse(brushInside, x, y, width, height);
                
                                    byte newR = pen.Color.R;
                                    byte newG = pen.Color.G;
                                    byte newB = pen.Color.B;
                
                S 1 Reply Last reply
                0
                • C cjb110

                  I don't know what VisualStudio it came with but there is a VisualBasic PowerPack that added the basic graphics objects to the toolbox for C# WinForm usage. There's also multiple articles about creating LED controls on this site. Search If you want to do it yourself, then you need to start looking into UserControls, and basically you create a class inheriting from UserControl and then you override the OnPaint event. This is the OnPaint I did, its not great but it looks like a little led (with fade from colour to colour) which is all I wanted.

                      protected override void OnPaint(PaintEventArgs e)
                      {
                          base.OnPaint(e);
                  
                          Pen borderPen = new Pen(Color.Black);
                          borderPen.Width = ClientSize.Width <= 16 ? 2 : 4;
                  
                          if (this.IsError)
                          {
                              using (Graphics g = e.Graphics)
                              {
                                  SolidBrush brushError = new SolidBrush(ColorError);
                                  g.FillEllipse(brushError, 0, 0, ClientSize.Width, ClientSize.Height);
                  
                                  g.DrawEllipse(borderPen, 0, 0, ClientSize.Width, ClientSize.Height);
                  
                                  brushError.Dispose();
                              }
                          }
                          else
                          {
                              Pen pen = new Pen(PercentageColor);
                  
                              SolidBrush brushInside = new SolidBrush(PercentageColor);
                  
                              int colorStepGradient = 1;
                              byte colorGradient = 255 / 10;
                  
                              using (Graphics g = e.Graphics)
                              {
                                  g.SmoothingMode = SmoothingMode.AntiAlias;
                  
                                  int x = 0, y = 0;
                                  Color origPenColor = pen.Color;
                                  Color origBrushColor = brushInside.Color;
                                  int width = ClientSize.Width, height = ClientSize.Height;
                  
                                  //draw ellipses at smaller and smaller sizes, stepping by \_colorStepGradient
                                  for (; x <= width && y <= height; x += colorStepGradient, y += colorStepGradient, width -= 2 \* colorStepGradient, height -= 2 \* colorStepGradient)
                                  {
                                      g.DrawEllipse(pen, x, y, width, height);
                                      g.FillEllipse(brushInside, x, y, width, height);
                  
                                      byte newR = pen.Color.R;
                                      byte newG = pen.Color.G;
                                      byte newB = pen.Color.B;
                  
                  S Offline
                  S Offline
                  Stuck At Zero
                  wrote on last edited by
                  #19

                  I was just about to post that someone at work mentioned to me about the Visual Basic Power Pack. This is exactly what I was looking for. Thanks so much for posting and walking a mile in my shoes. I will look into the more specialized LED search you hyperlinked, but I suspect the Power Pack is all that I need. I definitely want to avoid coding the "LEDs" by hand since I'm dealing with a lot of these things. Thanks Again!!!

                  C 1 Reply Last reply
                  0
                  • L Lost User

                    Whether you use "Windows Forms" or "WPF" is usually determined when you create a new Visual Studio project: i.e. choosing Console; Windows Forms; WPF; etc. when you create a new project. WPF has been around for a while, but since I have only been using it from 2008 on, I don't know how 2005 supports it. Anyway, if you're limited to 2005, create a new project in VS, and if you have the option to create a WPF project, then you're good to go (unless "the powers that be" insist you use only Windows Forms).

                    S Offline
                    S Offline
                    Stuck At Zero
                    wrote on last edited by
                    #20

                    I'm a total newbie when it comes to C#, so I've been trying to wing it with online tutorials on making GUI apps. Powers that be don't really care HOW I do it. They just want to see a notional version of what they had envisioned.

                    1 Reply Last reply
                    0
                    • L Lost User

                      True. I should have said "generally"; or "moving forward". I assumed that at some point, he would be doing something other than "drawing circles". But I could be wrong.

                      S Offline
                      S Offline
                      Stuck At Zero
                      wrote on last edited by
                      #21

                      I don't know how much this software will morph over time, but the only other special thing I will need to look into is a "console" window on one of the tabs of my GUI to take manual commands as things are running. Outside of that, my best guess is that the "drawing circles" is the only quirky feature I need to have for this GUI that isn't obvious on the standard C# toolbox. Time will prove me wrong.

                      1 Reply Last reply
                      0
                      • S Stuck At Zero

                        I was just about to post that someone at work mentioned to me about the Visual Basic Power Pack. This is exactly what I was looking for. Thanks so much for posting and walking a mile in my shoes. I will look into the more specialized LED search you hyperlinked, but I suspect the Power Pack is all that I need. I definitely want to avoid coding the "LEDs" by hand since I'm dealing with a lot of these things. Thanks Again!!!

                        C Offline
                        C Offline
                        cjb110
                        wrote on last edited by
                        #22

                        A custom user control seems a perfect solution then tbh. You can go the route I took and do it all via code, or you could use the VB PowerPack to draw the circles on the empty canvas it gives you.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups