Top / .NET備忘録 / 12.コンテナコントロール / 02.子コントロールの色決定

子コントロールの色決定は、こんな感じになります。

    private const int
        WM_USER = 0x0400,
        WM_REFLECT = WM_USER + 0x1C00,
        WM_CTLCOLOR = 0x0019,
        WM_CTLCOLORMSGBOX = 0x0132,
        WM_CTLCOLOREDIT = 0x0133,
        WM_CTLCOLORLISTBOX = 0x0134,
        WM_CTLCOLORBTN = 0x0135,
        WM_CTLCOLORDLG = 0x0136,
        WM_CTLCOLORSCROLLBAR = 0x0137,
        WM_CTLCOLORSTATIC = 0x0138;
 
    [DllImport("gdi32.dll")]
    private static extern int SetBkColor(HandleRef hdc, int crColor);
 
    [DllImport("gdi32.dll")]
    private static extern uint SetTextColor(HandleRef hdc, int crColor);
 
    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_CTLCOLOR:
            case WM_CTLCOLORBTN:
            case WM_CTLCOLORDLG:
            case WM_CTLCOLORMSGBOX:
            case WM_CTLCOLORSCROLLBAR:
            case WM_CTLCOLOREDIT:
            case WM_CTLCOLORLISTBOX:
            case WM_CTLCOLORSTATIC:
 
            case WM_REFLECT + WM_CTLCOLOR:
            case WM_REFLECT + WM_CTLCOLORBTN:
            case WM_REFLECT + WM_CTLCOLORDLG:
            case WM_REFLECT + WM_CTLCOLORMSGBOX:
            case WM_REFLECT + WM_CTLCOLORSCROLLBAR:
            case WM_REFLECT + WM_CTLCOLOREDIT:
            case WM_REFLECT + WM_CTLCOLORLISTBOX:
            case WM_REFLECT + WM_CTLCOLORSTATIC:
                Control con = Control.FromHandle(m.LParam);
                if (con != null)
                {
                    HandleRef hDC = new HandleRef(con, m.WParam);
                    Color foreColor = con.ForeColor;
                    Color backColor = con.BackColor;
                    int intForeColor = ColorTranslator.ToWin32(foreColor);
                    int intBackColor = ColorTranslator.ToWin32(backColor);
                    SetTextColor(hDC, intForeColor);
                    SetBkColor(hDC, intBackColor);
                    m.Result = Win32BrushCache.GetDefaultInstance().GetWin32Brush(intBackColor);
                }
                else
                    DefWndProc(ref m);
                break;
 
            default:
                base.WndProc(ref m);
                break;
        }
    }



トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   最終更新のRSS
Last-modified: 2013-12-11 (水) 01:32:27 (1344d)