Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Using GetWindowHandle() to make the window background transparent.  (Read 3872 times)

0 Members and 1 Guest are viewing this topic.

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Code: [Select]
result=SetWindowLongPtrA(handle,GWL_EXSTYLE,WS_EX_LAYERED|WS_EX_TOOLWINDOW);

if(result==0)
{
MessageBox(handle,"Error","Error",MB_OK);
}

SetWindowPos(handle,HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);


SetLayeredWindowAttributes(handle,RGB(255,0,255),0,LWA_COLORKEY);


Any ideas why this doesn't work on windows 7? The winapi documentation said something about "The SetWindowLongPtr function fails if the process that owns the window specified by the hWnd parameter is at a higher process privilege in the UIPI hierarchy than the process the calling thread resides in." Could this be a possible reason for not allowing transparency?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
Re: Using GetWindowHandle() to make the window background transparent.
« Reply #1 on: August 23, 2024, 08:34:03 am »
You should really be moving away from Windows 7 at this point... ;D

Looking at Texus' TransparentWindows implementation, it just uses SetLayeredWindowAttributes(hWnd, 0, alpha, LWA_ALPHA);.
I guess you'll have to find some additional explanation of these functions.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Using GetWindowHandle() to make the window background transparent.
« Reply #2 on: August 24, 2024, 05:38:17 am »
Oh, I have windows 11 but I want to make this compatible with windows 7 for several specific users that don't want to switch to anything newer.  I guess you don't know any answer for this.  Thanks anyways.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Using GetWindowHandle() to make the window background transparent.
« Reply #3 on: August 25, 2024, 05:10:18 pm »
Any ideas why this doesn't work
[...]
"The SetWindowLongPtr function fails if[...]
Could this be a possible reason for not allowing transparency?
Is SetWindowLongPtr function failing? You haven't mentioned that.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Using GetWindowHandle() to make the window background transparent.
« Reply #4 on: August 26, 2024, 05:52:36 pm »
Yes. It returns the value that says its failing but it still seems to work right for my computer. :P So...I don't know what's wrong with it.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
Re: Using GetWindowHandle() to make the window background transparent.
« Reply #5 on: August 26, 2024, 11:25:20 pm »
"The SetWindowLongPtr function fails if the process that owns the window specified by the hWnd parameter is at a higher process privilege in the UIPI hierarchy than the process the calling thread resides in."
This basically translates to, that the hWnd (window handle) needs to belong to the process of the calling thread. Meaning, you can't create a window in another process and retrieve said handle and then try to use it.
UIPI = User Interface Privilege Isolation, which is an additional restriction, which I don't know much more about.
I assume this is given here.

Try to retrieve the actual error with GetLastError
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Re: Using GetWindowHandle() to make the window background transparent.
« Reply #6 on: August 27, 2024, 09:02:54 am »
Also note that the SetWindowLongPtrA documentation contain the following notice about the return type:
Quote
If the previous value is zero and the function succeeds, the return value is zero, but the function does not clear the last error information. To determine success or failure, clear the last error information by calling SetLastError with 0, then call SetWindowLongPtr. Function failure will be indicated by a return value of zero and a GetLastError result that is nonzero.

So the function can return 0 even if there isn't an error. And only calling GetLastError to figure out if there was an error is unreliable (because it could still contain an error from an earlier win32 function call).

So based on the docs you would need something like the following:
SetLastError(0);
result = SetWindowLongPtrA(...);
if ((result == 0) && (GetLastError() != 0))
{
    // TODO: Print out what the value of GetLastError() is, so you can look up what the real error is
    MessageBox(handle,"Error","Error",MB_OK);
}

If that still fails, then make sure you know what GetLastError() returns, if you weren't doing anything weird with processes or threads then there is always the possibility that it is failing for some other reason that isn't explicitly mentioned in the docs.
TGUI: C++ SFML GUI

 

anything