|
-
Jul 31st, 2010, 05:18 PM
#1
Thread Starter
Stack Overflow moderator
GControl
You know how Windows Forms Controls are drawn without proper transparency? And you don't have much control over them? The answer to the problem is using the Graphics object to draw controls. Here's a base GControl class in progress, and a GForm control that hosts GControl objects. Access the GControl container using (name of GForm control).MainControl. I'm working on some GProgressBars and GButtons, GLabels, etc. right now 
Here are GControl, GForm, and GControlCollection.
Code:
Namespace GForms
Class GForm
Inherits Control
' This is a GControl host, meant to be put inside a form.
' It's not actually a form by itself. All it contains is
' a generic GControl object that holds other GControls.
Private m_mainControl As New GControl()
Public Sub New()
Me.Dock = DockStyle.Fill
Me.DoubleBuffered = True
' no controls can be added
Me.SetStyle(ControlStyles.ContainerControl, False)
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim g As Graphics = e.Graphics
' draw the main control
Me.MainControl.DrawBackground(g, Me.ClientRectangle)
Me.MainControl.Draw(g, Me.ClientRectangle)
' raise the base event
MyBase.OnPaint(e)
End Sub
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
Me.MainControl.OnMouseDown(e.Location, e.Button)
MyBase.OnMouseDown(e)
End Sub
Protected Overrides Sub OnMouseUp(e As MouseEventArgs)
Me.MainControl.OnMouseUp(e.Location, e.Button)
MyBase.OnMouseUp(e)
End Sub
Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
Me.MainControl.OnMouseMove(e.Location, e.Button)
MyBase.OnMouseMove(e)
End Sub
Public ReadOnly Property MainControl() As GControl
Get
Return m_mainControl
End Get
End Property
End Class
Public Class GControl
#Region "Variables"
Friend m_parent As GControl = Nothing
Private m_controls As GControlCollection
#End Region
#Region "Events"
Public Event Changed As GEventHandler
#End Region
#Region "Constructor"
Public Sub New()
Me.m_controls = New GControlCollection(Me)
Me.Visible = True
Me.Color = SystemColors.WindowText
Me.Background = Nothing
Me.Dock = DockStyle.None
End Sub
#End Region
#Region "Drawing"
Public Overridable Sub Draw(g As Graphics, bounds As Rectangle)
' Draw the child controls
For Each c As GControl In Me.Controls
If c.Visible Then
Dim b As Rectangle = c.Bounds
c.DrawBackground(g, b)
c.Draw(g, b)
End If
Next
End Sub
Public Overridable Sub DrawBackground(g As Graphics, bounds As Rectangle)
' fill in the background
If Me.Background IsNot Nothing Then
g.FillRectangle(Me.Background, bounds)
End If
End Sub
#End Region
#Region "Mouse Events"
Public Overridable Function OnMouseDown(location As Point, buttons As MouseButtons) As Boolean
For Each c As GControl In Me.Controls
If c.Bounds.Contains(location) Then
If c.OnMouseDown(location, buttons) Then
Return True
End If
End If
Next
Return False
End Function
Public Overridable Function OnMouseUp(location As Point, buttons As MouseButtons) As Boolean
For Each c As GControl In Me.Controls
If c.Bounds.Contains(location) Then
If c.OnMouseUp(location, buttons) Then
Return True
End If
End If
Next
Return False
End Function
Public Overridable Function OnMouseMove(location As Point, buttons As MouseButtons) As Boolean
For Each c As GControl In Me.Controls
If c.Bounds.Contains(location) Then
If c.OnMouseMove(location, buttons) Then
Return True
End If
End If
Next
Return False
End Function
#End Region
#Region "Properties"
Public ReadOnly Property Parent() As GControl
Get
Return m_parent
End Get
End Property
Public ReadOnly Property Controls() As GControlCollection
Get
Return m_controls
End Get
End Property
Public ReadOnly Property Bounds() As Rectangle
Get
Dim parentBounds As Rectangle = (If(Me.Parent IsNot Nothing, Me.Parent.Bounds, Rectangle.Empty))
Dim b As New Rectangle(parentBounds.Left + Me.Left, parentBounds.Top + Me.Top, Me.Width, Me.Height)
If Me.Parent IsNot Nothing Then
Select Case Me.Dock
Case DockStyle.Bottom
b = New Rectangle(parentBounds.Left, parentBounds.Bottom - Me.Height, parentBounds.Width, Me.Height)
Exit Select
Case DockStyle.Right
b = New Rectangle(parentBounds.Left - Me.Width, parentBounds.Top, Me.Width, parentBounds.Height)
Exit Select
Case DockStyle.Top
b = New Rectangle(parentBounds.Left, parentBounds.Top, parentBounds.Width, Me.Height)
Exit Select
Case DockStyle.Left
b = New Rectangle(parentBounds.Left, parentBounds.Top, Me.Width, parentBounds.Height)
Exit Select
End Select
End If
Return b
End Get
End Property
#End Region
#Region "CSS-Style Properties"
' position + size
Public Property Left() As Integer
Get
Return m_Left
End Get
Set
m_Left = Value
End Set
End Property
Private m_Left As Integer
Public Property Top() As Integer
Get
Return m_Top
End Get
Set
m_Top = Value
End Set
End Property
Private m_Top As Integer
Public Property Width() As Integer
Get
Return m_Width
End Get
Set
m_Width = Value
End Set
End Property
Private m_Width As Integer
Public Property Height() As Integer
Get
Return m_Height
End Get
Set
m_Height = Value
End Set
End Property
Private m_Height As Integer
' visibility
Public Property Visible() As Boolean
Get
Return m_Visible
End Get
Set
m_Visible = Value
End Set
End Property
Private m_Visible As Boolean
' color
Public Property Color() As Color
Get
Return m_Color
End Get
Set
m_Color = Value
End Set
End Property
Private m_Color As Color
Public Property Background() As Brush
Get
Return m_Background
End Get
Set
m_Background = Value
End Set
End Property
Private m_Background As Brush
' docking
Public Property Dock() As DockStyle
Get
Return m_Dock
End Get
Set
m_Dock = Value
End Set
End Property
Private m_Dock As DockStyle
#End Region
End Class
Public Class GControlCollection
Implements IList(Of GControl)
Private parent As GControl = Nothing
Private innerList As New List(Of GControl)()
Public Sub New(parent As GControl)
Me.parent = parent
End Sub
Public ReadOnly Property IsReadOnly() As Boolean Implements ICollection(Of GControl).IsReadOnly
Get
Return False
End Get
End Property
Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
Return innerList.GetEnumerator()
End Function
Private Function IEnumerable_GetEnumerator() As IEnumerator(Of GControl) Implements IEnumerable(Of GControl).GetEnumerator
Return innerList.GetEnumerator()
End Function
Public Sub Clear() Implements ICollection(Of GControl).Clear
For Each c As GControl In innerList
c.parent = Nothing
Next
innerList.Clear()
End Sub
Public Sub CopyTo(array As GControl())
innerList.CopyTo(array)
End Sub
Public Sub CopyTo(array As GControl(), arrayIndex As Integer)
innerList.CopyTo(array, arrayIndex)
End Sub
Public Sub Add(control As GControl)
control.parent = parent
innerList.Add(control)
End Sub
Public Function Remove(control As GControl) As Boolean
If innerList.Remove(control) Then
control.parent = Nothing
Return True
End If
Return False
End Function
Public Sub RemoveAt(index As Integer) Implements IList(Of GControl).RemoveAt
If index < innerList.Count Then
innerList(index).parent = Nothing
innerList.RemoveAt(index)
Else
Throw New IndexOutOfRangeException()
End If
End Sub
Public ReadOnly Property Count() As Integer Implements ICollection(Of GControl).Count
Get
Return innerList.Count
End Get
End Property
Public Function IndexOf(control As GControl) As Integer
Return innerList.IndexOf(control)
End Function
Public Function Contains(control As GControl) As Boolean
Return Me.IndexOf(control) > -1
End Function
Public Default Property Item(index As Integer) As GControl
Get
Return innerList(index)
End Get
Set
innerList(index) = value
End Set
End Property
Public Sub Insert(index As Integer, control As GControl)
innerList.Insert(index, control)
control.parent = parent
End Sub
End Class
Public Delegate Sub GEventHandler(sender As GControl)
End Namespace
-
Jul 31st, 2010, 05:51 PM
#2
Thread Starter
Stack Overflow moderator
Re: GControl
Oh, and as this is written in C# and translated using the online tool, I will be updating the DLL (not yet available ) and the C# code more often than the VB.NET code. I'm currently changing the code to be lambda-free.
Last edited by minitech; Jul 31st, 2010 at 05:56 PM.
-
Aug 1st, 2010, 01:40 AM
#3
Re: GControl
 Originally Posted by minitech
I'm working on some GProgressBars and GButtons, GLabels, etc. right now 
What about GStrings??
Last edited by Arve K.; Aug 1st, 2010 at 01:41 AM.
Reason: ..
-
Aug 1st, 2010, 01:14 PM
#4
Thread Starter
Stack Overflow moderator
Re: GControl
Was that sarcasm? I can't tell.
-
Aug 1st, 2010, 05:38 PM
#5
Re: GControl
Sorry pal, I was a little bit tipsy when I wrote that, I didn't mean to be rude or anything. I hope we are OK? 
But I don't quite understand what your code do, is it something like this? Maybe you can upload a screenshot?
Arve
-
Aug 1st, 2010, 06:47 PM
#6
Thread Starter
Stack Overflow moderator
Re: GControl
Sorry pal, I was a little bit tipsy when I wrote that, I didn't mean to be rude or anything. I hope we are OK?
What I said wasn't sarcastic, I actually had no idea (and still don't) what you meant. 
The C# code is the more complete right now, as I said, I'm working on it 
This will basically be a clone of the System.Windows.Forms namespace, but each control will have CSS-style properties that you can set. It will also be drawn entirely using the Graphics object, so we finally get "real" transparency.
-
Aug 1st, 2010, 08:18 PM
#7
Re: GControl
 Originally Posted by minitech
What I said wasn't sarcastic, I actually had no idea (and still don't) what you meant. 
A little explanation: G-string - Wikipedia, the free encyclopedia
-
Aug 1st, 2010, 09:46 PM
#8
Thread Starter
Stack Overflow moderator
Re: GControl
That is so off topic
-
Aug 2nd, 2010, 12:51 AM
#9
Re: GControl
 Originally Posted by JuggaloBrotha
I can't actually believe you linked to a Wikipedia Article on the subject. Anyway, I will look into this Minitech sometime soon. I'm a bit busy, but I'd like to see where you go with it.
-
Aug 2nd, 2010, 08:48 AM
#10
Re: GControl
 Originally Posted by minitech
That is so off topic 
I personally have nothing to complain about this off-topic subject 
 Originally Posted by formlesstree4
I can't actually believe you linked to a Wikipedia Article on the subject. Anyway, I will look into this Minitech sometime soon. I'm a bit busy, but I'd like to see where you go with it.
Better to link to an actual definition site than explain it myself...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|