0

This looks like a duplicate but hear me first. This is more on the debugging side. I'm trying to remove the borders of my window using the method here.

What are some things that will make these functions not work? Hiding windows using ShowWindow(Handle, SW_HIDE) doesn't work also. I've made my own Window class with many functions so I don't wanna paste my whole code here.

Here's my Initialization function for the window:

HRESULT SampleWindow::InitializeSimple(SampleWindow* win)
{
    HRESULT         hr;
    HWND            hWnd;
    SampleWindow*   sampleWin;

    sampleWin = new SampleWindow();

    aMWindowProps->Center();
    hWnd = CreateWindowEx(
        NULL,
        aMWindowProps->aWindowClass,
        aMWindowProps->aWindowTitle,
        WS_OVERLAPPEDWINDOW,
        aMWindowProps->aRealLeft,
        aMWindowProps->aRealTop,
        aMWindowProps->GetRelativePosWidth(),
        aMWindowProps->GetRelativePosHeight(),
        HWND_DESKTOP,
        NULL,
        *aMWindowProps->aHInstance,
        sampleWin);

    aMWindowProps->aHwnd = &hWnd;

    hr = hWnd ? S_OK : E_FAIL;

    win->aHwnd = &hWnd;
    //ShowWindow(hWnd, SW_SHOWNORMAL);

    return hr;
}

WindowProps as you can see contains various info about the window being created. I also have a HWND pointer variable in my class which points to the window handler.

Here are some things I've tried on my main, where sw2 is a pointer to my window class:

    ShowWindow(*sw2->aHwnd, SW_SHOW);
    //ShowWindow(*sw2->aHwnd, nCmdShow);
    LONG lStyle = GetWindowLong(*sw2->aHwnd, GWL_STYLE);
    lStyle &= WS_POPUP;
    //lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU);
    SetWindowLong(*sw2->aHwnd, GWL_STYLE, lStyle);
    //ShowWindow(*sw2->aHwnd, SW_MINIMIZE);
    //ShowWindow(*sw2->aHwnd, SW_HIDE);
    //ShowWindow(*sw2->aHwnd, SW_HIDE);
    //SetWindowLong(*sw2->aHwnd, GWL_STYLE, GetWindowLong(*sw2->aHwnd, GWL_STYLE) && ~ WS_BORDER && ~ WS_SIZEBOX && ~ WS_DLGFRAME);
    SetWindowPos(*sw2->aHwnd, HWND_TOP, sw2->aMWindowProps->aRealLeft, sw2->aMWindowProps->aRealTop, sw2->aMWindowProps->aRealWidth, sw2->aMWindowProps->aRealHeight, SWP_FRAMECHANGED);
    //LONG lExStyle = GetWindowLong(*sw2->aHwnd, GWL_EXSTYLE);
    //lExStyle &= ~(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);
    //SetWindowLong(*sw2->aHwnd, GWL_EXSTYLE, lExStyle);
    //SetWindowPos(*sw2->aHwnd, NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER);

I'd just like some suggestions on where to debug my code. I know the functions work as I've tested it on a much simpler window project (sample project from Microsoft).

Community
  • 1
  • 1
kir
  • 581
  • 1
  • 6
  • 22
  • Are you sure you want the parent window to be `HWND_DESKTOP`? I'd try without that. – Retired Ninja Oct 24 '13 at 06:24
  • Thanks for the suggestion. Changed that line to NULL but didn't work. – kir Oct 24 '13 at 06:28
  • 5
    Your `InitializeSample()` function is fundamentally flawed, you are returning a pointer to a variable on the stack (`hWnd`) which will probably be no longer valid once your function returns. – Jonathan Potter Oct 24 '13 at 06:36

1 Answers1

1

As Jonathan Potter already pointed out in his comment, your mistake is:

aMWindowProps->aHwnd = &hWnd;

hr = hWnd ? S_OK : E_FAIL;

win->aHwnd = &hWnd;

where HWND hWnd is only valid in the scope of the current methode. I guess that you defined aHwnd in your class as something like:

HWND *aHwnd;

which is at least unnecessary. You seem to mistake a HANDLE (a HWND is nothing else) as a kind of instance/object itself. It isn't, it is more like a pointer or reference. You can always safely write:

HWND myAttribute=hWnd;

As long as you use it in the same process. (Window handles are even valid across process boundaries, but do not tell anyone about that).

I fact I know no situation where you keep a pointer to a handle instead of the handle itself.

Notice, I explicit wrote about handles, as HWND are a kind of standard window handles.

Martin Schlott
  • 4,369
  • 3
  • 25
  • 49
  • Thank you. I was messing around with pointers to get a mastery of it. This mistake is a one step forward. How would you recommend handling HINSTANCE? As you can see I have a pointer to the original also. Should I treat this the same as a HANDLE? – kir Oct 25 '13 at 08:11
  • @user2280704 yes. A HINSTANCE is also a kind of HANDLE. – Martin Schlott Oct 25 '13 at 08:51