none
winfow form 按钮控件2K显示器下backgroundimage切换延迟 RRS feed

  • 问题

  • 按钮会根据按钮的enable状态设置不同的backgroundimage, 但发现有时候设置backgroundimage后过了几秒后才显示,这个如何调整。

    按钮的enable是在一个异步线程设置的。

                return OpenDoorTask().ContinueWith(p =>
                 {
                     this.UiThread(() =>
                     {
                         btnOpenDoor.Enabled = true;
                         Log.Debug($"btnOpenDoor.Enabled = true")
    .....................

            private void btnOpenDoor_EnabledChanged(object sender, EventArgs e)
            {
                Log.Debug($"btnOpenDoor_EnabledChanged:{btnOpenDoor.Enabled}");
                if (btnOpenDoor.Enabled)
                {
                    btnOpenDoor.BackgroundImage = Resources.opendoor_icon;
                }
                else
                {
                    btnOpenDoor.BackgroundImage = Resources.opendoor_unenabled_icon;
                }
            }
    如果把屏幕分辨率调小一点,就没有问题。


    One world, one dream!


    2019年8月6日 3:02

全部回复

  • Hi,

    你可以尝试创建一个自定义Panel类来存放winforms的背景图片,具体代码如下:

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.Controls.Add(panelEnhanced);
                panelEnhanced.Dock = DockStyle.Fill;
            }
    
            PanelEnhanced panelEnhanced = new PanelEnhanced();
    
            bool flag = true;
            private void btChange_Click(object sender, EventArgs e)
            {
                if(flag)
                {
                    panelEnhanced.BackgroundImage = Image.FromFile(@"D:\pic\a.jpg");
                }
                else
                {
                    panelEnhanced.BackgroundImage = Image.FromFile(@"D:\pic\b.jpg");
                }
                flag = !flag;
            }
        }
    
        class PanelEnhanced : Panel
        {
            protected override void OnPaintBackground(PaintEventArgs e)
            {
                return;
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                this.DoubleBuffered = true;
                if (this.BackgroundImage != null)
                {
                    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    e.Graphics.DrawImage(
                        this.BackgroundImage,
                        new System.Drawing.Rectangle(0, 0, this.Width, this.Height),
                        0,
                        0,
                        this.BackgroundImage.Width,
                        this.BackgroundImage.Height,
                        System.Drawing.GraphicsUnit.Pixel);
                }
                base.OnPaint(e);
            }
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年8月6日 5:37
    版主
  • 这样做,岂不很麻烦,我有好多按钮的。

    One world, one dream!

    2019年8月6日 5:48