Nov 30, 2014 06:47 AM
(This post was last modified: Nov 30, 2014 06:53 AM by maruata.)
Here is another silly WinForm Theme.
Just for those who started to learn GDI Code.
I am presenting you very simple code for winform theme.
The form is too simple...please dont expect me to decorate this one.. I will release nice (style) form after someday!
![[Image: OGa8jSj.png]](https://i.imgur.com/OGa8jSj.png)
The form is movable. But only with the TitleBar !
Give me a suggestion....
VB Code
C# Code
Save the code or add it into your project. You just have to inherit the Class
For example:
private class Form2 : Form
replace this to
private class Form2 : Win8Form
Just for those who started to learn GDI Code.
I am presenting you very simple code for winform theme.
The form is too simple...please dont expect me to decorate this one.. I will release nice (style) form after someday!
![[Image: OGa8jSj.png]](https://i.imgur.com/OGa8jSj.png)
The form is movable. But only with the TitleBar !
Give me a suggestion....
VB Code
Code
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Namespace ZoSoft
Public Partial Class Win8Form
Inherits Form
'Variable
Public Const WM_NCLBUTTONDOWN As Integer = &Ha1
Public Const HT_CAPTION As Integer = &H2
<DllImportAttribute("user32.dll")> _
Public Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As Integer) As Integer
End Function
<DllImportAttribute("user32.dll")> _
Public Shared Function ReleaseCapture() As Boolean
End Function
Private _backColor As Color = Color.FromArgb(235, 237, 238)
Private _borderColor As Color = Color.Black
Private _titleBar As New TitleBar()
'Enum
'Properties
Public Property BackgroundColor() As Color
Get
Return _backColor
End Get
Set
_backColor = value
Invalidate()
End Set
End Property
Public Property BorderColor() As Color
Get
Return _borderColor
End Get
Set
_borderColor = value
Invalidate()
End Set
End Property
Public Sub New()
SetStyle(ControlStyles.SupportsTransparentBackColor Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.Selectable, True)
BackColor = Color.Fuchsia
TransparencyKey = Color.Fuchsia
'Controls
Me.Controls.Add(_titleBar)
AddHandler _titleBar.MouseDown, AddressOf TitleMouseDown
InitializeComponent()
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim g As Graphics = e.Graphics
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
'Size - Rectangle
Dim fSize As New Rectangle(1, 1, Width - 2, Height - 2)
'Fill BackColor
g.FillRectangle(New SolidBrush(BackgroundColor), fSize)
'Draw border
g.DrawRectangle(New Pen(New SolidBrush(BorderColor), 1F), fSize)
'Draw Controls
_titleBar.Location = New Point(1, 1)
_titleBar.Size = New Size(Width - 2, 30)
_titleBar.Font = Me.Font
_titleBar.ForeColor = Me.ForeColor
_titleBar.Text = Me.Text
_titleBar.Icon = Me.Icon
MyBase.OnPaint(e)
End Sub
Private Sub TitleMouseDown(sender As [Object], e As MouseEventArgs)
ReleaseCapture()
SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0)
End Sub
Private Class TitleBar
Inherits Control
'Variable
Private _icon As Icon
Private _closeButton As New CloseButton()
'Properties
Public Overrides Property Text() As String
Get
Return MyBase.Text
End Get
Set
MyBase.Text = value
End Set
End Property
Public Overrides Property ForeColor() As Color
Get
Return MyBase.ForeColor
End Get
Set
MyBase.ForeColor = value
End Set
End Property
Public Overrides Property Font() As Font
Get
Return MyBase.Font
End Get
Set
MyBase.Font = value
End Set
End Property
Public Property Icon() As Icon
Get
Return _icon
End Get
Set
_icon = value
End Set
End Property
'Constructor
Public Sub New()
SetStyle(ControlStyles.SupportsTransparentBackColor Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.Selectable, True)
BackColor = Color.Transparent
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim g As Graphics = e.Graphics
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
'Size - Rectangle
Dim fSize As New Rectangle(1, 1, Width - 1, Height - 1)
'Fill
g.FillRectangle(New SolidBrush(Color.FromArgb(189, 195, 199)), fSize)
'Draw Title
Dim fontHeight As Integer = Font.Height / 2
Dim meHeight As Integer = Height / 2
g.DrawString(Text, Font, New SolidBrush(ForeColor), 35, meHeight - fontHeight)
'Draw Icon
g.DrawIcon(Icon, New Rectangle(6, 4, 24, 24))
'Border
'g.DrawRectangle(new Pen(new SolidBrush(Color.Black), 1f), fSize);
MyBase.OnPaint(e)
End Sub
End Class
End Class
End NamespaceC# Code
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ZoSoft
{
public partial class Win8Form : Form
{
//Variable
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private Color _backColor = Color.FromArgb(235,237,238);
private Color _borderColor = Color.Black;
private TitleBar _titleBar = new TitleBar();
//Enum
//Properties
public Color BackgroundColor
{
get { return _backColor; }
set { _backColor = value; Invalidate(); }
}
public Color BorderColor
{
get { return _borderColor; }
set { _borderColor = value; Invalidate(); }
}
public Win8Form()
{
SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.OptimizedDoubleBuffer | ControlStyles.Selectable, true);
BackColor = Color.Fuchsia;
TransparencyKey = Color.Fuchsia;
//Controls
this.Controls.Add(_titleBar);
_titleBar.MouseDown += TitleMouseDown;
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//Size - Rectangle
Rectangle fSize = new Rectangle(1, 1, Width - 2, Height - 2);
//Fill BackColor
g.FillRectangle(new SolidBrush(BackgroundColor), fSize);
//Draw border
g.DrawRectangle(new Pen(new SolidBrush(BorderColor), 1f), fSize);
//Draw Controls
_titleBar.Location = new Point(1, 1);
_titleBar.Size = new Size(Width - 2, 30);
_titleBar.Font = this.Font;
_titleBar.ForeColor = this.ForeColor;
_titleBar.Text = this.Text;
_titleBar.Icon = this.Icon;
base.OnPaint(e);
}
private void TitleMouseDown(Object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
private class TitleBar : Control
{
//Variable
private Icon _icon;
private CloseButton _closeButton = new CloseButton();
//Properties
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}
public override Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
}
}
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
}
}
public Icon Icon
{
get { return _icon; }
set { _icon = value; }
}
//Constructor
public TitleBar()
{
SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.OptimizedDoubleBuffer | ControlStyles.Selectable, true);
BackColor = Color.Transparent;
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//Size - Rectangle
Rectangle fSize = new Rectangle(1, 1, Width - 1, Height - 1);
//Fill
g.FillRectangle(new SolidBrush(Color.FromArgb(189, 195, 199)), fSize);
//Draw Title
int fontHeight = Font.Height / 2;
int meHeight = Height / 2;
g.DrawString(Text, Font, new SolidBrush(ForeColor), 35, meHeight - fontHeight);
//Draw Icon
g.DrawIcon(Icon, new Rectangle(6, 4, 24, 24));
//Border
//g.DrawRectangle(new Pen(new SolidBrush(Color.Black), 1f), fSize);
base.OnPaint(e);
}
}
}
}Save the code or add it into your project. You just have to inherit the Class
For example:
private class Form2 : Form
replace this to
private class Form2 : Win8Form


