How to add a transparent border to a WS_POPUP window? - c++

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?

Related

WIN32 CPP Creating a window 400 x 400 creates a window smaller

RECT WindowMainRect{ 0, 0, 400, 400 };
HWND WindowMain;
HWND WindowChild;
INT main()
{
AdjustWindowRectEx(&WindowMainRect, WS_SYSMENU, FALSE, 0);
WNDCLASS WindowMainClass;
memset(&WindowMainClass, 0, sizeof(WNDCLASS));
WindowMainClass.lpfnWndProc = WindowMainProcedure;
WindowMainClass.lpszClassName = L"WindowMain";
if (!RegisterClass(&WindowMainClass)) MessageBox(NULL, L"Error", L"Register Class Error", MB_ICONWARNING);
WindowMain = CreateWindowEx(0, L"WindowMain", L"Window", WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, WindowMainRect.right, WindowMainRect.bottom, NULL, NULL, NULL, NULL);
ShowWindow(WindowMain, TRUE);
WindowChild = CreateWindow(L"Edit", NULL, WS_CHILD | WS_BORDER, 0, 0, 350, 350, WindowMain, NULL, NULL, NULL);
ShowWindow(WindowChild, TRUE);
MSG Msg;
Msg.message = WM_NULL;
while (Msg.message != WM_QUIT)
{
if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
}
The code above creates a window by 400 x 400 and adjusted by WS_SYSMENU.
Assumptively, the client area of this window should be 400 x 400 pixels with a 1:1 aspect ratio,
I create a child window at (0,0) with the width of 350, and height of 350.
Although, visibly the client area is not 1:1 and is vertically stretched.
Using photoshop I counted the pixels, I expected it to be above 400 pixels vertically and 400 pixels horizontally,
Although the results returned ~386 pixels in width, and ~394 pixels in height.
How do I fix this and have the client area 1:1?

CreateWindowW window is too small

When I create my window, it is smaller than the width and height specified, that should be 840x840. The actual window created is only approx 825x782 (not including menus etc). I can't work out why the window is small and have never had this problem before. Thanks in advance.
CreateWindowW(wc.lpszClassName, L"Window",
WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 200, 840, 840,
NULL, NULL, NULL, NULL);
The solution is to use AdjustWindowRect function as commented by Simon Mourier, where rect is the size of the client window.
RECT rect = {0, 0, width, height};
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, true);
CreateWindowW(wc.lpszClassName, L"Window",
WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 200,
rect.right - rect.left, rect.bottom - rect.top,
NULL, NULL, hPrevInst, NULL);

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.

Windowed fullscreen

Using win32 I create a window like this:
RECT windowRect = {0, 0, width, height};
AdjustWindowRectEx(&windowRect, WS_OVERLAPPED | WS_CAPTION | WS_THICKFRAME, FALSE, WS_EX_APPWINDOW);
mWindowHandle = CreateWindow(wcex.lpszClassName, wcex.lpszClassName, WS_OVERLAPPED | WS_CAPTION | WS_THICKFRAME,
CW_USEDEFAULT, CW_USEDEFAULT, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top,
nullptr, nullptr, mInstanceHandle, nullptr);
For example, right now I have 1920x1080 max, if I pass those values the client area will have 1920x1080, but the overall window size will be too large, dissapearing off-screen.
width and height are arbitary values passed to the function. The effect I want to achieve is that if it is set to the maximum (or above) the resolution of the screen, the whole window will be clamped to fit the screen.
Is there an easy way to do this in win32?

Create semi-transparent panel with C++ and WINAPI

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.