国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

新舊MFC版本實現CEdit透明的2種方法的實例代碼

2020-01-26 16:20:55
字體:
來源:轉載
供稿:網友

  MFC 4.2(Visual Studio 6)實現起來很方便,只需要在對話框類下處理WM_CTLCOLOR消息,然后以下代碼即可:

復制代碼 代碼如下:

HBRUSH CAlphaEditboxDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    // TODO: Change any attributes of the DC here
    pDC->SetBkMode(TRANSPARENT);
    hbr=(HBRUSH)GetStockObject(HOLLOW_BRUSH);
    // TODO: Return a different brush if the default is not desired
    return hbr;
}

 然后在編輯控件的相關事件里調用一下Invalidate。

復制代碼 代碼如下:

void CAlphaEditboxDlg::OnKillfocusEditkey() 
{
    // TODO: Add your control notification handler code here
    Invalidate();
}

void CAlphaEditboxDlg::OnKillfocusEditmessage() 
{
    // TODO: Add your control notification handler code here
    Invalidate();
}

void CAlphaEditboxDlg::OnKillfocusEditpath() 
{
    // TODO: Add your control notification handler code here
    Invalidate();
}

  不要忘了,如果刪除字符,要重繪一下背景哦。這里只羅列了一部分。

  新版的MFC可謂相當麻煩,因為把背景設為CLR_NONE或者畫刷設為HOLLOW_BRUSH,微軟會默認會制黑色背景,這一點,微軟真是倒退了。廢話少說了,編輯控件子類化無可避免了,一定要處理WM_PAINT、WM_CHAR、WM_LBUTTONDOWN、WM_LBUTTONUP這幾個消息。如果你想去掉編輯控制自帶的邊框,還得處理WM_NCPAINT消息,不過這里什么代碼都不寫,目的是為避免執行默認的CDialogEx::OnNcPaint()方法給畫上邊框。下面代碼實現基本的透明效果,正常輸入沒問題,如果你想要實現刪除、選中與取消選中等功能,請追加處理WM_LBUTTONDOWN、WM_LBUTTONUP消息。

復制代碼 代碼如下:

//////////////////////////////////////////////////////////////////////////
//繪制窗口。
//////////////////////////////////////////////////////////////////////////
void CMyEdit::OnPaint()
{
    PAINTSTRUCT ps;
    TEXTMETRIC tm;
    int nSelStart=0,nSelEnd=0,nDrawStart=0,nDrawLen=0,nTxtLen=0;
    RECT r;
    CBitmap b;
    LPTSTR sz=(LPTSTR)calloc(1024,sizeof(TCHAR));
    CPaintDC* d2=(CPaintDC*)BeginPaint(&ps);
    CDC d1;
    CFont f;
    CWnd* p=GetParent();
    nTxtLen=GetWindowText(sz,1024);
    b.LoadBitmap(IDB_BITMAP1);
    d1.CreateCompatibleDC(p->GetDC());
    GetWindowRect(&r);
    p->ScreenToClient(&r);
    d1.SelectObject(b);
    d2->BitBlt(0,0,r.right-r.left,r.bottom-r.top,&d1,r.left,r.top,SRCCOPY);
    f.CreateFontIndirect(&m_lf);
    d2->SelectObject(f);
    d2->SetBkMode(TRANSPARENT);
    d2->GetTextMetrics(&tm);
    GetSel(nSelStart,nSelEnd);
    if (r.right-r.left<nTxtLen*tm.tmAveCharWidth)
    {
        nDrawStart=0-tm.tmAveCharWidth*nSelStart;
        nDrawLen=(r.right-r.left)/tm.tmAveCharWidth;
    }
    else
    {
        nDrawStart=0;
        nDrawLen=nTxtLen;
    }
    d2->TextOut(nDrawStart,3,sz,nDrawLen);
    d2->SelectObject(GetStockObject(NULL_BRUSH));
    d2->SelectObject(CreatePen(PS_DOT,1,RGB(255,0,0)));
    d2->Rectangle(0,0,r.right-r.left,r.bottom-r.top);
    POINT pt;
    pt=GetCaretPos();
    pt.x=nDrawLen*tm.tmAveCharWidth;
    SetCaretPos(pt);
    delete sz;
    EndPaint(&ps);
}

//////////////////////////////////////////////////////////////////////////
//暫不處理粘滯按鍵和功能鍵這2種情況。
//////////////////////////////////////////////////////////////////////////
void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    TEXTMETRIC tm;
    int nSelStart=0,nSelEnd=0,nDrawStart=0,nDrawLen=0,nTxtLen=0;
    RECT r;
    CBitmap b;
    LPTSTR sz=(LPTSTR)calloc(1024,sizeof(TCHAR));
    LPTSTR input=(LPTSTR)calloc(1024,sizeof(TCHAR));
    CClientDC d2(this);
    CDC d1;
    CFont f;
    CWnd* p=GetParent();
    nTxtLen=GetWindowText(sz,1024);
    wsprintf(input,L"%c",nChar);
    lstrcat(sz,input);
    SetWindowText(sz);
    b.LoadBitmap(IDB_BITMAP1);
    d1.CreateCompatibleDC(p->GetDC());
    GetWindowRect(&r);
    p->ScreenToClient(&r);
    d1.SelectObject(b);
    d2.BitBlt(0,0,r.right-r.left,r.bottom-r.top,&d1,r.left,r.top,SRCCOPY);
    f.CreateFontIndirect(&m_lf);
    d2.SelectObject(f);
    d2.SetBkMode(TRANSPARENT);
    d2.GetTextMetrics(&tm);
    GetSel(nSelStart,nSelEnd);
    if (r.right-r.left<nTxtLen*tm.tmAveCharWidth)
    {
        nDrawStart=0-tm.tmAveCharWidth*nSelStart;
        nDrawLen=(r.right-r.left)/tm.tmAveCharWidth;
    }
    else
    {
        nDrawStart=0;
        nDrawLen=nTxtLen;
    }
    d2.TextOut(nDrawStart,3,sz,nDrawLen);
    d2.SelectObject(GetStockObject(NULL_BRUSH));
    d2.SelectObject(CreatePen(PS_DOT,1,RGB(255,0,0)));
    d2.Rectangle(0,0,r.right-r.left,r.bottom-r.top);
    POINT pt;
    pt=GetCaretPos();
    pt.x=nDrawLen*tm.tmAveCharWidth;
    SetCaretPos(pt);
    delete sz;
    delete input;
    //CEdit::OnChar(nChar, nRepCnt, nFlags);
}

以上就是這些了,歡迎一起交流如何實現注釋中寫明的沒有實現有功能。我是菜鳥,大蝦請勿見笑。希望你能多多指點。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 成都市| 衡阳市| 昌乐县| 武山县| 新和县| 新余市| 安平县| 汾阳市| 城市| 上林县| 安宁市| 武城县| 武安市| 莆田市| 高密市| 丹东市| 邢台市| 潮州市| 同江市| 娱乐| 句容市| 龙井市| 津南区| 比如县| 怀来县| 江安县| 高密市| 得荣县| 宁武县| 内丘县| 赤峰市| 慈利县| 房产| 城固县| 高雄县| 尉犁县| 三门峡市| 宣恩县| 宁津县| 石阡县| 招远市|