1

I'm a beginner and today is my first day in learning to create Windows application. I have two buttons.

#define BUTTON_SW 1
#define BUTTON_SW2 2
HWND Button1;
HWND Button2;
Button1 = CreateWindow("button", "Enter", WS_VISIBLE | WS_CHILD, 215, 10, 40, 25, hwnd, HMENU(BUTTON_SW), NULL, NULL);   
Button2 = CreateWindow("button", "You'll be gone", WS_VISIBLE | WS_CHILD, 260, 10, 95, 25, hwnd, HMENU(BUTTON_SW2), NULL, NULL);

When Button1 is clicked, how can I hide Button2 or make it lose its WS_VISIBLE flag and reflect current situation correctly, like this?

LONG style = GetWindowLong(Button2,GWL_STYLE);
style = style | WS_VISIBLE; // style = style & ~WS_VISIBLE
SetWindowLong(Button2,GWL_STYLE,style);

This works very well. However, once WS_VISIBLE flag is assigned, button still stays invisible until the first mouseclick on it. Vice versa, when I use style = style & ~WS_VISIBLE; once WS_VISIBLE flag is removed, button becomes passive (unclickable) but stays visible.

How to fix this? Tried many things that I've found online but couldn't fix it. Also please don't suggest me to buy a decent book, I don't have the money for now.

(P.S: ShowWindow function with SW_HIDE/SW_SHOW somehow doesn't work for me, perhaps I'm using it wrong. Can you help me on how I can hide this Button2 correctly? I'm trying the following command, but nothing happens.)

ShowWindow(GetDlgItem(Button2, 2), SW_HIDE);

@Edit: I've noticed that when I minimize the app and maximize it again, the state of button updates. But how will it automatically update the state?

2 Answers2

0

In order to a window to reflect changes you must ask the OS to do it.

Learn about RedrawWindow and the invalidated area.

Note that some actions, like resizing or restoring from minimized, automatically makes the OS to invalidate the region and redraw it.

Use:

RedrawWindow(Button2,NULL,NULL,RDW_INVALIDATE | RDW_INTERNALPAINT);
Ripi2
  • 7,031
  • 1
  • 17
  • 33
  • Can you please show a small example how to redraw that `Button1`? Or should I redraw whole window? –  Apr 06 '20 at 18:09
  • And can you tell my why `ShowWindow(GetDlgItem(Button2, 2), SW_HIDE);` does nothing? –  Apr 06 '20 at 18:14
  • 1
    Read [this Question and its Answers](https://stackoverflow.com/q/2325894/3871028). You need to understand that some commands accumulate before a WM_PAINT message is fired. – Ripi2 Apr 06 '20 at 18:17
  • Even `RedrawWindow()` with also `RDW_UPDATENOW` flag didn't redraw the current state. I still need to minimize and restore it back to see that the button is gone. –  Apr 06 '20 at 18:28
  • I also tried using `InvalidateRect()` but the result is the same :( –  Apr 06 '20 at 18:30
  • 1
    I don't know the rest of your code, perhaps you have a problem there. Any how, I advice reading [MS docs Painting and Drawing](https://learn.microsoft.com/es-es/windows/win32/gdi/about-painting-and-drawing?redirectedfrom=MSDN) – Ripi2 Apr 06 '20 at 18:41
  • Phew... After quite a long reading, I've managed to fix it. Thanks for your help @Ripi2 :) –  Apr 06 '20 at 19:23
0

This should work

ShowWindow(Button2, SW_HIDE);

or

ShowWindow(GetDlgItem(DialogHWND, BUTTON_SW2), SW_HIDE);

GetDlgItem needs HWND of the parent window (dialog) as first argument.

Daniel Sęk
  • 2,504
  • 1
  • 8
  • 17
  • Thank you, after reading many things, I've found that myself. But this is simply the most accurate answer. –  Apr 08 '20 at 09:14