Make WinForms control appear on top of all other controls - winforms

I have a WinForm, in which I am hiding all borders and the Control Box. Inside the WinForm I have a RECT() (not a WinForms control) the RECT fills the entire WinForm.
I am trying to add a Label to the WinForm, but I want the label to appear on top of the RECT. The Label appears on the WinForm, but never on top of the RECT. I've tried using the following:
/*App Runs - Label does not show up*/
_label.Text = "This is the label";
_label.BackColor = Color.Cornsilk;
_form.Controls.Add(_label);
_form.Controls.SetChildIndex(_label, 0);
/*App Does Not Run*/
_label.Text = "This is the label";
_label.BackColor = Color.Cornsilk;
_form.Controls.SetChildIndex(_label, 0); //trying to set the index before I add the label to the form
_form.Controls.Add(_label);
/*App Runs - Label does not show up*/
_label.Text = "This is the label";
_label.BackColor = Color.Cornsilk;
_label.BringToFront();
_form.Controls.Add(_label);
/*App Runs - Label does not show up*/
_label.Text = "This is the label";
_label.BackColor = Color.Cornsilk;
_form.Controls.Add(_label);
_label.BringToFront();
As you can see, I've tried a lot of different stuff and nothing is working. I have also tried adding the label after the RECT has been added, to no avail. I am having a similar issue with adding a Background Image (though not the question being asked here). Does anyone know of a more forceful way to make the Label appear on top of the RECT?
Also, because of the API and dll's I am using, I cannot use something other than a RECT or WinForms.

You can use BringToFront on the label itself:
_label.BringToFront();
This will bring the label to the front of the Z order on the form, so it should display on top of other form elements.
I have a RECT() (not a WinFrom control) the RECT fills the entire WinForm
A "RECT" isn't a control - it's a definition size and position. Depending on what you're using to display your background, this may not work. If it's painting into the entire form, it could be overwriting your other controls, and "masking" them, no matter what you use for z order. Without more information, it could be difficult to provide guidance, but you'd have to make sure you cause the label to redraw after the "RECT".

Related

WPF Window HorizontalAlignent Stretch

I have a simple task that I want to accomplish: Have a WPF window launch with a Horizontal Alignment that is stretched to the total width of the current screen. I want to achieve a kind of custom Overlay MessageBox (I dont want to use third party controls such as MahApps), I am not using any third party references for this.
Please see what I have achieved so far (Not sure if the image will show, the link is http://imgur.com/e27DyNJ):
I have tried setting the width with a Controller object that I wrote which works, that basically sets the Width, Height, Left and Top to the width of the primary monitor. Downside is the window then pops up on the primary screen, not on the screen that is currently in use.
As far as I know, WPF doesn't have any multi-screen functions. You could PInvoke some native Multiple Display Monitor Functions, wrap them in a managed class and utilize them in that regard, though.
As a workaround, I have done the following:
var screen = System.Windows.Forms.Screen.FromRectangle(new System.Drawing.Rectangle((int)window.Left, (int)window.Top, (int)window.Width, (int)window.Height));
window.Width = screen.WorkingArea.Width;
window.Left = screen.WorkingArea.Left;
where window is the instance of my window I want to resize.
This works with the current screen the window was opened on.

Is this how I make custom controls both transparent and flicker-free in Windows? Or do I have one of these steps wrong?

I could begin by asking the question outright or by citing my sources (this, this, this, and this) descriptively, but I'll walk you ll through what I'm trying to do instead.
Let's start with a main window. It has its own window class whose hbrBackground is set to COLOR_BTNFACE + 1. Now let's do
EnableThemeDialogTexture(hwnd, ETDT_ENABLE | ETDT_USETABTEXTURE)
so the tab control we're about to add will be drawn with visual styles. (Try Windows XP with the standard Luna theme for best results.) Now let's add a tab control and two tabs.
On the first tab, we create an instance (let's call it container) of a new window class. This window class is going to hold various controls. I could set hbrBackground to COLOR_BTNFACE + 1, but then it will draw over the tab background. So I want this new child window to be transparent. So what I do is
set the class hbrBackground to GetStockObject(HOLLOW_BRUSH)
set container's extended style to WS_EX_TRANSPARENT
set the class WM_ERASEBKGND handler to do SetBkMode((HDC) wParam, TRANSPARENT); return 0; to set the device context and have Windows draw the transparent background.
So far so good, right? I'm not sure if I'm really doing all this correctly, and I'd like this to also be flicker-free, which doesn't seem to happen: when I resize the window (at least in wine) I get either flicker or garbage drawn (even in child controls, somehow!). Windows XP in a VM just shows flicker. I tried tweaking some settings but to no avail.
But wait, now I want to have another control, one that just draws some bitmap data. On the next tab, create another container, then have a third window class area as a child of that. area only draws in the upper-left 100x100 area and has scrollbars; the rest of the window area should be transparent.
Right now, what I have for area is:
the window class hbrBackground set to NULL and styles CS_HREDRAW and CS_VREDRAW set
the extended window style being 0
the WM_ERASEBKGND simply doing return 1;
the WM_PAINT filling the entire update rect with COLOR_BTNFACE + 1 before drawing, and rendering all of it
This is flicker-free, but obviously not transparent. NOW I'm really not sure what to do, because I want the area to be transparent in such a way that it shows the tab control background. Again, I tried tweaking settings to bring them closer to what I tried above with container, but I got either flicker or invalidation leftovers when I tried.
So how do I get both of these custom control types (the container and the drawing area) to be both flicker-free and transparent?
I presently must target Windows XP at a minimum, though if the solution would be easier with Vista+ only I'd be happy to keep that solution on the side in case I ever drop XP support (unfortunately Stack Overflow doesn't let me hand out silver medals...).
Thanks!
To paint your window in a manner that is "flicker free", you will need to paint your window to a bitmap, then copy the bitmap to the destination device context. On Windows XP, you will need to create a bitmap, adjust the origin of the drawing DC and then paint your window. On Vista and later you can use BeginBufferedPaint and its associated routines to do the buffering for you.
Once you have buffered painting working, you can then use WM_PRINTCLIENT to paint your window's parent window into the your drawing DC before you do any actual drawing. Unfortunately, not all windows will support WM_PRINTCLIENT.
You could consider using DrawThemeParentBackground, rather than WM_PRINTCLIENT directly.
Combining these two methods together will leave you with transparent flicker-free drawing.

Winforms semi-transparent PNG over semi-transparent PNG

I think I must be missing something obvious, but I'm unable to find this after several hours of searching. Is there no way to use a PictureBox or other control to contain an image with partial transparent/alpha-blended pixels, and place that over another image and have the blending be based on the image under it?
For example, this produces the results I want:
Place a panel on a form.
Add an OnPaint handler.
In the OnPaint handler draw 1 PNG, then draw another PNG over it, using Graphics.DrawImage for both.
This does not:
Place a PictureBox on a form and set it to a PNG.
Place another PictureBox on the form and set it to a PNG.
Place the 2nd picture box over the first.
...even if the 2nd picture box is just empty and has a background color of Transparent, it still covers the picture below it.
I've read this stems from all winform controls being windows, so by nature they aren't transparent.
...but even the 15 year old platform I'm migrating from, Borland's VCL, had several windowless controls, so it's hard to imaging winforms doesn't at least have some easy solution?
My first example above is one answer, true, but that adds a lot of work when you can only use one big panel and draw all of your "controls" inside of it. Much nicer if you can have separate controls with separate mouse events/etc. Even if not an image control, and a control I have to draw myself, that would be fine, as long as I can just put one image in each control. In VCL they called this a "paint box", just a rectangle area you could place on a form and draw whatever you want on it. Has it's own mouse events, Bounds, etc. If you don't draw anything in it, it is like it's not even there (100% transparent) other than the fact it still gets mouse events, so can be used as a "hot spot" or "target" as well.
The PictureBox control supports transparency well, just set its BackColor property to Transparent. Which will make the pixels of its Parent visible as the background.
The rub is that the designer won't let you make the 2nd picture box a child of the 1st one. All you need is a wee bit of code in the constructor to re-parent it. And give it a new Location since that is relative from the parent. Like this:
public Form1() {
InitializeComponent();
pictureBox1.Controls.Add(pictureBox2);
pictureBox2.Location = new Point(0, 0);
pictureBox2.BackColor = Color.Transparent;
}
Don't hesitate to use OnPaint() btw.
Sorry, I just found this... once I decided to Google for "winforms transparent panel" instead of the searches I was doing before, the TransPictureBox example show seems to do exactly what I need:
Transparency Problem by Overlapped PictureBox's at C#
Looks like there are 2 parts to it:
Set WS_EX_TRANSPARENT for the window style
Override the "draw background" method (or optionally could probably make the control style Opaque).

Windows Forms User Control not painting well in WPF application

So, this is the issue:
I have a Windows Forms User Control that I placed in main Window of my WPF application. I override paint method of User Control. It paints ok in "ideal" case. But, after showing the control in main window, I added MessageBox. This is the code snippet:
board = new BoggleBoard(Boggle.CurrentGame.Size);
boardHost.Child = board;
MessageBox.Show("You have " + time + " seconds to find as many words as you can. Click OK when you are ready to play);
If I don't show MessageBox, everything is ok. But with the code above, after MessageBox is shown, my control is painted, but just like boardHost (Windows Form Host) has lower opacity, so I get dark area around the control. I say "like" cause I tried with
boardHost.Opacity = 1;
but it doesn't help, I still get the same thing.
What might cause this problem?
Here is a screenshot. As obvious, the dark area around the board should not be there. And it is not visible if I don't show MessageBox after it is drawn.
http://i.stack.imgur.com/RZs2W.png

How should I display a notification bar in WinForms?

You all know the "You've got new answers!" notification bar on SO. I'd like the same thing in a Form, preferably just as smooth. Is there a simple way? Or do I have to completely create this myself?
My searches did not yield any good results, only lots of progress bars and popups in the system notification area, but that's not what I'm looking for.
The messages I want to display belong to a Form, not to the whole application
You could simply animate a panel dropping down from the top of the client area of the form.
Increasing the y coordinate of the panel in a timed loop. The panel would start invisible and slowly become visible. (The panel would start at -panel.height and work its way down to 0.)
Create two panels in your form, a notification panel docked to top, and below that a content panel anchored to top. In your Form.Load, set the height of the notification panel to zero. Don't set the height to zero in Design View, you won't be able to click on the notification panel to edit it.
Then when you get a notification, draw the contents in the notification panel and create a System.Windows.Form.Timer that increases the height of the notification panel by a few pixels every few dozen milliseconds or so. Stop when the panel is of the desired height. Do the same with a negative height to hide the panel.
This does not require repainting or recalculating sizes or positions of anything, does not overdraw anything, and looks slick. I have done this and it works.
If you want it constrained to a particular form, it should be easy enough to put a Panel on the form with its Dock set to DockStyle.Top, then place a label for the description and a button that hides it.
It's not difficult to do with a panel or a UserControl, but the fiddly part is making the contents of the form slide down as the bar slides down. To simplify that I would use a SplitContainer. The top splitpanel contains the notification bar and the splitter distance is initially 0. Slide the bar into view by incrementing the SplitterDistance property. Doing it this way means you don't have to worry about making the other contents of the form slide down (which is a hassle because it prevents you from using docking).
The only downside to using SplitContainer I can think of is that the animation of the bar will be slightly different: the text won't scroll down with the bar, it will be revealed in place as the bar slides down. If this bothers you, you can fix it by having the text (or your panel/custom control) slide down as you increase the splitter distance (only a couple more lines of code).
Showing the bar:
for (int i = 0; i <= 33; i++)
{
splitContainer1.SplitterDistance = i;
Thread.Sleep(5);
Refresh();
}
Hiding the bar:
for (int i = 33; i >= 0; i--)
{
splitContainer1.SplitterDistance = i;
Thread.Sleep(5);
Refresh();
}
Of course, if you don't mind the notification bar simply covering the top part of your form, then you can just do the whole thing very easily with a panel. :)
I was looking for the same thing just now and found this on code project
I have not used it yet, So I have no idea how solid it is.

Resources