تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
[مقال] Fun with Windows Forms & Graphics - Spy Form
#2
السلام عليكم و رحمة الله و بركاته

الأن و بعد أن قمنا بكتابة الجزء الأول عن كيفية بناء  SpyForm لنبدأ في توضيح كيفية الاستفادة من هذا الفورم في بناء فكرة بسيطة و هي عبارة عن رسم شئ يتحرك خلف الماوس و هذه المرحلة تحتاج الي بعض المعرفة البسيطة بكيفية الرسم علي الفورم

بالتاكيد يمكن الاستفادة من هذا الفورم بأساليب أهم كثيرا و أعتقد ان اهم شئ ممكن ان نستفيد به هو أن نرسم NonClientArea للفورم الرئيسي و لكن لهذا حديث أخر

و الأن لنعود الي SpyForm ونقوم بالتعديل في الكود الخاص به قليلا

أولا: سوف نقوم بإضافة صورة متحركة بحجم صغير مناسب و نضيفها الي Resources المشروع
و سيتم تحريك هذه الصورة علي الفورم باستخدام  ImageAnimator Class

ثانيا:  اهم شئ في الرسم علي الفورم و لكي يكون الرسم دقيقا يجب علينا أن نحدد المكان الذي سوف نرسم به الصورة و هو سيكون عبارة عن مستطيل ابعاده عبارة عن ابعاد الصورة و لكن نقطة رسم هذا المستطيل ستكون مرتبطة بمكان Cursor او بمكان MousePosition علي شاشة الكمبيوتر لهذا سوف نحتاج الي استخدام  Translation او Offset لنقطة بداية الرسم لكي نربطها مع MousePosition علي الشاشة


و لمن لا يعلم كيفية نقل مكان الرسم يمكنه أن يسأل و سوف اقوم بالتوضيح له لاحقا خاصة و اننا هنا ليس هدفنا توضيح عمليات الرسم علي الفورم 


و الكود التالي يوضح الشكل النهائي للكلاس SpyForm

كود :
Public Class SpyForm
   Inherits Form

   Private animatedImage As Bitmap = My.Resources.Catty
   Private currentlyAnimating As Boolean = False

   Public Sub New()
       Me.InitializeForm()

   End Sub

   Private Sub InitializeForm()

       ' set both back color and transparencykey to while color
       MyBase.BackColor = Color.White
       MyBase.TransparencyKey = Color.White

       ' do not show the form in task bar
       MyBase.ShowInTaskbar = False
       ' set the startposition to manual
       MyBase.StartPosition = FormStartPosition.Manual
       ' set the top most to true
       MyBase.TopMost = True

       ' set formborderystyle to non
       MyBase.FormBorderStyle = Windows.Forms.FormBorderStyle.None

       ' get the computer screen rectangle
       Dim rect As Rectangle = CType(Nothing, Rectangle)
       Dim devices As Screen() = Screen.AllScreens
       For i As Integer = 0 To devices.Length - 1
           Dim device As Screen = devices(i)
           rect = System.Drawing.Rectangle.Union(rect, device.Bounds)
       Next

       ' set the form bounds to screen rectangle
       MyBase.Bounds = rect

       ' set form double buffer
       MyBase.DoubleBuffered = True

       ' give a name to the form
       MyBase.Name = "SpyForm"

   End Sub

   Protected Overrides Sub OnPaint(e As PaintEventArgs)
       MyBase.OnPaint(e)

       'Begin the animation of the mage
       AnimateImage()
       'Get the next frame ready for rendering.
       ImageAnimator.UpdateFrames()

       Dim g As Graphics = e.Graphics

       ' define the location of the form
       Dim formLocation As Point = New Point(MyBase.Location.X, MyBase.Location.Y)
       ' define a rect and translate its location to the cursor position
       Dim rect As Rectangle = TranslateRectangle(formLocation, Screen.FromPoint(Control.MousePosition).Bounds)
       ' define a point wher we shall draw the animated image
       Dim pt As Point = TranslatePoint(formLocation, Control.MousePosition)

       ' get the image size
       Dim imageSize As Size = New Size(animatedImage.Width, animatedImage.Height)

       Dim imagelocation As Point = pt

       '
       If imagelocation.X + imageSize.Width > rect.Right Then
           imagelocation.X = rect.Right - imageSize.Width
       End If

       If imagelocation.Y + imageSize.Height > rect.Bottom Then
           imagelocation.Y = rect.Bottom - imageSize.Height
       End If

       ' define the image rect to draw the animated image within it
       Dim imageRect As Rectangle = New Rectangle(imagelocation, imageSize)

       ' draw the animated image on the form
       g.DrawImage(animatedImage, imageRect)

   End Sub

   Friend Function TranslateRectangle(pt As Point, rect As Rectangle) As Rectangle
       Return New Rectangle(rect.X + pt.X, rect.Y + pt.Y, rect.Width, rect.Height)
   End Function

   Friend Function TranslatePoint(pt1 As Point, pt2 As Point) As Point
       Return New Point(pt2.X + pt1.X, pt2.Y + pt1.Y)
   End Function

   Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)
       'Force a call to the Paint event handler.
       Me.Invalidate()
   End Sub

   Friend Sub AnimateImage()
       If Not currentlyAnimating Then
           ' Begin the animation
           ImageAnimator.Animate(animatedImage, New EventHandler(AddressOf Me.OnFrameChanged))
           currentlyAnimating = True
       End If
   End Sub

End Class



في المرفقات ستجدون الكود كاملا

المراجع

1- ImageAnimator Class
2- Control.MousePosition Property
3- Rectangle.Offset Method (Point)

أتمني من الله ان يكون الموضوع مفيدا لكم
و كل عام و انتم بخير




.rar   Test_SpyForm.rar (الحجم : 76.15 ك ب / التحميلات : 89)
الرد }}}


الردود في هذا الموضوع
Fun with Windows Forms & Graphics - Spy Form - بواسطة silverlight - 23-06-15, 08:09 PM
RE: Fun with Windows Forms & Graphics - Spy Form - بواسطة silverlight - 23-06-15, 09:33 PM


التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم