重複の可能性:
WinForms の透かし TextBox
現在、C# アプリケーションの設定ダイアログをプログラミングしています。入力フィールドは次のようになります。
→
それを実現するための最良の方法は何ですか?背景画像を作成することを考えましたが、それを行うためのより良い方法(動的なもの)があるかどうかを知りたいです...
重複の可能性:
WinForms の透かし TextBox
現在、C# アプリケーションの設定ダイアログをプログラミングしています。入力フィールドは次のようになります。
→
それを実現するための最良の方法は何ですか?背景画像を作成することを考えましたが、それを行うためのより良い方法(動的なもの)があるかどうかを知りたいです...
パネルはホワイトセットをご使用くださいBackColor。
パネル コントロールで、左側に を に設定して挿入TextBoxしBorderStyleますNone。
パネル コントロールの右側に、LabelBackColorを「Firstname」にTransparent設定して を挿入します。Text

新しいテキストボックスクラスを作成するだけです
 public class MyTextBox : TextBox
    {
        public MyTextBox()
        {
            SetStyle(ControlStyles.UserPaint, true);
        }
     protected override void OnTextChanged(EventArgs e)
    {
        base.OnTextChanged(e);
        this.Invalidate();
    }
        protected override void OnPaint(PaintEventArgs e)
        {
 e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), new System.Drawing.RectangleF(0, 0, this.Width , this.Height ), System.Drawing.StringFormat.GenericDefault);
            e.Graphics.DrawString("Lloyd", this.Font, new SolidBrush(Color.Red), new System.Drawing.RectangleF(0, 0, 100, 100), System.Drawing.StringFormat.GenericTypographic);
            base.OnPaint(e);
        }
    }
Draw String パラメータに適切な変更を加えます
3 つのコントロールの構成を作成し、別の に配置しUserControlます。3 つのコントロールは次のとおりです。
Hassan からレシピを取得し、それを に付けて、UserControl1 つの新しいコントロールとして使用します。
私はこれがあなたが必要とするほとんどのことをすると思います:
public class MyTextBox : TextBox
{
    public const int WM_PAINT = 0x000F;
    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_PAINT:
                Invalidate();
                base.WndProc(ref m);
                if (!ContainsFocus && string.IsNullOrEmpty(Text))
                {
                    Graphics gr = CreateGraphics();
                    StringFormat format = new StringFormat();
                    format.Alignment = StringAlignment.Far;
                    gr.DrawString("Enter your name", Font, new SolidBrush(Color.FromArgb(70, ForeColor)), ClientRectangle, format);
                }
                break;
            default:
                base.WndProc(ref m);
                break;
        }
    }
}
キャレットの位置が間違って計算されるため、TextBox で OnPaint をオーバーライドすることは通常はお勧めできません。
ラベルは、TextBox が空でフォーカスがない場合にのみ表示されることに注意してください。しかし、これはそのような入力ボックスのほとんどがどのように動作するかです。
キューを常に表示する必要がある場合は、ラベルとして追加できます。
public class MyTextBox : TextBox
{
    private Label cueLabel;
    public TextBoxWithLabel()
    {
        SuspendLayout();
        cueLabel = new Label();
        cueLabel.Anchor = AnchorStyles.Top | AnchorStyles.Right;
        cueLabel.AutoSize = true;
        cueLabel.Text = "Enter your name";
        Controls.Add(cueLabel);
        cueLabel.Location = new Point(Width - cueLabel.Width, 0);
        ResumeLayout(false);
        PerformLayout();
    }
}