矩形形状开始闪烁 [英] Rectangle shape starts flickering

查看:133
本文介绍了矩形形状开始闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



有人能告诉我如何避免在窗体中闪烁矩形形状。我在我的表格中使用了矩形形状控制。我有一个相同形式的串口通信,所以当通信正在进行中时,我只是最小化了形式&当我恢复矩形形状时,每500ms闪烁一次,因为我已设置串口每500ms接收一次数据。因此&当我收到数据时,它开始闪烁。如何避免这样的事情??



我用过.doublebuffered = true;在构造函数&

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





仍然没有找到任何改进,所以plz帮我解决了这个问题。 />


提前谢谢。



我有一个mdi表单,其中我打开了1个子表单,包含矩形形状,串口通信&其中的许多其他控件。我做的是,我将选择comport&按开始按钮开始通过串口通信。通信开始后,处理接收的数据并进行处理。 UI会相应更新。我觉得在更新UI时,矩形形状正在被感染。以下是我的代码。



要修复矩形形状@屏幕中心,我使用了以下代码:



  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部分:



< pre lang =c#> 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 );

尝试
{
如果(sp。 IsOpen == false
{
sp.Open();
comstartbtn.Enabled = false ;
cmpprtcmbbox.Enabled = false ;
comstpbtn.Enabled = true ;
((MainWindow) this .MdiParent).toolStripStatusLabel1.Text = STATUS: + sp.PortName + 已成功打开;

}

}
catch (例外情况)
{

}
}

私有 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()。包含( 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 );
}
}
}





这个UV1ledbulb位于矩形形状上。

解决方案

您的代码覆盖 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窗口的问题 [ ^ ],

< a href =http://www.codeproject.com/Answers/172678/MDIContainer-giving-error#answer2> 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


这篇关于矩形形状开始闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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