مشاركة عبر


كيفية القيام بما يلي: إنشاء Windows Forms غير مستطيل

Previously, creating non-rectangular forms was a الوقت-consuming و labor-intensive عملية that involved API calls و extensive programming efforts. Th هو هو لم تعد الحالة.

ملاحظة

This عملية involves a great deal of graphics processing for the أجهزة involved; كـ a النتيجة, computers will perform differently based تشغيل ذاكرة و graphics cards موجود. When applications involve مخصص رسم, دوماً اختبار تشغيل a variety of video cards إلى ensure satisfactory الأداء قبل deploying إلى users.

The عملية of creating non-rectangular forms has الثاني عناصر: creating the shaped نموذج, و coding some programming logic إلى allow the نموذج إلى be moved و مغلق. هذه الخطوة الثانية ضروري لأن لا يوجد شريط العنوان وبلا وظيفة الناتجة عن ذلك، مثل القدرة على نموذج بشكل cusإلىm إلى تحريك النموذج شاشة وإغلاقه. Thus, it هو necessary إلى write تعليمات برمجية إلى replicate these features. For a المزيد معلومات حول creating كلاهما forms و عناصر التحكم of non-rectangular shapes, see كيفية القيام بما يلي: إنشاء نموذج Windows Shaped.

Creating a non-rectangular نموذج consists of three steps:

  • إنشاء a صورة نقطية that will act كـ the نموذج's سطح. (Effectively, you will "قص خارج" the desired شكل of the نموذج من a مستطيل.)

  • إنشاء مشروع تطبيقات Windows ثم قم بتعيين الخصائص الخاصة به للتخلص من شريط العنوان واستخدام الصورة النقطية كـ خلفية النموذج.

  • Enter الزر الزر رمز إعادة إنشاء الوظيفة التي شريط العناوين المتوفرة، مثل نقل النموذج وإغلاقه.

ملاحظة

قد تختلف مربعات الحوار وأوامر القائمة التى تشاهدها الان عن تلك الموصوفة في التعليمات اعتماداً على الإعدادات النشطة أو الإصدار الخاص بك. لتغيير الإعدادات الخاصة بك, اختر إعدادات الاستيراد و التصدير ضمن القائمة أدوات . لمزيد من المعلومات، راجع العمل مع إعدادات.

إلى إنشاء a shaped نموذج

  1. إنشاء a صورة نقطية of a non-rectangular شكل of واحد اللون مع a distinct الخلفية of another اللون. استخدم أي paint برنامج you مثل. The شكل you draw will ultimately be your نموذج, so be sure إلى draw it قطر أيمن متوسط enough إلى be useful.

    ملاحظة

    اختيار an easy-إلى-remember الخلفية اللون, such كـ أزرق, كـ this will be important later تشغيل.

  2. في ‏‫Visual Studio, إنشاء a جديد مشروع تطبيقات Windows. لمزيد من المعلومات، راجع كيفية القيام بما يلي: إنشاء مشروع تطبيقات Windows.

  3. في the خصائص نافذة:

    • التعيين the FormBorderStyle خاصية إلى بلا.

      This خاصية removes the عنوان bar من the نموذج. (كما أنه يزيل الوظيفة التي توفرها لشريط عنوان، بما في ذلك القدرة على يغلق النموذج ونقل النموذج. However, this shortcoming هو addressed below في تعليمات برمجية.)

    • قم بتعيين الخاصية BackgroundImage للنموذج إلى ملف صورة نقطية قمت بإنشائها سابقا. هناك هو لست بحاجة إلى إضافة الملف إلى نظام المشروع؛ th هو سيتم إجراء تلقائياً عندما تقوم بتعيين كصورة خلفية.

      This خاصية sets the نسخة نقطية نسخة إلى be the الخلفية of the نموذج. (When used في tandem مع the TransparencyKey خاصية specified below, this خاصية defines the شكل of the نموذج.)

    • التعيين the TransparencyKey خاصية إلى the الخلفية اللون of the صورة نقطية ملف.

      This خاصية tells the تطبيق which parts of the نموذج you want إلى see through.

      ملاحظة

      Monitors التعيين إلى a اللون depth of أكبر من 24-بت can have عرض problems مع certain parts of the نموذج not being شفاف, despite إعداد of the TransparencyKey خاصية. إلى avoid this problem, ensure that the جهاز عرض's اللون depth هو التعيين إلى أصغر من 24-بت في the عرض لوحة التحكم. When developing applications that ميزة this الشفافية, احتفظ في mind that you will have إلى make your users aware of this issue.

إلى write تعليمات برمجية إلى يغلق the نموذج

  1. إضافة a زر عنصر تحكم إلى the نموذج. لمزيد من المعلومات، راجع كيفية: إضافة عناصر إلى نماذج Windows.

  2. إضافة تعليمات برمجية that will allow the مستخدم إلى يغلق the نموذج بواسطة invoking its يغلق أسلوب.

    The following مثال shows how you can إضافة a زر that, when clicked, will يغلق the نموذج.

    Private Sub Button1_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button1.Click
       Me.Close()
    End Sub
    
    
    private void button1_Click(object sender, System.EventArgs e)
    {
       this.Close();
    }
    
    ملاحظة #Cملاحظة #C

    تأكد من إلى قم بإضافة التعليمة البرمجية إلى تمكين معالج الأحداث. Using the تعليمات برمجية من the following مثال, it would look مثل:

    this.Button1.Click += new System.EventHandler(this.button1_Click);
    

إلى write تعليمات برمجية إلى تحريك the نموذج (اختياري)

  1. إنشاء a إجراء إلى تحريك the نموذج when it هو dragged. Enter الزر تعليمات برمجية similar إلى the following إلى إنشاء a جديد يؤشر كائن. This will act كـ a متغير when you calculate how إلى تحريك the نموذج. The isMouseDown حقل هو used إلى مقطع صوتي whether the مستخدم هو holding the ماوس زر أسفل. The نموذج should تحريك فقط when the ماوس هو held أسفل.

    Private mouseOffset As Point
    Private isMouseDown As Boolean = False
    
    
    private Point mouseOffset;
    private bool isMouseDown = false;
    
  2. إنشاء an معالج الأحداث for the نموذج's MouseDown حدث. في the handler, إضافة تعليمات برمجية that allows a مستخدم إلى انقر anywhere تشغيل the نموذج إلى يسحب it. For تفاصيل حول creating حدث handlers, see كيفية القيام بما يلي: إنشاء معالجات الأحداث باستخدام "مصمم".

    Enter الزر تعليمات برمجية similar إلى the following إلى تعيين coordinates إلى the mouseOffset متغير based تشغيل the الموضع الحالي of the ماوس مؤشر. في the تعليمات برمجية below, notice that the offset موضع هو calculated using النظام معلومات حول the حد الحجم (FrameBorderSize.عرض) و عنوان bar ارتفاع(CaptionHeight). هذه يجب أخذها في الاعتبار عند اختبار الإزاحة، لأنه يتم إجراء بعض القياسات مع مساحة العميل وإجراء بعض بإحداثيات شاشة. وهكذا، تساوي الإزاحة إلى يكون عرض حد بالإضافة إلى ارتفاع التسمية التوضيحية زائد الإزاحة في إلى منطقة المحتويات الفعلية للنموذج.

    Private Sub Form1_MouseDown(ByVal sender As Object, _
        ByVal e As MouseEventArgs) Handles MyBase.MouseDown
        Dim xOffset As Integer
        Dim yOffset As Integer
    
        If e.Button = MouseButtons.Left Then
            xOffset = -e.X - SystemInformation.FrameBorderSize.Width
            yOffset = -e.Y - SystemInformation.CaptionHeight - _
                    SystemInformation.FrameBorderSize.Height
            mouseOffset = New Point(xOffset, yOffset)
            isMouseDown = True
        End If
    End Sub
    
    
    private void Form1_MouseDown(object sender, 
        System.Windows.Forms.MouseEventArgs e)
    {
        int xOffset;
        int yOffset;
    
        if (e.Button == MouseButtons.Left) 
        {
            xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
            yOffset = -e.Y - SystemInformation.CaptionHeight - 
                SystemInformation.FrameBorderSize.Height;
            mouseOffset = new Point(xOffset, yOffset);
            isMouseDown = true;
        }    
    }
    
    ملاحظة #Cملاحظة #C

    تأكد من إلى قم بإضافة التعليمة البرمجية إلى تمكين معالج الأحداث. Using the تعليمات برمجية من the following مثال, it would look مثل:

    this.MouseDown += new
       System.Windows.Forms.MouseEventHandler
       (this.Form1_MouseDown);
    
  3. إنشاء an معالج الأحداث for the نموذج's MouseMove حدث.

    Enter الزر تعليمات برمجية similar إلى the following. When the يسار ماوس زر هو clicked و the ماوس هو dragged, the نموذج's الموقع خاصية هو التعيين إلى the جديد موضع.

    Private Sub Form1_MouseMove(ByVal sender As Object, _
        ByVal e As MouseEventArgs) Handles MyBase.MouseMove
        If isMouseDown Then
            Dim mousePos As Point = Control.MousePosition
            mousePos.Offset(mouseOffset.X, mouseOffset.Y)
            Location = mousePos
        End If
    End Sub
    
    
    private void Form1_MouseMove(object sender, 
        System.Windows.Forms.MouseEventArgs e)
    {
        if (isMouseDown) 
        {
            Point mousePos = Control.MousePosition;
            mousePos.Offset(mouseOffset.X, mouseOffset.Y);
            Location = mousePos;
        }
    }
    
    ملاحظة #Cملاحظة #C

    تأكد من إلى قم بإضافة التعليمة البرمجية إلى تمكين معالج الأحداث. Using the تعليمات برمجية من the following مثال, it would look مثل:

    this.MouseMove += new
       System.Windows.Forms.MouseEventHandler
       (this.Form1_MouseMove);
    
  4. إنشاء an معالج الأحداث for the نموذج's MouseUp حدث. Enter الزر تعليمات برمجية similar إلى the following.

    Private Sub Form1_MouseUp(ByVal sender As Object, _
        ByVal e As MouseEventArgs) Handles MyBase.MouseUp
        ' Changes the isMouseDown field so that the form does
        ' not move unless the user is pressing the left mouse button.
        If e.Button = MouseButtons.Left Then
            isMouseDown = False
        End If
    End Sub
    
    
    private void Form1_MouseUp(object sender, 
        System.Windows.Forms.MouseEventArgs e)
    {
        // Changes the isMouseDown field so that the form does
        // not move unless the user is pressing the left mouse button.
        if (e.Button == MouseButtons.Left) 
        {
            isMouseDown = false;
        }
    }
    
    ملاحظة #Cملاحظة #C

    تأكد من إلى قم بإضافة التعليمة البرمجية إلى تمكين معالج الأحداث. Using the تعليمات برمجية من the following مثال, it would look مثل:

    this.MouseUp += new
       System.Windows.Forms.MouseEventHandler
       (this.Form1_MouseUp);
    

راجع أيضًا:

المهام

كيفية القيام بما يلي: إنشاء نموذج Windows Shaped

كيفية القيام بما يلي: إنشاء Windows Forms شفافة

المرجع

Windows Forms نظرة عامة