public class MyText : TextBox
{
public const int IME_CMODE_FULLSHAPE = 0x8;
public const int IME_CHOTKEY_SHAPE_TOGGLE = 0x11;
private Image m_img = null;
public Image Img
{
get { return m_img; }
set { m_img = value; }
}
bool isfrom = false;
int height = 0;
bool hot = false;
public PictureBox border = null;
/// <summary>
/// 返回底图
/// </summary>
public Image GetImg
{
get
{
return m_img;
}
}
/// <summary>
/// 文本框控件
/// </summary>
/// <param name="parent">父控件</param>
/// <param name="rect">范围</param>
/// <param name="img">底图</param>
/// <param name="istop">是否顶部</param>
/// <param name="tab">是否回车键切换tab位置</param>
public MyText(System.Windows.Forms.Control parent, Rectangle rect, Image img = null, bool istop = true, bool tab = true)
{
Font = new System.Drawing.Font(parent.Font.Name, 12);
this.BackColor = Color.White;
Location = new Point(rect.X, rect.Y);
Size = new Size(rect.Width, rect.Height - 10);
BorderStyle = BorderStyle.None;
isfrom = istop;
m_img = img;
border = new PictureBox();
border.BackgroundImage = Tool.EditImg[0];
border.Location = new Point(rect.X - 3, rect.Y - 4);
height = rect.Size.Height;
border.Size = new Size(rect.Width + 6, height);
border.Paint += OnDraw;
parent.Controls.Add(border);
border.BringToFront();
if (tab)
{
KeyPress += (object s, KeyPressEventArgs ev) =>
{
if (ev.KeyChar == '\r')
{
System.Windows.Forms.SendKeys.Send("{Tab}");
}
};
}
GotFocus += (object s, EventArgs ev) =>
{
IntPtr HIme = Tool.ImmGetContext(this.Handle);
if (Tool.ImmGetOpenStatus(HIme)) //如果输入法处于打开状态
{
int iMode = 0;
int iSentence = 0;
bool bSuccess = Tool.ImmGetConversionStatus(HIme, ref iMode, ref iSentence); //检索输入法信息
if (bSuccess)
{
if ((iMode & IME_CMODE_FULLSHAPE) > 0) //如果是全角
Tool.ImmSimulateHotKey(this.Handle, IME_CHOTKEY_SHAPE_TOGGLE); //转换成半角
}
}
border.BackgroundImage = Tool.EditImg[2];
};
LostFocus += (object s, EventArgs ev) =>
{
border.BackgroundImage = Tool.EditImg[0];
};
MouseEnter += (object s, EventArgs ev) =>
{
if(!this.Focused)
border.BackgroundImage = Tool.EditImg[1];
};
MouseLeave += (object s, EventArgs ev) =>
{
if (!this.Focused)
border.BackgroundImage = Tool.EditImg[0];
};
}
void OnDraw(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (m_img != null)
{
if (isfrom)
g.DrawImage(m_img, new Rectangle(0, 0, Size.Width + 6, height), new Rectangle(Location.X, Location.Y, Size.Width + 6, height), GraphicsUnit.Pixel);
else
g.DrawImage(m_img, new Rectangle(0, 0, Size.Width + 6, height), new Rectangle(Location.X + Parent.Location.X, Location.Y + Parent.Location.Y, Size.Width + 6, height), GraphicsUnit.Pixel);
}
else
{
SolidBrush b = new SolidBrush(BackColor);
g.FillRectangle(b, 0, 0, Size.Width + 6, height);
b.Dispose();
}
if (border.BackgroundImage != null)
Tool.DrawImage(g, border.BackgroundImage, new Rectangle(0, 0, Size.Width + 6, height), new Point(0, 0));
}
}
Skin TextBox
最新推荐文章于 2022-05-17 01:00:52 发布