تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
[مقال] Fun with Windows Forms & Graphics - Spy Form
#1
السلام عليكم و رحمة الله و بركاته
 
مقدمة:
 
من الأشياء الطريفة جدا و القديمة أيضا في عالم الكمبيوتر هي برنامج عبارة عن صورة متحركة تتحرك خلف الماوس  و مثل هذه الأفكار البرمجية البسيطة قد يستمتع بها كثيرا بعض من مستخدمي برامج الكمبيوتر و خاصة الأطفال. و غالبا بعض المبرمجين يستخدمون هذه الأفكار البرمجية لإضفاء مؤثرات لها شكل لطيف علي برامجهم و الهدف من ذلك هو إسعاد المستخدم و جذبه الي برامجهم.
 
قد يتخيل البعض ان مثل هذه البرامج تحتاج الي مجهود ضخم من المبرمج لكنها و في واقع الأمر عبارة عن برامج بسيطة جدا يمكن كتابتها باستخدام مجموعة من الأسطر القليلة من الكود.
 
المرحلة الأولي لتنفيذ الفكرة:
لتنفيذ المرحلة الأولي يجب علينا ان نبني فورم بسيط جدا و لنطلق عليه  SpyForm
 
و الخطوات التالية توضح كيفية بناء SpyForm

1- افتح مشروع عادي WindowsApplication
2- اضف كلاس الي هذا الفورم و ليكن اسمه SpyForm
3- في الكلاس SpayForm اكتب Inherits Form


كود :
Public Class SpyForm
  Inherits Form

End Class

4- لنعدل في خصائص الفورم برمجيا حتي نجعله خفيا و ابعاده تملأ الشاشة الكود التالي يوضح ذلكPublic Class SpyForm
كود :
  Inherits Form

  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

End Class

الخطوة التالية هي أن نضيف هذا الفورم الي الفورم الرئيسي في مشروعنا
في الفورم الرئيسي نقوم بتعريف متغير يعبر عن SpyForm ثم في الحدث Load نضيف هذا Spyform و في الحدث Closing نتخلص منه او يمكن عمل Overrides للخدثان OnLoad و OnformClosing كما هو مبين في الكود االتالي

كود :
Public Class Form1

  ' define spay form
  Private spy As SpyForm

  Protected Overrides Sub OnLoad(e As EventArgs)
      Me.ShowSpyFormOnLoad()
      MyBase.OnLoad(e)
  End Sub

  Protected Overrides Sub OnFormClosing(e As FormClosingEventArgs)
      Me.DisposeSpyFormOnClosing()
      MyBase.OnFormClosing(e)
  End Sub

  ' show the spy form while loading of Form1
  Private Sub ShowSpyFormOnLoad()
      Me.spy = New SpyForm
      Me.spy.Show()
  End Sub

  ' dispose the spy form while closing of Form1
  Private Sub DisposeSpyFormOnClosing()
      Me.spy.Close()
      Me.spy.Dispose()
      Me.spy = Nothing
  End Sub

End Class

قم بتشغيل برنامجك حينها ستري فقط الفورم الرئيسي

إن أردت ان تري SpyForm عليك ان تعيد ضبط خاصية BackColor الخاصة به الي لون اخر

او يمكنك ضبط خاصية TransparencyKey Color الخاصة بهذا SpyForm الي اي لون اخر 

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

و في المشاركات التالية سوف نتحدث عن كيفية الاستفادة من هذا الفورم

و اعتقد ان بعض القراء لهذا الموضوع سيبدعون في الاستفادة من هذا SpyForm في برامجهم 

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

بالتاكيد يمكن استخدام الفورم الرئيسي و لكن الكود في تلك الحالة سوف يحتاج الي بعض التعديلات القليلة


.rar   Test_SpyForm.rar (الحجم : 64.87 ك ب / التحميلات : 90)
الرد }}}
تم الشكر بواسطة: abulayth , ربيع , الطالب , Amir_Alzubidy
#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)
الرد }}}



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


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