简体   繁体   English

如何在运行时将组合框更改为ownerDraw?

[英]How to change combobox to ownerdraw at runtime?

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_OWNERDRAW | CBS_HASSTRINGS but somehow this does not help. CBS_HASSTRINGS,但是以某种方式这没有帮助。

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. 我怀疑ownerdraw是需要在CreateWindow时设置的类似样式,并且以后可能无法修改。

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. 最好的选择是保存您关心的属性-控件ID,大小和位置-以及对话框中之前的HWND。 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. 最后,您将获得一个新的HWND,因此必须确保您的代码从那时起使用旧的而不是旧的。 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. 最后,使用SetWindowPos(hwndPrev ...)将新的HWND移动到Z顺序的正确位置,以便基于Z顺序的制表符和标签可以像使用新窗口一样使用新窗口。旧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM