-2

I'm making a program, with c++ and winapi, where you can click a button, then select a window and it changes the transparency of that window.

I have no problems of making the window transparent, but i can't get the hwnd of a window.

When I use this function the program just crashes (No errors are being printed to the console).

HWND getHWND(HWND self)
{
  HWND found = GetForegroundWindow();
  if(found == self || found == NULL)
    return getHWND(self);
  return found;
}

I tried using this, but I don't like it, because the user has only 2 seconds to click on another window. (I'know I could use a bigger delay, but then it might be to much.. so this isn't optimal)

HWND getHWND()
{
  Sleep(2000);
  return GetForegroundWindow();
}
z32byTe
  • 13
  • 1

1 Answers1

3

Don't poll for the active window, especially not with a recursive function. Just let the OS notify you when a window is clicked.

When the user clicks on your button, you can use SetCapture(), or a mouse hook via SetWindowsHookEx(), to direct subsequent mouse clicks to your app even if they are clicking on someone else's window. When you detect a click, release the capture/hook, and then use WindowFromPoint() to get the HWND at the location of the click.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I am using HWND handle and I am successfully getting the handle of a specific window but when I Try to display that window only black screen appears. Can you please tell me what could be the issue and how to solve that. – Abdul ahad Nov 11 '21 at 10:31