DataGridView的带图片滚动浏览
给一个司机班做的班次浏览
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using Ini;
using System.Resources;
using System.Threading;
namespace cshow
{
public partial class Form1 : Form
{
private Point mouseOffset; //记录鼠标指针的坐标
private bool isMouseDown = false; //记录鼠标按键是否按下
ArrayList obj;
int screenrows, picscal;
static string runPath=@AppDomain.CurrentDomain.BaseDirectory;
System.Resources.ResourceManager rm = null;
public Form1()
{
InitializeComponent();
rm = new System.Resources.ResourceManager("cshow.Form1", System.Reflection.Assembly.GetExecutingAssembly());
}
private void Form1_Load(object sender, EventArgs e)
{
/**//* DBAccess.DBAccess dba = new DBAccess.DBAccess();
OleDbDataReader reader2 = dba.GetReader("select workdate as 日期,isout as 接送,manname as 客人姓名,mannum as 人数,code as 航班,worktime as 时间,outtime as 出发时间,tozone as 到着地,dyer as 对应者,drivers as 司机,automotive as 车辆,other as 其他 from today where workdate=#" + System.DateTime.Now.ToString("yyyy-MM-dd") + "# order by id");
if (!reader2.HasRows)
{
dba.CloseDB();
return;
}
dba.CloseDB();
*/
System.Windows.Forms.Screen screen=Screen.PrimaryScreen;
System.Drawing.Rectangle rct=screen.Bounds;
this.Height=rct.Height;//高度
this.Width=rct.Width;//宽度
IniFile ini = new IniFile(runPath + "config.ini");
obj = new ArrayList();
dg1.Width = this.Width;
dg1.Height = this.Height;
btnPanel.Left = this.Width - btnPanel.Width - 10;
if (ini.IniReadValue("other", "twoScreen") == "1")
{
this.Left = rct.Width + 1;
}
screenrows = Int16.Parse(ini.IniReadValue("other", "screenrows"));
picscal = Int16.Parse(ini.IniReadValue("pic", "scal"));
todayDate.Text = rm.GetString("todayTitle") + " " + System.DateTime.Now.ToString("d");
show(SetObj());
int x=Int32.Parse(ini.IniReadValue("time","scrolltime"));
int y = Int32.Parse(ini.IniReadValue("time", "AtoBformtime"));
if (x > 0)
{
timerScroll1.Interval = x * 60000;
timerScroll1.Enabled = true;
}
if (y > 0)
{
timerA.Interval = y * 60000;
timerA.Enabled = true;
}
}
private ArrayList SetObj()
{
int i;
obj.Clear();
ArrayList[] al=new ArrayList[50];
for(i=0; i < 20; i++)
{
al[i] = new ArrayList();
}
DBAccess.DBAccess dba = new DBAccess.DBAccess();
OleDbDataReader reader = dba.GetReader("select workdate,isout,manname,mannum,code,worktime,outtime,tozone,dyer,drivers,automotive,other from today where workdate=#"+System.DateTime.Now.ToString("yyyy-MM-dd")+"# order by id");
i = 0;
while (reader.Read())
{
al[i].Add(reader[0].ToString().Trim());
al[i].Add(reader[1].ToString().Trim());
al[i].Add(reader[2].ToString().Trim());
al[i].Add(reader[3].ToString().Trim());
al[i].Add(reader[4].ToString().Trim());
al[i].Add(reader[5].ToString().Trim());
al[i].Add(reader[6].ToString().Trim());
al[i].Add(reader[7].ToString().Trim());
al[i].Add(reader[8].ToString().Trim());
al[i].Add(GetPic(@reader[9].ToString().Trim(), 0));
al[i].Add(GetPic(@reader[10].ToString().Trim(), 1));
al[i].Add(reader[11].ToString().Trim());
al[i].Add(reader[9].ToString().Trim());
al[i].Add(reader[10].ToString().Trim());
obj.Add(al[i]);
i++;
}
for (int j = obj.Count; j < screenrows; j++)
{
al[j].Add(DBNull.Value);
al[j].Add(DBNull.Value);
al[j].Add(DBNull.Value);
al[j].Add(DBNull.Value);
al[j].Add(DBNull.Value);
al[j].Add(DBNull.Value);
al[j].Add(DBNull.Value);
al[j].Add(DBNull.Value);
al[j].Add(DBNull.Value);
al[j].Add(new Bitmap(1,1));
al[j].Add(new Bitmap(1,1));
al[j].Add(DBNull.Value);
al[j].Add(DBNull.Value);
al[j].Add(DBNull.Value);
obj.Add(al[j]);
}
dba.CloseDB();
return obj;
}
private void show(ArrayList obj)
{
int i = 0;
string sjname = "", qcname = "";
//string picpath = "Image\";
//string sjimage = "", qcimage = "";
Bitmap sjimage = null;
Bitmap qcimage = null;
DataTable firstdt = new DataTable("first");
firstdt.Columns.Add(new DataColumn("1", typeof(object)));
firstdt.Columns.Add(new DataColumn("2", typeof(object)));
firstdt.Columns.Add(new DataColumn("3", typeof(object)));
firstdt.Columns.Add(new DataColumn("4", typeof(object)));
DataTable dt = new DataTable("today");
dt.Columns.Add(new DataColumn(rm.GetString("workdateStr"), typeof(DateTime)));
dt.Columns.Add(new DataColumn(rm.GetString("isOutStr"), typeof(string)));
dt.Columns.Add(new DataColumn(rm.GetString("mannameStr"), typeof(string)));
dt.Columns.Add(new DataColumn(rm.GetString("mannumStr"), typeof(string)));
dt.Columns.Add(new DataColumn(rm.GetString("codeStr"), typeof(string)));
dt.Columns.Add(new DataColumn(rm.GetString("worktimeStr"), typeof(DateTime)));
dt.Columns.Add(new DataColumn(rm.GetString("outtimeStr"), typeof(DateTime)));
dt.Columns.Add(new DataColumn(rm.GetString("tozoneStr"), typeof(string)));
dt.Columns.Add(new DataColumn(rm.GetString("dyerStr"), typeof(string)));
dt.Columns.Add(new DataColumn(rm.GetString("driversStr"), typeof(Bitmap)));
dt.Columns.Add(new DataColumn(rm.GetString("autoStr"), typeof(Bitmap)));
dt.Columns.Add(new DataColumn(rm.GetString("otherStr"), typeof(string)));
while (i<=obj.Count-1)
{
ArrayList reader = (ArrayList)obj[i];
DataRow dr;
dr = dt.NewRow();
if (i == 0)
{
sjname = @reader[12].ToString().Trim();
qcname = @reader[13].ToString().Trim();
//sjimage = picpath + "man\" + sjname + ".jpg";
//qcimage = picpath + "auto\" + qcname + ".jpg";
sjimage = (Bitmap)reader[9];
qcimage = (Bitmap)reader[10];
DataRow firstdr;
firstdr = firstdt.NewRow();
firstdr[0] = rm.GetString("workdateStr");
firstdr[1] = reader[0] == DBNull.Value ? "" : DateTime.Parse(reader[0].ToString().Trim()).ToString("MM月dd日");
firstdr[2] = rm.GetString("outtimeStr");
firstdr[3] = reader[6] == DBNull.Value ? "" : DateTime.Parse(reader[6].ToString().Trim()).ToString("hh点mm分");
firstdt.Rows.Add(firstdr);
//firstdr[7] = new Bitmap(sjimage, true);
//firstdr[8] = new Bitmap(qcimage, true);
firstdr = firstdt.NewRow();
firstdr[0] = rm.GetString("mannameStr");
firstdr[1] = reader[2].ToString().Trim();
firstdr[2] = rm.GetString("mannumStr");
firstdr[3] = reader[3].ToString().Trim();
firstdt.Rows.Add(firstdr);
firstdr = firstdt.NewRow();
firstdr[0] = rm.GetString("codeStr");
firstdr[1] = reader[4].ToString().Trim();
firstdr[2] = rm.GetString("worktimeStr");
firstdr[3] = reader[5] == DBNull.Value ? "" : DateTime.Parse(reader[5].ToString().Trim()).ToString("hh点mm分");
firstdt.Rows.Add(firstdr);
firstdr = firstdt.NewRow();
firstdr[0] = rm.GetString("isOutStr");
firstdr[1] = reader[1].ToString().Trim();
firstdr[2] = rm.GetString("dyerStr");
firstdr[3] = reader[8].ToString().Trim();
firstdt.Rows.Add(firstdr);
firstdr = firstdt.NewRow();
firstdr[0] = rm.GetString("tozoneStr");
firstdr[1] = reader[7].ToString().Trim();
firstdr[2] = rm.GetString("otherStr");
firstdr[3] = reader[11].ToString().Trim();
firstdt.Rows.Add(firstdr);
i++;
continue;
}
dr[0] = reader[0];
dr[1] = reader[1].ToString().Trim();
dr[2] = reader[2].ToString().Trim();
dr[3] = reader[3].ToString().Trim();
dr[4] = reader[4].ToString().Trim();
dr[5] = reader[5];
dr[6] = reader[6];
dr[7] = reader[7].ToString().Trim();
dr[8] = reader[8].ToString().Trim();
dr[9] = GetSmallPic((Bitmap)reader[9]);
dr[10] = GetSmallPic((Bitmap)reader[10]);
dr[11] = reader[11].ToString().Trim();
dt.Rows.Add(dr);
i++;
}
this.firstdg.DataSource = firstdt.DefaultView;
picsj.Image = (Image)sjimage;
picauto.Image = (Image)qcimage;
// if(sjname.Trim()!="")
//LoadToPic(picsj, sjimage);
// if(qcname.Trim()!="")
//LoadToPic(picauto, qcimage);
gg1.Text = rm.GetString("driversStr") + ":" + sjname;
gg2.Text = rm.GetString("autoStr") + ":" + qcname;
this.dg1.DataSource = dt.DefaultView;
}
private void LoadToPic(PictureBox pic1,Bitmap bmp)
{
//新建第二个bitmap类型的bmp2变量,我这里是根据我的程序需要设置的。
Bitmap bmp2 = new Bitmap(bmp.Width,bmp.Height);
//将第一个bmp拷贝到bmp2中
Graphics draw = Graphics.FromImage(bmp2);
draw.DrawImage(bmp, 0, 0);
pic1.Image = (Image)bmp2;//读取bmp2到picturebox
//string FILE =fn;
draw.Dispose();
//bmp1.Dispose();//释放bmp文件资源
}
private Bitmap GetSmallPic(Bitmap bmp)
{
int iScale = 0;
Bitmap tmp = null;
if (bmp!=null)
{
//System.Drawing.Image image = System.Drawing.Image.FromHbitmap(bmp.GetHbitmap());
int iScaleWidth = bmp.Width / 80;
int iScaleHeight = bmp.Height / 78;
if (iScaleHeight <= iScaleWidth)
iScale = iScaleWidth;
else
iScale = iScaleHeight;
if (iScaleHeight <= 0 || iScaleWidth <= 0)
iScale = 1;
//取得图片大小
//System.Drawing.Size size = new Size(image.Width / iScale, image.Height / iScale);
System.Drawing.Size size = new Size(bmp.Width / iScale, bmp.Height / iScale);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.Blue);
//在指定位置画图
g.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.GraphicsUnit.Pixel);
tmp = (Bitmap)bitmap;
}
else
{
tmp = new Bitmap(1,1);
}
return tmp;
}
private Bitmap GetPic(string piccode, int f)
{
Bitmap tmp = null;
if (piccode.Trim() != "")
{
try
{
string subpath = f == 0 ? "man\" : "auto\";
string originalFilename = runPath + "Image\" + subpath + piccode + ".jpg";
//生成的高质量图片名称
//string strGoodFile = @AppDomain.CurrentDomain.BaseDirectory + "SmallImage\" + subpath + "small-" + piccode + ".jpg";
//缩小的倍数
/**//*if (piccode.Trim() == "")
{
originalFilename = @AppDomain.CurrentDomain.BaseDirectory+"Image\NULL.jpg";
}*/
//从文件取得图片对象
System.Drawing.Image image = System.Drawing.Image.FromFile(originalFilename);
//取得图片大小
System.Drawing.Size size = new Size(image.Width, image.Height);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.Blue);
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.GraphicsUnit.Pixel);
tmp = (Bitmap)bitmap;
}
catch
{
return new Bitmap(1,1);
}
}
else
{
tmp = new Bitmap(1, 1);
}
return tmp;
}
private void CreatSmallPic(string piccode,int f)
{
string subpath = f == 0 ? "man\" : "auto\";
string originalFilename = @AppDomain.CurrentDomain.BaseDirectory + "Image\" + subpath + piccode + ".jpg";
//生成的高质量图片名称
string strGoodFile = @AppDomain.CurrentDomain.BaseDirectory + "SmallImage\" + subpath + "small-" + piccode + ".jpg";
//缩小的倍数
int iScale = 5;
//从文件取得图片对象
System.Drawing.Image image = System.Drawing.Image.FromFile(originalFilename);
//取得图片大小
System.Drawing.Size size = new Size(image.Width / iScale, image.Height / iScale);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.Blue);
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.GraphicsUnit.Pixel);
//保存高清晰度的缩略图
bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg);
//取得原图像的普通缩略图
/**//*System.Drawing.Image img = image.GetThumbnailImage(image.Width / iScale, image.Height / iScale, null, IntPtr.Zero);
//保存普通缩略图
img.Save(strBadFile, System.Drawing.Imaging.ImageFormat.Jpeg);*/
g.Dispose();
}
private void dg1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
SetGrid();
if (e.ColumnIndex == 0)
e.CellStyle.Format = "MM-dd";
if (e.ColumnIndex == 5 || e.ColumnIndex == 6)
e.CellStyle.Format = "t";
e.CellStyle.SelectionBackColor = e.CellStyle.BackColor;
e.CellStyle.SelectionForeColor = e.CellStyle.ForeColor;
e.CellStyle.Font = new Font("宋体", 14, FontStyle.Bold, GraphicsUnit.Pixel);
e.CellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
}
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
private void scrollShow()
{
int i;
object tmp;
object ot=obj[0];
if ((((ArrayList)obj[0])[0] == DBNull.Value && ((ArrayList)obj[0])[0] == DBNull.Value) || (((ArrayList)obj[1])[0] == DBNull.Value && ((ArrayList)obj[1])[0] == DBNull.Value))
{
return;
}
obj.RemoveAt(0);
for (i = 1; i < obj.Count; i++)
{
tmp=obj[i];
if (((ArrayList)tmp)[0] == DBNull.Value && ((ArrayList)tmp)[1] == DBNull.Value)
{
break;
}
}
obj.Insert(i,ot);
show(obj);
}
private void firstdg_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
e.CellStyle.Font = new Font("宋体", 20, FontStyle.Bold, GraphicsUnit.Pixel);
if (e.ColumnIndex%2 == 0)
{
e.CellStyle.BackColor = Color.LightBlue;
e.CellStyle.ForeColor = Color.Black;
e.CellStyle.Font = new Font("隶书", 24, FontStyle.Bold, GraphicsUnit.Pixel);
}
e.CellStyle.SelectionBackColor = e.CellStyle.BackColor;
e.CellStyle.SelectionForeColor = e.CellStyle.ForeColor;
e.CellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
}
private void btnClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Close();
}
private void btnScroll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
scrollShow();
}
private void btnChange_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
changeForm();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//Application.Exit();
}
private void timerA_Tick(object sender, EventArgs e)
{
changeForm();
}
private void timerScroll_Tick(object sender, EventArgs e)
{
scrollShow();
}
private void changeForm()
{
try
{
DBAccess.DBAccess dba = new DBAccess.DBAccess();
OleDbDataReader reader = dba.GetReader("select workdate as 日期,isout as 接送,manname as 客人姓名,mannum as 人数,code as 航班,worktime as 时间,outtime as 出发时间,tozone as 到着地,dyer as 对应者,drivers as 司机,automotive as 车辆,other as 其他 from today where workdate=#" + System.DateTime.Now.Date.AddDays(1).ToString("yyyy-MM-dd") + "# order by id");
bool haveRows = reader.HasRows;
dba.CloseDB();
if (haveRows)
{
timerScroll1.Enabled = false;
timerA.Enabled = false;
Form2 f2 = new Form2();
f2.frmmain = this;
f2.Show();
}
}
catch(Exception ec)
{ MessageBox.Show(ec.Message); }
}
private void SetGrid()
{
for (int i = 1; i < dg1.ColumnCount; i++)
{
dg1.Columns[i].ReadOnly = true;
dg1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; //不自动排序
//dg1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; //列标题字体居中
}
dg1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dg1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
for (int i = 0; i < dg1.Rows.Count; i++)
dg1.Rows[i].Height = 78;
dg1.Columns[0].Width=64;
dg1.Columns[1].Width = 64;
dg1.Columns[2].Width = 164;
dg1.Columns[3].Width = 64;
dg1.Columns[4].Width = 64;
dg1.Columns[5].Width = 100;
dg1.Columns[6].Width = 100;
dg1.Columns[7].Width = 80;
dg1.Columns[8].Width = 80;
dg1.Columns[9].Width = 80;
dg1.Columns[10].Width = 80;
dg1.Columns[11].Width = 82;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight -
SystemInformation.FrameBorderSize.Height;
mouseOffset = new Point(xOffset, yOffset);
isMouseDown = true;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
Location = mousePos;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
// 修改鼠标状态isMouseDown的值
// 确保只有鼠标左键按下并移动时,才移动窗体
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}
}
}
本文原创,由csdn搬家过来