VC++/MFC memory DC may terminate incorrectly when discarded

Asked 2 years ago, Updated 2 years ago, 103 views


in VC++/MFC under dialog-based application environment Memory DCs are deployed for faster graph drawing.
This dialog allows you to resize in the resize box.
# Maybe I'm thinking wrong, but...
In the WM_SIZE message, discard the memory DC and re-enter the new window size. On the other hand, by obtaining a memory DC, we are resizing it.

However, if the application is running and terminates incorrectly in the discarding part of the memory DC
I'm confused about the fact that
Five memory DCs have been created and transferred to the screen.

I would appreciate it if someone could give me guidance.
Thank you for your cooperation.

mfc visual-c++

2022-09-30 14:05

1 Answers

First of all, it would be better to show the target source code to resolve the issue sooner.I highly recommend it.

Now, I'm using memory DC myself, but I don't see any problems.
It also supports WM_SIZE and is processed in the following steps:
Compared to your own code, you may find a defect.
In particular, if you do not remove the bitmap being selected, you cannot discard it, so many bad things will happen.

class MyMEMDC:public CDC
{
    CBitMap_BitMap;// Main bitmap
    CDC*m_OwnerDC;//Original DC
    short m_Bmp_dmmy_data[16]; // for deselection
    CBitmap m_Bmp_dmmy; // For deselection
MyMEMDC()
{
    // Dummy bitmap
    m_Bmp_dmy.CreateBitmap(1,1,1,1,&m_Bmp_dmy_data);
}

voidReSize(
    CWnd*ex_owner,
    const CRect&ex_rc)
    {
        CDC::SelectObject(m_Bmp_dmy); // Remove the main bitmap currently being selected.
        m_BitMap.DeleteObject();// Discards the main bitmap.
        DeleteDC(); // Discard Memory DC
        m_OwnerDC=ex_owner->GetDC();// Obtain DC for Owner HWND
        CreateCompatibleDC(m_OwnerDC);// Build a Memory DC
        m_BitMap.CreateCompatibleBitmap (m_OwnerDC,ex_rc width and height) // Recreate the main bitmap
        CDC:: SelectObject(&m_BitMap);// Re-select the BitMap that you rebuilt.
    }
};

In addition, there may be cases where exclusive processing (drawing at external timing) is required during resizing.


2022-09-30 14:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.