📄 gemotionlist.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Windows.Forms;
namespace gowk.forms
{
/// <summary>
/// GEmotionList 的摘要说明。
/// </summary>
public class GEmotionList :System.Windows.Forms.UserControl
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private static readonly int HEIGHT=24;
private static readonly int WIDTH=24;
private static readonly int COLUMNS=15;
private static readonly int ROWS=6;
int curPage=0;
int pageCount=0;
private gowk.controls.GPanel gPanel1;
private gowk.controls.GButton gButton1;
public EmotionList innerList;
private gowk.controls.GButton btnForword;
private gowk.controls.GButton btnback;
private gowk.controls.GButton btnSelect;
private gowk.controls.GButton lbl;
string dir;
public event GEmotionListItemEventHandler ItemSelected,ItemActived;
public GEmotionList()
{
InitializeComponent();
this.Initial();
this.LoadEmotion();
this.InitialSkin();
this.UpdataLbl();
// this.IsRound=true;
this.SetStyle(ControlStyles.SupportsTransparentBackColor|ControlStyles.DoubleBuffer|ControlStyles.UserPaint|ControlStyles.AllPaintingInWmPaint,true);
// this.BackColor=Color.Transparent;
this.gPanel1.Visible=false;
gowk.common.SkinConfig.Instance.SkinChanged+=new EventHandler(Instance_SkinChanged);
}
private void InitialSkin()
{
gowk.common.SkinConfig cf=gowk.common.SkinConfig.Instance;
this.btnback.GImage.ActiveImage=cf.GetImage("/gowk_skin/images/scrollbar/scrollbar_leftarrowa");
this.btnback.GImage.PressedImage=cf.GetImage("/gowk_skin/images/scrollbar/scrollbar_leftarrowp");
this.btnback.GImage.NormalImage=cf.GetImage("/gowk_skin/images/scrollbar/scrollbar_leftarrow");
this.btnForword.GImage.ActiveImage=cf.GetImage("/gowk_skin/images/scrollbar/scrollbar_rightarrowa");
this.btnForword.GImage.PressedImage=cf.GetImage("/gowk_skin/images/scrollbar/scrollbar_rightarrowp");
this.btnForword.GImage.NormalImage=cf.GetImage("/gowk_skin/images/scrollbar/scrollbar_rightarrow");
this.btnSelect.GImage.Image=cf.GetImage("/gowk_skin/images/other/addimage");
}
private void Instance_SkinChanged(object sender, EventArgs e)
{
this.InitialSkin();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
if(this.innerList==null)return;
int focus_index=this.GetMouseOnItemIndex();
Graphics g=e.Graphics;
int start=COLUMNS*ROWS*this.curPage;
int end=Math.Min(this.innerList.Count,start+COLUMNS*ROWS);
for(int i=start;i<end;i++)
{
Rectangle r=this.GetItemRectangle(i);
if(i==focus_index)
{
Rectangle rb=r;
rb.Width-=2;
rb.Height-=2;
// rb.Inflate(-1,-1);
g.DrawRectangle(new Pen(Color.Gray),rb);
}
r.Inflate(-2,-2);
try
{
g.DrawImage(this.innerList[i].Image,r);
}
catch(System.Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message);
}
}
}
private Emotion old;
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove (e);
if(this.innerList==null)return;
//
if(this.contains(e.X,this.gPanel1.Left-WIDTH,this.gPanel1.Right+WIDTH))
{
// int x=e.X>=this.Width/2?(this.Width-this.gPanel1.Width-4):4;
System.Diagnostics.Trace.WriteLine(string.Format("width:{0}",this.Width/2));
System.Diagnostics.Trace.WriteLine(string.Format("e.x:{0}",e.X));
int x=this.gPanel1.Left<=4?this.Width-this.gPanel1.Width-4:4;
System.Diagnostics.Trace.WriteLine(x.ToString());
this.gPanel1.Location=new Point(x,4);
}
Emotion m=this.GetCurrentEmotion();
if(this.old!=m)
{
old=m;
this.gButton1.BackgroundImage=m==null?null:m.Image;
if(this.ItemActived!=null)this.ItemActived(this,new GEmotionListItemEventArgs(m));
this.Invalidate();
}
}
private Emotion GetCurrentEmotion()
{
int index=this.GetMouseOnItemIndex();
if(index!=-1 && index<this.innerList.Count)
{
return this.innerList[index];
}
return null;
}
protected override void OnClick(EventArgs e)
{
base.OnClick (e);
if(this.innerList==null)return;
Emotion m=this.GetCurrentEmotion();
if(this.ItemSelected!=null)this.ItemSelected(this,new GEmotionListItemEventArgs(m));
}
private int GetMouseOnItemIndex()
{
Rectangle rect=this.ES;
Point p=this.PointToClient(Control.MousePosition);
if(rect.Contains(p))
{
int column=(int)(p.X/WIDTH);
int row=(int)(p.Y/HEIGHT);
return column+row*COLUMNS+this.curPage*ROWS*COLUMNS;
}
return -1;
}
private Rectangle GetItemRectangle(int index)
{
int i=index%(ROWS*COLUMNS);
return new Rectangle(3+i%COLUMNS*WIDTH,3+((int)(i/COLUMNS))*HEIGHT,WIDTH,HEIGHT);
}
private bool contains(int value,int min,int max)
{
return value<max && value>min;
}
private Rectangle ES
{
get
{
return new Rectangle(0,0,COLUMNS*WIDTH,ROWS*HEIGHT);
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
base.OnPaintBackground (pevent);
Graphics g=pevent.Graphics;
Pen p=new Pen(Color.LightBlue);
for(int i=0;i<=COLUMNS;i++)
{
g.DrawLine(p,3+i*WIDTH,3+0,i*WIDTH+3,ROWS*HEIGHT+3);
}
for(int j=0;j<=ROWS;j++)
{
g.DrawLine(p,3+0,3+j*HEIGHT,COLUMNS*WIDTH+3,j*HEIGHT+3);
}
}
private int GetPage(int cnt)
{
int cip=COLUMNS*ROWS;
int p=(int)(cnt/cip);
if(cnt%cip!=0)p++;
return p;
}
private void Initial()
{
string d=System.IO.Path.Combine(Application.StartupPath,"emotion");
if(!System.IO.Directory.Exists(d))return;
this.dir=d;
int cnt=Directory.GetFiles(dir).Length;
this.pageCount=this.GetPage(cnt);
this.UpdataLbl();
}
public void Forword()
{
if(this.curPage>=this.pageCount-1)return;
this.curPage++;
int ps=this.GetPage(this.innerList.Count);
if(ps<this.pageCount)this.LoadEmotion();
this.Invalidate();
}
private void UpdataLbl()
{
this.lbl.Text=string.Format("{0}/{1}",this.curPage+1,this.pageCount);
}
public void Back()
{
if(this.curPage>0)this.curPage--;
this.Invalidate();
}
/* public int PageIndex
{
get{return this.curPage;}
set
{
System.Diagnostics.Debug.Assert(value<this.PageCount && value>=0);
if(this.curPage==value)return;
if(value
this.curPage=value;
this.LoadEmotion();
}
}*/
public int PageCount
{
get{return this.pageCount;}
}
public void LoadEmotion()
{
if(this.dir==null)return;
if(this.innerList==null)this.innerList=new EmotionList();
string[] files=Directory.GetFiles(dir);
int start=COLUMNS*ROWS*this.curPage;
for(int i=start;i<start+COLUMNS*ROWS && i<files.Length;i++)
{
string em=files[i];
if(".gif .bmp .jpg .jpeg .png .ico .psd ".IndexOf(System.IO.Path.GetExtension(em))==-1)
{
System.Diagnostics.Trace.WriteLine(em);
continue;
}
try
{
Emotion emo=new Emotion();
emo.Image=Image.FromFile(em);
emo.Path=em;
this.innerList.Add(emo);
}
catch
{
}
}
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.gPanel1 = new gowk.controls.GPanel();
this.gButton1 = new gowk.controls.GButton();
this.btnForword = new gowk.controls.GButton();
this.btnback = new gowk.controls.GButton();
this.lbl = new gowk.controls.GButton();
this.btnSelect = new gowk.controls.GButton();
this.gPanel1.SuspendLayout();
this.SuspendLayout();
//
// gPanel1
//
this.gPanel1.BackColor = System.Drawing.Color.White;
this.gPanel1.Controls.Add(this.gButton1);
this.gPanel1.GBorderStyle = gowk.controls.GBorderStyle.DashDotDot;
this.gPanel1.IsRound = true;
this.gPanel1.Location = new System.Drawing.Point(4, 4);
this.gPanel1.Name = "gPanel1";
this.gPanel1.Size = new System.Drawing.Size(72, 72);
this.gPanel1.TabIndex = 0;
//
// gButton1
//
this.gButton1.BackColor = System.Drawing.Color.Transparent;
this.gButton1.GBorderStyle = gowk.controls.GBorderStyle.Null;
this.gButton1.IsRound = true;
this.gButton1.Location = new System.Drawing.Point(26, 25);
this.gButton1.Name = "gButton1";
this.gButton1.Size = new System.Drawing.Size(24, 24);
this.gButton1.TabIndex = 0;
this.gButton1.BackgroundImageChanged += new System.EventHandler(this.gButton1_BackgroundImageChanged);
//
// btnForword
//
this.btnForword.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.btnForword.GBorderStyle = gowk.controls.GBorderStyle.Solid;
this.btnForword.IsRound = true;
this.btnForword.Location = new System.Drawing.Point(320, 160);
this.btnForword.Name = "btnForword";
this.btnForword.Size = new System.Drawing.Size(16, 16);
this.btnForword.TabIndex = 2;
this.btnForword.Click += new System.EventHandler(this.gButton2_Click);
//
// btnback
//
this.btnback.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.btnback.GBorderStyle = gowk.controls.GBorderStyle.Solid;
this.btnback.IsRound = true;
this.btnback.Location = new System.Drawing.Point(288, 161);
this.btnback.Name = "btnback";
this.btnback.Size = new System.Drawing.Size(16, 16);
this.btnback.TabIndex = 3;
this.btnback.Click += new System.EventHandler(this.gButton3_Click);
//
// lbl
//
this.lbl.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.lbl.GBorderStyle = gowk.controls.GBorderStyle.Null;
this.lbl.IsRound = false;
this.lbl.Location = new System.Drawing.Point(184, 161);
this.lbl.Name = "lbl";
this.lbl.Size = new System.Drawing.Size(80, 24);
this.lbl.TabIndex = 4;
//
// btnSelect
//
this.btnSelect.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.btnSelect.GBorderStyle = gowk.controls.GBorderStyle.Solid;
this.btnSelect.IsRound = true;
this.btnSelect.Location = new System.Drawing.Point(16, 160);
this.btnSelect.Name = "btnSelect";
this.btnSelect.Size = new System.Drawing.Size(22, 22);
this.btnSelect.TabIndex = 5;
this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
//
// GEmotionList
//
this.BackColor = System.Drawing.SystemColors.Control;
this.Controls.Add(this.btnSelect);
this.Controls.Add(this.lbl);
this.Controls.Add(this.btnback);
this.Controls.Add(this.btnForword);
this.Controls.Add(this.gPanel1);
this.Name = "GEmotionList";
this.Size = new System.Drawing.Size(368, 192);
this.gPanel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void gButton1_BackgroundImageChanged(object sender, System.EventArgs e)
{
if(this.gButton1.BackgroundImage==null)
{
this.gPanel1.Hide();
}
else
{
this.gPanel1.Show();
}
}
private void gButton3_Click(object sender, System.EventArgs e)
{
this.Back();
this.UpdataLbl();
}
private void gButton2_Click(object sender, System.EventArgs e)
{
this.Forword();
this.UpdataLbl();
}
private void btnSelect_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.OpenFileDialog of=new OpenFileDialog();
of.Filter=@"*.gif/*.bmp/*.jpg/*.png|*.gif;*.bmp;*.jpg;*.png|所有文件|*.*";
if(of.ShowDialog()==DialogResult.OK)
{
string dst=Path.Combine(Application.StartupPath,"emotion\\"+Path.GetFileName(of.FileName));
File.Copy(of.FileName,dst,true);
Emotion m=new Emotion();
m.Image=Image.FromFile(of.FileName);
m.Path=of.FileName;
this.innerList.Add(m);
if(this.ItemSelected!=null)this.ItemSelected(this,new GEmotionListItemEventArgs(m));
}
of.Dispose();
}
}
public class Emotion
{
public string Text;
public object Tag;
// public string XX;
public Image Image;
public string Path;
}
public class EmotionList:System.Collections.ArrayList
{
public new Emotion this[int i]
{
get{return (Emotion)base[i];}
set{base[i]=value;}
}
}
public class GEmotionListItemEventArgs:System.EventArgs
{
public Emotion Emotion;
public GEmotionListItemEventArgs(Emotion e)
{
this.Emotion=e;
}
}
public delegate void GEmotionListItemEventHandler(object sender,GEmotionListItemEventArgs e);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -