这篇文章主要为大家详细介绍了C#实现会移动的文字效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了C#实现会移动的文字效果的具体代码,供大家参考,具体内容如下
1 题目描述
(1)Form1窗体设计界面如下:
(2)窗体左侧为一个靠左停靠的panel,其中包含一个label控件;
(3)初试状态时,“水平移动”选中,当用户单击“开始移动”按钮时,label在panel中水平从左向右移动,单击“暂停移动”按钮时,label停在原位置不动;
(4)在label移动过程中,若用户切换移动方式,则弹出对话框,提示先暂停移动;在label暂停移动时,用户切换移动方式,label在原位置以新的移动方式进行移动;
2 源码详解
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Csharp7_2
{
    public partial class Form1 : Form
    {
        static int x = 0;
        static int y = 0;
        static int flag = 0;
        static int v = 0;
        static int h = 0;
        public Form1()
        {
            InitializeComponent();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (radioButton1.Checked && flag == 1)
            {
                if (label1.Location.X + label1.Size.Width >= (panel1.Location.X + panel1.Size.Width))
                {
                    v = 1;
                }
                if (label1.Location.X < panel1.Location.X)
                {
                    v = 0;
                }
                if (v == 0)
                {
                    x = 1;
                    y = 0;
                }
                if (v == 1)
                {
                    x = -1;
                    y = 0;
                }
            }
            if (radioButton2.Checked && flag == 1)
            {
                if (label1.Location.Y + label1.Size.Height >= (panel1.Location.Y + panel1.Size.Height))
                {
                    h = 1;
                }
                if (label1.Location.Y < panel1.Location.Y)
                {
                    h = 0;
                }
                if (h == 0)
                {
                    x = 0;
                    y = 1;
                }
                if (h == 1)
                {
                    x = 0;
                    y = -1;
                }
            }
            if (flag == 1)
            {
                Point p = new Point(label1.Location.X + x, label1.Location.Y + y);
                label1.Location = p;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            flag = 1;
            timer1.Start();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            flag = 0;
            timer1.Stop();
        }
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked == true && flag == 1)
            {
                flag = 0;
                radioButton2.Checked = true;
                radioButton1.Checked = false;
                MessageBox.Show("请先停止移动");
            }
        }
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true && flag == 1)
            {
                flag = 0;
                radioButton1.Checked = true;
                radioButton2.Checked = false;
                MessageBox.Show("请先停止移动");
            }
        }
    }
}
3 实现效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得得之家。
				 沃梦达教程
				
			本文标题为:C#实现会移动的文字效果
				
        
 
            
        
             猜你喜欢
        
	     - WPF使用DrawingContext实现绘制刻度条 2023-07-04
 - Unity3D实现渐变颜色效果 2023-01-16
 - Oracle中for循环的使用方法 2023-07-04
 - c# 模拟线性回归的示例 2023-03-14
 - 在C# 8中如何使用默认接口方法详解 2023-03-29
 - Unity Shader实现模糊效果 2023-04-27
 - user32.dll 函数说明小结 2022-12-26
 - .NET CORE DI 依赖注入 2023-09-27
 - 如何使用C# 捕获进程输出 2023-03-10
 - C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
 
						
						
						
						
						
				
				
				
				