منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب
حساب مكان الفورم علي شاشة الكمبيوتر - نسخة قابلة للطباعة

+- منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب (http://vb4arb.com/vb)
+-- قسم : قسم لغة الفيجوال بيسك VB.NET (http://vb4arb.com/vb/forumdisplay.php?fid=182)
+--- قسم : قسم مقالات VB.NET (http://vb4arb.com/vb/forumdisplay.php?fid=184)
+--- الموضوع : حساب مكان الفورم علي شاشة الكمبيوتر (/showthread.php?tid=22468)



حساب مكان الفورم علي شاشة الكمبيوتر - silverlight - 09-11-17

الدالة التالية عبارة عن كود يقوم بحساب مكان الفورم علي شاشة الكمبيوتر

الدالة


PHP كود :
   Private Function TryGetWindowRect(ByRef rect) As Boolean
        If rect 
<> Rectangle.Empty Then
            Dim screenRect 
As Rectangle Nothing
            Dim allScreens 
As Screen() = Screen.AllScreens
            For i 
As Integer 0 To allScreens.Length 1
                Dim scr 
As Screen allScreens(i)
 
               screenRect Rectangle.Union(screenRectscr.Bounds)
 
           Next
            Dim ht 
As Integer screenRect.Height
            Dim w 
As Integer screenRect.Width
            Dim x 
As Integer screenRect.X
            Dim y 
As Integer screenRect.Y

            Dim topleft 
As Point = New Point(Math.Max(rect.Leftx), Math.Max(rect.Topy))
 
           Dim bottomRight As Point = New Point(Math.Min(rect.Rightw), Math.Min(rect.Bottomht))
 
           rect.Location topleft
            rect
.Height Math.Abs(bottomRight.rect.Top)
 
           rect.Width Math.Abs(bottomRight.rect.Left)
 
           Return True
        End 
If
 
       rect Rectangle.Empty
 
       Return False
    End 
Function 

الدالة لها استخدامات كثيرة و الأمر متروك لكم

مثال توضيحي
المثال يوضح كيفية الحصول علي مكان الفورم و عرض قيمته علي شكل String في Label
وذلك عندما ياغير مكان الفور او عندما تتغير أبعاد الفورم

PHP كود :
Public Class Form1

    Private 
Function TryGetWindowRect(ByRef rect) As Boolean
        If rect 
<> Rectangle.Empty Then
            Dim screenRect 
As Rectangle Nothing
            Dim allScreens 
As Screen() = Screen.AllScreens
            For i 
As Integer 0 To allScreens.Length 1
                Dim scr 
As Screen allScreens(i)
 
               screenRect Rectangle.Union(screenRectscr.Bounds)
 
           Next
            Dim ht 
As Integer screenRect.Height
            Dim w 
As Integer screenRect.Width
            Dim x 
As Integer screenRect.X
            Dim y 
As Integer screenRect.Y

            Dim topleft 
As Point = New Point(Math.Max(rect.Leftx), Math.Max(rect.Topy))
 
           Dim bottomRight As Point = New Point(Math.Min(rect.Rightw), Math.Min(rect.Bottomht))
 
           rect.Location topleft
            rect
.Height Math.Abs(bottomRight.rect.Top)
 
           rect.Width Math.Abs(bottomRight.rect.Left)
 
           Return True
        End 
If
 
       rect Rectangle.Empty
 
       Return False
    End 
Function


 
   Private Sub Form1_LocationChanged(sender As ObjectAs EventArgsHandles MyBase.LocationChanged
        Dim r 
As Rectangle Me.Bounds
        If TryGetWindowRect
(rThen
            Label1
.Text r.ToString
        End 
If
 
   End Sub

    Private Sub Form1_SizeChanged
(sender As ObjectAs EventArgsHandles MyBase.SizeChanged
        Dim r 
As Rectangle Me.Bounds
        If TryGetWindowRect
(rThen
            Label1
.Text r.ToString
        End 
If
 
   End Sub
End 
Class 
أيضا الدالة يمكن إعادة صياغتها بالشكل التالي

PHP كود :
   Private Function BoundsToScreen(ByRef rect) As Rectangle
        Dim screenRect 
As Rectangle Nothing
        Dim allScreens 
As Screen() = Screen.AllScreens
        For i 
As Integer 0 To allScreens.Length 1
            Dim scr 
As Screen allScreens(i)
 
           screenRect Rectangle.Union(screenRectscr.Bounds)
 
       Next
        Dim ht 
As Integer screenRect.Height
        Dim w 
As Integer screenRect.Width
        Dim x 
As Integer screenRect.X
        Dim y 
As Integer screenRect.Y

        Dim topleft 
As Point = New Point(Math.Max(rect.Leftx), Math.Max(rect.Topy))
 
       Dim bottomRight As Point = New Point(Math.Min(rect.Rightw), Math.Min(rect.Bottomht))
 
       rect.Location topleft
        rect
.Height Math.Abs(bottomRight.rect.Top)
 
       rect.Width Math.Abs(bottomRight.rect.Left)
 
       Return rect
    End 
Function 

الإستخدام مع الفورم


PHP كود :
Dim windowBounds As Rectangle BoundsToScreen(Me.Bounds

أتمني ان يكون الكود مفيدا للبعض منكم
تقبلوا تحياتي

مثال أخر

افتح مشروع 
أضف للمشروع فورم ثاني

و في الفورم الأول اكتب الكود بالشكل التالي


PHP كود :
Public Class Form1

    Private seconfForm 
As Form = New Form2

    Private 
Function TryGetWindowRect(ByRef rect) As Boolean
        If rect 
<> Rectangle.Empty Then
            Dim screenRect 
As Rectangle Nothing
            Dim allScreens 
As Screen() = Screen.AllScreens
            For i 
As Integer 0 To allScreens.Length 1
                Dim scr 
As Screen allScreens(i)
 
               screenRect Rectangle.Union(screenRectscr.Bounds)
 
           Next
            Dim ht 
As Integer screenRect.Height
            Dim w 
As Integer screenRect.Width
            Dim x 
As Integer screenRect.X
            Dim y 
As Integer screenRect.Y

            Dim topleft 
As Point = New Point(Math.Max(rect.Leftx), Math.Max(rect.Topy))
 
           Dim bottomRight As Point = New Point(Math.Min(rect.Rightw), Math.Min(rect.Bottomht))
 
           rect.Location topleft
            rect
.Height Math.Abs(bottomRight.rect.Top)
 
           rect.Width Math.Abs(bottomRight.rect.Left)
 
           Return True
        End 
If
 
       rect Rectangle.Empty
 
       Return False
    End 
Function

 
   Private Sub Form1_LocationChanged(sender As ObjectAs EventArgsHandles MyBase.LocationChanged
        Dim windowBounds 
As Rectangle BoundsToScreen(Me.Bounds)
 
       Dim pt As Point = New Point(windowBounds.Left windowBounds.WidthwindowBounds.Top)
 
       If seconfForm IsNot Nothing Then
            seconfForm
.Location pt
            seconfForm
.Bounds = New Rectangle(pt, New Size(windowBounds.Size.Width 8windowBounds.Size.Height 8))

 
       End If
 
   End Sub

    Private Sub Form1_SizeChanged
(sender As ObjectAs EventArgsHandles MyBase.SizeChanged
        Dim windowBounds 
As Rectangle BoundsToScreen(Me.Bounds)
 
       Dim pt As Point = New Point(windowBounds.Left windowBounds.WidthwindowBounds.Top)
 
       If seconfForm IsNot Nothing Then
            seconfForm
.Location pt
            seconfForm
.Bounds = New Rectangle(pt, New Size(windowBounds.Size.Width 8windowBounds.Size.Height 8))
 
       End If
 
   End Sub

    Private Sub Form1_Load
(sender As ObjectAs EventArgsHandles MyBase.Load
        seconfForm
.FormBorderStyle FormBorderStyle.None
        seconfForm
.Show()
 
       Dim windowBounds As Rectangle BoundsToScreen(Me.Bounds)

 
       Dim pt As Point = New Point(windowBounds.Left windowBounds.WidthwindowBounds.Top)
 
       If seconfForm IsNot Nothing Then
            seconfForm
.Location pt
            seconfForm
.Bounds = New Rectangle(pt, New Size(windowBounds.Size.Width 8windowBounds.Size.Height 8))
 
       End If
 
   End Sub

    Private 
Function BoundsToScreen(ByRef rect) As Rectangle
        Dim screenRect 
As Rectangle Nothing
        Dim allScreens 
As Screen() = Screen.AllScreens
        For i 
As Integer 0 To allScreens.Length 1
            Dim scr 
As Screen allScreens(i)
 
           screenRect Rectangle.Union(screenRectscr.Bounds)
 
       Next
        Dim ht 
As Integer screenRect.Height
        Dim w 
As Integer screenRect.Width
        Dim x 
As Integer screenRect.X
        Dim y 
As Integer screenRect.Y

        Dim topleft 
As Point = New Point(Math.Max(rect.Leftx), Math.Max(rect.Topy))
 
       Dim bottomRight As Point = New Point(Math.Min(rect.Rightw), Math.Min(rect.Bottomht))
 
       rect.Location topleft
        rect
.Height Math.Abs(bottomRight.rect.Top)
 
       rect.Width Math.Abs(bottomRight.rect.Left)
 
       Return rect
    End 
Function

End Class 


ملحوظة اخيرة الحسابات قد تحتلف طبقا لنسخة الويندوز
لأن الفورم يرسم نفسه علي شاشة الكمبيوتر بأساليب مختلفة حسب نسخة الويندوز نفسه


RE: حساب مكان الفورم علي شاشة الكمبيوتر - الوايلي - 09-11-17

يعطيك العافية شرح رائع بإنتظار المزيد


RE: حساب مكان الفورم علي شاشة الكمبيوتر - kebboud - 18-06-23

فيه الكثير من الأخطاء