Introduction
I had faced a big problem with the TabControl included in Visual Studio 2005. So I applied a "Mnemonic" (hot key) feature to the TabControl, and for this reason I had set the DrawMode property to OwnerDrawFixed. But setting this property removed the XP-look style for the button.
So I tried to implement the DrawItem event for
re-drawing the button, but it did not seem to work. I even tried the
"Sky Bound component" which is freely available on the net, but it did
not match my requirement.
So, finally I decided to build my own control. I browsed the
internet for some resources concerning this, but I was not satisfied
with what I found.
So here is the control, in which I have applied the XP-look style with the hot key feature.
Background
First of all, we need to set the double buffering technique to
improve painting and to allow the control to change its appearance:
Me.SetStyle(ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Then, you need to override the OnPaint event and draw your own control. The basic steps involved are:
Collapse Protected Overrides Function ProcessMnemonic(ByVal charCode As Char) As Boolean
For Each p As TabPage In Me.TabPages
If Control.IsMnemonic(charCode, p.Text) Then
Me.SelectedTab = p
Me.Focus()
Return True
End If
Next p
Return False
End Function
Protected Overrides Sub OnPaint(ByVal e As _
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
DrawControl(e.Graphics)
End Sub
Friend Sub DrawControl(ByVal g As Graphics)
If Not Visible Then
Return
End If
Dim render As New VisualStyleRenderer(VisualStyleElement.Tab.Pane.Normal)
Dim TabControlArea As Rectangle = Me.ClientRectangle
Dim TabArea As Rectangle = Me.DisplayRectangle
TabArea.Y = TabArea.Y + 1
TabArea.Width = TabArea.Width + 1
Dim nDelta As Integer = SystemInformation.Border3DSize.Width
TabArea.Inflate(nDelta, nDelta)
render.DrawBackground(g, TabArea)
Dim i As Integer = 0
While i < Me.TabCount
DrawTab(g, Me.TabPages(i), i)
i += 1
End While
End Sub
Friend Sub DrawTab(ByVal g As Graphics, ByVal tabPage _
As TabPage, ByVal nIndex As Integer)
Dim recBounds As Rectangle = Me.GetTabRect(nIndex)
Dim tabTextArea As RectangleF = _
CType(Me.GetTabRect(nIndex), RectangleF)
Dim bSelected As Boolean = (Me.SelectedIndex = nIndex)
Dim render As New VisualStyleRenderer(VisualStyleElement.Tab.Pane.Normal)
render = New VisualStyleRenderer(VisualStyleElement.Tab.TabItem.Pressed)
If bSelected Then
recBounds.Height = recBounds.Height + 10
render = New VisualStyleRenderer(VisualStyleElement.Tab.TabItem.Pressed)
render.DrawBackground(g, recBounds)
render.DrawEdge(g, recBounds, Edges.Diagonal, _
EdgeStyle.Sunken, EdgeEffects.Flat)
Else
recBounds.Y = recBounds.Y + 1
render = New VisualStyleRenderer(VisualStyleElement.Tab.TabItem.Normal)
render.DrawBackground(g, recBounds)
render.DrawEdge(g, recBounds, Edges.Diagonal, _
EdgeStyle.Sunken, EdgeEffects.Flat)
End If
If (tabPage.ImageIndex >= 0) AndAlso _
(Not (ImageList Is Nothing)) _
AndAlso (Not (ImageList.Images(tabPage.ImageIndex) _
Is Nothing)) Then
Dim nLeftMargin As Integer = 8
Dim nRightMargin As Integer = 2
Dim img As Image = ImageList.Images(tabPage.ImageIndex)
Dim rimage As Rectangle = New Rectangle(recBounds.X + _
nLeftMargin, recBounds.Y + 1, img.Width, img.Height)
Dim nAdj As Single = CType((nLeftMargin + _
img.Width + nRightMargin), Single)
rimage.Y += (recBounds.Height - img.Height) / 2
tabTextArea.X += nAdj
tabTextArea.Width -= nAdj
g.DrawImage(img, rimage)
End If
Dim stringFormat As StringFormat = New StringFormat
stringFormat.Alignment = StringAlignment.Center
stringFormat.LineAlignment = StringAlignment.Center
If FlagControl Then
stringFormat.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show
Else
stringFormat.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Hide
End If
Dim br As Brush
br = New SolidBrush(tabPage.ForeColor)
g.DrawString(tabPage.Text, Font, br, tabTextArea, stringFormat)
End Sub
Using the Code
分享到:
相关推荐
and project managers responsible for existing .NET code bases and for a wide audience of non-technical managers and CTOs who want to understand the unique challenges faced by .NET teams involved in ...
This document is intended for people applying to Ph.D. programs in computer science or related areas in USA university
If you are already familiar with MVC 5, the content in this book can get you started with ASP.NET Core 1.1 in a fast, no-fluff way. It's important to mention that this book is practical and tactical,...
This book teaches you all the essential knowledge required to learn and apply time-proven SOLID principles of object-oriented design and important design patterns in ASP.NET Core 1.0 (formerly ASP.NET...
Learn how to design Single Page Applications (SPAs) with the latest versions of the popular frameworks Angular 2 and ASP.NET Web API An explicit focus on understanding how the two tools fit together ...
Essential Windows Communication Foundation shows developers exactly how to make the most of WCF with .NET 3.5 and Visual Studio 2008. Drawing on extensive experience working with early adopters, ...
You'll learn how to use Microsoft Visual Studio® 2010 and Microsoft .NET Framework 4.0; develop a solid, fundamental understanding of C# language features; and then get to work creating actual ...
领域驱动设计与模式实战,英文版,效果应该还不错,当然好坏自己还得看,至少不会太让人失望的,中文翻译...only explains how to use the patterns, but also how the patterns are used in O/R Mappers, for example.
1 Getting Started with ASP.NET MVC 1.1 How ASP.NET MVC Works 1.2 Installing MVC 1.3 MVC in Five Minutes: Building Quote-O-Matic...12.2 Using MSBuild to Automate the Build 12.3 Deploying to Production
This document ... Enterprise architecture and technology innovation leaders should leverage this reference to optimize Gartner support when defining and developing enterprise strategies and plans.
3 edition (October 30, 2004) <br>Applying UML and Patterns is the world\'s #1 business and college introduction to \"thinking in objects\"and using that insight in real-world object-oriented ...
Entity Framework Core in Action teaches you how to access and update relational data from .NET applications. Following the crystal-clear explanations, real-world examples, and around 100 diagrams, you...
### 深度学习在答案选择中的应用:一项研究与开放任务 #### 一、引言 本文探讨了深度学习框架在非事实性问题解答(Question Answering, QA)任务中的应用,尤其关注于答案的选择过程。传统的问答系统通常依赖于...
Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and the Unified Process, Second Edition
in this program i am first creating a training data set by applying the angular values to the 2 dof DK model and then supplying the data to the anfis function the function DK47 is the direct ...
What's Inside Querying a relational database with LINQ Using EF Core in business logic Integrating EF with existing C# applications Applying domain-driven design to EF Core Getting the best ...
《Applying Design for Six Sigma to Software and Hardware Systems》是一本由Eric Maass和专家共同编写的书籍,旨在向工程师们介绍如何将六西格玛设计(DFSS)应用于软件和硬件系统开发,以提高产品的成功概率。...
...由于其重要性,我们迫切希望这些算法是正确的,能够保护我们免受潜在的窃听和攻击。...即便是最优秀的专家设计的算法,也可能存在漏洞。 ...人们希望确保这些算法没有可利用的弱点,或者至少能够找到这些弱点并加以修复...