zoukankan      html  css  js  c++  java
  • VC++实现编辑框输入提示效果

      编辑框在第一次输入时最好给出一个虚拟的输入提示信息文本,这样的效果更佳友好。,我在编辑框添加灰色提示字(html+VC)一文中简单介绍了一些方法,但是效果欠佳。

      原始的编辑框CEdit类没有这样的功能,我们可以通过继承来改造得到:https://www.codeproject.com/Articles/737/Dim-Edit-Control

     DimEditCtrl.h
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
     
    /*|*
    |*|  File:      DimEditCtrl.h
    |*|
    |*|  By:        James R. Twine, TransactionWorks, Inc.
    |*|             Copyright 2000, TransactionWorks, inc.
    |*|  Date:      Thursday, September 21, 2000
    |*|
    |*|  Notes:     This Is The Implementation Of A "Dim Edit Control".
    |*|             It Provides Visual Instructions Within The Edit
    |*|             Control Itself.  It Can Be Used To Indicate Special
    |*|             Properties Of A Edit Control Used On A Crowded
    |*|             Interface
    |*|
    |*|             May Be Freely Incorporated Into Projects Of Any Type
    |*|             Subject To The Following Conditions:
    |*|
    |*|             o This Header Must Remain In This File, And Any
    |*|               Files Derived From It
    |*|             o Do Not Misrepresent The Origin Of This Code
    |*|               (IOW, Do Not Claim You Wrote It)
    |*|
    |*|             A "Mention In The Credits", Or Similar Acknowledgement,
    |*|             Is *NOT* Required.  It Would Be Nice, Though! :)
    *|*/

    #if !defined(AFX_DIMEDITCTRL_H__CF8D88FB_6945_11D4_8AC4_00C04F6092F9__INCLUDED_)
    #define AFX_DIMEDITCTRL_H__CF8D88FB_6945_11D4_8AC4_00C04F6092F9__INCLUDED_

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // DimEditCtrl.h : header file
    //


    //
    //  This Specifies The Length Of The Dim Text Buffer...
    //
    static  const   int     DIM_TEXT_LEN =  128;                // Dim Text Buffer Length


    /////////////////////////////////////////////////////////////////////////////
    // CDimEditCtrl window

    class CDimEditCtrl : public CEdit
    {
        
    // Construction
    public:
        
    /**/
        CDimEditCtrl();                                 
    // Constructor

        
    // Attributes
    public:

        
    // Operations
    public:

        
    void    SetShowDimControl( bool bShow );                // Show Or Hide The Dim Control
        void    SetDimText( LPCTSTR cpText );                   // Set The Dim Text
        void    SetDimColor( COLORREF crDColor );               // Set The Dim Color
        void    SetDimOffset( char cRedOS, char cGreenOS,
                              
    char cBlueOS );                           // Set The Dim Color Offset

        
    // Overrides
        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CDimEditCtrl)
    public:
        
    virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT &rect, CWnd *pParentWnd, UINT nID, CCreateContext *pContext = NULL);
    protected:
        
    virtual void PreSubclassWindow();
        
    //}}AFX_VIRTUAL

        
    // Implementation
    public:
        
    virtual ~CDimEditCtrl();

        
    // Generated message map functions
    protected:
        
    //{{AFX_MSG(CDimEditCtrl)
        afx_msg void OnChange();
        afx_msg 
    void OnSetfocus();
        afx_msg 
    void OnPaint();
        afx_msg BOOL OnEraseBkgnd(CDC *pDC);
        afx_msg 
    void OnSettingChange(UINT uFlags, LPCTSTR lpszSection);
        afx_msg 
    void OnKillfocus();
        
    //}}AFX_MSG
        //  afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
        //  afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);

        DECLARE_MESSAGE_MAP()

        
    void        DrawDimText( void );                        // Draw The Dim Text

        COLORREF    m_crDimTextColor;                           
    // "Hard" Dim Text Color
        TCHAR       m_caDimText[ DIM_TEXT_LEN + 1 ];            // Dim Text Buffer
        bool        m_bShowDimText;                             // Are We Showing The Dim Text?
        bool        m_bUseDimOffset;                            // Are We Using The Offset Colors (Not Hard Color)?
        char        m_cRedOS;                                   // Red Color Dim Offset
        char        m_cGreenOS;                                 // Green Color Dim Offset
        char        m_cBlueOS;                                  // Blue Color Dim Offset
        int         m_iDimTextLen;                              // Length Of Dim Text
    };

    /////////////////////////////////////////////////////////////////////////////

    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.

    #endif // !defined(AFX_DIMEDITCTRL_H__CF8D88FB_6945_11D4_8AC4_00C04F6092F9__INCLUDED_)
     DimEditCtrl.cpp
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
     
    /*|*
    |*|  File:      DimEditCtrl.cpp
    |*|
    |*|  By:        James R. Twine, TransactionWorks, Inc.
    |*|             Copyright 2000, TransactionWorks, inc.
    |*|  Date:      Thursday, September 21, 2000
    |*|
    |*|  Notes:     This Is The Implementation Of A "Dim Edit Control".
    |*|             It Provides Visual Instructions Within The Edit
    |*|             Control Itself.  It Can Be Used To Indicate Special
    |*|             Properties Of A Edit Control Used On A Crowded
    |*|             Interface
    |*|
    |*|             May Be Freely Incorporated Into Projects Of Any Type
    |*|             Subject To The Following Conditions:
    |*|
    |*|             o This Header Must Remain In This File, And Any
    |*|               Files Derived From It
    |*|             o Do Not Misrepresent The Origin Of This Code
    |*|               (IOW, Do Not Claim You Wrote It)
    |*|
    |*|             A "Mention In The Credits", Or Similar Acknowledgement,
    |*|             Is *NOT* Required.  It Would Be Nice, Though! :)
    *|*/

    #include "stdafx.h"
    #include "DimEdit.h"
    #include "DimEditCtrl.h"

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif

    /////////////////////////////////////////////////////////////////////////////
    // CDimEditCtrl

    CDimEditCtrl::CDimEditCtrl() :
        m_bShowDimText( 
    true ),                                 // Set The Dim Flag
        //  m_cRedOS( -0x40 ),                                      // Set The Default Dim Offset Colors
        //  m_cGreenOS( -0x40 ),                                    // Set The Default Dim Offset Colors
        //  m_cBlueOS( -0x40 ),                                     // Set The Default Dim Offset Colors
        //  m_bUseDimOffset( true ),                                // Use The Offset Colors
        m_iDimTextLen( 0 ),                                     // No Dim Text Set Yet
        m_crDimTextColor( RGB( 0x00, 0x00, 0x00 ) )             // No "Hard" Dim Text Color
    {
        m_caDimText[ 
    0 ] = _T( '' );                          // Terminate The Buffer
        SetDimOffset( -0x40, -0x40, -0x40 );                    // Set The Dim Offset

        
    return;                                                 // Done!
    }


    CDimEditCtrl::~CDimEditCtrl()
    {
        
    return;                                                 // Done!
    }


    BEGIN_MESSAGE_MAP(CDimEditCtrl, CEdit)
        
    //{{AFX_MSG_MAP(CDimEditCtrl)
        ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
        ON_CONTROL_REFLECT(EN_SETFOCUS, OnSetfocus)
        ON_WM_PAINT()
        ON_WM_ERASEBKGND()
        ON_WM_SETTINGCHANGE()
        ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus)
        
    //}}AFX_MSG_MAP
        //  ON_WM_LBUTTONDOWN()
        //  ON_WM_LBUTTONDBLCLK()
    END_MESSAGE_MAP()

    /////////////////////////////////////////////////////////////////////////////
    // CDimEditCtrl message handlers

    void    CDimEditCtrl::PreSubclassWindow()
    {
        CEdit::PreSubclassWindow();                             
    // Do Default...

        SetShowDimControl( 
    true );                              // Default To Show The Dim Control

        
    return;                                                 // Done!
    }


    void    CDimEditCtrl::SetDimText( LPCTSTR cpDimText )
    {
        
    if( cpDimText )                                         // If Dim Text Specified
        {
            _tcsncpy( m_caDimText, cpDimText, DIM_TEXT_LEN );   
    // Copy Over The Text
            m_caDimText[ DIM_TEXT_LEN ] = _T( '' );           // Enforce Termination (I Am Paranoid, I Know!)
            m_iDimTextLen = _tcslen( m_caDimText );             // Store Length Of The Dim Text
        }
        
    else                                                    // If No Dim Text
        {
            m_caDimText[ 
    0 ] = _T( '' );                      // Just Terminate The Buffer (No Text)
            m_iDimTextLen = 0;                                  // No Dim Text
        }
        
    if( m_bShowDimText )                                    // If Showing Any Dim Text
        {
            DrawDimText();                                      
    // Draw The Dim Text
        }
        
    return;                                                 // Done!
    }


    void CDimEditCtrl::SetShowDimControl( bool bShow )
    {
        m_bShowDimText = bShow;                                 
    // Set The Dim Flag
        if( bShow )                                             // If Showing Any Dim Text
        {
            DrawDimText();                                      
    // Draw The Dim Text
        }
        
    return;                                                 // Done!
    }


    BOOL    CDimEditCtrl::Create( LPCTSTR lpszClassName, LPCTSTR lpszWindowName,
                                  DWORD dwStyle, 
    const RECT &rect, CWnd *pParentWnd, UINT nID,
                                  CCreateContext *pContext )
    {
        BOOL    bCreated = CWnd::Create( lpszClassName,
                                         lpszWindowName, dwStyle, rect,
                                         pParentWnd, nID, pContext );           
    // Try To Create Ourselves...

        
    if( bCreated )                                          // If We Got Created
        {
            SetShowDimControl( 
    true );                          // Show The Dim Control
        }
        
    return( bCreated );                                     // Return Creation Status
    }


    void CDimEditCtrl::OnChange()
    {
        
    int iLen = GetWindowTextLength();                   // Get Control's Text Length

        
    if( !iLen )                                             // If No Text
        {
            SetShowDimControl( 
    true );                          // Show The Dim Text
        }
        
    else                                                    // If Text Now In The Control
        {
            SetShowDimControl( 
    false );                         // Disable The Dim Text
        }
        
    return;                                                 // Done!
    }


    void CDimEditCtrl::OnSetfocus()
    {
        
    if( m_bShowDimText )                                    // If Showing Any Dim Text
        {
            DrawDimText();                                      
    // Draw The Dim Text
        }
        
    return;                                                 // Done!
    }


    void    CDimEditCtrl::OnPaint()
    {
        Default();                                              
    // Do Default Control Drawing

        
    if( m_bShowDimText )                                    // If Showing Any Dim Text
        {
            DrawDimText();                                      
    // Draw The Dim Text
        }
        
    return;                                                 // Done!
    }


    void CDimEditCtrl::DrawDimText( void )
    {
        
    if( !m_iDimTextLen )                                    // If No Dim Text
        {
            
    return;                                             // Stop Here
        }
        CClientDC   dcDraw( 
    this );
        CRect       rRect;
        
    int         iState = dcDraw.SaveDC();                   // Save The DC State

        GetClientRect( &rRect );                                
    // Get Drawing Area
        rRect.OffsetRect( 11 );                               // Add Sanity Space

        dcDraw.SelectObject( (*GetFont()) );                    
    // Use The Control's Current Font
        dcDraw.SetTextColor( m_crDimTextColor );                // Set The Text Color
        dcDraw.SetBkColor( GetSysColor( COLOR_WINDOW ) );       // Set The Bk Color
        dcDraw.DrawText( m_caDimText, m_iDimTextLen, &rRect,
                         ( DT_CENTER | DT_VCENTER ) );                  
    // Draw The Dim Text

        dcDraw.RestoreDC( iState );                             
    // Restore The DC State

        
    return;                                                 // Done!
    }


    BOOL CDimEditCtrl::OnEraseBkgnd(CDC *pDC)
    {
        BOOL    bStatus = CEdit::OnEraseBkgnd(pDC);

        
    if( ( bStatus ) && ( m_bShowDimText ) )                 // If All Good, And Showing Any Dim Text
        {
            DrawDimText();                                      
    // Draw The Dim Text
        }
        
    return( bStatus );                                      // Return Erase Status
    }

    /*
    void CDimEditCtrl::OnLButtonDown(UINT nFlags, CPoint point)
    {
        TRACE( _T( "Click... " ) );
        CEdit::OnLButtonDown(nFlags, point);
    }

    void CDimEditCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
    {
        TRACE( _T( "DClick... " ) );
        CEdit::OnLButtonDblClk(nFlags, point);
    }
    */


    void CDimEditCtrl::SetDimOffset( char cRedOS, char cGreenOS, char cBlueOS )
    {
        COLORREF    crWindow = GetSysColor( COLOR_WINDOW );
        BYTE        btRedOS = ( GetRValue( crWindow ) + cRedOS );
        BYTE        btGreenOS = ( GetGValue( crWindow ) + cGreenOS );
        BYTE        btBlueOS = ( GetBValue( crWindow ) + cBlueOS );

        m_bUseDimOffset = 
    true;                                 // Set The Flag
        m_cRedOS = cRedOS;                                      // Store Red Offset
        m_cGreenOS = cGreenOS;                                  // Store Green Offset
        m_cBlueOS = cBlueOS;                                    // Store Blue Offset
        m_crDimTextColor = RGB( (BYTE)btRedOS, (BYTE)btGreenOS,
                                (BYTE)btBlueOS );                               
    // Build The New Dim Color

        
    return;                                                 // Done!
    }


    void CDimEditCtrl::SetDimColor( COLORREF crColor )
    {
        m_bUseDimOffset = 
    false;                                // Unset The Flag
        m_crDimTextColor = crColor;                             // Set The New Dim Color
        m_cRedOS = m_cGreenOS = m_cBlueOS = 0;                  // No Offset

        
    return;                                                 // Done!
    }


    void CDimEditCtrl::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
    {
        CEdit::OnSettingChange(uFlags, lpszSection);

        
    if( m_bUseDimOffset )                                   // If Using An Offset For The Dim Color
        {
            COLORREF    crWindow = GetSysColor( COLOR_WINDOW );

            m_crDimTextColor = RGB( GetRValue( crWindow ) +
                                    m_cRedOS, GetGValue( crWindow ) +
                                    m_cGreenOS, GetBValue( crWindow ) +
                                    m_cBlueOS );                                
    // Rebuild The Dim Color
        }
        
    return;                                                 // Done!
    }

    void CDimEditCtrl::OnKillfocus()
    {
        
    int iLen = GetWindowTextLength();                   // Get Control's Text Length

        
    if( !iLen )                                             // If No Text
        {
            SetShowDimControl( 
    true );                          // Show The Dim Text
        }
        
    else                                                    // If Text Now In The Control
        {
            SetShowDimControl( 
    false );                         // Disable The Dim Text
        }
        
    return;

    }

  • 相关阅读:
    scrapy user-agent随机更换
    python框架Scrapy中crawlSpider的使用——爬取内容写进MySQL
    异步代理池2--正确实现并发
    python asyncio异步代理池
    SSH 上传下载文件
    scrapy 自定义扩展
    scrapy pipelines 以及 cookies
    scrapy 去重策略修改
    提车注意事项
    mysql 笔记
  • 原文地址:https://www.cnblogs.com/MakeView660/p/9354290.html
Copyright © 2011-2022 走看看