winform下图片+文字 跑马灯功能实现
2012-10-22 17:44
267 查看
x年没写.net的程序了,工作需要写个跑马灯程序,也当找找感觉吧。
天下文章一大抄,这个代码也是从网上的一个原型修改而来的...以后我可能要接触比较多的.net下的应用,大家多交流...
【运行效果】从右至左跑动
【资源列表】背景是一个空白图片
【实现原理】
创建一个picturebox,然后在其grapics循环(位移)绘制imgage和string。
与主窗口为同一线程,CPU占用率可接受;对于屏幕抖动没有做太多处理,对于我当前的产品已经够用了。
【源代码】
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
#region 全局变量
System.Windows.Forms.PictureBox m_pbPicRoll; // 界面容器
List<Image> m_lsImages = new List<Image>();
List<string> m_lsImageNames = new List<string>();
Graphics m_ghContainer;
Image m_imgBackground;
Rectangle m_rcShowRegion;
int m_nCurImgIndex = 0; // 当前显示的图片
int m_nImgHeight; // 图片呈现的高度
int m_nImgWidth; // 图片呈现的宽度
int m_nScrollDelta; // 图片滚动边距
int m_nScrollPicNums; // 屏幕呈现的图片数
int m_nTitleRegionHeight;// 文字区高度
Font m_ftPicTileFont;
SolidBrush m_bhPicTitleBrush;
StringFormat m_sfTitleFormat;
Timer tm_Roll = new Timer();
#endregion 全局变量
public Form1()
{
InitializeComponent();
this.form_layout();
}
private void form_layout()
{
this.DoubleBuffered = true;
this.StartPosition = FormStartPosition.CenterScreen;
this.Size = new Size(800, 800);
this.BackColor = Color.LightBlue;
m_pbPicRoll = new PictureBox();
m_pbPicRoll.Location = new Point(50, 50);
m_pbPicRoll.Size = new Size(600, 300);
this.Controls.Add(m_pbPicRoll);
}
private void Form1_Load(object sender, EventArgs e)
{
this.init_img_roll();
}
public static string get_filename(String path)
{
if (path.Contains("\\"))
{
string[] arr = path.Split('\\');
return arr[arr.Length - 1];
}
else
{
string[] arr = path.Split('/');
return arr[arr.Length - 1];
}
}
private void init_img_roll()
{
m_imgBackground = Image.FromFile(string.Format("{0}/Images/bg.png", Application.StartupPath));
string[] strImgFiles = Directory.GetFiles(string.Format("{0}/Images", Application.StartupPath), "*.jpg");
string strImgFormat = ".jpg";
char[] szTemp = strImgFormat.ToCharArray();
string strFileName;
foreach (string str_path in strImgFiles)
{
m_lsImages.Add(Image.FromFile(str_path));
strFileName = get_filename(str_path).TrimEnd(szTemp);
m_lsImageNames.Add(strFileName);
}
m_ghContainer = m_pbPicRoll.CreateGraphics();
m_nImgHeight = 200;
m_nImgWidth = 200;
m_nScrollPicNums = m_pbPicRoll.Width / m_nImgWidth + 1; // 每次滚的图片个数
m_nTitleRegionHeight = 30;
m_ftPicTileFont = new Font("Arial", 14);
m_bhPicTitleBrush = new SolidBrush(Color.Black);
m_sfTitleFormat = new StringFormat();
m_sfTitleFormat.Alignment = StringAlignment.Center;
m_rcShowRegion = new Rectangle(new Point(0, 0), new Size(m_pbPicRoll.Width, m_nImgHeight + m_nTitleRegionHeight));
tm_Roll.Interval = 150;
tm_Roll.Tick += new EventHandler(timer_roll_tick);
tm_Roll.Start();
}
private void timer_roll_tick(object sender, EventArgs e)
{
if (Math.Abs(m_nScrollDelta) >= m_nImgHeight) // 图片顶端滚出picturebox
{
m_nCurImgIndex = (++m_nCurImgIndex) % m_lsImages.Count;
m_nScrollDelta = 0;
}
Bitmap btPrestScope = new Bitmap(m_pbPicRoll.Width, m_nImgHeight + m_nTitleRegionHeight);
Graphics ghPrestScope = Graphics.FromImage(btPrestScope);
// 仅刷新文字区背景,以解决文字覆盖问题
ghPrestScope.DrawImage(m_imgBackground,m_rcShowRegion);
for (int i = 0; i < m_nScrollPicNums; i++ )
{
ghPrestScope.DrawImage(m_lsImages[(m_nCurImgIndex + i) % m_lsImages.Count],
new Rectangle(new Point(m_nScrollDelta + i * m_nImgWidth, 0),
new Size(m_nImgWidth - 10, m_nImgHeight - 10))); // 10为了图片与周围拉开距离
ghPrestScope.DrawString(m_lsImageNames[(m_nCurImgIndex + i) % m_lsImageNames.Count],
m_ftPicTileFont,
m_bhPicTitleBrush,
new Point(m_nScrollDelta + (int)((i+0.5) * m_nImgWidth), 200 + 2), // 0.5为了居中,2为了垂直拉开点距离
m_sfTitleFormat);
}
m_ghContainer.DrawImage(btPrestScope,m_rcShowRegion);
ghPrestScope.Dispose();
btPrestScope.Dispose();
m_nScrollDelta -= 2; // 每次滚动2个像素
}
}
}
增加鼠标点击图片操作
转载请注明出处:/article/1938342.html
天下文章一大抄,这个代码也是从网上的一个原型修改而来的...以后我可能要接触比较多的.net下的应用,大家多交流...
【运行效果】从右至左跑动
【资源列表】背景是一个空白图片
【实现原理】
创建一个picturebox,然后在其grapics循环(位移)绘制imgage和string。
与主窗口为同一线程,CPU占用率可接受;对于屏幕抖动没有做太多处理,对于我当前的产品已经够用了。
【源代码】
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
#region 全局变量
System.Windows.Forms.PictureBox m_pbPicRoll; // 界面容器
List<Image> m_lsImages = new List<Image>();
List<string> m_lsImageNames = new List<string>();
Graphics m_ghContainer;
Image m_imgBackground;
Rectangle m_rcShowRegion;
int m_nCurImgIndex = 0; // 当前显示的图片
int m_nImgHeight; // 图片呈现的高度
int m_nImgWidth; // 图片呈现的宽度
int m_nScrollDelta; // 图片滚动边距
int m_nScrollPicNums; // 屏幕呈现的图片数
int m_nTitleRegionHeight;// 文字区高度
Font m_ftPicTileFont;
SolidBrush m_bhPicTitleBrush;
StringFormat m_sfTitleFormat;
Timer tm_Roll = new Timer();
#endregion 全局变量
public Form1()
{
InitializeComponent();
this.form_layout();
}
private void form_layout()
{
this.DoubleBuffered = true;
this.StartPosition = FormStartPosition.CenterScreen;
this.Size = new Size(800, 800);
this.BackColor = Color.LightBlue;
m_pbPicRoll = new PictureBox();
m_pbPicRoll.Location = new Point(50, 50);
m_pbPicRoll.Size = new Size(600, 300);
this.Controls.Add(m_pbPicRoll);
}
private void Form1_Load(object sender, EventArgs e)
{
this.init_img_roll();
}
public static string get_filename(String path)
{
if (path.Contains("\\"))
{
string[] arr = path.Split('\\');
return arr[arr.Length - 1];
}
else
{
string[] arr = path.Split('/');
return arr[arr.Length - 1];
}
}
private void init_img_roll()
{
m_imgBackground = Image.FromFile(string.Format("{0}/Images/bg.png", Application.StartupPath));
string[] strImgFiles = Directory.GetFiles(string.Format("{0}/Images", Application.StartupPath), "*.jpg");
string strImgFormat = ".jpg";
char[] szTemp = strImgFormat.ToCharArray();
string strFileName;
foreach (string str_path in strImgFiles)
{
m_lsImages.Add(Image.FromFile(str_path));
strFileName = get_filename(str_path).TrimEnd(szTemp);
m_lsImageNames.Add(strFileName);
}
m_ghContainer = m_pbPicRoll.CreateGraphics();
m_nImgHeight = 200;
m_nImgWidth = 200;
m_nScrollPicNums = m_pbPicRoll.Width / m_nImgWidth + 1; // 每次滚的图片个数
m_nTitleRegionHeight = 30;
m_ftPicTileFont = new Font("Arial", 14);
m_bhPicTitleBrush = new SolidBrush(Color.Black);
m_sfTitleFormat = new StringFormat();
m_sfTitleFormat.Alignment = StringAlignment.Center;
m_rcShowRegion = new Rectangle(new Point(0, 0), new Size(m_pbPicRoll.Width, m_nImgHeight + m_nTitleRegionHeight));
tm_Roll.Interval = 150;
tm_Roll.Tick += new EventHandler(timer_roll_tick);
tm_Roll.Start();
}
private void timer_roll_tick(object sender, EventArgs e)
{
if (Math.Abs(m_nScrollDelta) >= m_nImgHeight) // 图片顶端滚出picturebox
{
m_nCurImgIndex = (++m_nCurImgIndex) % m_lsImages.Count;
m_nScrollDelta = 0;
}
Bitmap btPrestScope = new Bitmap(m_pbPicRoll.Width, m_nImgHeight + m_nTitleRegionHeight);
Graphics ghPrestScope = Graphics.FromImage(btPrestScope);
// 仅刷新文字区背景,以解决文字覆盖问题
ghPrestScope.DrawImage(m_imgBackground,m_rcShowRegion);
for (int i = 0; i < m_nScrollPicNums; i++ )
{
ghPrestScope.DrawImage(m_lsImages[(m_nCurImgIndex + i) % m_lsImages.Count],
new Rectangle(new Point(m_nScrollDelta + i * m_nImgWidth, 0),
new Size(m_nImgWidth - 10, m_nImgHeight - 10))); // 10为了图片与周围拉开距离
ghPrestScope.DrawString(m_lsImageNames[(m_nCurImgIndex + i) % m_lsImageNames.Count],
m_ftPicTileFont,
m_bhPicTitleBrush,
new Point(m_nScrollDelta + (int)((i+0.5) * m_nImgWidth), 200 + 2), // 0.5为了居中,2为了垂直拉开点距离
m_sfTitleFormat);
}
m_ghContainer.DrawImage(btPrestScope,m_rcShowRegion);
ghPrestScope.Dispose();
btPrestScope.Dispose();
m_nScrollDelta -= 2; // 每次滚动2个像素
}
}
}
增加鼠标点击图片操作
private void onmouseclick_picroll(object sender, MouseEventArgs e) { int nReminder = 0, nQuotient = 0, nClickedImgIndex = 0; nQuotient = Math.DivRem(e.X - m_nScrollDelta, m_nImgWidth,out nReminder); // m_nScrollDelta在本例中必为负数 nClickedImgIndex = (m_nCurImgIndex + nQuotient) % m_lsImages.Count; System.Console.WriteLine(string.Format("x:{0}, y:{1}, scroll:{2},regionindex is {3}, offset is {4}", e.X, e.Y, m_nScrollDelta, nQuotient, nReminder)); if (nClickedImgIndex < m_lsImageNames.Count) { System.Console.WriteLine(m_lsImageNames[nClickedImgIndex]); MessageBox.Show(string.Format("您现在点击的特色农业是:{0}",m_lsImageNames[nClickedImgIndex]),"test"); } else { System.Console.WriteLine(string.Format("out of index:{0}, we must have a global exception handler.", nClickedImgIndex)); } }
转载请注明出处:/article/1938342.html
相关文章推荐
- 1.3 实现图片压缩、添加文字或图片水印、指定位置裁剪等功能的ImageUtility(内有效果图)
- C# WinForm 实现增删改查等功能(Access版) 系列之七-图片处理
- 本图片处理类功能非常之强大可以实现几乎所有WEB开发中对图像的处理功能都集成了,包括有缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等功能
- Android系统自带分享功能的实现(可同时分享文字和图片)
- 安卓之文字,链接,图片,音乐,分享功能的实现
- PHP文字转图片功能原理与实现方法分析
- 在Toast中实现文字滚动功能(也就是跑马灯效果)
- Android系统自带分享功能的实现(可同时分享文字和图片)
- Android中使用webSocket实现文字及单张图片发送聊天功能
- java 聊天室之 传送图片文字等功能的实现
- Android系统自带分享功能的实现(可同时分享文字和图片)
- Azure 认知服务 (5) 计算机视觉API - 使用C#代码实现读取图片中的文字(OCR)功能
- 用javascript实现的图片马赛克后显示并切换加文字功能
- 用百度AI的OCR文字识别结合PHP实现了图片的文字识别功能
- 1.3 实现图片压缩、添加文字或图片水印、指定位置裁剪等功能的ImageUtility(内有效果图) (转)
- C# 图片添加旋转字体文字,实现图片装逼功能
- Android Intent分享文件|分享图片|分享文字|功能的实现
- 朋友圈实现图片+文字转发功能(必看篇)
- 基于文字识别---实现点击图片任意位置文字即可翻译的功能
- Android编程实现支持拖动改变位置的图片中叠加文字功能示例