在MFC方法中显示不是ONPaint的图像 [英] Displaying image in MFC method that is not ONPaint

查看:135
本文介绍了在MFC方法中显示不是ONPaint的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在对话框中动态显示图像,如果我把代码放在on paint方法并使用dc从那里,我不能这样做,虽然我需要显示在窗口后工作没有问题显示,我使用的代码如下,我得到客户端窗口的直流从资源创建位图和尝试显示在窗口中,但没有显示,任何建议可能是错误的? p>

I am trying to display an image in a dialog dynamically, it works no problem if I put the code in the on paint method and use the dc from there, I can't do this though I need to display after the window is shown, the code I am using is as follows, I am getting the dc of the client window creating the bitmap from a resource and "trying" to display it in the window but nothing displays, Any suggestions what might be wrong?

void CProcessSteps::OnShowWindow(BOOL bShow, UINT nStatus) 
{
    CDialog::OnShowWindow(bShow, nStatus);  

    SetupInstructions();<-----------------Call To Method
}




void CProcessSteps::OnPaint() 
{   
    CPaintDC dc(this);
}

void CProcessSteps::SetupInstructions()
{
    CDC *pDC = new CDC();<------------------------------Problem starts here
    CFontUtil cfu;
    cfu.SetFont(&LineFont,30);

    CDC memDC;
    memDC.CreateCompatibleDC(pDC);

    int stepTop = 10;
    int stepEnd = 230;
    int imageLeft = 30;
    STEP_STRUCT* step;

    CBitmap iconImage;
    iconImage.LoadBitmap ( IDB_TID_CHECK );

    memDC.SelectObject(&iconImage);

    CRect iconRect;
    BITMAP bmInfo;
    iconImage.GetObject ( sizeof ( bmInfo ), &bmInfo );
    iconRect.SetRect ( imageLeft, stepTop, imageLeft+bmInfo.bmWidth, stepTop+bmInfo.bmHeight );

    pDC = this->GetDC();
    pDC->BitBlt(imageLeft, stepTop, imageLeft+bmInfo.bmWidth, stepTop+bmInfo.bmHeight, &memDC, 0, 0, SRCCOPY);

    //RedrawWindow();<-------- tried this here no luck

    int stepCount = m_pageStructure->PageSteps.GetCount();<----------------------------Bellow this works correctly

    POSITION pos = m_pageStructure->PageSteps.GetHeadPosition();
    while (pos)
    {
        step = m_pageStructure->PageSteps.GetNext(pos);

        CStatic *label = new CStatic;
        label->Create(_T( step->StepInstruction ),WS_CHILD | WS_VISIBLE, CRect(80, stepTop, 480, stepEnd), this);
        label->SetFont(&LineFont, true);


        label->GetWindowRect(rect);
        ScreenToClient(rect);

        pDC = label->GetDC();
        pDC->SelectObject(&LineFont);
        pDC->DrawText(step->StepInstruction, &rect, DT_CALCRECT|DT_WORDBREAK); 
        label->ReleaseDC(pDC);
        label->MoveWindow(rect);    

        stepTop += rect.Height();
        stepTop += 30;
        stepEnd += rect.Height();
    }
}


推荐答案

I认为在BeginPaint函数中有更多的只是给你CDC。和BeginPaint只能从OnPaint方法调用。

I think there's more in the BeginPaint-function than just giving you the CDC. And BeginPaint can only be called from the OnPaint-method.

要解决你的问题,使用Invalidate函数强制重绘从你的SetupInstructions方法。然后在OnPaint函数中绘图。

To solve your problem, use the Invalidate-functions to force a repaint from your "SetupInstructions" method. Then do the drawing inside the OnPaint function.

这篇关于在MFC方法中显示不是ONPaint的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆