2

これは私が更新しているコードですListBox:

this.Invoke(new Action(() => data = new List<string>()));
this.Invoke(new Action(() => data.Add("Gpu Temeprature --- " + sensor.Value.ToString())));
this.Invoke(new Action(() => listBox1.DataSource = null));
this.Invoke(new Action(() => listBox1.DataSource = data));
this.Invoke(new Action(() => listBox1.Invalidate()));

変数データはList<string>、一度データに新しいものを追加したため、センサーの値(sensor.Value)が更新され、古い値が削除されるたびに新しい値が追加されます。

項目自体がListBox1 秒ほど点滅しないこともあれば、 だけsensor.Valueが点滅することもあります。

を追加しようとしValidate()ましたListBoxが、役に立ちませんでした。

これは、バックグラウンドの作業イベントです。

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;
    while (true)
    {

        if ((worker.CancellationPending == true))
        {
            e.Cancel = true;
            break;
        }
        else
        {
            if (tempCpuValue >= (float?)nud1.Value || tempGpuValue >= (float?)nud1.Value)
            {
                soundPlay = true;
                blinking_label();
                NudgeMe();
            }
            else
            {
                soundPlay = false;
                stop_alarm = true;

            }
            cpuView();
            gpuView();
        }
    }
}

listBox には、さらに 2 つのイベントがあります。

private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
    e.ItemHeight = 25;
}

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index == -1)
    {
    }
    else
    {

        ColorText.ColorListBox(data, e);

    }
}

また、listBox 内の項目に色を付ける別のクラスの ColorText 関数:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace GatherLinks
{
    class ColorText
    {


        public static void Texts(RichTextBox box, string text, Color color)
        {
            box.SelectionStart = box.TextLength;
            box.SelectionLength = 0;

            box.SelectionColor = color;
            box.AppendText(text);
            box.SelectionColor = box.ForeColor;
        }

        public static void ColorListBox(List<string> data, DrawItemEventArgs e)
        {
            string strLeft = null;
            string strMid = "---";
            string strRight = null;
            if (data[e.Index].Contains(strMid))
            {
                int index = data[e.Index].IndexOf(strMid);
                strLeft = data[e.Index].Substring(0, index);
                strRight = data[e.Index].Substring(index + strMid.Length);
            }

            using (Font f = new Font(FontFamily.GenericSansSerif, 20, FontStyle.Regular))
            {
                float startPos;
                e.Graphics.DrawString(strLeft, f, Brushes.Red, e.Bounds.X, e.Bounds.Y);
                startPos = e.Graphics.MeasureString(strLeft, f).Width;
                e.Graphics.DrawString(strMid, f, Brushes.Black, e.Bounds.X + startPos, e.Bounds.Y);
                startPos = e.Graphics.MeasureString(strLeft + strMid, f).Width;
                e.Graphics.DrawString(strRight, f, Brushes.Green, e.Bounds.X + startPos, e.Bounds.Y);
            }
        }
    }
}

ListBox数秒ごと、または新しいセンサーが更新されるたびに点滅しないようにするにはどうすればよいですか? 正確な理由と時期はわかりませんが、ListBoxアイテムとセンサーの値が点滅しています。

4

3 に答える 3

0

これをフォームに配置することを解決したら、次のようにします。

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
        return cp;
    }
}
于 2013-03-07T21:42:59.953 に答える
0

DataSource問題は、をクリアしてから再度設定しているという事実から来ていると思います。データ ソースを に設定し、データが変更されたときにリセットする必要はありませんnull

次の 2 行を削除して、ちらつきがなくなるかどうかを確認してください。

this.Invoke(new Action(() => listBox1.DataSource = null));
this.Invoke(new Action(() => listBox1.DataSource = data));
于 2013-03-07T21:26:07.890 に答える
0

メソッドの使用Invoke法が間違っているため、点滅の原因である可能性があります。

コードの最初のセクションを次のように変更してみてください。

this.Invoke(new Action(() =>
    {
        data = new List<string>();
        data.Add("Gpu Temeprature --- " + sensor.Value.ToString());
        listBox1.DataSource = null;
        listBox1.DataSource = data;
        listBox1.Invalidate()
    }));

メソッドを呼び出すたびInvokeに、メッセージがアプリケーションのメッセージ キューにポストされ、後で UI がビジーでないときに UI スレッドで処理されます。複数回呼び出すことInvokeで、個別に処理される複数のメッセージを送信します。

于 2013-03-07T21:26:45.700 に答える