且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

矩形形状开始闪烁

更新时间:2023-12-06 16:32:40

您的代码覆盖 CreateParams 是无关紧要的,硬编码立即常量是坏的。相反,您可以使用 DoubleBuffered ,或者使用 SetStyle 并设置样式 System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer

http://msdn.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms .control.setstyle.aspx [ ^ ]。



如果这没有帮助,请尝试创建最小化的完整代码示例并发布。请看我对这个问题的评论。



-SA


Jagadisha_Ingenious问:

先生,如果不是MDI,那么任何其他替代方案......?因为我的项目中有多个表单...

当然。我可以解释一下该做什么。请看我过去的答案:



如何在WPF中创建MDI父窗口? [ Solution 2 ],

在WPF中使用MDI窗口的问题 [ ^ ],

MDIContainer给出错误 [ ^ ],

如何设置子窗体最大化,最后的子窗体最小化 [ ^ ]。



-SA

Hi all,

Can anybody tell me how to avoid flickering of rectangle shape in windows form. I have used rectangle shape control in my form & i have a serial port communication in the same form, so when the communication is in process i just minimized the form & when i restored the rectangle shape flickers every 500ms since i have set the serial port to receive data every 500ms. So as & when i receive data it starts flickering. How to avoid such a thing...?

I have used this.doublebuffered = true; in the constructor &

protected override CreateParams CreateParams
       {
           get
           {
               CreateParams cp = base.CreateParams;
               cp.ExStyle |= 0x02000000;
               return cp;
           }
       }



Still found no improvements, so plz help me out in resolving this issue.

Thanks in advance.

I have a mdi form in which i have opened 1 of the childform, containing rectangle shape, serial port communication & many other controls in it. What i do is, i will select the comport & press start button to start communicating through serial port. Once the communication starts the received data is processed & UI is updated accordingly. What i feel is while updating the UI the rectangle shape is being infected. The following is my code.

To fix the rectangle shape @ the centre of the screen i have used the following code:

private void DisplayWindow_MaximumSizeChanged(object sender, EventArgs e)
  {
    rectangleShape1.Left = (Screen.PrimaryScreen.Bounds.Width / 2)-183;
    rectangleShape2.Left = (Screen.PrimaryScreen.Bounds.Width / 2) - 153;
  }

private void rectangleShape1_ParentChanged(object sender, EventArgs e)
  {
     rectangleShape1.Anchor = AnchorStyles.Top;
     rectangleShape2.Anchor = AnchorStyles.Top;
   }



Serial Port Comm part:

private void comstartbtn_Click(object sender, EventArgs e)
        {
            sp.PortName = cmpprtcmbbox.SelectedItem.ToString();
            sp.Parity = Parity.None;
            sp.StopBits = StopBits.One;
            sp.BaudRate = 9600;
            sp.DataBits = 8;
            timer = new System.Threading.Timer(new TimerCallback(Receive_Data), null, 0, 500);

            try
            {
                if (sp.IsOpen == false)
                {
                    sp.Open();
                    comstartbtn.Enabled = false;
                    cmpprtcmbbox.Enabled = false;
                    comstpbtn.Enabled = true;
                    ((MainWindow)this.MdiParent).toolStripStatusLabel1.Text = "STATUS : " + sp.PortName+ " Opened successfully";  

                }

            }
            catch (Exception ex)
            {

            }
        }
 
 private void Receive_Data(object obj)
    {           
            
            try
            {
                int btr = sp.BytesToRead;
                data_9 = new byte[btr];
                sp.Read(data_9, 0, btr);

                for (int i = 0; i < btr; i++)
                {
                    if (data_9[i] == 36 && start_collction == 0)
                    {
                        start_collction = 1;
                    }

                    if (start_collction == 1)
                    {
                        serialData.Append(data_9[i].ToString() + ","); 
                        if (serialData.ToString().Contains("65"))
                        {
                            start_collction = 0;
                            q = serialData.ToString().Split(',');
                            List<string> list = new List<string>(q);
                            list.Remove("65");
                            list.Remove("");

                            if (q[1] == "1") 
                            {
                             UV1ledbulb.Blink(0);
                             }
                           }
                         }



This UV1ledbulb lies on the rectangle shape.

Your code overriding CreateParams is irrelevant, and hard-coded immediate constant is bad. Instead, you can just use DoubleBuffered, or, alternatively, use SetStyle and set the styles System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer:
http://msdn.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx[^].

If this does not help, try to create a minimized complete code sample and post it. Please see my comment to the question.

—SA


Jagadisha_Ingenious asked:

Sir if not MDI, then any other alternatives for it…? Since i have multiple forms in my project…

Sure. I can explain what to do instead. Please see my past answers:

How to Create MDI Parent Window in WPF? [Solution 2],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

—SA