VC 透明编辑框的实现

来源:互联网 发布:python查看版本 编辑:程序博客网 时间:2024/05/16 15:43

本方法对于背景为位图的对话框有效,其实也就是换了一种方式让编辑框相对于对话框是透明的,

把编辑框在当前对话框的位图给截取一部分出来用于填充编辑框

 

实现如下

 

派生一个CEdit的类,添加

CBitmap m_bmp;

CBrush m_brHollow;

 

添加WM_CTLCOLOR,WM_LBUTTONUP,EN_CHANGE,WM_ERASEBKGND响应消息

 CMyEdit::CMyEdit()
{
 m_brHollow.CreateStockObject(HOLLOW_BRUSH); 
 
}

 

HBRUSH CMyEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
 pDC->SetBkMode(TRANSPARENT);
 pDC->SetTextColor(RGB(0,0,0));
 return m_brHollow;
}

void CMyEdit::OnLButtonUp(UINT nFlags, CPoint point)
{
 Invalidate();
 CEdit::OnLButtonUp(nFlags, point);
}

void CMyEdit::OnChange()
{
 Invalidate();
}

BOOL CMyEdit::OnEraseBkgnd(CDC* pDC)
{
 BITMAP bm;
 m_bmp.GetBitmap(&bm);
 CDC dcMem;
 dcMem.CreateCompatibleDC(pDC);

 CRect rect;
 CRect parentRect;
 GetParent()->GetClientRect(&parentRect);
 GetParent()->ClientToScreen(&parentRect);
 GetClientRect(&rect);
 ClientToScreen(&rect);

 float prWidth  = parentRect.Width();     //get dailog width
 float ctWidth  = rect.Width();;          //get control width
 float prHeight = parentRect.Height();    //get dailog height
 float ctHeight = rect.Height();          //get control height
 float Width    = rect.left - parentRect.left;
 float Height   = rect.top - parentRect.top;
 float tWidth   = ctWidth/prWidth;//取得控件与对话框宽度的比值
    float tHeight  = ctHeight/prHeight;//取得控件与对话框高度的比值

 //获取控制左上角点在位图中的相对位置
 float tLeft    = Width/prWidth;
 float tTop     = Height/prHeight;

 dcMem.SelectObject(&m_bmp);
 pDC->StretchBlt(0,0,
  rect.Width(),
  rect.Height(),
  &dcMem,
  (bm.bmWidth * tLeft),
  (bm.bmHeight * tTop),
  (bm.bmWidth * tWidth),
  (bm.bmHeight * tHeight),
  SRCCOPY);
 
    return TRUE;
}

 

 

在对话框中添加编辑框的成员变量
m_xxx;
在OninitDialog里添加
m_xxx.m_bmp.LoadBitmap(IDB_BITMAP4);
::SendMessage(m_xxx.m_hWnd, WM_PAINT, NULL, NULL);

原创粉丝点击