Implement sticky overlays in .NET - c#

I'm looking for a nice way to render overlays over a native Windows form not owned by myself preferably using a library available for .NET (WinForms, GTK#, ...). Precisely, I'd like to display a few labels or text boxes at a given location on the window.
Device Context & System.Drawing: What I'm currently doing is drawing directly onto the other windows' device context, which causes flickering, as parts of the other application are redrawn in unpredictable intervals. I therefore would have to catch its WM_PAINT event using hook magic, but that's actually not as far down as I'd like to go unless there's no simpler way.
Transparent window overlay with visible child labels: another technique I tried was creating a Windows.Forms.Form with the other windows' size, using TransparencyKey to make only the children visible. This seems pretty hard to get correct, as I don't want the window to be the upper-most one but only exactly one Z-level above the foreign window. The upside would be, that I could add more behaviour to it, as I could actually handle click events, etc.
How would you implement it / deal with the problems in the two techniques described above?

Definitely go with the transparent window approach as that should be simpler to implement. Creating a transparent form is very easy. You already know how to use the TransparenyKey to get the background to not drawn. Also remove the border from the window and remove the min/max/close buttons so you do not have any chrome showing.
Create your window as owned by the window of interest and it will always be on top of the target and act like a modeless dialog. I.e. it is visible only when the owning window is visible.

Thanks for your answer, but I'm still a little confused. How exactly would you set the owner of a window to anything not owned by your own application? I guess that's not even possible when the other application is unmanaged, is it?
Edit:
I now got a little step closer. The example code is in Boo.
[DllImport("user32.dll", SetLastError: true, CharSet: CharSet.Auto)]
public def SetParent(child as IntPtr, parent as IntPtr):
pass
def createAttachedForm(parentHandle as IntPtr):
f = Form()
f.Text = "My overlay"
f.Show()
SetParent(f.Handle, parentHandle)
Application.Run(f)
Now only the TransparencyKey thing doesn't seem to work. Instead the form is completely invisible when the value is set.

Setting the owner of a form can be done with
Form.Show(IWin32Window w)
where w exposes the Handle to the parent window.

Related

Windows form with a transparent background that can be clicked through

INTRODUCTION
Using C# or VB.NET. I'm trying to make a form's background transparent; this form will be overlaped to other window, it will be a top-most window, so the transparent form (and its controls) must have the ability that they must not receive focus and they must can be clicked trough, this means if for example I perform a left-click on the transparent background, then the window on background of that (in the Z-order window) is the window that must receive the click instead.
Notes:
For avoiding the focus I'm overriding the CreateParams property as explained here.
For making my form transparent, I'm calling Win32 DwmExtendFrameIntoClientArea function and also using SharpDX library as explained here. But I think this really doesn't matter with the question itself.
PROBLEM
I'll show a demostration of what I mean using images. Here below is a image of a form (with no transparency, just to simplify understanding) overlapped to a window of a text editor program; note that my form doesn't receive focus. Well, the problem is when I do click on the form's background (or one of its controls) the window on background (the text editor window) still have focus but it can't receive the click.
Here is the same image of above but with a transparent form:
RESEARCH
I'm not really sure about what to investigate, so I'm going blind trying to find something useful in a trial-and-error stage by overriding the Window procedure (WndProc) of the transparent form to test related windows messages, like WM_NCHITEST or WM_MOUSEACTIVATE message as said here:
Windows form with a transparent background that cannot be clicked through
Make a form not focusable in C#
How do I create an "unfocusable" form in C#?
You can do this by sending click (mouse up & mouse down) messages to the window underneath the transparent window using WinAPI.
PostMessageA
You'll need to find the window underneath the point you require.
WindowFromPoint
You'll have to translate the position of the click events accordingly since messages are processed based on relative window position, not absolute screen position.
I actually did this quite successfully to automatically play a facebook game many years ago.
Check the RAD designer in Visual Studio.
Is the label docked to fill?
Where is the main form clickable?
The transparent color is click-though in the main parent, however, components will still retain clicks.

Do it yourself MDI

I have created an MDI application that I want to add a ToolStripContainer to. My first try was to just add one and dock it on Top. Problem I got here was moving ToolStrips inside the container's ToolStripPanels would create an extra Row which was invisible.
Then I saw this question and tried to implement my do-it-yourself MDI interface, using the ToolStripContainer's ContentPanel as where the windows go. This way, I can have my ToolStrips anywhere around the MDI.
The code I have is as follows:
Form f = new Form();
f.TopLevel = false;
tsContainer.ContentPanel.Controls.Add(f);
It all works remarkably well, but I am wondering if this is smart, would I not run into problems later on? Also, if this is possible, why have the entire MDI functionality in Windows?
By setting the TopLevel property to false, the form essentially turns into a UserControl. Do note that you have to explicitly set the Visible property to true, it isn't automatic.
But yes, there certainly are problems and it doesn't behave like an MDI child window at all. The biggest issue should be readily visible, the form can no longer be activated. Very noticeable from its caption bar, it will always be rendered with the colors for a non-active window. It however still works like a caption bar, the user can drag the window and move it around. Which has the clear failure mode that the window is going to get clipped without scrollbars. Not only that, the window can still be maximized and minimized by double-clicking the caption. An MDI child does this very differently. And the visual style for the frame will be wrong in Windows 8.x, looking like a Win7 frame instead, a quirk that has no known workaround.
Realistically, you have to set the FormBorderStyle property to None so it behaves more like a true child window. Or write a bunch of fairly nasty code in the WndProc() override to tame the window, you'll be doomed to re-invent an MDI child, imperfectly.
Which is otherwise a fine way to embed a form. The only remaining quirk after fixing the border is that the form still gets added to the Application.OpenForms collection.

Balloon pop up over control mouse enter/exit

Hello,
Above is the program I am writing. On the right panel is basically two custom controls (the blue rectangle area) I created and just added them as controls to the background panel control when this winform program loads.
I used MS paint to draw out the pop up balloon that I want to see when my mouse enter this control's area. I want to do the following:
1. If mouse enter the control area, the yellow area balloon pop up and populate with the information of that specific control
2. If mouse move out of the control area, the pop up balloon disappear.
Can this be done with Winform application? I looked around and found out about Tooltip class but so far from researching I don't know if it does what I want to do.
I could be wrong but googling around gave me the impress that Tooltip offers very little in term of style. Ideally I want to make this pop up balloon into almost like a border-less pop up window where I can put image , font ect.....at will. Also Tooltip works if you hover over a button or specific field whereas I want the entire control area.
Can this be done? I appreciate if you can point me to any work around if there is one.
I wrote a comment, but I figure I'll expand it into a full answer. This is assuming you want a new control, which isn't a tooltip, for maximum customizability. I did something similar to this for work recently, to act as a non-modal info popup that disappears when clicked.
Creating a Custom Popup Form
What you want is essentially a floating popup that appears over your form, which means you'll want to define a new Form object, rather than a UserControl, as it won't actually be embedded within your other form.
Give it a multiline, non-editable textbox that you can fill with the information you want to populate, then simply call a new instance of the form on your Mouse_Enter event. Close it upon Mouse_Leave.
Adjusting The Style
You'll have to play with it a bit to get it to actually act like a popup and not just a window. I'd recommend setting it to a non-modal popup, and removing the border. You can write a function to automatically size it to its contents. I don't imagine you'll want the user manually resizing it.
Some other things to look into would be overriding the CreateParams property that comes with the basic Form object. You can force DropShadows and TopMost forms without making the form modal. Overriding ShowWithoutActivation to always return true will prevent the form from stealing focus when it pops up.
I'm not sure if you can pull off rounded edges like you have in your mockup. Perhaps you can pull it off with some wizardry in the OnPaint() function, but I couldn't tell you how to do it.
It might be a bit of a pain for fiddling around with, but you can get some good functionality and appearance out of it. If you think you can pull it off acceptably with the ToolTip class, go for it. It took me about a week to get my notifications where I wanted them (though I added several features that you probably don't need to worry about).
Examples
Some keywords to look up in related searches would be Toast Notification and Non-Modal Popup. This might be some use:
http://www.codeproject.com/Articles/442983/Android-Style-Toast-Notification-for-NET
Since you already have implemented custom user controls you might want to try it again. Make a control that is that style and color, changes it's size based on it's text. You can feed it information (such as the text to display) from your existing user control object. You can also have the mouse enter/leave code reside in your first user control.
If you're not sure how to make a rectangle with round corners you can either make it on the fly using a graphics object (which will turn into a bitmap on the screen) or make it how you want it to look in GIMP (or photoshop if you have it) then use that image as the background on your user control. Make the default background transparent (so your voids above the round corners are not grey). If you make a pre loaded image you'll need to be aware you will only be able to scale it equally in Y and X directions. unequal scaling will make it look distorted.
Can you use the Mouse_Enter event on the control?

How to make a form behave like windows taskbar using c#

I have created a taskbar using tabcontrols which works similarly to the Windows taskbar.
I want to make my own taskbar fixed on top of the windows.
It should not be movable, resizable, and it should be "docked" to the top.
I have not tried to do this, but my first attempt would be the following:
set form to be 'TopMost'
set border to 'None'
set Location: (0,0), Size(Screen.Width,H), where H is the Height you want your form to be
set 'ShowInTaskBar' to false
Something you might consider: how will you handle multiple screens?
You'll likely want to add a context menu allowing you to exit the application, since it won't have the normal system buttons. Also, you might consider what happens when the user presses ALT-F4: will your program close? If not, you'll need to add some code in the FormClosing event handler to cancel closing.
Hope this sets you in the right direction!
A long long time ago Jeffery Richter demonstrated how to do a true taskbar in a sample called AppBar (I've used it myself as a boiler plate), so here are some pages that may be of interest:
http://www.google.com/search?q=Jeffrey+Richter+appbar+c#

Drawing a toolbar on a window's titlebar

How do I draw a toolbar on the titlebar of a C# winforms window in windows vista? I have a window where it works, except when the window is unminimized/unmaximized it increases in size by about 16x32px. If possible, please provide code examples.
The only way to effectively do it is to handle the WM_NCPAINT message when you override the WndProc method for the Form.
Passing to the base implementation of WndProc will cause the windows frame to be painted, but at that point, you would be responsibile for painting your toolbar elements on the title bar.
You will also want to handle all the other WM_NC* messages as well, which will allow you to process button clicks and the like (which you will need to handle events for your painted controls).
I've been looking around for the same problem, because I wanted to code a ribbon bar with that big button in the upper left corner... The only good source I found was at CodeProject on this blog and this one and another one.

Categories

Resources