Using SubclassDlgItem to Change Control Types

I have a C ++ MFC application with a dialog box where I want to dynamically change the type of a control based on a selection in a combo box. The dialog resource starts with a simple old edit control, which I then call SubclassDlgItem on to switch to the custom control type.

So far so good. Now when the user changes the selection in another Combobox on the screen, I want to change this control to a different custom type. Thus, I destroy the existing control by calling delete on the pointer to the custom class for that control. Then I call :: CreateEx to re-create my edit control, and I call SubclassDlgItem again to create a new custom control.

My problem is that this flickers a little and I think I am getting an edit control created with :: CreateEx on top of my custom control. Any ideas on how to get rid of the flickering, especially if the user quickly changes the contents of the checkbox combo box?

+1


a source to share


2 answers


One of my colleagues suggested calling CWnd :: LockWindowUpdate () before I make this switch. So it boils down to something like this:

CRect r;
DWORD dwStyle = WS_CHILD|WS_TABSTOP|WS_VISIBLE;
m_pParent->GetDlgItem(m_nID)->GetWindowRect(&r);
m_pParent->ScreenToClient(r);
m_pParent->LockWindowUpdate();
m_pParent->InvalidateRect(r);
delete m_pCust;   // Delete the old custom control
m_pCust = NULL;
::CreateWindowEx(0, "EDIT", "", dwStyle, r.left, r.top, r.Width(), r.Height(), m_pParent->m_hWnd, (HMENU)m_nID, AfxGetInstanceHandle(), NULL);
m_pCust = new CustomCtrl();
pCust->SubclassDlgItem(m_nID, m_pParent);
m_pParent->UnlockWindowUpdate()

      



There is a bit more to do with what my user control does. I ended up calling m_pParent-> InvalidateRect (r) to get my control to draw correctly at the end.

Also, it turns out that the :: CreateEx control overlap is that I was calling UnsubclassDlgItem before removing the old custom control

0


a source


You can create a set of all possible controls in the same area of ​​the parent window and show only the corresponding one and hide all others. When the user triggers a control change, you hide the active control and show the new relevant one. It should look smooth.



+3


a source







All Articles