1

I have a button and an EDIT window i named "textbox" initially. What i want to achieve is: when i press the button, the EDIT window's border will be remove and it's text is also changed. Here is how i initial them:

HWND textbox; //global variable

//in WM_CREATE:
    CreateWindowEx(NULL, L"BUTTON", L"Remove border",
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 10, 10, 150, 40, hwnd,
        (HMENU)IDC_TEXTBOX, NULL, NULL);

    textbox = CreateWindowEx(
        NULL, L"EDIT", NULL,
        WS_CHILD | WS_BORDER | WS_VISIBLE | ES_MULTILINE,
        100, 100, 200, 100, hwnd, (HMENU) 0, NULL, NULL
    );

    SetWindowText(textbox, L"the initial text");

Since there is WS_BORDER in its style at the start, i thought removing it from window style will remove the border so this is my first attempt:

//In WM_COMMAND
    case IDC_TEXTBOX: //if button is pressed
        lStyle = GetWindowLongPtr(textbox, GWL_STYLE);
        lStyle &= ~(WS_CHILD | WS_VISIBLE | ES_MULTILINE);
        SetWindowLongPtr(textbox, GWL_STYLE, lStyle);

        SetWindowPos(textbox, NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE |
            SWP_NOZORDER | SWP_NOOWNERZORDER);

        SetWindowText(textbox, L"how to remove the border around this text???");
        break;

The code above didn't work. The window disappeared after i pressed the button. In the second attempt i followed the answer in this question:

case IDC_TEXTBOX:
        lStyle = GetWindowLongPtr(textbox, GWL_STYLE);
        lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU);
        SetWindowLongPtr(textbox, GWL_STYLE, lStyle);

        lExStyle = GetWindowLongPtr(textbox, GWL_EXSTYLE);
        lExStyle &= ~(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);
        SetWindowLongPtr(textbox, GWL_EXSTYLE, lExStyle);

        SetWindowPos(textbox, NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE |
            SWP_NOZORDER | SWP_NOOWNERZORDER);

        SetWindowText(textbox, L"how to remove the border around this text???");
        break;

This time, the text has changed but the border is still around after pressing the button: Border is still around

so the question is: What did i do wrong in the 1st and 2nd attempt? And what should i do to remove the EDIT window border?

Halsey
  • 119
  • 1
  • 8
  • The `lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU)` removes all the listed styles, not keeps them. This is because in your linked question they wanted to remove the entire nonclient area. You want `lStyle &= ~WS_BORDER`. – GSerg Jun 05 '21 at 10:42
  • See [Enabling Visual Styles](https://learn.microsoft.com/en-us/windows/win32/controls/cookbook-overview) and revisit, whether the question you asked is really about the problem you need to solve. – IInspectable Jun 05 '21 at 10:46
  • You have to redraw the window/dialog after removing the style. Use `InvalidateRect`. – i486 Jun 05 '21 at 11:11
  • @i48 No, you don't need that. Calling `SetWindowPos` is the only requirement. – IInspectable Jun 05 '21 at 12:17
  • @GSerg thanks for your clarification. I applied the change you mentioned but the problem still remains. The border is still there – Halsey Jun 05 '21 at 14:35
  • 1
    @IInspectable I have read [Enabling Visual Styles](https://learn.microsoft.com/en-us/windows/win32/controls/cookbook-overview#using-comctl32dll-version-6-in-an-application-that-uses-only-standard-extensions) and also about [Visual Styles](https://learn.microsoft.com/en-us/windows/win32/controls/themes-overview). I failed to see the connection between it and my question and i need your guidance. Visual styles is about appearance of common controls and so how does it relate to EDIT control's border? – Halsey Jun 05 '21 at 14:48
  • 2
    Many of the system controls cache their initial styles and never update them. Looks like the border flag is one of them. Probably your best option is to create it without a border and then draw a border around it yourself if you want one. – Jonathan Potter Jun 05 '21 at 20:52
  • As Jonathan said, some system controls only cache their styles when they are initialized. If you want to modify it, you need to recreate the control or draw the border yourself. – Zeus Jun 07 '21 at 02:53

1 Answers1

0

I would have made this a comment under your answer, but I don't have enough reputation.

Others in the comments have said that "Many of the system controls cache their initial styles and never update them" but strangely you can add a border after creation but just not remove it. So it seems that the border style is actually not cached.

My current solution is to recreate the control. Even though I can live with this solution (at the moment), I do not like it.

-Edit-

Never mind the above. I found that adding WS_BORDER after creation is actually a different border than the border added with WS_BORDER at creation. When you create the edit control with WS_BORDER and add WS_BORDER to it again with SetWindowLong() then the edit control now has 2 visible borders appearing as a single 2 pixel border. The WS_BORDER added after creation can be removed after creation but the initial WS_BORDER added at creation cannot be removed. So it appears that the initial edit border actually does get cached.