C# Winfrom 自定義控制元件——帶圖片的TextBox
阿新 • • 發佈:2019-08-04
效果:
描述:
本來是想用GDI在左邊畫圖片上去的,文字是居中對齊,如果文字是左對齊,文字會把圖片遮住控制元件長這樣: 但這樣做,輸入框在獲取焦點時候,會把圖片擋住就像這樣: 輸入完成之後圖片就會顯示完整。所以我又採用了PictureBox+TextBox組合的方式來完成這種效果。完成這種效果需要完成以下步驟: 0.分別設定PictureBox和TextBox的控制元件的Anchor和Dock屬性,這樣在窗體上拉動控制元件大小的時候,圖片框和文字輸入框能一起變大; 1.調整TextBox的border為none; 2.調整PictureBox的backColor為White; 3.調整作為控制元件載體的UserControl的BackColor為White; 4.設定作為控制元件載體的UserControl的BorderStyle為FixedSingle;因為這裡需要重繪UserContorl的邊框就需要這樣的設定,我這裡重繪成了DarkRed顏色。提醒:
程式碼:
public partial class MyPicturerTextBox : UserControl { public MyPicturerTextBox() { InitializeComponent(); this.BorderStyle = BorderStyle.FixedSingle; } private Image userImg; [Description("文字框裡的圖片")] public Image UserImg { get { return userImg; } set { if (value != null) { this.Img.Image = value; } userImg = value; } } private string txt; [Description("輸入的文字")] public string Txt { get { return txt; } set { if (!string.IsNullOrEmpty(value)) { this.textBox.Text = value; } txt = value; } } [Description("TextChanged事件")] public event EventHandler MyTextChanged; private void textBox_TextChanged(object sender, EventArgs e) { MyTextChanged?.Invoke(sender, e); } /// <summary> /// 獲得當前程序,以便重繪控制元件 /// </summary> /// <param name="hWnd"></param> /// <returns></returns> [System.Runtime.InteropServices.DllImport("user32.dll")] static extern IntPtr GetWindowDC(IntPtr hWnd); [System.Runtime.InteropServices.DllImport("user32.dll")] static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == 0xf || m.Msg == 0x133) { //攔截系統訊息,獲得當前控制元件程序以便重繪。 //一些控制元件(如TextBox、Button等)是由系統程序繪製,過載OnPaint方法將不起作用. //所有這裡並沒有使用過載OnPaint方法繪製TextBox邊框。 // //MSDN:重寫 OnPaint 將禁止修改所有控制元件的外觀。 //那些由 Windows 完成其所有繪圖的控制元件(例如 Textbox)從不呼叫它們的 OnPaint 方法, //因此將永遠不會使用自定義程式碼。請參見您要修改的特定控制元件的文件, //檢視 OnPaint 方法是否可用。如果某個控制元件未將 OnPaint 作為成員方法列出, //則您無法通過重寫此方法改變其外觀。 // //MSDN:要了解可用的 Message.Msg、Message.LParam 和 Message.WParam 值, //請參考位於 MSDN Library 中的 Platform SDK 文件參考。可在 Platform SDK(“Core SDK”一節) //下載中包含的 windows.h 標頭檔案中找到實際常數值,該檔案也可在 MSDN 上找到。 IntPtr hDC = GetWindowDC(m.HWnd); if (hDC.ToInt32() == 0) { return; } //只有在邊框樣式為FixedSingle時自定義邊框樣式才有效 if (this.BorderStyle == BorderStyle.FixedSingle) { //邊框Width為1個畫素 System.Drawing.Pen pen = new Pen(Brushes.DarkRed, 1); //繪製邊框 System.Drawing.Graphics g = Graphics.FromHdc(hDC); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1); pen.Dispose(); } //返回結果 m.Result = IntPtr.Zero; //釋放 ReleaseDC(m.HWnd, hDC); } } protected override void OnResize(EventArgs e) { base.OnResize(e); this.Refresh(); } }