How to create shell styled title bar buttons in .NET - c#

This is a follow-up to my earlier question Draw Custom Buttons on Windows Vista/7 Aero Titlebar.
I revisited the topic quite recently and found this article which is essentially a hack to 'drawing' buttons on Aero-enabled title bar (Windows Vista & 7). What the code does is to create a transparent window over the current one and places the buttons on it, giving the impression of additional buttons on the title bar. The only problem is the buttons look like regular WinForms buttons!
My question is, how do I read the windows shell style (aka theme) in order to create buttons styled just like those in the Control Box (see image)?
I'd like answers to be in .NET (VB.NET or C#). I'm okay with unmanaged code.

So if I understand you correctly, you want to read what Windows 7 calls the "Window Color" aspect of the current theme.
Acording to MSDN http://msdn.microsoft.com/en-us/magazine/cc163435.aspx, you want DwmGetColorizationColor: "retrieves the current color that is being used for DWM glass composition. This value is based on the current color scheme. Changing the setting causes a WM_WMCOLORIZATIONCOLORCHANGED notification."
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmGetColorizationColor(out int pcrColorization, [MarshalAs(UnmanagedType.Bool)]out bool pfOpaqueBlend);
"You can check to see the composition color and opacity by calling the DwmGetColorizationColor function. If this function succeeds, it will set a GDI+ ARGB color value and a Boolean indicating whether the color is opaque. Just like changing the Aero scheme in the control panel, there's a message broadcast when the composition color has changed. WM_DWMCOLORIZATIONCOLORCHANGED is sent when this happens, but in this case the parameters tell you what the new color and opacity are."

Related

Drawing a custom button with hot or pressed state

I've created a custom button control. Basically one button-rectangle, but with two areas inside the rectangle that have a different behavior. For that reason I want to draw the hot and pressed state ONLY for the specific areas, not the hole button.
My current approach is drawing the basic-button using ButtonRenderer.DrawButton(...) with an emtpy text, draw the hot or pressed state if required and finally drawing the text. So far so good, but how do I get the (gradient) colors for the hot/pressed state?
I've tried SystemColors, KnownColors and VisualStyleRenderer.GetColor(ColorProperty.XYZ) but none of them seems to match? How can I read those colors from the host system?
EDIT:
Sample picture below:
I want the colors of both the hot and the pressed button-state - (light) blue in case of this win7 screenshot. If you zoom in you can see that a slight color gradient in both the upper and the lower half is used.
The last button shows what I want to accomplish.
Sure, I could extract the colors from the screenshots and hardcode them or use images like suggested, but that would work only for this specific system, wouldn't it?
Thanks for your answers, Jimi.
According to the accepted answer of your linked SO-question I checked ButtonBaseAdapter and ButtonStandardAdapter. As you also mentioned, ButtonRenderer.DrawButton calls VisualStyleRenderer.DrawBackground which calls the native API UxTheme.DrawThemedBackground to draw the button - the color determination happens inside.
This native API call also draws the button-border and thats the reason why I can't use it.
But I was able to solve my Problem, an unusual way, but it works.
I render both relevant states (hot and pressed) to a bitmap and extract the colors using .GetPixel. In OnPaint I create LinearGradientBrush-instances from the extracted colors and draw the hot/pressed effect over the specific areas of the normal button.
It's not the exact same behavior like a normal button (for both states the border color also changes), but I think it would look really strange if I change the border color only for a part of the button (where the hot/pressed effect is displayed) to match the normal button behavior...
If no other answers or solutions come up I'll mark this post in a few days as an answer...

Remove Title Bar from Windows Form (Windows 10 style)

I have a very simple class which inherits from System.Windows.Forms.Form and removes the WS_CAPTION window style. It works in Windows XP & 7 perfectly. In windows 10 a piece of the titlebar is still drawn and it ends up looking ugly when using a custom titlebar control.
I know there are likely several ways to accomplish this kind of look, but I've chosen this for multiple reasons. I'm not interested in the alternative methods--that's not the intention of my question here.
My question is what is causing such a difference between the way this is rendered in windows 10 vs windows 7? The difference is not merely stylistic. It appears that the titlebar is still being rendered in some capacity even though the WS_CAPTION flag has been removed.
class BorderlessForm : System.Windows.Forms.Form
{
protected override System.Windows.Forms.CreateParams CreateParams
{
get
{
var _CreateParams = base.CreateParams;
_CreateParams.Style &= ~0x00C00000; // remove WS_CAPTION
return _CreateParams;
}
}
}
Windows 7 screenshot (desired appearance consistent with MSDN description):
Windows 10 screenshot (undesirable appearance inconsistent with MSDN description):
What you are observing is not a title bar but sizing border.
My question is what is causing such a difference between the way this is rendered in windows 10 vs windows 7?
The cause is different look and feel implementation on different versions of Windows.
In case you are interested how to get rid of the sizing border even on Windows 10: Remove the WS_THICKFRAME flag.
Alternatively (and perhaps more preferable) you can change your form's FormBorderStyle to some other value. Test whatever works best for you.
However there is nothing what defines the overall form border precisely. It's up to the look and feel (theme). Technically you cannot expect that form's border won't differ under different implementation. You can only ensure by testing.

Changing cursor on mouse-over not working on Windows App

I am working on a Windows Store App using C#.
I am trying to change the cursor when the user hover over a rectangle but it is causing me problems.
Here is my code:
Rectangle item = sender as Rectangle;
item.Cursor = Cursors.AppStarting;
mouseVerticalPosition = e.GetCurrentPoint(null).Position.Y;
mouseHorizontalPosition = e.GetCurrentPoint(null).Position.X;
isMouseCaptured = true;
item.CapturePointer(e.Pointer);
It says 'Windows.UI.Xaml.Shapes.Rectangle' does not contain a definition for 'Cursor'.
You're confusing WPF with WinRT/XAML. These are both XAML-based UI technologies, but although superficially similar when looking at some basic controls and properties - these are completely separate implementations that have many differences once you start looking at the details. One of these is the Cursor property missing in the Windows Runtime.
You can use Window.Current.CoreWindow.PointerCursor property to get or set the cursor on the current window.
You can also use some attached behaviors I wrote in WinRT XAML Toolkit here to get an API similar to the WPF one where you set a cursor per element. There's a sample you can check here that shows how you can set a cursor on an element like this:
Extensions:FrameworkElementExtensions.SystemCursor="Arrow"

Get default Windows System Colors in .NET

I'm writing a custom Button control as part of a (soon to be) free Control suite, and I would like to base my (default) Control colors on the corresponding Windows System colors. So, after looking up "default windows system colors" online I could not find information on the System Colors for Windows controls (especially not Button controls).
Is there a way to get this color information (e.g. Button Border Color, Button Highlight Color, Button Hover Color, Button Clicked Background Color, etc) in .NET?
Yes. In fact, there is an entire class dedicated to this:
The SystemColors class.
...or for WPF (thanks #ORMapper), The System.Windows.SystemColors class.
There is a System Color Class out, which will provide you the Colors.
For WinForms use:
System.Drawing.SystemColors
For WPF use:
System.Windows.SystemColors
You can use also the GetSysColor function api function.
Valter
You could use the Win API, GetSysColor function...
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int GetSysColor(int nIndex);
The function returns the red, green, blue (RGB) color value of the given element.
To display the component of the RGB value, use the GetRValue, GetGValue, and GetBValue macros.
System colors for monochrome displays are usually interpreted as shades of gray.
To paint with a system color brush, an application should use GetSysColorBrush(nIndex), instead of CreateSolidBrush(GetSysColor(nIndex)), because GetSysColorBrush returns a cached brush, instead of allocating a new one.
I am wanting the same thing. My approach is to, upon initialization, create a temporary window with the background color specified in GetSysColor(COLOR_BTNFACE), the "standard" background color for dialog boxes. Then, I create a button with no text and get the colors. This temporary window is never displayed, and is destroyed immediately (WM_CREATE exit code = -1).

Taking screenshots in Windows Vista, Windows 7, with transparent areas outside the app region

I am trying to take a screenshot of an application and I would like to make the parts of the rectangle that are not part of the applications region be transparent. So for instance on a standard windows application I would like to make the rounded corners transparent.
I wrote a quick test application which works on on XP (or vista/windows 7 with aero turned off):
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
// Just find a window to test with
IntPtr hwnd = FindWindowByCaption(IntPtr.Zero, "Calculator");
WINDOWINFO info = new WINDOWINFO();
info.cbSize = (uint)Marshal.SizeOf(info);
GetWindowInfo(hwnd, ref info);
Rectangle r = Rectangle.FromLTRB(info.rcWindow.Left, info.rcWindow.Top, info.rcWindow.Right, info.rcWindow.Bottom);
IntPtr hrgn = CreateRectRgn(info.rcWindow.Left, info.rcWindow.Top, info.rcWindow.Right, info.rcWindow.Bottom);
GetWindowRgn(hwnd, hrgn);
// fill a rectangle which would be where I would probably
// write some mask color
g.FillRectangle(Brushes.Red, r);
// fill the region over the top, all I am trying to do here
// is show the contrast between the applications region and
// the rectangle that the region would be placed in
Region region = Region.FromHrgn(hrgn);
region.Translate(info.rcWindow.Left, info.rcWindow.Top);
g.FillRegion(Brushes.Blue, region);
}
When I run this test app on XP (or Vista/Windows 7 with Aero off), I get something like this, which is great because I can eek an xor mask out of this that can be used later with BitBlt.
removed dead Imageshack link - Screenshot
Here is the problem, on Vista or Windows 7 with Aero enabled, there isn't necessarily a region on the window, in fact in most cases there isn't. Can anybody help me figure out some way to build a mask like this on these platforms?
Here are some of the approaches I have already tried...
1. Using the PrintWindow function: This doesn't work because it gives back a screenshot taken of the window with Aero off and this window is a different shape from the window returned with Aero on
2 Using the Desktop Window Manager API to get a full size thumbnail: This didn't work because it draws directly to the screen and from what I can tell you can't get a screenshot directly out of this api. Yeah, I could open a window with a pink background, show the thumbnail, take a screenshot then hide this temporary window but thats a horrible user experience and a complete hack I would rather not have my name on.
3. Using Graphics.CopyFromScreen or some other pinvoke variant of this: This doesn't work because I can't assume that the window I need information from is at the top of the z-order on the screen.
Right now, the best solution I can think of is to special case Aero on Windows 7 and Vista to manually rub out the corners by hard coding some graphics paths I paint out but this solution would suck since any application that performs custom skinning will break this.
Can you think of another or better solution?
If you are here, thanks for taking time to read this post, I appreciate any help or direction that you can offer!
If you are looking for a finished application, there is 7capture, which captures also the translucency, so images can be saved to PNG format for later compositing.
EDIT:
The original question and comments indicate you are looking to produce a region on Windows Vista/7 that you can then use to mask out parts the captured image, as is done with Windows XP and non-Aero UIs. Using a region is not going to give you the result you are looking for, since the window outline is not computed as a region, but as an image with variable transparency - RGBA. The Alpha channel in that image is your mask, but it's not an on-off mask like a region, but a gradual mask with a range of values from pixels being fully included to being fully masked out.
Although it uses undocumented APIs, the code at http://spazzarama.wordpress.com/2009/02/12/screen-capture-with-vista-dwm/ will capture to a RGBA buffer which you can then use to render or save the image with the shadow and other translucency effects intact.
In DwmCapture.cs Change
BackBufferFormat = Format.X8R8G8B8
to
BackBufferFormat = Format.A8R8G8B8
(X8->A8)
And you should then be able to access both the usual RGB data plus transparency from the captured buffer. This can then be saved as a PNG or other format with alpha-channel for composing.
Removed idea that is terrible but would have been awesome back in the '90s
You say that using the DWM API only allows you to capture directly to the screen... could you create a window offscreen (say, X = -100000px, Y = -100000px) but visible (maybe even hidden?) and draw the screenshot to it? Since when using the DWM each window has a backing texture, I'm thinking it might still get drawn fine even though the target isn't directly onscreen.
Also, if you want to go the DirectX route and access the actual DX texture backing the window, I found a few leads that might help (especially the first link):
http://spazzarama.wordpress.com/2009/02/12/screen-capture-with-vista-dwm/
http://channel9.msdn.com/forums/TechOff/251261-Help-Getting-the-shared-window-texture-out-of-DWM-/
http://web.archive.org/web/20080625183653/http://www.aeroxp.org/board/index.php?showtopic=6286
http://www.youtube.com/watch?v=hRTgFTMnT_U
Using Graphics.CopyFromScreen or some other pinvoke variant of this:
This doesn't work because I can't
assume that the window I need
information from is at the top of the
z-order on the screen.
If you know which window you need the information from, can you bring it to the front, call Graphics.CopyFromScreen, and then reset its z-index? I know from experience that Aero does odd things when items are in the background in order to make their glass interface work correctly (partial rendering etc). This may not be great UX; however, it would be a special case and used only when Aero is turned on.
You can take a look at the source code of AeroShot, as described on the main page, it can capture rounded edges and with the Aero Glass transparency effect and save it to a PNG file. It's written in C#.

Categories