[C#] - 用户自定义控件(含源代码)-透明文本框=(2)

2018-12-05 20:57

SizeF sf_font = g.MeasureString(\, this.Font); g.Dispose();

return (int)sf_font.Height; }

private void GetBitmaps() {

if (myBitmap == null

|| myAlphaBitmap == null || myBitmap.Width != Width || myBitmap.Height != Height || myAlphaBitmap.Width != Width || myAlphaBitmap.Height != Height) {

myBitmap = null;

myAlphaBitmap = null; }

if (myBitmap == null) {

myBitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);//(Width,Height); myUpToDate = false; }

if (!myUpToDate) {

//Capture the TextBox control window

this.SetStyle(ControlStyles.UserPaint, false);

win32.CaptureWindow(this, ref myBitmap);

this.SetStyle(ControlStyles.UserPaint, true);

this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.BackColor = Color.FromArgb(myBackAlpha, myBackColor);

} //--

Rectangle r2 = new Rectangle(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height); ImageAttributes tempImageAttr = new ImageAttributes();

//Found the color map code in the MS Help

ColorMap[] tempColorMap = new ColorMap[1]; tempColorMap[0] = new ColorMap();

tempColorMap[0].OldColor = Color.FromArgb(255, myBackColor);

tempColorMap[0].NewColor = Color.FromArgb(myBackAlpha, myBackColor);

tempImageAttr.SetRemapTable(tempColorMap);

if (myAlphaBitmap != null) myAlphaBitmap.Dispose();

myAlphaBitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);//(Width,Height);

Graphics tempGraphics1 = Graphics.FromImage(myAlphaBitmap);

tempGraphics1.DrawImage(myBitmap, r2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, GraphicsUnit.Pixel, tempImageAttr);

tempGraphics1.Dispose();

//----

if (this.Focused && (this.SelectionLength == 0)) {

Graphics tempGraphics2 = Graphics.FromImage(myAlphaBitmap); if (myCaretState) {

//Draw the caret

Point caret = this.findCaret();

Pen p = new Pen(this.ForeColor, 3);

tempGraphics2.DrawLine(p, caret.X, caret.Y + 0, caret.X, caret.Y + myFontHeight); tempGraphics2.Dispose(); }

}

}

private Point findCaret() {

/* Find the caret translated from code at

* http://www.vb-helper.com/howto_track_textbox_caret.html * * and *

* http://www.microbion.co.uk/developers/csharp/textpos2.htm *

* Changed to EM_POSFROMCHAR *

* This code still needs to be cleaned up and debugged * */

Point pointCaret = new Point(0); int i_char_loc = this.SelectionStart;

IntPtr pi_char_loc = new IntPtr(i_char_loc);

int i_point = win32.SendMessage(this.Handle, win32.EM_POSFROMCHAR, pi_char_loc, IntPtr.Zero); pointCaret = new Point(i_point);

if (i_char_loc == 0) {

pointCaret = new Point(0); }

else if (i_char_loc >= this.Text.Length) {

pi_char_loc = new IntPtr(i_char_loc - 1);

i_point = win32.SendMessage(this.Handle, win32.EM_POSFROMCHAR, pi_char_loc, IntPtr.Zero); pointCaret = new Point(i_point);

Graphics g = this.CreateGraphics();

String t1 = this.Text.Substring(this.Text.Length - 1, 1) + \; SizeF sizet1 = g.MeasureString(t1, this.Font); SizeF sizex = g.MeasureString(\, this.Font); g.Dispose();

int xoffset = (int)(sizet1.Width - sizex.Width); pointCaret.X = pointCaret.X + xoffset;

if (i_char_loc == this.Text.Length)

{

String slast = this.Text.Substring(Text.Length - 1, 1); if (slast == \) {

pointCaret.X = 1;

pointCaret.Y = pointCaret.Y + myFontHeight; } }

}

return pointCaret; }

private void myTimer1_Tick(object sender, EventArgs e) {

//Timer used to turn caret on and off for focused control

myCaretState = !myCaretState; myCaretUpToDate = false; this.Invalidate(); }

private class uPictureBox : PictureBox {

public uPictureBox() {

this.SetStyle(ControlStyles.Selectable, false); this.SetStyle(ControlStyles.UserPaint, true);

this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true);

this.Cursor = null; this.Enabled = true;

this.SizeMode = PictureBoxSizeMode.Normal;

}

//uPictureBox

protected override void WndProc(ref Message m) {

if (m.Msg == win32.WM_LBUTTONDOWN || m.Msg == win32.WM_RBUTTONDOWN || m.Msg == win32.WM_LBUTTONDBLCLK || m.Msg == win32.WM_MOUSELEAVE || m.Msg == win32.WM_MOUSEMOVE) {

//Send the above messages back to the parent control

win32.PostMessage(this.Parent.Handle, (uint)m.Msg, m.WParam, m.LParam); }

else if (m.Msg == win32.WM_LBUTTONUP) {

//?? for selects and such this.Parent.Invalidate(); }

base.WndProc(ref m); }

} // End uPictureBox Class

#endregion // end private functions and classes

#region Component Designer generated code ///

/// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///

private void InitializeComponent() {

components = new System.ComponentModel.Container(); }

#endregion

#region New Public Properties

[

Category(\),

Description(\), Browsable(true),

DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)

]

public int BackAlpha {

get { return myBackAlpha; } set {

int v = value; if (v > 255) v = 255;

myBackAlpha = v; myUpToDate = false; Invalidate(); } }

#endregion

} // End AlphaTextBox Class

} // End namespace ZBobb //----

个性签名:做要做好,做到不三不四不如不做。


[C#] - 用户自定义控件(含源代码)-透明文本框=(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:英语视听说2第10单元答案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: