All-aero window have controls' colors blended - how to avoid it - c++

I'm looking for a way to paint my custom controls into both the client and non-client area of a dialog. More or less the white area below
I used DwmExtendFrameIntoClientArea and I managed to get that effect by extending the client area on the entire window with
MARGINS mar = {-1, -1, -1, -1};
DwmExtendFrameIntoClientArea ( hWnd, &mar );
but now every control which I set with a transparent background
SetBkMode(hdc, TRANSPARENT);
have their colors blended with the aero stuff (the same problem you can see here).
Is there a way for the controls to retain their right color and avoid blending with the background?

It is because the window treated the black colour as the transparency key.
You just need to set another value:
SetWindowLong(hWnd,GWL_EXSTYLE,WS_EX_LAYERED);
// Choose a colour that you will not use in the program, eg RGB(200,201,202)
SetLayeredWindowAttributes(hWnd,RGB(200,201,202),0,LWA_COLORKEY);

Related

setting transparency for layered windows

I have a multi-level window, I need to display translucent text. The problem is that I can call SetLayeredWindowAttributes() only once, ie I need to make the background transparent first:
SetLayeredWindowAttributes(hwnd, 0, 0, LWA_COLORKEY);
and then draw text and apply transparency to the entire window:
SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA);
but each subsequent call to SetLayeredWindowAttributes() overrides the previous one.
I tried to display the text using AlphaBlend(), but since what I need to mix with lies in another window, the text just turned gray.

Win32 Edit Controls of WS_EX_LAYERED parent dont receive mouse/click events when their background is transparent

I want to put some edit fields ontop of a splash screen which is rendered in another top level window (transparent PNG similar to this http://code.logos.com/blog/2008/09/displaying_a_splash_screen_with_c_part_ii.html) .
I made a secondary window which is always on top of my splash screen, and made it also transparent with WS_EX_LAYERED.
Now i set the background color of the edit fields in the wndproc by catching WM_CTLCOLOREDIT.
This works fine, my input controls are transparent (e.g. invisible) and only the entered text is visible on the splash screen.
Now the issue comes that the mouse cursor which indicates here is a text box does not work, neither can i click in that box to have it focus. The problem all disappears if i do NOT make the background of the edit control transparent. There is also no WM_NCHITTEST when its transparent. The only time im getting a mousecursor is if there is (visible)text already entered in the box
g_HWNControlsParent = CreateWindowEx( WS_EX_LAYERED,.....);
hwLoginField = CreateWindowEx(NULL,"EDIT", "-User-", WS_CHILD|WS_VISIBLE|WS_TABSTOP, ....g_HWNControlsParent);
SetLayeredWindowAttributes(g_HWNControlsParent,RGB(0, 0, 0), 0, LWA_COLORKEY) ;
in HWNControlsParent wndproc
case WM_CTLCOLOREDIT: { // BG Color of Input Fields
HDC hdc = (HDC)wParam;
SetTextColor(hdc, RGB(230,230,230));
SetBkColor(hdc, RGB(0,0,0)); // Color of Background where Text is entered
SetDCBrushColor(hdc, RGB(0,0,0)); // Color of Background where no Text is
return (LRESULT) GetStockObject(DC_BRUSH); // return a DC brush.
}
If you use an alpha of 1 instead of 0 for the transparent regions, they will still be transparent but will respond to mouse clicks.

Drawing a system-like cursor, top-most, anywhere

I need to draw a system-like cursor that I simply can control the position of.
In other words, I need to draw a transparent image that looks just like the system cursor and I need it to be rendered on top of all other windows.
I've tried multiple approaches, but they all seem to have some downside.
I've figured out that I can load the cursor image by using LoadImage() and passing the resource OCR_NORMAL and casting it into a HBITMAP.
HICON NormalCursor = (HICON)LoadImage(NULL, MAKEINTRESOURCE(OCR_NORMAL), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
Then getting the "desktop" HDC
hDC = GetDC(NULL);
Then I can try to draw it using DrawIconEx()
DrawIconEx(hDC, (int)x, 0, NormalCursor, 0, 0, NULL, NULL, DI_DEFAULTSIZE | DI_NORMAL);
The DI_NORMAL flag is supposed to combine the DI_IMAGE & DI_MASK flags giving me a transparent image/icon/cursor, but this is my result on the desktop:
Not to mention that if it moves it creates trails.
By making a transparent window using SetLayeredWindowAttributes like this:
SetLayeredWindowAttributes(hWnd, RGB(0, 0, 0), 50, LWA_COLORKEY);
And having the background color of my window to be black, I can remove the background from the window. But due to doing alpha based on a color I get ugly black pixels around my cursor icon.
Can I make the background of a window transparent in some other way than using a color mask?
How do I draw a transparent cursor on top of all windows properly?
I would recommend that you do make your own window, and do something like what's described at http://www.codeproject.com/KB/GDI-plus/CsTranspTutorial3.aspx . It's in C#, but most of it is just win32 calls. It does a nice job of variable transparency, too, not just 0%/100%.
Isn't the outline of the cursor black? Is the problem just that you're making the outline transparent too? Why don't you just change the transparency color (and the background color of the window) to anything other than black or white?

How do I clear a Direct2D render target to fully transparent

I'm trying to draw semi-transparent rectangles on an invisible HWND. However, clearing the window with ID2D1HwndRenderTarget::Clear just makes the entire window black, so when I draw rectangles on top, they look semi-black.
If I don't Clear() and don't draw, then the window is invisible, as it should be. Clear() is the culprit here; however if I don't use it then painting messes up pretty badly.
Here's the code I'm using in my WindowProc:
case WM_PAINT:
// Begin drawing
pRenderTarget->BeginDraw();
pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
// Clear the window
pRenderTarget->Clear();
// Paint the panel and its children
D2DSurface()->StartPainting();
{
D2DSurface()->PaintTraverse(panel);
}
D2DSurface()->FinishPainting();
// Finish drawing
HRESULT hr = plat->pRenderTarget->EndDraw();
Thanks in advance!
Transparency is a problem. The only window that support per-pixel transparency are WS_EX_LAYERED windows. These windows were designed for uses like drag-drop icons, and that leads to problems in every other usage.
Using D2D with WS_EX_LAYERED windows requires that you use a DXGI render target. To EndDraw, you get a DC out of the render target then pass that into the layered window and tell the layered window to update itself. (Try this with a HWNDRenderTarget - it will probably crash the device driver).
I suspect your window appears transparent because it's not drawing at all.
Another gotcha you may run into... if the window is sufficiently transparent, mouse clicks will go through it to the underlying window.
Use ID2D1DCRenderTarget instead of ID2D1HwndRendTarget, then bind the device context (DC) of your window before drawing begins.
Of course, you will need to set the WS_EX_LAYERED for your window and then call SetLayeredWindowAttributes to set the transparent color:
SetLayeredWindowAttributes(hWnd, RGB(0, 0, 0), 0, LWA_COLORKEY);
Screenshot:
When creating your RenderTarget, you'll have to tell D2D that you want to use alpha (in premultiplied mode) in the pixel format:
HRESULT hr = mD2DFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties( D2D1_RENDER_TARGET_TYPE_DEFAULT, D2D1::PixelFormat( DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED ) ),
D2D1::HwndRenderTargetProperties( mWindow, size ),
&mRenderTarget );
After this, calling Clear() with an alpha value of zero works just fine.

How Can I Create Rounded Rectangle Buttons in WM6?

Yes, like those pretty buttons on the iPhone. ;)
I've been searching and reading for days now and everytime I find something that will get me close (like CreateRoundRectRgn), it blows up because Windows Mobile 6 GDI+ doesn't support it.
I can do the whole owner draw thing and such. But how do I curve those hard corners and reshape a button? :/
Note Tools available: Native Win32 only (no MFC)
That thought has occured to me, but it leaves two issues:
1) Won't the bitmap with rounded edges still leave the corners of the button visible.
2) Bitmaps are great for fixed screen size. But having a variety of resolutions, my goal is to dynamically create the button bitmap in memory at run-time and use it that way.
I've got it working with square buttons. Yet I have seen rounded edge buttons used by other software. There must be a way to reshape buttons.
Getting pretty buttons like that is typically done by doing a complete owner-drawn button and drawing an image that a graphic designer created to it rather than letting GDI do any of the control painting. You simply need an image for "up" and an image for "pressed". You can manually draw in the focus or use yet another image with a ROP mask to draw it on the button as well. To get the nice "rounded" effects, you simply create the image with a background color that you then use as a transparency color.
Tee scaling issue is somewhat unique to WinMo, since iPhone really has only one resolution. If you need to target different resolution WinMo devices you can do one of 2 things (which you use depends on the images you're using). Eitehr just draw the image scaled, or include different size versions of the images and decide at runtime which to use based on screen resolution.
You can use the RoundRect GDI function to do it on an owner drawn control.
//Set up a brush and pen
HBRUSH brush = CreateSolidBrush(RGB(255, 0, 0));
HPEN pen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
//Select it
HGDIOBJ old_brush = SelectObject(hdc, brush);
HGDIOBJ old_pen = SelectObject(hdc, pen);
//Draw your rectangle
RoundRect(hdc, m_rect.left, m_rect.top, m_rect.right, m_rect.bottom, 10, 10);
//restore the old state of your HDC
SelectObject(hdc, old_brush);
SelectObject(hdc, old_pen);
//Clean up
DeleteObject(brush);
DeleteObject(pen);
If you want to do something fancier like filling it with a gradient you can draw your gradient to an off screen buffer and use CreatePatternBrush to draw from it.