Ferin Jose
Ferin Jose

Reputation: 45

How to place image using SDI application when the dialog first appears?

I am developing MFC based SDI application using CFormView class in VC++. My problem is that I need to load image when the dialog initially appears. How to place image in an SDI application..I know for dialog based applications it can be done using OnInitDialog application.But for SDI application there is no such function. I tried placing the image using OnInitialUpdate() and OnPaint() function. But it failed..What should I do to place the image to dialog when it first appears? Please Help

Thanks in advance

Code as I placed in OnInitialUpdate()

void CECUSimulatorView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
hBitmap = LoadImage(0,_T("F:/ECUSimulator/ECUSimulator_New/res/LedOff.bmp"),               IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
ImageLoading();
}

Code for the function ImageLoading()

void CECUSimulatorView::OnInitialUpdate()
{
HDC hDC, hDCToDisplay = NULL;
hDC = CreateCompatibleDC(hDCToDisplay);
SelectObject(hDC,hBitmap);
hDCToDisplay = ::GetDC(m_picture.m_hWnd);
m_picture.GetWindowRect(&picRect);
BitBlt(hDCToDisplay,0 , 0, (picRect.right - picRect.left), (picRect.bottom -picRect.top), hDC, 0 ,0 ,SRCCOPY);
DeleteDC(hDC);
DeleteDC(hDCToDisplay);
}

Here

HANDLE hBitmap; CStatic m_picture; //Picture Control CRect picRect; //Picture Control Rect

I removed the code from OnInitialUpdate() and placed it in OnPaint() function as follows:

void CECUSimulatorView::OnPaint() { CPaintDC dc(this); // device context for painting

hBitmap = LoadImage(0,_T("F:/ECUSimulator/ECUSimulator_New/res/LedOff.bmp"), IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
ImageLoading();

}

Upvotes: 0

Views: 1175

Answers (3)

Ferin Jose
Ferin Jose

Reputation: 45

I found the answer for my problem. I used CBitmap class as follows:

CBitmap m_bBitmap1;

In OnInitialUpdate() I wrote as follows :

m_bBitmap1.LoadBitmapW(IDB_BITMAP1);

In OnPaint() I wrote as follows:

m_picture.SetBitmap(m_bBitmap1);

Wherever(In which all functions)need to load image just call the above line of code in corresponding functions..

Upvotes: 0

Edward Clements
Edward Clements

Reputation: 5132

[the comments in the previous answer was getting very extended, hence a new answer]

a) derive a new class (say CMyPicture) based on CStatic, change m_picture to this new class

b) in CMyPicture, create a handler for WM_PAINT, this will normally be called CMyPicture::OnPaint()

c) change your ImageLoading() function to take a CDC *pDC as parameter and use this DC to render the image

d)

void CMyStatic::OnPaint(void)
{   CPaintDC dc(this);
    ImageLoading(&dc); // or even move the painting logic here
}

NOTE: you can load the image in OnInitialUpdate(), no need to load it each time you are painting

An example (for a dialog, but the painting logic should not be affected) in this SO Answer.

Upvotes: 0

Edward Clements
Edward Clements

Reputation: 5132

Calling LoadImage() in OnInitialUpdate() is ok, the actual painting needs to be done in one of the two following ways:

a) in CECUSimulatorView::OnDraw() -- easier, but may introduce flickering

b) override the OnPaint() function of the m_picture control using ClassWizard and draw the picture there

Upvotes: 0

Related Questions