110,539
社区成员
发帖
与我相关
我的任务
分享
BP = new Bitmap(_w + 100, _h);
//此处的100为两个首尾相接的字幕间距
BP = new Bitmap(_w + (Lb.Width-100), _h);
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{
//code by dylike
private string ADText = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private int _ScrollLeft = 0;
private Bitmap _textbmp;
//本代码可以保证滚动流畅并且不闪画面,如需最佳效果,建议:Timer1.Interval=10
//界面上放一Label控件,Autosize=fase,手动调整需要的宽度,高度将由代码控制,无需关注
private void Timer1_Tick(System.Object sender, System.EventArgs e)
{
//Try
using (_tmpBP == new Bitmap(Label1.Width, Label1.Height)) {
using (Graphics G = Graphics.FromImage(_tmpBP)) {
if (_textbmp != null) {
_ScrollLeft = (_ScrollLeft <= -_textbmp.Width * 2 ? Label1.Width : _ScrollLeft - 1);
// _ScrollLeft - 1中的1为每次移动速度,此处为每次移动1个像素
using (System.Drawing.TextureBrush imgBrs = new System.Drawing.TextureBrush(_textbmp)) {
imgBrs.WrapMode = Drawing2D.WrapMode.Tile;
imgBrs.TranslateTransform(_ScrollLeft, 0);
G.FillRectangle(imgBrs, new Rectangle(0, 0, Label1.Width, Label1.Height));
}
}
}
if (_tmpBP != null) {
if (Label1.BackgroundImage != null)
Label1.BackgroundImage = null;
Label1.BackgroundImage = new Bitmap(_tmpBP);
}
}
//Catch
//End Try
}
private Bitmap _Gettextbitmap(Label Lb, string ScrollText)
{
try {
this.DoubleBuffered = true;
Bitmap BP = default(Bitmap);
SizeF _Sz = default(SizeF);
using (SolidBrush Brs = new SolidBrush(Lb.ForeColor)) {
using (StringFormat _sf = new StringFormat()) {
_sf.Alignment = StringAlignment.Center;
_sf.LineAlignment = StringAlignment.Near;
using (Graphics G = this.CreateGraphics) {
_Sz = G.MeasureString(ScrollText, Lb.Font, new Size(99999, 99999), _sf);
}
}
_Sz = new SizeF(_Sz.Width + 1, _Sz.Height + 1);
int _w = 0;
int _h = 0;
Panel1.Size = _Sz.ToSize;
_w = _Sz.Width;
_h = _Sz.Height;
BP = new Bitmap(_w + 100, _h);
//此处的100为两个首尾相接的字幕间距
using (Graphics G2 = Graphics.FromImage(BP)) {
G2.DrawString(ScrollText, Lb.Font, Brs, 0, 0);
}
}
return BP;
} catch {
return null;
}
}
private void Form1_Shown(object sender, System.EventArgs e)
{
//注意,写在此方法内是为了确保Label1控件已经创建
_textbmp = _Gettextbitmap(Label1, ADText);
Label1.BackgroundImageLayout = ImageLayout.None;
Label1.Text = "";
Label1.AutoSize = false;
Label1.Height = _textbmp.Height;
}
public Form1()
{
Shown += Form1_Shown;
}
}
Graphic g=this.createGraphic();
g.drawString("Hello",this.font, new Point(50,50));
g.dispose();
Thread th = null;
private void frm_Sub_Load(object sender, EventArgs e)
{
this.Width = Screen.PrimaryScreen.Bounds.Width;
Random r = new Random();
this.Location = new Point(0,r.Next(0,Screen.PrimaryScreen.Bounds.Height-this.Height));
this.label1.Width = this.Width;
label1.Height = this.Height-10;
label1.Location = new Point(this.Width, 5);
label1.Top = 0;
label1.Font = new Font(FontFamily.GenericSansSerif, (float)(((this.Height - 10) * 72) / 96));
label1.Text = "你好啊~~~~~~~~~~~abc ABC";
th = new Thread(new ThreadStart(runSub));
th.IsBackground = true;
th.Start();
}
private void runSub()
{
while (true)
{
if (this.InvokeRequired)
{
try
{
this.Invoke(new subTitle(sub));
}
catch { }
}
Thread.Sleep(10);
}
}
private delegate void subTitle();
private void sub()
{
if (label1.Left > -label1.Width)
{
label1.Left -=1;
}
else
{
label1.Left = this.Width;
}
}