【问题标题】:The codes are working. What should I do to change the font color?代码正在运行。我应该怎么做才能改变字体颜色?
【发布时间】:2021-12-13 22:53:27
【问题描述】:

// 使用 System.Runtime.InteropServices 添加引用;

    private const int EM_SETCUEBANNER = 0x1501;
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
    private void Form1_Load(object sender, EventArgs e)
    {
        SendMessage(textBox_1.Handle, EM_SETCUEBANNER, 0, "long line");
        SendMessage(textBox_2.Handle, EM_SETCUEBANNER, 0, "short line");
    }

【问题讨论】:

标签: c# placeholder watermark


【解决方案1】:

如果您希望 TextBox 带有 placeholder 并且能够自定义占位符颜色 - 您不应该使用 SendMessageEM_SETCUEBANNER。他们总是使用默认的灰色来制作占位符。

要获得所需的结果,您应该创建自己的HintTextBox(或者您想命名它)。以下示例取自 @Reza Aghaei 答案 (here) 并稍作编辑。

代码:

HintTextBox.cs

using System.Drawing;
using System.Windows.Forms;

namespace MyWinFormsApp
{
    public class HintTextBox : TextBox
    {
        public HintTextBox()
        {
            Width = 150; // Some default width
            Multiline = true; // Multiline by default    
        }

        private string hint = "Input some text..."; // Default placeholder text
        private Color hintColor = Color.Red; // Default placeholder color

        // Set another Hint text through this property
        public string Hint
        {
            get => hint;
            set
            {
                hint = value;
                Invalidate();
            }
        }

        // Set placeholder color you wish through this property
        public Color HintColor
        {
            get => hintColor;
            set
            {
                hintColor = value;
                Invalidate();
            }
        }

        // Drawing placeholder on WM_PAINT message
        protected override void WndProc(ref Message message)
        {
            base.WndProc(ref message);

            if (message.Msg == 15) // WM_PAINT or 0xF
            {
                if (!Focused &&
                    string.IsNullOrEmpty(Text) &&
                    !string.IsNullOrEmpty(Hint))
                {
                    using (Graphics g = CreateGraphics())
                    {
                        TextRenderer.DrawText(g, Hint, Font,
                                              ClientRectangle, hintColor, BackColor,
                                              TextFormatFlags.Top | TextFormatFlags.Left);
                    }
                }
            }
        }
    }
}

用法示例:

private void Form1_Load(object sender, EventArgs e)
{
    // Create two HintTextBoxes with some placeholder and color
    HintTextBox hintTextBox1 = new HintTextBox();
    // Customize placeholder color through HintColor property
    hintTextBox1.HintColor = Color.Magenta;
    // Set placeholder with a text you wish
    hintTextBox1.Hint = "My awesome hint";

    // Some another HintTextBox
    HintTextBox hintTextBox2 = new HintTextBox();
    hintTextBox2.HintColor = Color.SeaGreen;
    hintTextBox2.Hint = "My another hinted TextBox...";

    // For example, I put both HintTextBoxes to FlowLayoutPanel
    FlowLayoutPanel flowLayoutPanel = new FlowLayoutPanel();
    flowLayoutPanel.Dock = DockStyle.Fill;
    flowLayoutPanel.FlowDirection = FlowDirection.TopDown;
    flowLayoutPanel.Controls.Add(hintTextBox1);
    flowLayoutPanel.Controls.Add(hintTextBox2);

    // Add FlowLayoutPanel with 2 HintTextBoxes on a Form
    this.Controls.Add(flowLayoutPanel);
}

结果:

【讨论】:

  • 很好的例子,谢谢。
  • @sebahattin küçük 如果对您有帮助,请标记或支持答案。您也可以在这里投票支持 Reza Aghaei 的回答:stackoverflow.com/questions/4902565/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
  • 2022-11-10
  • 2018-06-28
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多