加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

.net – 为什么winform面板仅在鼠标悬停或鼠标点击时更新?

发布时间:2020-12-14 01:39:49 所属栏目:Linux 来源:网络整理
导读:我创建了一个测试程序,它应该在 linux(PC LinuxOS)下反复地来回更改面板的背景颜色,但它并没有真正起作用.要么它只更新面板的背景颜色只有当你点击某个东西或鼠标悬停在winform上然后它停止或程序在运行一段时间后完全崩溃. 以下是2个面板,一个按钮和一个计
我创建了一个测试程序,它应该在 linux(PC LinuxOS)下反复地来回更改面板的背景颜色,但它并没有真正起作用.要么它只更新面板的背景颜色只有当你点击某个东西或鼠标悬停在winform上然后它停止或程序在运行一段时间后完全崩溃.

以下是2个面板,一个按钮和一个计时器的winform外观:

这是它背后的代码:

namespace TestIndicator;

interface

uses
  System.Drawing,System.Collections,System.Collections.Generic,System.Windows.Forms,System.ComponentModel;

type
  /// <summary>
  /// Summary description for MainForm.
  /// </summary>
  MainForm = partial class(System.Windows.Forms.Form)
  private
    method d_Click(sender: System.Object; e: System.EventArgs);
    method timer1_Tick(sender: System.Object; e: System.EventArgs);
  protected
    method Dispose(disposing: Boolean); override;
  public
    constructor;
  end;

var
    TurnOnRx,TurnOnTx:Boolean;

implementation

{$REGION Construction and Disposition}
constructor MainForm;
begin
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent();

  //
  // TODO: Add any constructor code after InitializeComponent call
  //
  TurnOnRx := true;
  TurnOnTx := true;
end;

method MainForm.Dispose(disposing: Boolean);
begin
  if disposing then begin
    if assigned(components) then
      components.Dispose();

    //
    // TODO: Add custom disposition code here
    //
  end;
  inherited Dispose(disposing);
end;
{$ENDREGION}

method MainForm.d_Click(sender: System.Object; e: System.EventArgs);
begin
    timer1.Enabled := not timer1.Enabled;  
end;

method MainForm.timer1_Tick(sender: System.Object; e: System.EventArgs);
begin
    if TurnOnTx then
    begin
        TurnOnTx:=false;
        TxLight.BackColor := Color.Red;
    end
    else
    begin
        TurnOnTx:=true;
        TxLight.BackColor := Color.black;
    end;

    if TurnOnRx then
    begin
        TurnOnRx := false;
        RxLight.BackColor := Color.Lime;
    end
    else
    begin
        TurnOnRx := true;
        RxLight.BackColor := Color.Black;
    end;
end;

end.

解决方法

也许定时器间隔太短,颜色变化太快?

namespace TestIndicator;
interface
uses
  System.Drawing,TurnOnTx:Boolean;

implementation

{$REGION Construction and Disposition}
constructor MainForm;
begin
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent();

  //
  // TODO: Add any constructor code after InitializeComponent call
  //
  TurnOnRx := true;
  TurnOnTx := true;

  timer1.Inverval := 1000; 
end;

method MainForm.Dispose(disposing: Boolean);
begin
  if disposing then begin
    if assigned(components) then
      components.Dispose();

    //
    // TODO: Add custom disposition code here
    //
  end;
  inherited Dispose(disposing);
end;
{$ENDREGION}

method MainForm.d_Click(sender: System.Object; e: System.EventArgs);
begin
    timer1.Enabled := not timer1.Enabled;  
end;

method MainForm.timer1_Tick(sender: System.Object; e: System.EventArgs);
begin
    if TurnOnTx then
    begin
        TurnOnTx:=false;
        TxLight.BackColor := Color.Red;
    end
    else
    begin
        TurnOnTx:=true;
        TxLight.BackColor := Color.black;
    end;

    if TurnOnRx then
    begin
        TurnOnRx := false;
        RxLight.BackColor := Color.Lime;
    end
    else
    begin
        TurnOnRx := true;
        RxLight.BackColor := Color.Black;
    end;

    TxLight.Refresh();
    RxLight.Refresh();

    Application.DoEvents();
end;

end.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读