大家好,又见面了,我是你们的朋友全栈君。
一.编程思想
(1).考虑聊天功能的实现,文本的获取以及显示;
(2).窗体制作时需要的控件;
(3).考虑是否可以发送空格以及空字符;
(4).刚开始进入界面时焦点的位置;
(5).获取输入内容鼠标光标跟踪最后一个字符处;
(6).窗口抖动依赖位置的变化进行实现;
(6).点击按钮对应的触发事件;
二.代码的实现
1.需要的控件简介:
TextBox:允许用户输入文本,并提供多行编辑和密码字符掩码功能;
Button:当用户单击它时引发事件;
Timer:按用户定义的间隔引发事件的组件
2.窗体加载时完成事件
private void 聊天窗口_Load(object sender, EventArgs e)
{
//设置窗体在桌面的位置
this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
textBox1.BackColor = Color.SkyBlue;//设置textbox1的颜色
textBox2.BackColor = Color.GreenYellow;//设置textbox2的颜色
this.BackColor = Color.Black;//设置窗体的颜色
//设置按钮的文本
button1.Text = "发送";
button2.Text = "抖动";
button3.Text = "关闭";
textBox1.ReadOnly = true;//设置输出为仅读
this.Opacity = 0.4;//设置不透明度
}
3.点击发送按钮时的代码,要设置不能发送空内容,并且获取焦点,设置发送时显示的发送人以及当前时间和输入的内容
private void button1_Click(object sender, EventArgs e)
{
//设置文本内容不为空字符以及不能为空格
if (textBox2.Text.Trim() == "")//移除所有空白前导字符以及尾部字符
{
this.textBox2.Text = "";//设置为空
this.textBox2.Focus();//获取焦点
MessageBox.Show("发送内容不能为空,请至少包含一个字符!");
}
//第二种方法
//if (string.IsNullOrWhiteSpace(textBox2.Text))//指示指定的字符串是null,空还是仅有空白字符组成
//{
// this.textBox2.Text = "";
// this.textBox2.Focus();
// MessageBox.Show("发送内容不能为空,请至少包含一个字符!");
//}
else {
//获取textbox2内容在textbox1显示
textBox1.Text +="\r\n" + "{借愁哥哥(1848514604)}" + DateTime.Now + "\r\n" + "\r\n" + textBox2.Text + "\r\n";//设置发送时显示的发送人以及当前时间和输入的内容
textBox2.Text = "";
this.textBox2.Focus();//为textbox2设置焦点
}
}
4.设置窗体的抖动
private void button2_Click(object sender, EventArgs e)
{
int x= this.Left;
int y= this.Top;
//第一种方法timer解决
//for (int i = 0; i < 10; i++)
//{
// this.Location = new Point(x,y);
// this.Location = new Point(x-5,y);
// this.Location = new Point(x-5,y-5);
// this.Location = new Point(x,y-5);
// this.Location = new Point(x+5,y-5);
// this.Location = new Point(x+5,y);
// this.Location = new Point(x+5,y+5);
// this.Location = new Point(x,y+5);
// this.Location = new Point(x-5,y+5);
// this.Location = new Point(x-5,y);
// this.Location = new Point(x,y);
//}
//第二种方法之4个点
//for (int i = 0; i < 10; i++)
//{
// this.Location = new Point(x+10,y);
// Thread.Sleep(100);
// this.Location = new Point(x+10,y+10);
// Thread.Sleep(100);
// this.Location=new Point(x,y+10);
// Thread.Sleep(100);
// this.Location = new Point(x, y);
// Thread.Sleep(100);
//}
//第二种方法之9个点
for (int i = 0; i < 10; i++)
{
this.Location = new Point(x,y);
Thread.Sleep(10);
this.Location = new Point(x+10, y);
Thread.Sleep(10);
this.Location = new Point(x+10, y+10);
Thread.Sleep(10);
this.Location = new Point(x, y+10);
Thread.Sleep(10);
this.Location = new Point(x-10, y+10);
Thread.Sleep(10);
this.Location = new Point(x-10, y);
Thread.Sleep(10);
this.Location = new Point(x-10, y-10);
Thread.Sleep(10);
this.Location = new Point(x, y-10);
Thread.Sleep(10);
this.Location = new Point(x+10, y-10);
Thread.Sleep(10);
this.Location = new Point(x+10, y);
Thread.Sleep(10);
this.Location = new Point(x, y);
Thread.Sleep(10);
}
textBox1.Text+= "\r\n" + "{借愁哥哥(1848514604)}" + DateTime.Now + "\r\n" + "\r\n" + "对方向您发送了一条抖动消息" + "\r\n";
textBox2.Focus();//获取焦点
}
5.关闭按钮的设计
private void button3_Click(object sender, EventArgs e)
{
this.Close();//关闭此窗体
}
6.textbox1的设计
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
//设置光标定位
this.textBox1.Focus();//为控件设置焦点
this.textBox1.Select(this.textBox1.TextLength, 0);//选择文本框中的文本范围,textlength为获取控件中文本长度
this.textBox1.ScrollToCaret();//将控件内容滚动到当前插入符号位置
}
6.textbox2的设计
private void textBox2_TextChanged_1(object sender, EventArgs e)
{
//设置textbox颜色
Random n = new Random();
textBox2.ForeColor = Color.FromArgb(n.Next(0, 255), n.Next(0, 255), n.Next(0, 255));
//textBox1.BackColor = textBox2.ForeColor;
this.Text = "正在输入...";
}
三.其他控件也可自加,看最终效果:
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/161482.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...