I am using C++/CLI to make a WPF interface for an otherwise native DLL.
I want to create a window that can only be minimized, not closed. I have searched and found that the only way to do it is through WIN32 calls. So, I am using the following code.
// window_ is a Window ^ initialized from XAML and checked against nullptr
// its WindowStyle is SingleBorderWindow
System::Windows::Interop::WindowInteropHelper helper(window_);
IntPtr winhandle = helper.Handle;
void * winhandleptr = winhandle.ToPointer();
HWND hWnd = static_cast<HWND>(winhandleptr);
SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) & ~WS_SYSMENU);
but winhandle gets a value of 0. The SetWindowLong() call fails with the invalid handle.
What am I doing wrong here?
Works fine when I try it. The window looks like this:
The only failure mode I can think of is that you called your code too soon, before the native window is created. You’ll need the Loaded event, this is mine:
Needs more work. No minimize button, closing the window with Alt+F4 still works.