想让一个label在form窗体上间隔一段时间闪烁一次隔一段时间在闪烁一次

霜寒月冷 2008-10-22 11:15:12
想让一个label在form窗体上间隔一段时间闪烁一次隔一段时间在闪烁一次

间隔时间成1,3,5....2n-1,递增的;
不要用timer控件,想用多线程using System.Threading
不知道如何操作.大家帮看下.
...全文
129 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
beckfun 2008-10-23
  • 打赏
  • 举报
回复
创建一个线程,在线程类里面定义一个label控件,在界面上拖一个label控件,
ClassStartThread startThread = new ClassStartThread( this.label1);
Thread st = new Thread(new ThreadStart(startThread.StartThread));
tUdpThread.IsBackground = true;
tUdpThread.Start();
[code=C#]
class ClassStartThread
{
private int tm=0;
private int lt=0;
private Label label1;
public ClassStartUdpThread(Label label2)
{
this.label1 = label2;
tm = DateTime.Now.Second;
tm += DateTime.Now.Minute * 60;
tm += DateTime.Now.Hour * 3600;
}
public void StartThread()
{
for(int i=0;;i++)
{
lt= DateTime.Now.Second;
lt += DateTime.Now.Minute * 60;
lt += DateTime.Now.Hour * 3600;
if((lt-tm)==((2*i)-1))
{
this.label1.Enabled=false;
}
Thread.Sleep(10);
this.label1.Enabled=true;
}
}
}[/code]

大致就这样,随便写的, 也不知道有没有错误,有错误,请楼下的指正!在此先谢过了!
gogogo 2008-10-23
  • 打赏
  • 举报
回复
为什么不用timer控件,以下的代码,timer1用于显示闪的效果,timer2用于控制你所谓的递增间隔的时间闪烁的控制,更简单更容易理解
Form1.Designer.cs
namespace WindowsApplication5
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.label1 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.timer2 = new System.Windows.Forms.Timer(this.components);
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(58, 53);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(71, 29);
this.label1.TabIndex = 0;
this.label1.Text = "我闪";
//
// timer1
//
this.timer1.Interval = 200;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// timer2
//
this.timer2.Interval = 200;
this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
//
// button1
//
this.button1.Location = new System.Drawing.Point(270, 78);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(386, 606);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Timer timer2;
private System.Windows.Forms.Button button1;
}
}


Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication5
{
public partial class Form1 : Form
{
private int nN = 0, nCount = 0;

public Form1()
{
InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)
{
label1.Visible = true;
timer1.Enabled = false;
}

private void button1_Click(object sender, EventArgs e)
{
timer2.Enabled = true;
}

private void timer2_Tick(object sender, EventArgs e)
{
if (nCount >= 2 * nN - 1)
{
nCount = 0;
nN++;
label1.Visible = false;
timer1.Enabled = true;
}
nCount++;
}
}
}
enihs 2008-10-23
  • 打赏
  • 举报
回复
quartz调度组件可完成你要的效果
zhangyu_xl 2008-10-23
  • 打赏
  • 举报
回复
学习
palmax 2008-10-23
  • 打赏
  • 举报
回复
闪烁?

可见->不可见->可见->..... 这样?

那加个timer就搞定了

要想显眼,还是定时改变label的字体颜色好些
霜寒月冷 2008-10-23
  • 打赏
  • 举报
回复
beckfun
gogogo

非常感谢你们的热心帮助!!问题基本解决了啊!!
error_one 2008-10-22
  • 打赏
  • 举报
回复
说错了.... 不是透明 应该的可见 不可见
error_one 2008-10-22
  • 打赏
  • 举报
回复
for循环
if 1,3,5....2n-1
Sleep 时间
把label透明度设为0
否则 100
霜寒月冷 2008-10-22
  • 打赏
  • 举报
回复
等半天没人回复,先去睡觉,明天在来看帖子了啊!!

111,118

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧