Icon - Document/View model

Minimize the child window to redraw the icon.

2025-11-17

Written by: xiaobin

If you add a minimize button to your dialog/form, you will need the code below to draw the icon. For MFC applications using the document/view model, this is automatically done for you by the framework.

Meet the conditions

Minimize box: True
Style: Child

message map

    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()

query drag icon

afx_msg HCURSOR OnQueryDragIcon();
(HCURSOR)m_hIcon;

paint

Draw Icon:

    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);

        CRect rect;
        GetClientRect(&rect);

        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        dc.DrawIcon(x, y, m_hIcon);
    }

Ref