Shadow for custom mouse cursor (2) - windows

After reading the answer to the Shadow for custom mouse cursor question, I did some testing.
From my Delphi application I loaded both 32bit and 24bit versions of my cursor. Never a shadow. I did the same with the standard Windows arrow pointer. No shadow either.
Also I tried the opposite and assigned my custom cursors to the "Normal select" pointer in the Windows mouse settings. Both the 24bit and 32bit pointer were given a shadow.
So, it appears having an alpha channel in a cursor doesn't influence the shadow behaviour. But what am I doing wrong in my application?
First step is adding the cursor to the application's resources via an .rc file with
CUR_EDGE_R Cursor DISCARDABLE "edge_r.cur"
And in the application it is loaded with:
const
crEdgeR = TCursor(135);
.
.
Screen.Cursors[crEdgeR] := LoadCursor(HInstance, 'CUR_EDGE_R');
So, what is the way to have Windows add a shadow to a custom cursor?
Note: I have seen posts on the internet suggesting to create two version of the mouse cursor, one with and one without shadow, and load the appropriate cursor, depending on the user's settings. This is not the solution I'm looking for.

In order to place a shadow under your custom cursor, you simply design the cursor with an Alpha channel (which enables semi transparency) and draw your shadow in place. You can even use Photoshop/GIMP (or any other capable graphics editor) to produce your cursor with a "Drop Shadow" effect, save it as a PNG, then use an Icon Editor (such as Microangello) to convert the PNG to a cursor.
As I understand it, none of the cursors shipped with Windows Vista/7 are designed as "flat", using some system effect to generate their shadows.

Related

Common Controls on a Transparent Window?

While there are lots of variations of the question, there doesn't seem to be a specific answer to a simple case of wanting to use built-in common controls on a transparent window using Win32. I don't want the controls to be transparent, I just want the border around it to be transparent. I can't believe MS didn't update the .dll's to handle transparency when they added it, but I guess they forgot? Is there a specific method that works. A button can get close with WS_EX_TRANSPARENT, but flaky where it works most of the time but at times part of the border shows up. Edit controls, change depending on when get focus or not.
So the question is simply:
Is there a way to make common controls on transparent window so there is no white border around them?
If not, is there a good replacement library that does it via owner draw?
If some, which ones and what is the method?
Seems silly to reinvent the wheel just because of the area around the control.
TIA!!
If I am not mistaken, you can take the following steps to achieve this effect.
1.Create a GDI+ Bitmap object with the PixelFormat32bppPARGB pixel format.
2.Create a Graphics object to draw in this Bitmap object.
3.Do all your drawing into this object using GDI+.
4.Destroy the Graphics object created in step 2.
5.Call the GetHBITMAP method on the Bitmap object to get a Windows HBITMAP.
6.Destroy the Bitmap object.
7.Create a memory DC using CreateCompatibleDC and select the HBITMAP from step 5 into it.
8.Call UpdateLayeredWindow using the memory DC as a source.
9.Select previous bitmap and delete the memory DC.
10.Destroy the HBITMAP created in step 5.
This method should allow you to control the alpha channel of everything that is drawn: transparent for the background, opaque for the button.
A similar discussion: Transparent window containing opaque text and buttons

Overlay window drawing which appears in screen captures of other applications

We need to overlay a target window with a custom shape and tracks the position of the target window such that the overlay drawing always appears above it. Also, the overlay drawing should appear in screenshots taken using BitBlt or PrintClient by screen-capturing tools like Camtasia, Debut, etc. Also, moving the target window around should not leave traces of the drawing at earlier location. The target window is not made using our code.
So far we've tried several ways but each method has its problems:
1) Layered Window:
Using a layered window as the child/owned window of the target window is the easiest thing and it works. But the problem is on Windows 7 and XP, layered windows do not appear in a BitBlt done without the CAPTUREBLT flag and the screen-capturing tools may call BitBlt without the flag, thereby skipping our window from the capture.
2) Region Window:
The crude approach to support all Windows versions then is to use a region window using SetWindowRgn and make the target window its owner. However, region windows are generally very slow in rendering complex shapes and also impact the performance of other windows, sometimes to the point of freezing the application. Region window also leaves traces on dragging the application window.
3) Subclassing and Drawing on HDC:
Another approach is to sub-class the target window and draw the shape on its HDC on the OnPaint() event inside the window procedure hook. The shape can be drawn on the desktop window instead too. The problem is that applications may draw without a paint event, like when some text is selected using the cursor, and such drawing may erase a part of the custom drawing. Tracking all possible paint events is not a good way to do this.
4) Drawing continuously in a timer:
The last resort is to draw the custom shape on the target window in a timer continuously so the drawing is always above the target, even on text selection. But this may show a bit of flicker when the text is selected. Not to mention, it is very performance heavy to draw constantly.
Question
What is the best way to achieve an overlay drawing which works on all Windows versions (since XP) at the same time appearing in screen-captures. We've exhausted all approaches. Is there some method I'm missing? Can the above mentioned ways be used with some improvement? Is DirectX an option? I'm looking for methods with fast rendering capacity to achieve this. Appreciate any help!
I think the best solution to draw an overlay window, is to use a double-buffered technique GDI, just like in this example:
Overlay Drawing in CScrollView
I have myself the same task, and the only solution was that one. Hope it help you.

Direct2D: leave a region of a window render target untouched

I am drawing to a regular HwndRengerTarget but other windows, which have nothing to do with Direct2D, overlap it.
The problem is that these windows get painted over when I draw to the HwndRengerTarget.
I would like to tell Direct2D not to touch a specific region of the HwndRengerTarget (i.e. don't touch the pixels that are already on the screen), so that these windows remain correctly visible.
Is that possible?
If I draw normally then call RedrawWindow on the windows, it flickers a lot.
Thanks.
If you want to manually restrict your rendering to a certain region you can use layers (ID2D1Layer objects).
More info here Layers Overview
If the visible region is rectangular it may be simpler to use axis aligned clips via methods PushAxisAlignedClip and PopAxisAlignedClip.
ID2D1RenderTarget::PushAxisAlignedClip
Another method of restricting drawing to a certain shape is to render it to a bitmap and then use this bitmap via a bitmap brush in the FillGeomtry method.
ID2D1RenderTarget::FillGeometry
Why not arrange the windows (HWNDs) so that the Direct2D one is at the bottom of the z-index? It should be the first child of its parent. Then clipping will be automatic. You may need the WS_CLIPSIBLINGS window style.
I had the same problem.
Fixed by calling CreateWindowEx LAST for the D2D child HWND.
So AFTER all the other child windows are created.

Fading the cursor

I have a Windows application for which I would like to indicate that the content is dead/frozen/not interactable by fading it to 50%.
Is there a way to take the standard Windows cursor and fade it?
I have never seen a case of the Windows cursor being anything but 100% opaque, and I heavily suspect that there's no option to make it transparent because the uses are limited and the abuses are many (i.e. make cursor invisible, would cause massive frustration).
I don't know of any way to do that, and it makes sense there's none - it is not a part of Window's look and feel. You'd usually fade the dead/frozen/not interactable control rather than the cursor, and/or change the cursor to a standard hourglass or so.
Having that say, if fading the cursor is definitively what you want, you can create a faded image of the standard cursor, and load it when needed.

Shadow for custom mouse cursor

On "newer" Windows systems there is an option to make the OS draw a shadow beneath the mouse cursor:
However the custom cursors in my Delphi app don't show a shadow even if this setting is enabled. Do you have any idea how to arrange it so that Windows adds the shadow automagically? Or do I really have to check the user's preferences and conditionally draw the shadow myself like Mike Lischke did?
(This is a question that has been bugging me and others for some time:
Custom mouse cursor shadow
Custom cursor shadow
Custom cursor with shadow)
No, you don't need to check the user's global setting and draw the cursor yourself. You just need to provide a cursor with the alpha channel. Windows will either use alpha blending to draw your cursor or not, depending on the user's setting. Your cursors probably don't include the alpha channel so the shadow is never shown.

Resources