09-11-17, 11:46 AM
(آخر تعديل لهذه المشاركة : 09-11-17, 03:49 PM {2} بواسطة silverlight.)
الدالة التالية عبارة عن كود يقوم بحساب مكان الفورم علي شاشة الكمبيوتر
الدالة
الدالة لها استخدامات كثيرة و الأمر متروك لكم
مثال توضيحي
المثال يوضح كيفية الحصول علي مكان الفورم و عرض قيمته علي شكل String في Label
وذلك عندما ياغير مكان الفور او عندما تتغير أبعاد الفورم
أيضا الدالة يمكن إعادة صياغتها بالشكل التالي
الإستخدام مع الفورم
أتمني ان يكون الكود مفيدا للبعض منكم
تقبلوا تحياتي
مثال أخر
افتح مشروع
أضف للمشروع فورم ثاني
و في الفورم الأول اكتب الكود بالشكل التالي
ملحوظة اخيرة الحسابات قد تحتلف طبقا لنسخة الويندوز
لأن الفورم يرسم نفسه علي شاشة الكمبيوتر بأساليب مختلفة حسب نسخة الويندوز نفسه
الدالة
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(screenRect, scr.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.Left, x), Math.Max(rect.Top, y))
Dim bottomRight As Point = New Point(Math.Min(rect.Right, x + w), Math.Min(rect.Bottom, y + ht))
rect.Location = topleft
rect.Height = Math.Abs(bottomRight.Y - rect.Top)
rect.Width = Math.Abs(bottomRight.X - 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(screenRect, scr.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.Left, x), Math.Max(rect.Top, y))
Dim bottomRight As Point = New Point(Math.Min(rect.Right, x + w), Math.Min(rect.Bottom, y + ht))
rect.Location = topleft
rect.Height = Math.Abs(bottomRight.Y - rect.Top)
rect.Width = Math.Abs(bottomRight.X - rect.Left)
Return True
End If
rect = Rectangle.Empty
Return False
End Function
Private Sub Form1_LocationChanged(sender As Object, e As EventArgs) Handles MyBase.LocationChanged
Dim r As Rectangle = Me.Bounds
If TryGetWindowRect(r) Then
Label1.Text = r.ToString
End If
End Sub
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
Dim r As Rectangle = Me.Bounds
If TryGetWindowRect(r) Then
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(screenRect, scr.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.Left, x), Math.Max(rect.Top, y))
Dim bottomRight As Point = New Point(Math.Min(rect.Right, x + w), Math.Min(rect.Bottom, y + ht))
rect.Location = topleft
rect.Height = Math.Abs(bottomRight.Y - rect.Top)
rect.Width = Math.Abs(bottomRight.X - 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(screenRect, scr.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.Left, x), Math.Max(rect.Top, y))
Dim bottomRight As Point = New Point(Math.Min(rect.Right, x + w), Math.Min(rect.Bottom, y + ht))
rect.Location = topleft
rect.Height = Math.Abs(bottomRight.Y - rect.Top)
rect.Width = Math.Abs(bottomRight.X - rect.Left)
Return True
End If
rect = Rectangle.Empty
Return False
End Function
Private Sub Form1_LocationChanged(sender As Object, e As EventArgs) Handles MyBase.LocationChanged
Dim windowBounds As Rectangle = BoundsToScreen(Me.Bounds)
Dim pt As Point = New Point(windowBounds.Left + windowBounds.Width, windowBounds.Top)
If seconfForm IsNot Nothing Then
seconfForm.Location = pt
seconfForm.Bounds = New Rectangle(pt, New Size(windowBounds.Size.Width - 8, windowBounds.Size.Height - 8))
End If
End Sub
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
Dim windowBounds As Rectangle = BoundsToScreen(Me.Bounds)
Dim pt As Point = New Point(windowBounds.Left + windowBounds.Width, windowBounds.Top)
If seconfForm IsNot Nothing Then
seconfForm.Location = pt
seconfForm.Bounds = New Rectangle(pt, New Size(windowBounds.Size.Width - 8, windowBounds.Size.Height - 8))
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
seconfForm.FormBorderStyle = FormBorderStyle.None
seconfForm.Show()
Dim windowBounds As Rectangle = BoundsToScreen(Me.Bounds)
Dim pt As Point = New Point(windowBounds.Left + windowBounds.Width, windowBounds.Top)
If seconfForm IsNot Nothing Then
seconfForm.Location = pt
seconfForm.Bounds = New Rectangle(pt, New Size(windowBounds.Size.Width - 8, windowBounds.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(screenRect, scr.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.Left, x), Math.Max(rect.Top, y))
Dim bottomRight As Point = New Point(Math.Min(rect.Right, x + w), Math.Min(rect.Bottom, y + ht))
rect.Location = topleft
rect.Height = Math.Abs(bottomRight.Y - rect.Top)
rect.Width = Math.Abs(bottomRight.X - rect.Left)
Return rect
End Function
End Class
ملحوظة اخيرة الحسابات قد تحتلف طبقا لنسخة الويندوز
لأن الفورم يرسم نفسه علي شاشة الكمبيوتر بأساليب مختلفة حسب نسخة الويندوز نفسه
Retired


ربي زدني علما