Windows API Button Fails to Show (Resolved)

When creating a button using Windows API, the button successfully creates. I can see it in HandleSpy, and the handle matches the one given by Xojo, but the button never appears on the Window… any suggestions?

Function CreateButton() As Integer
  Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (dwExStyle As Integer, lpClassName As CString, lpWindowName As CString, dwStyle As Integer, x As Integer, y As Integer, nWidth As Integer, nHeight As Integer, hWndParent As Integer, hMenu As Integer, hInstance As Integer, lpParam As Integer) As Integer

  Const WS_CHILD = &H40000000

  Return CreateWindowEx(0, "Button", "A Button", WS_CHILD, 10, 10, 200, 25, Window1.Handle, 0, InstanceHandle, 0)
End Function

Function InstanceHandle() As Integer
  Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (lpModuleName As CString) As Integer
  
  Dim hMyInstance as Integer 
  
  hMyInstance = GetModuleHandle(App.ExecutableFile.Name)
  
  Return hMyInstance
End Function

@Norman Palardy @Joe Ranieri

Do you guys have a suggestion as to why Windows API created controls don’t appear? I need to have native listviews and datepickers appear dynamically with certain styles and properties and the Xojo method is more complex than if I use straight API to achieve the same ends.

The code above is a demo which should create a visible simple pushbutton, but doesn’t. I reverted to simpler demo to see what was going on when the datepickers and listviews failed to appear.

if I run the same apis from c++, the button appears on the Xojo window as it should from the Xojo code alone (using the handle specified by Xojo and the app.Instance handle).

The pushbutton is created when run from Xojo, as a handle to the button is returned…it just never visibly shows.

[quote=183570:@Matthew Combatti]@Norman Palardy @Joe Ranieri

Do you guys have a suggestion as to why Windows API created controls don’t appear? I need to have native listviews and datepickers appear dynamically with certain styles and properties and the Xojo method is more complex than if I use straight API to achieve the same ends.

The code above is a demo which should create a visible simple pushbutton, but doesn’t. I reverted to simpler demo to see what was going on when the datepickers and listviews failed to appear.

if I run the same apis from c++, the button appears on the Xojo window as it should from the Xojo code alone (using the handle specified by Xojo and the app.Instance handle).

The pushbutton is created when run from Xojo, as a handle to the button is returned…it just never visibly shows.[/quote]
Check the datepicker’s code in WFS, that might help you.

I can create controls just fine in c++… the code is not the issue. Something in Xojo is preventing the controls from displaying. They create just fine, only don’t display in the Xojo window class from Xojo. If I can figure out what’s going wrong with something as simple as a button, then the datepicker and listview won’t be an issue. Sadly, I could’ve probably rewritten the entire application already in C++ while waiting for an answer :slight_smile: I need to create the controls with API, because using Xojo alone I’ll have to hook into the controls and still use apis to get the desired styles to work. The code is for a client, so I may have to tell him to avoid investing in Xojo because he’s on a deadline, and I’ll just rewrite the code in pure c++, which seems to have no issues. I don’t want to disillusion him, that the task will be easier using Xojo, when using C++ is actually faster and far less of a mess at this point. (The last part I’ve been trying to avoid admitting :-p)

Try setting the position with the SWP_SHOWWINDOW flag:

[code] Declare Function SetWindowPos Lib “User32” (HWND As Integer, HWNDInsertAfter As Integer, X As Integer, Y As Integer, cX As Integer, cY As Integer, Flags As Integer) As Boolean
Const SWP_SHOWWINDOW = &h0040

Dim width, height, x, y, bHandle As Integer
bHandle = CreateButton()
Call SetWindowPos(bHandle, 0, x, y, width, height, SWP_SHOWWINDOW)[/code]

[quote=183658:@Andrew Lambert] Declare Function SetWindowPos Lib “User32” (HWND As Integer, HWNDInsertAfter As Integer, X As Integer, Y As Integer, cX As Integer, cY As Integer, Flags As Integer) As Boolean
Const SWP_SHOWWINDOW = &h0040

Dim width, height, x, y, bHandle As Integer
bHandle = CreateButton()
Call SetWindowPos(bHandle, 0, x, y, width, height, SWP_SHOWWINDOW)[/quote]

Holy cow! really? Didn’t even think to use SetWindowPos as C++ doesn’t require that since coordinates are defined in the CreateWindowEx API :-)… Thanks Andrew!

YAY! and AddHandler works perfect with the WinProcs :slight_smile:

Ran into an issue using SetWindowPos.

Instead,

Declare Function ShowWindow Lib “user32” (hwnd As Integer, nCmdShow As Integer) As Integer
Const SW_SHOWNORMAL = 1
Call ShowWindow(bHandle, SW_SHOWNORMAL)

should be used. In one instance of refreshing the ListView, the object would disappear. Using ShowWindow makes the object stay visible. Thereafter, SetWindowPos, is excellent for moving and sizing if needed. ShowWindow requires no coordinate parameters as the object was created, sized, and positioned in the initial creation command.

**On a side note, some Xojo controls become invisible occasionally upon window sizing in which Invalidate and Refresh do not work to repaint the object. Using the ShowWindow() api on the object does indeed negate this effect and ‘prevents’ it.

Figured I’d share for future developer search requests.