Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Create window without border and transparent background?

This thread is locked; no one can reply to it. rss feed Print
Create window without border and transparent background?
geograman
Member #16,048
August 2015

Hey hackers,

Is it somehow possible to create a always on top window without borders and transparent background?

Would like to use it as an overlay.

Thanks :)

torhu
Member #2,727
September 2002
avatar

A window that fills the whole screen and has no borders is possible, but for the other two I think you need to use the OS' API. Just google it.

Relevant Allegro documentation:
http://alleg.sourceforge.net/a5docs/5.0.11/display.html#al_set_new_display_flags
http://alleg.sourceforge.net/a5docs/5.0.11/platform.html

Edgar Reynaldo
Member #8,592
May 2007
avatar

Under Windows, you can do it this way :

#SelectExpand
1 2 al_set_new_display_flags(ALLEGRO_FRAMELESS | ALLEGRO_WINDOWED); 3 4 ALLEGRO_DISPLAY* display = al_create_display(w,h); 5 6 HWND winhandle = al_get_win_window_handle(display); 7 8 SetWindowPos(winhandle , HWND_TOPMOST , 0 , 0 , -1 , -1 , SWP_NOMOVE | SWP_NOSIZE); 9 10 BringWindowToTop(winhandle); 11 12 COLORREF trans_color = RGB(0,0,0); 13 14 if (0 == SetWindowLong(winhandle , GWL_EXSTYLE , WS_EX_LAYERED)) { 15 printf("Couldn't set WS_EX_LAYERED style attribute\n"); 16 } 17 18 if (!SetLayeredWindowAttributes(winhandle , trans_color , 255 , LWA_COLORKEY)) { 19 printf("Couldn't set color key!\n"); 20 }

geograman
Member #16,048
August 2015

Thanks for the quick answers!

I tried your code Edgar Reynaldo, but unfortunately the window background is still black...

Is there another thing I need to have a look at?

torhu
Member #2,727
September 2002
avatar

Edgar Reynaldo
Member #8,592
May 2007
avatar

You have to actually still draw the color key's color to the window to make transparent areas. Which means drawing to the HDC of the transparent window you are using. Allegro doesn't seem to do this correctly so in my case I used a Device Independent Bitmap in 32 bit depth to do the drawing and then BitBlt that to my window HDC.

And 255 is fine, that is the opacity level overall. It is separate from the color key, which is the color that indicates which areas are transparent.

geograman
Member #16,048
August 2015

I changed the 255 to 0 but it did not change the background.

Then I changed the windows design from standard Aero Design to a Basic one...

I have read somewhere that this could help and it did.

Nevertheless...of course it's no option for the user to change their windows design for my app... their must be another way.

Edgar Reynaldo
Member #8,592
May 2007
avatar

You must have just missed my post.

An alpha of 0 means the whole window would be fully transparent. And you couldn't see it.

Use 255, and then paint with the color key to get transparent areas.

Edit
Ie. try this (it should draw a white box on your screen) :

#SelectExpand
1COLORREF colorkey = RGB(0,0,0); 2HBRUSH hbr = CreateSolidBrush(RGB(255,255,255)); 3HBRUSH hbr2 = CreateSolidBrush(colorkey); 4RECT winrect; 5winrect.left = 0; 6winrect.right = al_get_display_width(display); 7winrect.top = 0; 8winrect.bottom = al_get_display_height(display); 9 10HDC winhdc = GetDC(winhandle); 11FillRect(winhdc , &winrect , hbr); 12 13winrect.left += al_get_display_width(display)/4; 14winrect.right -= al_get_display_width(display)/4; 15winrect.top += al_get_display_height(display)/4; 16winrect.bottom -= al_get_display_height(display)/4; 17 18FillRect(winhdc , &winrect , hbr2); 19 20DeleteObject(hbr); 21DeleteObject(hbr2); 22 23ReleaseDC(winhdc);

geograman
Member #16,048
August 2015

Mhhh... I am probably doing something very wrong.
I am only getting a white rectangle with a black smaller one within. (screen attached)

This is my code:

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_primitives.h> 4#include <allegro5/allegro_image.h> 5#include <allegro5/allegro_native_dialog.h> 6#include <allegro5/allegro_windows.h> 7 8int main() { 9 10 ALLEGRO_DISPLAY *display = NULL; 11 12 if (!al_init()) { 13 fprintf(stderr, "Couldn't initialize allegro!\n"); 14 return -1; 15 } 16 17 if (!al_init_primitives_addon()) { 18 fprintf(stderr, "Couldn't initialize primitives addon!\n"); 19 return -1; 20 } 21 22 al_set_new_display_flags(ALLEGRO_FRAMELESS | ALLEGRO_WINDOWED); 23 24 display = al_create_display(600, 600); 25 if (!display) { 26 fprintf(stderr, "Couldn't create allegro display!\n"); 27 return -1; 28 } 29 30 al_clear_to_color(al_map_rgb(0, 0, 0)); 31 32 HWND winhandle = al_get_win_window_handle(display); 33 34 SetWindowPos(winhandle, HWND_TOPMOST, 0, 0, -1, -1, SWP_NOMOVE | SWP_NOSIZE); 35 36 BringWindowToTop(winhandle); 37 38 COLORREF trans_color = RGB(0, 0, 0); 39 40 if (0 == SetWindowLong(winhandle, GWL_EXSTYLE, WS_EX_LAYERED)) { 41 printf("Couldn't set WS_EX_LAYERED style attribute\n"); 42 } 43 44 if (!SetLayeredWindowAttributes(winhandle, trans_color, 255, LWA_COLORKEY)) { 45 printf("Couldn't set color key!\n"); 46 } 47 48 //al_draw_line(100, 100, 400, 400, al_map_rgb(255, 0, 0), 3); 49 //al_draw_filled_rectangle(10, 10, 120, 120, al_map_rgb(255, 255, 255)); 50 51 COLORREF colorkey = RGB(0, 0, 0); 52 HBRUSH hbr = CreateSolidBrush(RGB(255, 255, 255)); 53 HBRUSH hbr2 = CreateSolidBrush(colorkey); 54 RECT winrect; 55 winrect.left = 0; 56 winrect.right = al_get_display_width(display); 57 winrect.top = 0; 58 winrect.bottom = al_get_display_height(display); 59 60 HDC winhdc = GetDC(winhandle); 61 FillRect(winhdc, &winrect, hbr); 62 63 winrect.left += al_get_display_width(display) / 4; 64 winrect.right -= al_get_display_width(display) / 4; 65 winrect.top += al_get_display_height(display) / 4; 66 winrect.bottom -= al_get_display_height(display) / 4; 67 68 FillRect(winhdc, &winrect, hbr2); 69 70 //DeleteObject(hbr); 71 //DeleteObject(hbr2); 72 73 //al_flip_display(); 74 al_rest(5.0); 75 al_destroy_display(display); 76 return 0; 77}

Edgar Reynaldo
Member #8,592
May 2007
avatar

Your code works for me on Vista using MinGW 4.8.1. It shows a white box with a cutout in the middle where it shows through.

Windows does some funny things with GDI. It may not be writing the exact color you need to the window and so then it doesn't match the colorkey. Try using GetPixel to check the value of the pixel and whether it matches the color key or not.

Go to: