C#如何实现会移动的文字效果

发布时间:2021-04-30 10:11:15 作者:小新
来源:亿速云 阅读:215

这篇文章主要介绍了C#如何实现会移动的文字效果,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

C#实现会移动的文字效果的具体内容如下

1 题目描述

(1)Form1窗体设计界面如下:

C#如何实现会移动的文字效果

(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#如何实现会移动的文字效果

C#如何实现会移动的文字效果

C#如何实现会移动的文字效果

感谢你能够认真阅读完这篇文章,希望小编分享的“C#如何实现会移动的文字效果”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. css中怎么实现文字效果
  2. 如何利用纯CSS实现动态的文字效果

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:Golang中Bit数组怎么实现

下一篇:Android Studio如何实现透明状态栏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》