سؤال

يظهر الكود أدناه مشكلة بسيطة للغاية؛ آمل أن أفتقد مجرد إعداد شخص ما قد يكون قادرا على الكشف عنه.

هدف

(1) إطلاق WinForm الرئيسي (Mainform).
(2) اضغط على الزر لعرض WinForm الثانوي (Shadowform) شبه شفافة ويجب أن تراكب بالضبط Mainform.

ما يحدث في الواقع

السيناريو 1: إطلاق Winform الرئيسي ثم اضغط على الزر: شاشات ShadowForm مع الحجم الصحيح ولكن الموقع غير الصحيح، وانخفاض وإلى اليمين (كما لو كان متتالي). اضغط على زر لإغلاق ShadowForm مرة أخرى. اضغط على الزر مرة أخرى لإعادة فتح ShadowForm والآن هو في الموضع الصحيح، تغطي Mainform.

السيناريو 2.: إطلاق WinForm الرئيسي، حركه، ثم اضغط على الزر: ShadowForm يعرض مع الحجم الصحيح ولكن الموقع غير الصحيح (حيث كان Mainform قبل نقله). اضغط على زر لإغلاق؛ اضغط مرة أخرى لإعادة فتح والآن Shadowform في الموضع الصحيح.

using System;
using System.Windows.Forms;

namespace LocationTest
{
    static class Program
    {
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }

    public class MainForm : Form
    {
        ShadowForm shadowForm = new ShadowForm();
        Button button1 = new Button();
        System.ComponentModel.IContainer components = null;

        public MainForm()
        {
            this.SuspendLayout();
            this.button1.Location = new System.Drawing.Point(102, 44);
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.Text = "button1";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.button1);
            this.ResumeLayout(false);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (shadowForm.Visible) { shadowForm.Hide(); }
            else
            {
                shadowForm.Size = Size; // this always works
                shadowForm.Location = Location; // this fails first time, but works second time!
                shadowForm.Show();
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null)) { components.Dispose(); }
            base.Dispose(disposing);
        }
    }

    public class ShadowForm : Form
    {
        private System.ComponentModel.IContainer components = null;

        public ShadowForm()
        {
            this.SuspendLayout();
            this.BackColor = System.Drawing.Color.Black;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Opacity = 0.5;
            this.Click += new System.EventHandler(this.ShadowForm_Click);
            this.ResumeLayout(false);
        }

        private void ShadowForm_Click(object sender, EventArgs e)
        {
            Hide();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null)) { components.Dispose(); }
            base.Dispose(disposing);
        }
    }
}
هل كانت مفيدة؟

المحلول

يجب عليك ضبط يبدأ فى مكان إلى دليل قبل تحديد موقعك في المرة الأولى.

shadowForm.StartPosition = FormStartPosition.Manual;
shadowForm.Size = Size; // this always works
shadowForm.Location = Location; // this fails first time, but works second time!
shadowForm.Show();

أو كما اقترح جويل:

shadowForm.StartPosition = FormStartPosition.CenterParent;  // Location shouldn't need to be set
shadowForm.Size = Size; // this always works
shadowForm.Show();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top