C#實(shí)現(xiàn)會(huì)移動(dòng)的文字效果
本文實(shí)例為大家分享了C#實(shí)現(xiàn)會(huì)移動(dòng)的文字效果的具體代碼,供大家參考,具體內(nèi)容如下
1 題目描述
(1)Form1窗體設(shè)計(jì)界面如下:

(2)窗體左側(cè)為一個(gè)靠左??康膒anel,其中包含一個(gè)label控件;
(3)初試狀態(tài)時(shí),“水平移動(dòng)”選中,當(dāng)用戶單擊“開始移動(dòng)”按鈕時(shí),label在panel中水平從左向右移動(dòng),單擊“暫停移動(dòng)”按鈕時(shí),label停在原位置不動(dòng);
(4)在label移動(dòng)過程中,若用戶切換移動(dòng)方式,則彈出對(duì)話框,提示先暫停移動(dòng);在label暫停移動(dòng)時(shí),用戶切換移動(dòng)方式,label在原位置以新的移動(dòng)方式進(jìn)行移動(dòng);
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("請(qǐng)先停止移動(dòng)");
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked == true && flag == 1)
{
flag = 0;
radioButton1.Checked = true;
radioButton2.Checked = false;
MessageBox.Show("請(qǐng)先停止移動(dòng)");
}
}
}
}
3 實(shí)現(xiàn)效果



以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用struct直接轉(zhuǎn)換下位機(jī)數(shù)據(jù)的示例代碼
這篇文章主要介紹了C#使用struct直接轉(zhuǎn)換下位機(jī)數(shù)據(jù)的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
C#入門之checked和unchecked的區(qū)別實(shí)例解析
這篇文章主要介紹了C#中checked和unchecked的區(qū)別,是學(xué)習(xí)C#必須要牢固掌握的,需要的朋友可以參考下2014-08-08
C#基于純數(shù)學(xué)方法遞歸實(shí)現(xiàn)貨幣數(shù)字轉(zhuǎn)換中文功能詳解
這篇文章主要介紹了C#基于純數(shù)學(xué)方法遞歸實(shí)現(xiàn)貨幣數(shù)字轉(zhuǎn)換中文功能,涉及C#針對(duì)字符串的遍歷、轉(zhuǎn)換與數(shù)學(xué)運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
C#基礎(chǔ)知識(shí)之base關(guān)鍵字介紹
本文主要介紹base關(guān)鍵字的使用方法,base關(guān)鍵字可以調(diào)用基類重寫的方法,可以調(diào)用基類的構(gòu)造方法,還可以在EntityFramework中使用,下面一一介紹。2016-04-04
C#影院售票系統(tǒng)畢業(yè)設(shè)計(jì)(1)
這篇文章主要介紹了C#影院售票系統(tǒng)畢業(yè)設(shè)計(jì),獻(xiàn)上了9個(gè)類的設(shè)計(jì),需要的朋友可以參考下2015-11-11
解決C#中WebBrowser的DocumentCompleted事件不執(zhí)行的實(shí)現(xiàn)方法
本篇文章是對(duì)C#中WebBrowser的DocumentCompleted事件不執(zhí)行解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

