Create semi-transparent panel with C++ and WINAPI - c++

I have a window with a background image and I want to create a panel (like in .NET) with a white border and a semi-transparent background RGBA(255, 255, 255, 124), so the image in the background could be also visible. Should I use STATIC control or a child window for that? I know that in order to set the RGBA color of a window I have to use SetLayeredWindowAttributes, but I don't know if that would work with STATIC control.
What type of control is used normally for this and what WINAPI functions could I use to set the border thickness and color?
Thanks
SOME CODE:
// Create the static control (inside the WM_CREATE of the parent window)
hPanel = CreateWindowEx(
WS_EX_TRANSPARENT,
L"STATIC",
L"",
WS_CHILD | WS_VISIBLE | SS_WHITEFRAME,
10, 90,
200, 120,
hWnd,
(HMENU)IDC_PANEL,
hInstance,
NULL);
// Trying to set the alpha level
SetWindowLong(hPanel , GWL_EXSTYLE,
GetWindowLong(hPanel , GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(hPanel , 0, (255 * 70) / 100, LWA_ALPHA);
// Changing the background color of the STATIC in WM_CTLCOLORSTATIC
case WM_CTLCOLORSTATIC:
hStatic = (HDC)wParam;
switch (GetDlgCtrlID((HWND)lParam)) {
case IDC_PANEL:
SetTextColor(hStatic, RGB(255, 255, 255));
SetBkColor(hStatic, RGB(26, 127, 231));
break;
default:
SetTextColor(hStatic, RGB(255, 255, 255));
SetBkMode (hStatic, TRANSPARENT);
}
return (LRESULT)GetStockObject(NULL_BRUSH);
break;
So, the only thing I get with this code is a transparent bordered control. I don't get the background color and the alpha effect.

Related

How to adjust window border size

I have a window, which I want it to behave like a toggle button. Once clicked it will add 4px border and clicking after will make the border disappear. I figured how to make the window behave like a toggle button using BS_PUSHLIKE and Button_SetCheck() but can't seem to figure out how to adjust the border size for this window.
Thanks to all who take their time to help
Maybe you can use MoveWindow to resize the window, and then draw the border yourself, like this,
Draw a borderless window first:
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
100, 100, 800, 600, nullptr, nullptr, hInstance, nullptr);
LONG lStyle = GetWindowLong(hWnd, GWL_STYLE);
lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU);
SetWindowLong(hWnd, GWL_STYLE, lStyle);
Then handle the window border in the WM_LBUTTONDOWN message:
int num = 0;
case WM_LBUTTONDOWN:
{
RECT rcWind;
HDC dc = GetDC(hWnd);
GetWindowRect(hWnd, &rcWind);
if (num >= 0)
{
num--;
RECT rcClient;
MoveWindow(hWnd, rcWind.left - 4, rcWind.top - 4, 8 + rcWind.right - rcWind.left, 8 + rcWind.bottom - rcWind.top, TRUE);
GetClientRect(hWnd, &rcClient);
HPEN hPen = CreatePen(PS_SOLID, 4, RGB(255, 128, 1));
HGDIOBJ hOldPen = SelectObject(dc, hPen);
Rectangle(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
DeleteObject(hPen);
}
else if (num < 0)
{
MoveWindow(hWnd, rcWind.left + 4, rcWind.top + 4, rcWind.right - rcWind.left - 8, rcWind.bottom - rcWind.top - 8, TRUE);
num++;
}
}
break;

My specified font size does not apply when i install the program in windows7

I specify a font and its size for my listview items using win32 api and it works properly in Windows Xp. I install it in Windows 7 and see size of fonts are too small and hard to read although i specified 17 for its size.
I increased default font sizes in Windows 7 but still the fonts in my program are too small.
This is the code that i specify font for the listview items inside Window procedure :
case WM_DRAWITEM:
{
LPDRAWITEMSTRUCT pDIS=(LPDRAWITEMSTRUCT)lParam;
HDC hDC=pDIS -> hDC;
RECT rc = pDIS -> rcItem;
HBRUSH bg = (HBRUSH) (::GetStockObject(DC_BRUSH));
HPEN pn=(HPEN)(::GetStockObject(NULL_PEN));
::SelectObject( hDC , bg );
::SelectObject( hDC , pn );
::SetTextColor( hDC , RGB(0,0,0));
HFONT hF;
hF=CreateFont(17, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Tahoma");
HFONT hOldFont = (HFONT) SelectObject(hDC, hF);
if( (pDIS->itemID % 2) != 0 )
::SetDCBrushColor(hDC, RGB(255,255,255));
else{
::SetDCBrushColor(hDC, RGB(223, 241, 255));
}
::Rectangle( hDC , rc.left , rc.top , rc.right , rc.bottom );
char buffer[1000] = {0};
ListView_GetItemText(pDIS -> hwndItem, pDIS -> itemID, 0, (LPWSTR)buffer, 1000);
::DrawText(hDC, (LPWSTR)buffer, -1, &rc, DT_SINGLELINE | DT_VCENTER);
SelectObject(hDC, hOldFont);
DeleteObject(hF);
}
break;
How can i make Windows display my desired font size and not that small font?
Thanks!
Use SystemParametersInfo to find the default font as follows:
NONCLIENTMETRICS metrics;
metrics.cbSize = sizeof(NONCLIENTMETRICS);
::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS),
&metrics, 0);
hfont = CreateFontIndirect(&metrics.lfMessageFont);
Use .lfMessageFont for ListView and other child controls.
This will retrieve the correct font name and font size.
This font size is already adjusted for DPI settings of system and application.
You can create this font once during windows creation. Then assign it as the main font for the window and listview control. This will update the header control for the listview.
HFONT hfont;
...
case WM_CREATE:
{
if (!hfont)
{
NONCLIENTMETRICS metrics;
metrics.cbSize = sizeof(NONCLIENTMETRICS);
::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS),
&metrics, 0);
hfont = CreateFontIndirect(&metrics.lfMessageFont);
}
hListView = CreateWindow(WC_LISTVIEW ...);
SendMessage(hWnd, WM_SETFONT, (WPARAM)hfont, MAKELPARAM(TRUE, 0));
SendMessage(hListView, WM_SETFONT, (WPARAM)hfont, MAKELPARAM(TRUE, 0));
...
break;
}
Also, do not use (LPWSTR) cast to hide compiler warnings and errors. Use casting only when you are sure it's applicable. In this case, char and wchar_t are very different storage types, casting may in some special cases, but it's far from reliable.
char buffer[1000] creates a buffer of size 1000 bytes. But you are making a call to ListView_GetItemText to read 1000 Unicode characters, which in this case is 2000 bytes and results in buffer overrun. You can change as follows:
case WM_DRAWITEM:
{
LPDRAWITEMSTRUCT pDIS = (LPDRAWITEMSTRUCT)lParam;
HDC hDC = pDIS->hDC;
RECT rc = pDIS->rcItem;
COLORREF textcolor = RGB(0, 0, 0);
COLORREF bkcolor = RGB(255, 255, 255);
if((pDIS->itemID % 2) == 0)
{
bkcolor = RGB(223, 241, 255);
}
if(pDIS->itemState & ODS_SELECTED)
{
textcolor = RGB(255, 255, 255);
bkcolor = RGB(0, 0, 255);
}
SetDCBrushColor(hDC, bkcolor);
SelectObject(hDC, GetStockObject(DC_BRUSH));
SelectObject(hDC, GetStockObject(NULL_PEN));
SetTextColor(hDC, textcolor);
Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
auto oldfont = SelectObject(hDC, hfont);
wchar_t buffer[1000] = { 0 };
ListView_GetItemText(pDIS->hwndItem, pDIS->itemID, 0, buffer, 1000);
DrawText(hDC, buffer, -1, &rc, DT_SINGLELINE | DT_VCENTER);
SelectObject(hDC, oldfont);
return TRUE;
}
*hfont is not destroyed in above window procedure. It should be cleaned up elsewhere.

Background color WINAPI

My problem is that my button has a background color for some reason, and I do not know why. I do not put the desktop background in the entire area of ​​the button.
case WM_CREATE:
{
hwndButton = CreateWindowEx(0, L"BUTTON", L"Some static text",
WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
25, 125, 300, 300, hWnd, 0, 0, 0);
}
case WM_DRAWITEM:
{
//RECT r;
LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)lParam;
if (hwndButton == lpDIS->hwndItem) {
SetBkColor(lpDIS->hDC, RGB(0, 255, 0));
//FillRect(lpDIS->hDC,&r,CreateSolidBrush(RGB(100,100,200)));
SetTextColor(lpDIS->hDC, RGB(100, 0, 100));
WCHAR staticText[99] = L"test";
TextOut(lpDIS->hDC, lpDIS->rcItem.left, lpDIS->rcItem.top, staticText, 10);
}
Your code only draws as much of the button background as the text requires, use ExtTextOut and specify ETO_OPAQUE to fill the entire space. Either that or use FillRect to actually draw the button background and use ExtTextOut without ETO_OPAQUE to draw with a transparent background.

How to add a transparent border to a WS_POPUP window?

I am using Window 7 and I wanted to add a transparent border to my window. The same border as we can see on skype for instance.
Window Creation:
hWnd = CreateWindow(L"Example", 0, WS_POPUP, 100, 100, 400, 500, NULL, NULL, hinst, NULL);
Is there a way to add the transparent border to WS_POPUP window without the WS_SYSMENU?

SDL: Fullscreen translucent background

I'm trying to write a program that has a translucent background covering the whole screen. After some research it appeared that SDL would be the way to go.
I've written the code to create a full screen window with a background whose alpha is equal to 100 (out of 255), but for some reason it just draws the solid colour. What have I done wrong?
// Initialise SDL
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
this->throwSDLError("SDL_Init Error");
}
// Create the window and renderer
if (SDL_CreateWindowAndRenderer(0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP, &(this->window), &(this->renderer)) != 0) {
this->throwSDLError("Could not create the window and renderer");
}
// Set the blend mode to specify how the alpha channel is used
if (SDL_SetRenderDrawBlendMode(this->renderer, SDL_BLENDMODE_BLEND) != 0) {
this->throwSDLError("Could not set render draw blend mode");
}
// Set the colour to draw
if (SDL_SetRenderDrawColor(this->renderer, 200, 200, 200, 100) != 0) {
this->throwSDLError("Could not set the drawing colour");
}
// Clear the screen using the colour
if (SDL_RenderClear(this->renderer) != 0) {
this->throwSDLError("Could not render the screen");
}
// Present the rendered screen
SDL_RenderPresent(this->renderer);
On Windows, you can create a transparent window by using SetLayeredWindowAttributes to chroma-key the background color from a borderless SDL window.
Code:
// SDL window with transparent background v1.2
#include <SDL.h>
#include <SDL_syswm.h>
#include <Windows.h>
// Makes a window transparent by setting a transparency color.
bool MakeWindowTransparent(SDL_Window* window, COLORREF colorKey) {
// Get window handle (https://stackoverflow.com/a/24118145/3357935)
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version); // Initialize wmInfo
SDL_GetWindowWMInfo(window, &wmInfo);
HWND hWnd = wmInfo.info.win.window;
// Change window type to layered (https://stackoverflow.com/a/3970218/3357935)
SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
// Set transparency color
return SetLayeredWindowAttributes(hWnd, colorKey, 0, LWA_COLORKEY);
}
int main(int argc, char** argv) {
// Get resolution of primary monitor
int desktopWidth = GetSystemMetrics(SM_CXSCREEN);
int desktopHeight = GetSystemMetrics(SM_CYSCREEN);
SDL_Window* window = SDL_CreateWindow("SDL Transparent Window",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
desktopWidth, desktopHeight, SDL_WINDOW_BORDERLESS);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
// Set background color to magenta and clear screen
SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255);
SDL_RenderClear(renderer);
// Draw blue square in top-left corner
SDL_Rect rect1 = {0, 0, 100, 100};
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderFillRect(renderer, &rect1);
// Draw red square in center of the screen
SDL_Rect rect2 = {(desktopWidth-100)/2, (desktopHeight-100)/2, 100, 100};
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect2);
// Add window transparency (Magenta will be see-through)
MakeWindowTransparent(window, RGB(255, 0, 255));
// Render the square to the screen
SDL_RenderPresent(renderer);
// Loop until user quits
bool quit = false;
SDL_Event event;
while (!quit) {
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Result:
Explanation:
First, create a borderless window with that covers the entire desktop. Choose a solid masking color and use it as your background. (In my case, I used magenta). You can then key-out your masking color with the Win32 API function SetLayeredWindowAttributes.
Any part of the window with this color will be completely see-through. Other windows behind your program can be interacted with as normal. By default, other applications can be moved on top of your borderless window.
If you want your SDL window to always be on top of other windows, you can set the SDL_WINDOW_ALWAYS_ON_TOP flag when creating your window.
See Also
Stack Overflow: Creating a transparent window in C++ Win32
Stack Overflow: How detect current screen resolution?
Microsoft: Layered Windows