Borderless window. How to add a shadow and remove 1px border?
Asked Answered
V

1

7

I need to create a borderless window with specified background color. I know how to remove a non client area and get something like this:

enter image description here

It's cool but not truly what I want. If you take a closer look at any aero window - there's a shadow around it (actually this is not a shadow but some glow). I found somewhere that I can use this code to add a shadow:

const MARGINS shadow_on = { 1, 1, 1, 1 };
DwmExtendFrameIntoClientArea(hwnd, &shadow_on);

It's almost do it's job (thought this is absolutely not clear to me - documentation says nothing about relationship of shadow and this function). Almost. There's a thin border appeared around the window. It looks like it's semitransparent and it breaks the look and feel of the window:

enter image description here

I know that it's possible - the visual studio even change the color of this border somehow!

Update: as IInspectable noticed in comments I can use negative margins in DwmExtendFrameIntoClientArea(). I set -1 value and got this result:

enter image description here

As you can see - it's even weirder. I tried to fill a background with color, but without luck.

Verbenia answered 6/5, 2015 at 9:40 Comment(15)
You can clip the window area to remove the glow. Did You try to clip it already in order to remove the glow ? It might be, that You cannot get rid of the glow and use shadow at the same time.Mita
@Mita no, you didn't understand - I need a glow - I don't need a border which appears when I'm adding a glow.Verbenia
Then do not use the mentioned function. Draw it Yourself. One px lines, on all sides, with a slight ligther green.Mita
If I'll not use this function there will be no glow at all - like in first screenshot.Verbenia
DwmExtendFrameIntoClientArea: "Use negative margin values to create the "sheet of glass" effect where the client area is rendered as a solid surface with no window border."Azazel
Why not use CS_DROPSHADOW in class style?Precarious
@Azazel Unfortunately this didn't help - I updated a post with a new picture.Verbenia
@BarmakShemirani This style gives a shadow on right side and bottom only - not aero shadow.Verbenia
I see, calling DwmExtenFrameIntoClientArea with negative margin values turns the entire client area into Aero Glass. Have you tried using a margin with all members set to zero?Azazel
Yes - that way there's no shadow (see first screenshot).Verbenia
Wait I'm confused. What kind of shadow do you want? Could you produce a mockup of what you want to see? Is CS_DROPSHADOW not it?Accelerometer
@Accelerometer What I wanted to achieve is called aero shadow - it's a glowing around window perimeter. CS_DROPSHADOW adds shadow near bottom and right window edges only. Anyway, I found how to achieve what I want: 1. CreateWindowEx with WS_EX_LAYERED flag. 2. call SetLayeredWindowAttributes(hWnd, RGB(0, 0, 0), 0, LWA_COLORKEY); - the color should differ from window's background (specified in WNDCLASSEX). 3. call DwmExtendFrameIntoClientArea() with margins where at least one margin not zero.Verbenia
All right then. Glad to see you've found how to do it; good luck from here on out!Accelerometer
@nikitablack: Could you add an answer to this question, showing a bit more detail like the window and class styles you're using. I'm unable to get the Aero shadow to appear on a WS_POPUP using the DwmExtendFrameIntoClientArea as you've shown (with or without the semi-transparent border).Rodroda
@AdrianMcCarthy Unfortunately, I don't have a working example and I absolutely don't remember what I did. The only thing I remember is that visual studio (and other MS tools) used another windows for the shadow - that's how they managed to change it's color. Basically they add a new window on every edge and somehow rendered semitransparent glow.Verbenia
H
3

To remove one pixel border after calling this function:

const MARGINS shadow_on = { 1, 1, 1, 1 };
DwmExtendFrameIntoClientArea(hwnd, &shadow_on);

You need to override WndProc WM_NCCALCSIZE message, and return 0 as the result. Also you need to create window using WS_CAPTION style. (On Windows XP this code won't produce rectangular window, but there is no shadow on WinXP, so on Windows XP you should fallback to WS_POPUP window style)
By the way, to add shadow it is enough to use this margins:
const MARGINS shadow_on = { 1, 0, 0, 0 };

Here is clean windows API code example how to create such window, it is written on Delphi: https://mcmap.net/q/1627046/-how-to-create-window-without-border-and-with-shadow-like-github-app

Heracles answered 14/6, 2017 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.