I’d like to change a combobox control to owner-draw at runtime. In the resource script the control is layed out as a standard control.
I’ve experimented with setting the style bits to CBS_OWNERDRAW | CBS_HASSTRINGS but somehow this does not help.
Before doing subclass, set window style..
// turn to ownerdraw
DWORD dwStyle = ::GetWindowLong(hwnd, GWL_STYLE);
dwStyle |= CBS_OWNERDRAWVARIABLE | CBS_HASSTRINGS;
SetWindowLong(hwnd, GWL_STYLE, dwStyle);
Does anybody know the trick?
Not all styles can be successfully changed at runtime after window creation – for example, even though it’s controlled via styles, you can’t change a wrapping multiline edit to a wrapping multiline edit at runtime; you need to create a whole new edit control, which is what Notepad does. I suspect that ownerdraw is a similar style that needs to be set at CreateWindow time and likely cannot be modified afterwards.
Your best bet is to save the properties you care about – control ID, size and location – and also the HWND that was before it in the dialog. Destroy the old control and create a new identical replacement – but with the style you want. You’ll end up with a new HWND, so will have to ensure your code uses that instead of the old one from then on. And if the old control had keyboard focus when you deleted it, you should give the new control keyboard focus also so that focus doesn’t just ‘disappear’. Finally, use SetWindowPos(hwndPrev…) to move the new HWND into the right place in the Z-order so that tabbing and labels – which are based on Z-order – work with the new window the same way they did with the old.