C#控制MainForm Resizeing时的闪烁。 [英] C# Controls flickering while MainForm Resizeing.

查看:117
本文介绍了C#控制MainForm Resizeing时的闪烁。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,现在这是一个闪烁的问题,在MainForm调整大小时更改控件的大小和位置。每次用户更改主窗体的大小时,所有控件都会显着闪烁。激活双缓冲和SetStyle的一些标志仅在Cursor移动时消除闪烁,但在调整窗体大小时不会。有没有办法改变我的控件的dinamicaly位置和大小,避免闪烁?

Ok so now it is a flickering problem with changing controls Size and Location while MainForm is resizeing. Every time user changes the Size of the main form all controls are dramaticaly flickering. Activating double buffering and some flags of "SetStyle" remove flickering only while Cursor is moving, but not while Resizeing the form. Is there a way to change dinamicaly Location and Size of my controls and avoid there flickering?

推荐答案

检查你做了一些冲突的油漆事件或添加一些动态控制你在load事件或构造函数中调整运行时...



如果你不想显示闪烁

意味着下面代码将完成绘画后展示你的表格



把这个属性放在你的表格中并检查效果

check you have done some paint event that is conflicting or adding some dynamic control and which you are adjusting runtime on load event or in constructor...

if you want to not show flicker
means below code will show your form after it complete painting

put this property in your form and check effect
protected override CreateParams CreateParams 
{
    get {
        CreateParams cp = base.CreateParams;
        cp.ExStyle = cp.ExStyle | 0x2000000;
        return cp;
    }
}



快乐编码!

:)


Happy Coding!
:)


尝试将Main表单的DoubleBuffered属性设置为True。这应该有助于减少闪烁。



抱歉,没有仔细阅读你的问题,显然你已经尝试过了。我不明白为什么它不起作用 - 它应该减少闪烁,即使调整表格... [/ EDIT]
Try setting the Main form''s DoubleBuffered property to True. That should help reduce flickering.

Sorry, didn''t read your question carefully enough, clearly you''ve already tried that. I don''t understand why it didn''t work, though - it should reduce flickering even when resizing the form... [/EDIT]


引用:

每个人都好。

所以这解决了我的任务。我使用基于标准控件的修改后的自定义控件来补充所有标准控件。



以下是我的操作方法。如果您有更好的解决方案,请告诉我们!



1)使用标准Microsoft Visual Studio构造函数创建您想要的任何内容。



2)基于标准控件创建自定义控件。

Hello to every One.
So this solved me task. I repleced all standard controls with modified custom controls based on standard controls.

Here is how i did it. If you have better solution please let me know!

1) Create what ever you want with Standard Microsoft Visual Studio Constructor.

2) Create custom controls based on standard controls.

namespace Project_Name
{
    public class CustomPanel : Panel
    {
        private void CustomPanel_Resize(object sender, EventArgs e)
        {
            if (this.Visible) this.Refresh();

        }


        public CustomPanel()
        {
            this.Resize +=new EventHandler(CustomPanel_Resize);
            this.SetStyle(ControlStyles.UserPaint |
                          ControlStyles.AllPaintingInWmPaint |
                          ControlStyles.OptimizedDoubleBuffer,
                          true);
        }

    }


    public class CustomTabControl : TabControl
    {

        private void CustomTabControl_Resize(object sender, EventArgs e)
        {
           if (this.Visible) this.Refresh();
        }

        public CustomTabControl()
        {
            this.Resize +=new EventHandler(CustomTabControl_Resize);
            this.SetStyle(ControlStyles.UserPaint |
                                    ControlStyles.AllPaintingInWmPaint |
                                    ControlStyles.OptimizedDoubleBuffer,
                                    true);
        }
    }


    public class CustomButton : Button
    {
         private void CustomButton_Resize(object sender, EventArgs e)
        {
           if (this.Visible) this.Refresh();
        }


         public CustomButton()
        {
            this.Resize += new EventHandler(CustomButton_Resize);
            this.SetStyle(ControlStyles.UserPaint |
                                    ControlStyles.AllPaintingInWmPaint |
                                    ControlStyles.OptimizedDoubleBuffer,
                                    true);
        }

    }
}







注意(1):我为项目中使用的所有标准控件创建了自定义控件(图片框,复选框,菜单条纹等)。

注意(2):我需要动态创建控件。在这种情况下,我总是使用这个自定义控件。



3)在每个表格中你都可以找到Form Aame.Designer.cs。复制此文件并重命名。

FOR EXALE:您正在使用名为MainForm的名称。

然后它的desinger的文件名将有一个名称MainForm.Desirgen.cs。

在资源管理器中复制此文件并将其重命名为Custom_MainForm.Designer.cs。



4)在工作室中打开这个新的重命名文件。并删除




NOTE (1): i created custom controls for all standard controls i used in the project (pictureboxes, checkboxes, menu stripes and so on).
NOTE (2): I need to dynamically create controls. In this case, I always use the this custom controls.

3)In every Form you can find "Form Aame".Designer.cs. Copy this file and rename.
FOR EXALE: You are working with the from named "MainForm".
Then the file name of it`s desinger will have a name "MainForm.Desirgen.cs".
In explorer copy this file and rename it "Custom_MainForm.Designer.cs".

4) Open this new renamed file in your studio. And delete

private System.ComponentModel.IContainer components = null;











and

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





5)重命名此功能





5) Rename this function

private void InitializeComponent()





我用其他名字





with some other name i used

private void Initialize_Custom_Component()







6)使用Ctrl + F - > Repleace如下所示:

例如:






6) Using "Ctrl+F -> Repleace " repleace all like this:
For example:

            this.panel = new System.Windows.Forms.Panel();
            this.button = new System.Windows.Forms.Button();
            this.groupBox = new System.Windows.Forms.GroupBox();
            this.label = new System.Windows.Forms.Label();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.textBox = new System.Windows.Forms.TextBox();
//
//
//
        private System.Windows.Forms.Panel panel ;
        private System.Windows.Forms.Button button ;
        private System.Windows.Forms.GroupBox groupBox ;
        private System.Windows.Forms.Label label ;
        private System.Windows.Forms.RadioButton radioButton1 ;
        private System.Windows.Forms.TextBox textBox ;





进入这个





into this

             this.panel = new CustomPanel();
            this.button = new CustomButton();
            this.groupBox = new CustomGroupBox();
            this.label = new CustomLabel();
            this.radioButton1 = new CustomRadioButton();
            this.textBox = new CustomTextBox();
//
//
//
        private CustomPanel panel ;
        private CustomButton button ;
        private CustomGroupBox groupBox ;
        private CustomLabel label ;
        private CustomRadioButton radioButton1 ;
        private CustomTextBoxtextBox ;







7)转到表单的构造函数并重命名




7) Go to the constructor of your form and rename

InitializeComponent()





进入





into

Initialize_Custom_Component()





如果你们都做对了你的控件再也不会闪烁了。

但是要小心调整你的表格,这可能会降低你的表现。

所以我在我的全局项目属性表单中创建了一个复选框并将其命名为使用性能。

如果检查此chekbox我使用标准控件如果不是我使用我的自定义控件。



初始化的最终代码如下:





If you all did correct your Controls will never blink again.
But be carefull with resizing your form this may decrease your performance.
So i created a checkbox in my global project properties form and named it - "Use Performance".
If this chekbox is checked iam using standard controls if not i am using my custom controls.

And the final code of the initialization looks like

if (GLOBAL_PROPERTIES.Use_Performance_CheckBox_Checked) //public bool in static class
    InitializeComponent();
else
 Initialize_Custom_Component();





我的表格上有521个控件,我在第二个时间内有6-7个控件没有闪烁。我认为这是很好的转发。



我希望这个答案对某人有用。



快乐为你们所有人编码,我的朋友们。



I have 521 controls on my form, and i have 6-7 resizings in second without blinking. I think it is good resault.

I hope this answer will be usefull for somebody.

Happy Coding for all you, my friends.


这篇关于C#控制MainForm Resizeing时的闪烁。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆