I’ve been working on creating a cross-platform GUI library, and I’ve run into an interesting snag. To make the interface nice, I’m trying to make it possible to create a button without a parent and then assign it a parent after the constructor has finished. I’ve gotten the code to mostly work, but clicking on the button causes the parent window to lose focus.
I’m creating the button using CreateWindowEx() and giving it the style WS_POPUP | BS_PUSHBUTTON and a null parent. The reason I’m using WS_POPUP is to avoid the title bar size from affecting the button’s dimensions; without it, the button is squashed. After creating the button, I use SetParent() and replace the WS_POPUP flag with WS_CHILD.
I’ve been working on creating a cross-platform GUI library, and I’ve run into an
Share
The Windows API windows are not this flexible. As documented on MSDN you can’t change flags manually except by using an API call (e.g.
ShowWindow()to changeWS_VISIBLE). Why don’t you just delay the call toCreateWindowEx()until your library button object is assigned to a parent window?WS_POPUP means you want to create what a layman might call a window. In other words, a top-level frame that can contain widgets (WS_CHILD windows). The window you then set as the parent probably loses focus as button appears to the systm as a separate top-level window despite you changing the flags to WS_CHILD.
There is more to a windows behaviour than its currently set flags. If Windows is designed sensibily, the two types of window (
WS_POPUPandWS_CHILD) will be implemented by two completely different object under the hood, chosen depending on the flags passed to CreateWindowEx. Changing a flag isn’t going to magically morph the underlying object from one type to another.If the MSDN API docs don’t explicitly say that a flag is intended to be manipulated directly and there isn’t an API call to handle the change for you, then that aspect of a window’s identity is immutable.