Chinaunix首页 | 论坛 | 博客
  • 博客访问: 818718
  • 博文数量: 756
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 4980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:40
文章分类

全部博文(756)

文章存档

2011年(1)

2008年(755)

我的朋友

分类:

2008-10-13 16:10:34

WTL类库之 “带提示的Edit控件”

BRENT哥哥倾情奉献 之一
x.brent@gmail.com

使用方法:
#include "_AtlDimEdit.h"

_AtlDimEdit m_Edit;
m_Edit.SubclassWindow( GetDlgItem(IDC_EDIT1));

m_Edit.SetDimText( "输入用户名,例如\"A5\"" )
.SetDimColor(RGB(0x80,0x80,0x80)
.EnableWindow(TRUE);

void OnDestroy()
{
m_Edit.UnsubclassWindow();
}


==SRC==
////////////////////////////////////////////////////////////////////////////////////////////////////


// _AtlDimEdit.h

#pragma once

#include
#ifndef TString
typedef std::basic_string TString;
#endif

#ifndef __ATLCRACK_H__
#error _AtlDimEdit.h requires AtlCrack.h to be included first
#endif

namespace WTL
{
class _AtlDimEdit :
public CWindowImpl< _AtlDimEdit, CEdit >
{
DECLARE_WND_CLASS(__BXC_DIMEDIT)

private:
BOOL m_EnableDim;
UINT m_Alignment;
TString m_dimText;
DWORD m_dimColor;
UINT m_MarginLeft,m_MarginTop;

public:
_AtlDimEdit( ):CWindowImpl< _AtlDimEdit, CEdit >( ),
m_EnableDim( TRUE ),
m_Alignment(TA_LEFT),
m_MarginLeft(3),
m_MarginTop(1),
m_dimText( "<点击此处编辑内容>" ),
m_dimColor( RGB( 0x80, 0x80,0x80) ){}
_AtlDimEdit( const TString& dimText,const COLORREF dimColor = RGB( 128, 128, 128 ) )
: CWindowImpl< _AtlDimEdit, CEdit >( ),
m_EnableDim( TRUE ),
m_Alignment(TA_LEFT),
m_MarginLeft(3),
m_MarginTop(1),
m_dimText( dimText ),
m_dimColor( dimColor ){}

virtual ~_AtlDimEdit( ) {}

void SetEnableDim(BOOL b) { m_EnableDim = b; }
BOOL IsEnableDim() { return m_EnableDim; }

BOOL SubclassWindow( HWND hWnd )
{
return( CWindowImpl< _AtlDimEdit, CEdit >::SubclassWindow( hWnd ) );
}

_AtlDimEdit& SetDimMargins(UINT left,UINT top)
{
m_MarginLeft = left;
m_MarginTop = top;
}

_AtlDimEdit& SetDimText(TString dimText )
{
m_dimText = dimText;
return( *this );
}

_AtlDimEdit& SetDimColor(COLORREF dimColor)
{
m_dimColor = dimColor;
return( *this );
}

_AtlDimEdit& SetDimAligment(UINT n)
{
ATLASSERT( n== TA_LEFT || n == TA_CENTER);
m_Alignment = n;
return (*this);
}

BEGIN_MSG_MAP_EX( _AtlDimEdit )
MSG_WM_PAINT( OnPaint )
MSG_WM_SETFOCUS( OnSetFocus )
MSG_WM_KILLFOCUS( OnKillFocus )
END_MSG_MAP( )

void OnSetFocus( HWND hWnd )
{
DefWindowProc();
if( !GetWindowTextLength() )
Invalidate( );
}

void OnKillFocus( HWND hWnd )
{
DefWindowProc( );
if( !GetWindowTextLength() )
Invalidate( );
}

void OnPaint( HDC )
{
if( !m_EnableDim || GetWindowTextLength())
{
SetMsgHandled(FALSE);
return;
}

CPaintDC dc(*this);
CBrushHandle brush;
brush.CreateSysColorBrush( COLOR_WINDOW );
dc.FillRect( &dc.m_ps.rcPaint, brush );
dc.SelectFont( HFONT( GetStockObject( DEFAULT_GUI_FONT ) ) );

dc.SetTextColor( m_dimColor );
dc.SetTextAlign( m_Alignment );
dc.TextOut( m_MarginLeft, m_MarginTop, m_dimText.c_str( ) );
}
};
}
--------------------next---------------------

阅读(680) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~