9

私は C# winforms アプリケーション (VS.NET 2008、.NET 3.5 sp 1) に取り組んでいます。フォームに検索フィールドがあり、検索フィールドの横にラベルを表示するのではなく、検索フィールド自体の背景に灰色のテキストを表示したいと考えています (「検索用語」など)。ユーザーが検索フィールドにテキストを入力し始めると、テキストが消えるはずです。どうすればこれを達成できますか?

4

4 に答える 4

9

これを行うには、P/Inovke 相互運用コードを使用する必要があります。SendMessageWin32 API関数とEM_SETCUEBANNERメッセージを探します。

于 2008-09-05T20:53:36.683 に答える
4

リンクの代わりにコードを投稿することをお勧めします。ここから投稿しています

            //Copyright (c) 2008 Jason Kemp

            //Permission is hereby granted, free of charge, to any person obtaining a copy
            //of this software and associated documentation files (the "Software"), to deal
            //in the Software without restriction, including without limitation the rights
            //to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
            //copies of the Software, and to permit persons to whom the Software is
            //furnished to do so, subject to the following conditions:

            //The above copyright notice and this permission notice shall be included in
            //all copies or substantial portions of the Software.

            //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
            //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
            //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
            //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
            //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
            //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
            //THE SOFTWARE.

            using System;
            using System.Runtime.InteropServices;
            using System.Windows.Forms;
            using System.Text;


            public static class Win32Utility
            {
                [DllImport("user32.dll", CharSet = CharSet.Auto)]
                private static extern Int32 SendMessage(IntPtr hWnd, int msg,
                    int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);

                [DllImport("user32.dll")]
                private static extern bool SendMessage(IntPtr hwnd, int msg, int wParam, StringBuilder lParam);

                [DllImport("user32.dll")]
                private static extern bool GetComboBoxInfo(IntPtr hwnd, ref COMBOBOXINFO pcbi);

                [StructLayout(LayoutKind.Sequential)]
                private struct COMBOBOXINFO
                {
                    public int cbSize;
                    public RECT rcItem;
                    public RECT rcButton;
                    public IntPtr stateButton;
                    public IntPtr hwndCombo;
                    public IntPtr hwndItem;
                    public IntPtr hwndList;
                }

                [StructLayout(LayoutKind.Sequential)]
                private struct RECT
                {
                    public int left;
                    public int top;
                    public int right;
                    public int bottom;
                }

                private const int EM_SETCUEBANNER = 0x1501;
                private const int EM_GETCUEBANNER = 0x1502;

                public static void SetCueText(Control control, string text)
                {
                    if (control is ComboBox)
                    {
                        COMBOBOXINFO info = GetComboBoxInfo(control);
                        SendMessage(info.hwndItem, EM_SETCUEBANNER, 0, text);
                    }
                    else
                    {
                        SendMessage(control.Handle, EM_SETCUEBANNER, 0, text);
                    }
                }

                private static COMBOBOXINFO GetComboBoxInfo(Control control)
                {
                    COMBOBOXINFO info = new COMBOBOXINFO();
                    //a combobox is made up of three controls, a button, a list and textbox;
                    //we want the textbox
                    info.cbSize = Marshal.SizeOf(info);
                    GetComboBoxInfo(control.Handle, ref info);
                    return info;
                }

                public static string GetCueText(Control control)
                {
                    StringBuilder builder = new StringBuilder();
                    if (control is ComboBox)
                    {
                        COMBOBOXINFO info = new COMBOBOXINFO();
                        //a combobox is made up of two controls, a list and textbox;
                        //we want the textbox
                        info.cbSize = Marshal.SizeOf(info);
                        GetComboBoxInfo(control.Handle, ref info);
                        SendMessage(info.hwndItem, EM_GETCUEBANNER, 0, builder);
                    }
                    else
                    {
                        SendMessage(control.Handle, EM_GETCUEBANNER, 0, builder);
                    }
                    return builder.ToString();
                }
            }
于 2012-08-15T15:09:08.443 に答える
2

これが一般的に行われる方法は、テキストの色を灰色に設定し、ヒントテキストを事前に入力することだと思います。

次に、テキスト フィールドのフォーカス イベントのハンドラーを記述し、フィールドの内容と、フォーカスが得られたときと失われたときに基づいて色を変更します。ここにいくつかの疑似コードがあります (申し訳ありませんが、これは完全に C# コードではありません。私は今、脳に Actionscript を持っています):

TextInput myInput;
boolean showingHint = true;

myInput.text = "Search Terms";
myInput.color = 0xcccccc;

myInput.onFocusGained = function() {
  if(showingHint) {
    myInput.text = "";
    myInput.color = 0x000000;
    showingHint = false;
  }
}

myInput.onFocusLost = function() {
  if(!showingHint && myInput.text.length == 0) {
    myInput.text = "Search Terms";
    myInput.color = 0xcccccc;
    showingHint = true;
  }
}

ユーザーが手動でテキストを変更していない場合にのみ、フォーカスが失われたテキストを変更する必要があることに注意してください。別のブール値を使用して、ヒントを表示しているかどうかを追跡して、ユーザーが「ヒント」テキストを実際のコンテンツとして手動で入力したかどうかを区別できるようにします。同様に、ユーザー入力を誤って削除しないように、ヒントが表示されている場合にのみ入力ボックスの内容をクリアする必要があります。

于 2008-09-05T21:00:34.187 に答える
0

テキスト ボックス コントロールには、AutoCompleteModeAutoCompleteSourceという組み込み機能があります。カスタムまたはサードパーティのコントロールに入る前に、試してみる価値があるかもしれません.

于 2008-09-05T20:54:54.043 に答える