أفكار في الدوت نت


كتابة الأخ :silverlight

بسم الله الرحمن الرحيم

السلام عليكم ورحمة الله وبركاته

بالتأكيد الكثير منكم قد سمع بخاصية DisplayRectangle وهي خاصية ReadOnly ويمتلكها وبلا إستثناء جميع الكونترول الموجودة في الدوت نت ولمزيد من التفاصيل عن هذه الخاصية يمكنكم مراجعة اللينك التالي.

Control.DisplayRectangle Property

وتكمن أهم مميزات هذه الخاصية في أنها تمثل المنطقة التي يتم إضافة أي كونترول بها ويمكن إعتبارها المنطقة التي يتعامل معها المستخدم بشكل عام ويطلق عليها إسم Client Area وفي واقع الأمر وكما ستلاحظون من اللينك أعلاه لم توضح شركة مايكروسوفت الكثير عن هذه الخاصية لكن ولكي نقوم بعمل Overrides لهذه الخاصية ومن ثم نتأكد من نجاحنا في هذا الأمر يجب علينا تحديدا أن نتعامل مع OnLayout Method وذلك لكي نتأكد أن أي كونترول يتم إضافته يكون في مكانه المناسب في حالة Design Time وفي حالة Run Time

أيضا عمل Overrides لهذه الخاصية يتيح للمبرمج أن بحدد المنطقة التي يمكن أن نطلق عليها Non Client Area وبالتالي نستطيع أن نرسم بها ما نشاء بدون أن يؤثر ذلك علي حدود أو أبعاد DispalyRectangle

عموما لقد حاولت بقدر الإمكان أن أجعل الكود بسيطا وبالتأكيد يمكنكم استخدام نفس الفكرة الموجودة بالمثال وتطبيقها علي بعض الكونترول الأخري حيث يمكن تطبيقها مع الفورم أو البانل علي سبيل المثال ويجب الوضع في الاعتبار أن هذه الفكرة قد لا تنجح مع بعض الكونترول الأخري

أرجو أن تدرسوا الكود جيدا ومن لديه أي إستفسار يستطيع أن بسأل ما يشاء وأترككم مع الكود والمثال لدراسته

المثال

رمز برمجي:

' Permission is hereby granted, free of charge, to any person obtaining
' a copy of this software and associated documentation files (the
' "Software"), to deal in the Software without restriction, including
' without limitation the rights to use, copy, modify, merge, publish,
' distribute, sublicense, and/or sell copies of the Software, and to
' permit persons to whom the Software is furnished to do so, subject to
' the following conditions:
'
' The above copyright notice and this permission notice shall be
' included in all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
' EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
' MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
' LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
' OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
' WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'
' Copyright (c) 2011 Omar Amin Ibrahim.
'
' Author:
'	Omar Amin Ibrahim (silverlight1212@yahoo.com)
'

Imports System.Windows.Forms
Imports System.Drawing.Drawing2D
Imports System.Drawing

Public Class RiverNilePanel
    Inherits ContainerControl

#Region " Fields "

    Private _nonClientHeight As Integer = 23
    Private _backgroundColor As Color = Color.FromArgb(0, 0, 192)

#End Region

#Region " Constructor "

    Public Sub New()

        MyBase.SetStyle((ControlStyles.OptimizedDoubleBuffer Or (ControlStyles.AllPaintingInWmPaint Or (ControlStyles.ResizeRedraw Or ControlStyles.UserPaint))), True)

    End Sub

#End Region

#Region " Properties "

    Protected Overridable ReadOnly Property NonClientHeight As Integer
        Get
            Return _nonClientHeight
        End Get
    End Property

    Friend ReadOnly Property NonClientHeightInternal As Integer
        Get
            Return Me.NonClientHeight
        End Get
    End Property

    Protected Overrides ReadOnly Property DefaultPadding As Padding
        Get
            Return New Padding(3)
        End Get
    End Property

    Protected Overrides ReadOnly Property DefaultSize As Size
        Get
            Return New Size(150, 150)
        End Get
    End Property

    Public Overrides ReadOnly Property DisplayRectangle As System.Drawing.Rectangle
        Get
            Dim displayRect As Rectangle = MyBase.DisplayRectangle
            Dim NonClientAreaHeight = Me.NonClientHeightInternal + MyBase.Padding.Top
            Return New Rectangle(displayRect.X, (displayRect.Y + NonClientAreaHeight), displayRect.Width, Math.Max(0, (displayRect.Height - NonClientAreaHeight)))
        End Get
    End Property

    Public Overloads ReadOnly Property ClientRectangle As Rectangle
        Get
            Return New Rectangle(Me.DisplayRectangle.X, Me.DisplayRectangle.Y, Me.DisplayRectangle.Width, Me.DisplayRectangle.Height)
        End Get
    End Property

    Public Overloads Property ClientSize As Size
        Get
            Return New Size(Me.DisplayRectangle.Width, Me.DisplayRectangle.Height)
        End Get
        Set(ByVal value As Size)
            Me.SetClientSizeCore(value.Width, value.Height)
        End Set
    End Property

    Public Overridable Property BackgroundColor As Color
        Get
            Return Me._backgroundColor
        End Get
        Set(ByVal value As Color)
            Me._backgroundColor = value
            Me.Invalidate()
        End Set
    End Property

    Public Overrides Property BackColor As System.Drawing.Color
        Get
            Return MyBase.BackColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            MyBase.BackColor = value
            Me.Invalidate()
        End Set
    End Property

#End Region

#Region " Methods "

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)

        Using BackBrush As New SolidBrush(_backgroundColor)
            Dim controlBounds As Rectangle = New Rectangle(0, 0, Me.Bounds.Width, Me.Bounds.Height)
            e.Graphics.FillRectangle(BackBrush, controlBounds)
        End Using

        ' you may draw anything else in the non client area
        ' ......
        ' ......

        Dim _blend As New Blend
        Dim _Factors As Single() = {0.0F, 0.2F, 0.5F, 0.6F, 1.0F, 0.6F, 0.5F, 0.2F, 0.0F}
        Dim _Positions As Single() = {0.0F, 0.1F, 0.3F, 0.4F, 0.5F, 0.6F, 0.7F, 0.8F, 1.0F}
        _blend.Factors = _Factors
        _blend.Positions = _Positions

        Dim nonClientAreaRect As New Rectangle((MyBase.ClientRectangle.X + MyBase.Padding.Left), _
                                                       (MyBase.ClientRectangle.Y + MyBase.Padding.Top), _
                                                       (MyBase.ClientRectangle.Width - MyBase.Padding.Horizontal), _
                                                       Me.NonClientHeight)
        Dim opacity As Single = 50
        Dim a As Int32 = Me._backgroundColor.A
        Dim r As Int32 = Me._backgroundColor.R + CInt(((255 - Me._backgroundColor.R) / 100) * opacity)
        Dim g As Int32 = Me._backgroundColor.G + CInt(((255 - Me._backgroundColor.G) / 100) * opacity)
        Dim b As Int32 = Me._backgroundColor.B + CInt(((255 - Me._backgroundColor.B) / 100) * opacity)

        Dim _nonClientAreaLightColor As Color = Color.FromArgb(a, r, g, b)
        Using lgb As New LinearGradientBrush(nonClientAreaRect, _nonClientAreaLightColor, Me._backgroundColor, 90, True)
            lgb.Blend = _blend
            e.Graphics.FillRectangle(lgb, nonClientAreaRect)
        End Using

        ' fill the display rectangle
        Using displayBrush As New SolidBrush(Me.BackColor)
            Dim controlDisplayBounds As Rectangle = Me.DisplayRectangle
            e.Graphics.FillRectangle(displayBrush, controlDisplayBounds)
        End Using

    End Sub

    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
        MyBase.OnResize(e)
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnSizeChanged(ByVal e As System.EventArgs)
        MyBase.OnSizeChanged(e)
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnLayout(ByVal levent As System.Windows.Forms.LayoutEventArgs)
        Dim displayRect As Rectangle = Me.DisplayRectangle
        For Each ctrl As Control In MyBase.Controls
            If (ctrl.Visible AndAlso (ctrl.Location.Y < displayRect.Y) AndAlso (ctrl.Location.X < displayRect.X)) Then
                ctrl.Location = New Point(displayRect.X, displayRect.Top)
            End If
        Next
        MyBase.OnLayout(levent)
    End Sub

    Protected Overrides Sub OnPaddingChanged(ByVal e As System.EventArgs)
        MyBase.OnPaddingChanged(e)
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnBackColorChanged(ByVal e As System.EventArgs)
        MyBase.OnBackColorChanged(e)
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnParentBackColorChanged(ByVal e As System.EventArgs)
        MyBase.OnParentBackColorChanged(e)
        Me.Invalidate()
    End Sub

#End Region

End Class ' RiverNilePanel

تقبلوا تحياتي
أخوكم عمر

  1. لا توجد تعليقات حتى الآن.
  1. No trackbacks yet.

شاركنا برأيك ,لكي نرقى بالمدونة,يمكنك أضافة تعليق عن طريق حسابك في Facebook