過去のブログのアーカイブ
この記事は前身のブログのアーカイブを引き継いだものです.
画像が正しく表示できないなど,コンテンツの表示に問題がある恐れがあります.テキストボックスに文字を入れてない時に背景にうっすら表示させる仕組みが実はVistaからあるのです。
その仕様の紹介とサンプルソースのご紹介
仕様
SendMessageにてハンドルとメッセージ5377を送信することでできます。
パラメータはこのような感じ
| wParam | 1の場合、フォーカスがあっても文字が入力されていなければバーナを表示する |
| lParam | バーナテキスト |
ソースコード
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
bool showFocus = false;
string bunnerText = "バーナテキスト";
SendMessage(this.Handle, 5377, new IntPtr(showFocus ? 1 : 0), bunnerText);
表示はこのような感じです。

地味な機能ですがユーザーアクティビティがアップしたりスタイルがよくなったりといいことが多いので多様するのもいいかもしれません。
使うとしたらこんな感じかな

ソースコード全文
めんどくさい人用においておきます。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinAPI.Const;
namespace WinAPI.Control
{
[ToolboxBitmap(typeof(TextBox))]
public class TextBox : System.Windows.Forms.TextBox
{
private string _cueBannerText = string.Empty;
private bool _showCueFocused;
public TextBox()
{
}
[Category("Appearance"), DefaultValue("")]
public string CueBannerText
{
get
{
return this._cueBannerText;
}
set
{
this._cueBannerText = value;
this.SetCueText(this.ShowCueFocused);
}
}
[Browsable(false)]
public new bool Multiline
{
get
{
return base.Multiline;
}
set
{
base.Multiline = false;
}
}
[DefaultValue(false), Category("Appearance")]
public bool ShowCueFocused
{
get
{
return this._showCueFocused;
}
set
{
this._showCueFocused = value;
this.SetCueText(value);
}
}
private void SetCueText(bool showFocus)
{
NativeMethod.SendMessage(base.Handle, 5377, new IntPtr(showFocus ? 1 : 0), this._cueBannerText);
}
}
public partial class NativeMethod
{
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
}
}


