0
我嘗試用水印C#設置屬性
public partial class ModernBox : Form
{
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;
static Color ColorMain = Color.FromArgb(42, 42, 44);
static Color ColorTransparent = Color.Transparent;
Panel p_bro = new Panel{Visible = false};
Panel p_auth = new Panel{Visible = false};
WaterMarkTextBox textBox1;// = new WaterMarkTextBox{Location = new Point(10,40),Visible = true,Width = 200};
WaterMarkTextBox textBox2 = new WaterMarkTextBox{
Location = new Point(10,70),Visible = true,Width = 200,WaterMark = "123"};
protected override void WndProc(ref Message message)
{
base.WndProc(ref message);
if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
message.Result = (IntPtr)HTCAPTION;
}
public ModernBox()
{
InitializeComponent();
panel1.MouseMove += (o, e) => {
if (e.Button == MouseButtons.Left) {
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
};
panel1.BackColor = ColorMain;
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Width = 600;
this.Height = 400;
p_bro.Controls.Add(Program.presentation.webBrowser1);
Program.presentation.webBrowser1.Location = new Point(10,-70);
Program.presentation.webBrowser1.Height = 560;
p_bro.Location = new Point(0,30);
//this.Focus();
this.textBox1 = new WaterMarkTextBox{Location = new Point(10,40),Visible = true,Width = 400,WaterMark = "mark",Tip = "tip"};
this.textBox1.PerformLayout();
//textBox1.WaterMark = "wm1";
//textBox1.Tip = "tip1";
this.Controls.Add(p_bro);
this.Controls.Add(p_auth);
this.Controls.Add(this.textBox1);
this.Controls.Add(textBox2);
//ModeAuth();
//ModeBro();
}
public void ModeBro()
{
p_auth.Hide();
p_bro.Show();
Program.presentation.webBrowser1.BringToFront();
p_bro.MouseHover += (o, e) => { Program.presentation.webBrowser1.Focus(); };
p_bro.AutoScroll = false;
p_bro.HorizontalScroll.Enabled = false;
p_bro.HorizontalScroll.Visible = false;
p_bro.Width = Program.presentation.webBrowser1.Width;
p_bro.Height = Program.presentation.webBrowser1.Height-50;
this.Width = p_bro.Width;//Program.presentation.webBrowser1.Width;
this.Height = p_bro.Height-5;//Program.presentation.webBrowser1.Height-20;
}
public void ModeAuth()
{
p_bro.Hide();
p_auth.Show();
this.Width = 600;
this.Height = 400;
}
private void BtnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void BtnMaximize_Click(object sender, EventArgs e)
{
if(this.WindowState != FormWindowState.Minimized)
this.WindowState = FormWindowState.Maximized;
else
this.WindowState = FormWindowState.Normal;
}
private void BtnMinimaze_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
}
public class WaterMarkTextBox : TextBox
{
ToolTip TTip = new ToolTip();
private string _WaterMark;
public string WaterMark
{
get { return _WaterMark; }
set { _WaterMark = value; }
}
private string _Tip;
public string Tip
{
get { return _Tip; }
set { _Tip = value; }
}
public WaterMarkTextBox()
{
this.ForeColor = SystemColors.GrayText;
//if(WaterMark==null) MessageBox.Show("fail");
this.Text = _WaterMark;
this.Leave += new System.EventHandler(this._Leave);
this.Enter += new System.EventHandler(this._Enter);
this.MouseHover += new EventHandler(WaterMarkTextBox_MouseHover);
}
private void _Leave(object sender, EventArgs e)
{
if (this.Text.Length == 0)
{
this.Text = _WaterMark;
this.ForeColor = SystemColors.GrayText;
}
}
private void _Enter(object sender, EventArgs e)
{
//MessageBox.Show(_WaterMark);
if (this.Text == _WaterMark)
{
this.Text = "";
this.ForeColor = SystemColors.WindowText;
}
}
private void WaterMarkTextBox_MouseHover(object sender, EventArgs e)
{
if (Tip != null)
TTip.Show(Tip, this, 0, (int)(this.Height * 1.2), 2000);
}
}
使用文本框,正如你所看到的水印不會出現,在構造WaterMark
和Tip
價值NULL
。
但它的工作原理點擊
我需要做些什麼來解決這個問題?
VS 2010
但我想用這種方式 WaterMarkTextBox BTN =新WaterMarkTextBox使用它{提示=「myTip」,位置=新的點(30,30)} 我的意思是在一條作業線上進行原生屬性位置和我的財產提示。可能嗎? 如果即使它在構造函數之後調用,我的目標是看到文本,所以如果延遲將是秒,那並不重要。但在我的情況下,我不能看到文字,直到沒有點擊WaterTextBox1然後WaterTB2 –
@NikitaShevchenko仔細閱讀原始答案,然後更新。 –