MessageBox.Show
C#を使用して、によって表示されるダイアログでテキストを太字で表示するにはどうすればよいですか?
4 に答える
メッセージボックスは、他のものと同じように混乱する可能性のある通常のウィンドウである可能性があります。ただし、そのためのコードは少しザラザラしています。プロジェクトに新しいクラスを追加し、次のコードを貼り付けます。
using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
class BoldMessageBox : IDisposable {
private int mTries = 0;
private Form mOwner;
private Font mFont;
public BoldMessageBox(Form owner) {
mOwner = owner;
owner.BeginInvoke(new MethodInvoker(findDialog));
}
private void findDialog() {
// Enumerate windows to find the message box
if (mTries < 0) return;
EnumThreadWndProc callback = new EnumThreadWndProc(checkWindow);
if (EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero)) {
if (++mTries < 10) mOwner.BeginInvoke(new MethodInvoker(findDialog));
}
}
private bool checkWindow(IntPtr hWnd, IntPtr lp) {
// Checks if <hWnd> is a dialog
StringBuilder sb = new StringBuilder(260);
GetClassName(hWnd, sb, sb.Capacity);
if (sb.ToString() != "#32770") return true;
// Got it, get the STATIC control that displays the text
IntPtr hText = GetDlgItem(hWnd, 0xffff);
if (hText != IntPtr.Zero) {
// Get the current font
IntPtr hFont = SendMessage(hText, WM_GETFONT, IntPtr.Zero, IntPtr.Zero);
Font font = Font.FromHfont(hFont);
// And make it bold (note the size change to keep enough space!!)
mFont = new Font(font.FontFamily, font.SizeInPoints - 1f, FontStyle.Bold);
SendMessage(hText, WM_SETFONT, mFont.ToHfont(), (IntPtr)1);
}
// Done
return false;
}
public void Dispose() {
mTries = -1;
mOwner = null;
if (mFont != null) mFont.Dispose();
}
// P/Invoke declarations
private const int WM_SETFONT = 0x30;
private const int WM_GETFONT = 0x31;
private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp);
[DllImport("user32.dll")]
private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp);
[DllImport("kernel32.dll")]
private static extern int GetCurrentThreadId();
[DllImport("user32.dll")]
private static extern int GetClassName(IntPtr hWnd, StringBuilder buffer, int buflen);
[DllImport("user32.dll")]
private static extern IntPtr GetDlgItem(IntPtr hWnd, int item);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}
そして、次のように使用します。
private void button1_Click(object sender, EventArgs e) {
using (new BoldMessageBox(this)) {
MessageBox.Show("Nobugz waz here");
}
}
このアプローチには 1 つの欠陥があります。フォントを太字にした後でも、メッセージ ボックスがテキスト用に予約した静的コントロールにテキストが収まる必要があります。そのため、フォントを小さくする必要がありました。この値を微調整する必要がある場合があります。
できません。これは API MessageBoxExのラッパーです。
それを行うには、独自のカスタム メッセージ ボックスを作成します。
実装方法の例として、このチュートリアルに従うことができます。
このようなフォームを作成する基本的な手順は次のとおりです。
- 新しいフォームを作成する
- ラベルと 2 つのボタンを追加する
- ラベルのフォントを太字に設定する
- 両方のボタンにハンドラーを追加し、フォームを閉じて、ボタンが押されたプロパティを設定します。
できません。独自のボックスを作成する必要があります。これは WinForms だと思いますが、ASP.NET の場合は答える資格がありません。
Extended MessageBox .NET Assembly XMSG .NET Web ページ: 詳細、ダウンロード
さまざまな MessageBox のビジュアル設定をオンザフライで調整します。
調整可能な機能には、メッセージのフォントと色、ボタンのキャプション、フォントとツールチップ、ダイアログの背景、ダイアログの位置、ダイアログのアイコン、タイムアウトなどがあります。選択したメッセージ フォントに応じて、メッセージに合わせてダイアログ ウィンドウのサイズが自動的に変更されます。
オプションで表示できる追加のコントロール: チェック ボックス、テキスト入力、Web リンク、最大 3 つの追加ボタン。
.NET コードでは、通常の MessageBox.Show を呼び出します。Extended MessageBox はカスタムメイドのダイアログではありません。これは、拡張機能が追加された通常の MessageBox のままです。
サポートされている OS: XP、2000、2003、2008 Vista、Win7 -- 32 または 64 ビット。
ダウンロードには、完全に機能する試用版と、完全な C# ソース コードを含む通常版が含まれます。