Solved

Visual Basic 2008 Graphics - Drawing multiple circumscribed circles Part 2

Posted on 2009-05-07
7
755 Views
Last Modified: 2013-11-27
Circle within a circle within a circle...etc...etc...down to 1 point

Ellipses must have a constant radius on both x and y axis "Perfect Circles Only".

Please Include complete project (which includes form1) with screenshot.

CircleDemo2.jpg
0
Comment
Question by:NerdsOfTech
  • 3
  • 3
7 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 24331342
Same thing but going inwards using a diminishing radius value:
Public Class Form1
 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.DoubleBuffered = True

    End Sub
 

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

        Static R As New Random
 

        Dim C As Color

        Dim rc As Rectangle
 

        Dim center As New Point(Me.ClientRectangle.Width / 2, Me.ClientRectangle.Height / 2)

        Dim radius As Integer = Math.Min(Me.ClientRectangle.Width, Me.ClientRectangle.Height) / 2
 

        While radius > 0

            rc = New Rectangle(center.X - radius, center.Y - radius, radius * 2, radius * 2)

            C = Color.FromArgb(255, R.Next(0, 256), R.Next(0, 256), R.Next(0, 256))

            Using P As New Pen(C, 2)

                e.Graphics.DrawEllipse(P, rc)

            End Using

            radius = radius - 1

        End While

    End Sub
 

    Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged

        Me.Refresh()

    End Sub
 

End Class

Open in new window

CircleDemo3.jpg
0
 
LVL 19

Author Closing Comment

by:NerdsOfTech
ID: 31579259
Perfection! Cool effects when you resize it too! Thank you Idle_Mind once again.
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 24331751
Very nice.  What I was working on used a PictureBox.  Yours is cleaner.

And I was unfamiliar with DoubleBuffered.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered.aspx


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24331803
DoubleBuffered is great...but it only works for the FORM itself.  =\

If you want to DoubleBuffer a Panel or PictureBox (basically anything else) then you have to INHERIT that control and manual use SetStyle() to properly configure it:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle(VS.71).aspx

Something like:

    Public Class SomeThing
        Inherits SomeControl

        Public Sub New()
            Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
            Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
            Me.SetStyle(ControlStyles.UserPaint, True)
            Me.UpdateStyles()
        End Sub

    End Class

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24331921
@NerdsOfTech...thanx for the totally unexpected donation!  Much appreciated...  =)

I was playing around, just for grits and shins...try this out:
(Right Click the Form)
Public Class Form1
 

    Private CM As New ContextMenu

    Private PreviewWidth As Integer = 15

    Private PreviewMargin As Integer = 3

    Private SelectedColor As Color = Color.Cyan
 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.DoubleBuffered = True

        For Each C As Color In System.ComponentModel.TypeDescriptor.GetConverter(GetType(Color)).GetStandardValues

            If Not C.IsSystemColor Then

                Dim mi As New MenuItem(C.ToKnownColor.ToString)

                mi.Tag = C.ToKnownColor

                mi.OwnerDraw = True

                AddHandler mi.MeasureItem, AddressOf mi_MeasureItem

                AddHandler mi.DrawItem, AddressOf mi_DrawItem

                AddHandler mi.Click, AddressOf mi_Click

                CM.MenuItems.Add(mi)

            End If

        Next

        Me.ContextMenu = CM

    End Sub
 

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

        Static R As New Random
 

        Dim C As Color

        Dim rc As Rectangle

        Dim percent As Double
 

        Dim center As New Point(Me.ClientRectangle.Width / 2, Me.ClientRectangle.Height / 2)

        Dim radius As Integer = Math.Min(Me.ClientRectangle.Width, Me.ClientRectangle.Height) / 2

        Dim startRadius As Integer = radius
 

        While radius > 0

            rc = New Rectangle(center.X - radius, center.Y - radius, radius * 2, radius * 2)

            percent = (startRadius - radius) / startRadius

            C = Color.FromArgb(255, percent * SelectedColor.R, percent * SelectedColor.G, percent * SelectedColor.B)

            Using P As New Pen(C, 2)

                e.Graphics.DrawEllipse(P, rc)

            End Using

            radius = radius - 1

        End While

    End Sub
 

    Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged

        Me.Refresh()

    End Sub
 

    Private Sub mi_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs)

        Dim mi As MenuItem = CType(sender, MenuItem)

        Dim sz As SizeF = e.Graphics.MeasureString(mi.Text, Me.Font)

        e.ItemHeight = sz.Height

        e.ItemWidth = sz.Width + PreviewWidth + (2 * PreviewMargin)

    End Sub
 

    Private Sub mi_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs)

        Dim mi As MenuItem = CType(sender, MenuItem)

        e.DrawBackground()

        e.Graphics.DrawString(mi.Text, Me.Font, Brushes.Black, e.Bounds.X + PreviewWidth + (2 * PreviewMargin), e.Bounds.Y)

        Dim rc As New Rectangle(e.Bounds.X + PreviewMargin, e.Bounds.Y + PreviewMargin, PreviewWidth, e.Bounds.Height - (2 * PreviewMargin))

        Using sb As New SolidBrush(Color.FromKnownColor(mi.Tag))

            e.Graphics.FillRectangle(sb, rc)

        End Using

        e.Graphics.DrawRectangle(Pens.Black, rc)

    End Sub
 

    Private Sub mi_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim mi As MenuItem = CType(sender, MenuItem)

        SelectedColor = Color.FromKnownColor(mi.Tag)

        Me.Refresh()

    End Sub
 

End Class

Open in new window

CircleDemo4.jpg
0
 
LVL 19

Author Comment

by:NerdsOfTech
ID: 24332000
WOW! THAT is stellar! Now that... is extremely cool.
0
 
LVL 19

Author Comment

by:NerdsOfTech
ID: 24332021
Your welcome. You deserved it.

=NerdsOfTech
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now