1

I'd like to implemented a window that its top coordinate is always X (for simplicity let's say 0). Meaning, the window's top side is fixed on 0 which is the top of the screen.

I've already implemented the window, set its position using SetWindowPos but I'm struggling maintaining its top coordinate value.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
idanshmu
  • 5,061
  • 6
  • 46
  • 92
  • Is this for a child window? If you show what you have already done it would be easier to understand. – Barmak Shemirani Apr 06 '16 at 16:04
  • Just a simple exe the create a window using `CreateWindowEx`. alot of utility classes are involved in the window creation so it's hard to post all the relevant code. I'm hopefully looking for a simple way of manipulating existing `HWND` to accomplish that. – idanshmu Apr 06 '16 at 18:02
  • You mean you don't want the user to be able to move the window? (this requires removing the caption and implementing a custom resize) - or are you looking for `SetWindowPos(hwnd, 0, 0, 0, width, height, SWP_NOMOVE)` which will resize the window but it won't change the top-left corner value. – Barmak Shemirani Apr 06 '16 at 18:05
  • I want the user to be able to resize the window but the top side must be at the same coordinate. left, right & bottom sides may move – idanshmu Apr 06 '16 at 18:08
  • 1
    Is [this](https://blogs.msdn.microsoft.com/oldnewthing/20150504-00/?p=44944) what you are looking for? – andlabs Apr 06 '16 at 18:24

1 Answers1

1

You can create a window with no caption bar, for example

CreateWindow(className, title, WS_THICKFRAME | WS_POPUP, ...)

Then override WM_NCHITTEST to change the requests for moving the window up and down.

If window has caption bar, for example:

CreateWindow(className, title, WS_OVERLAPPEDWINDOW, ...)

Then add override for WM_WINDOWPOSCHANGING as well:

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {

    case WM_NCHITTEST:
    {
        LRESULT lresult = DefWindowProc(hwnd, msg, wParam, lParam);
        switch (lresult)
        {
        case HTTOP:      lresult = HTCLIENT; break;
        case HTTOPLEFT:  lresult = HTLEFT;   break;
        case HTTOPRIGHT: lresult = HTRIGHT;  break;
        }
        return lresult;
    }

    case WM_WINDOWPOSCHANGING:
    {
        WINDOWPOS* wndpos = (WINDOWPOS*)lParam;
        wndpos->y = 100;//choose a fixed position
        break;
    }

    ...
    }

    return DefWindowProc(hwnd, msg, wParam, lParam);
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77