现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

为CEdit设置背景图片

2012-08-29 15:58 工业·编程 ⁄ 共 1148字 ⁄ 字号 暂无评论

    以CSkinEdit( public CEdit )为例,设置Edit的属性:Multiline, 无Border.

    效果图:

wps_clip_image-23759

 

1.定义类成员变量:

CBitmap m_bmp;

CBrush m_brush;

在WM_CREATE消息中初始化它们:

int CSkinEdit::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
if (CEdit::OnCreate(lpCreateStruct) == -1)
  return -1;
// TODO: Add your specialized creation code here
HBITMAP hBmp = (HBITMAP)LoadImage( AfxGetInstanceHandle(), "C://background.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
ASSERT( hBmp != NULL );
m_bmp.Attach( hBmp );

m_brush.CreatePatternBrush( &m_bmp );
return 0;
}

1.映射消息:
ON_WM_CTLCOLOR_REFLECT()

HBRUSH CSkinEdit::CtlColor(CDC* pDC, UINT nCtlColor) 
{
// TODO: Change any attributes of the DC here

  pDC->SetBkMode(TRANSPARENT);

return (HBRUSH)m_brush;
// TODO: Return a non-NULL brush if the parent's handler should not be called
return NULL;
}

特别注意:
pDC的坐标原点为Edit控件左上角在父窗口的坐标, 所以C://background.bmp左上角对应Edit父窗口的左上角,而不是Edit控件的左上角

2.映射消息:
ON_CONTROL_REFLECT(EN_CHANGE, OnChange)

void CSkinEdit::OnChange() 
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CEdit::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.

// TODO: Add your control notification handler code here
InvalidateRect( NULL );
}
在Edit内容变化时刷新(包括键入字符,删除字符等)

给我留言

留言无头像?