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个像素
}
}
}
增加鼠标点击图片操作
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));
}
}
转载请注明出处:http://blog.csdn.net/shineych/article/details/8099707