Painting problem in windows form - c#

I have an small appliction window , pops up when a tray icon clicks. Inside that i have provided context menu (windows form context menu), the problem is, whenever i rightclicks on form, the context menu is appearing but on disappearing of context menu, border of context menu still exists. As i minimises/closes and reopen every thing works fine again.
Any one has any idea why it is behaving like so?
Ravi Naik.

Set DoubleBuffering to true on the form. This should sort the problem out.
Unless you are drawing the stuff yourself using a Graphics object? Are you?
If you are then look up Double Buffering, you can find out about this at a few places, such as Bob Powell and Code Project.
If it doesn't and you want a quick hack to get around it, on the context menu collapse event you could call form.Refresh() but avoid this at all costs.

Related

Take a screenshot behind window in C# WinForm

I'm working on an app in c# windows forms, I have a form called Form1 with a button, and after click this button the idea is to take a screenshoot of everything behind it except the Form1 itself (without minimizing Form1).
Any idea or suggestion?
Thank you!
If you want a solution without hiding, moving, minimizing etc., you need the following approach:
EnumWindows() to iterate all windows. This will give you the windows top to bottom.
Don't forget to exclude your own window
Exclude windows that are invisible. Check out GetWindowLongPtr with GWL_STYLE and compare against WS_VISIBLE
GetWindowRect() to get their size
PrintWindow() to get a bitmap of the window, no matter whether it's in the background
Create a bitmap with the size of the VirtualScreen
Paint the windows in reverse order (bottom to top) using DrawImage()
I found that this is quite fast (514 ms on a 2560x1440 screen with 20 visible windows to draw).
Limitations:
since it sends a WM_PRINT message to the application, you can not capture applications that are "not responding"
For me, Firefox does not render well. It's missing page contents.
For me, control panel content appears black, although it's there when getting a screenshot of the whole screen.
you can use hard code. Actually light shot and other tools working such as. When you click to screenshot button you may close the form window after the event and reopen it. it is the basic way of solution...
Please check this way: How can i capture the screen without the Form?

App has too many tabs opened in the task-bar

I'm making a WinForm app in C# (Visual studio) for fun. I came across a low-priority problem. It doesn't affect how the app works. But...
On the starting page, I have a button to open a Form1, inside Form1, there's a button to open a Form2 and so on.
At runtime, when I open a form, its parent form does not close and every form is a separate tab in the task-bar. As the layers grow, I'll have more-than-acceptable amount of tabs down there...
Is there a way to have only 1 tab?
I've tried:
Adding a parentForm.close() line when opening the form, but that was bad.
Instead of creating another form, putting everything in a panel, and bringing out another panel using code, but if there are too many layers, the code gets ridiculously long.
There has to be a simpler way right? Please shed some light.
Yes, you have a property in every form called ShowInTaskbar which is true by default. You can change that in the form properties under Window Style section or changing it by code manually:
Form2.ShowInTaskbar = false;
Form2.ShowDialog();
Configure to false all forms but the first one in order to achieve your desired behaviour.
Make sure your opened forms are dialogs or you are put them on top so user can never get in the situation where the form is behind and they cannot close it.
Anyway, with a proper form parenting configuration (if it fits your needs) you won't need this, as children forms won't appear in the taskbar.
If a form is parented within another form, the parented form is not displayed in the Windows taskbar.
Make sure you check the MSDN Documentation about this.

VS Designer Now Showing MainMenu

I can't seem to fix this issue (which i hope is small) i have somehow when adding a ContextMenu did something to have the MainMenu bar not show up when i debug, if i click on the "MainMenu" in the designer (1st image) the menu bar shows up in the designer fine:
But if i click off it, then click on another control like:
The MainMenu disappears from the designer, when i build / debug the MainMenu is gone, is there a way to fix this easily enough (i assumed it would be as simple as checking a setting but googling did not return anything that i saw that was helpful)
Any help would be appreciated.
It looks like your MainMenu has ended up hidden behind your other controls. Try right clicking on the main menu and selecting Bring To Front. If you can't right click on it, right click on the control that's filling the rest of the form and select Send to back.
When you click on a menu control in the component tray it will always show it at the top of the screen (even if you click on a ContextMenu).
One other thing worth doing is to open the Document Outline window from the View->Other Windows menu in Visual Studio. This gives you a hierarchichal view of all the controls on the current form. You should be able to see your main menu here (unless you've somehow removed it from your form!)

WPF SpellCheck context menu in WinForms won't extend past control

So I have an odd issue while trying to use a WPF TextBox with SpellCheck enabled inside a Winforms form, using the custom control defined in this post.
This works just fine, I've tried using it in my own sandbox project and it looks great. The problem is when I try to replace the Winforms TextBoxes inside one of our existing forms. The SpellCheck context menu is "trapped" inside the control, so all I can see are the up/down arrows of the menu.
I'm not too familiar with Winforms, as I mostly work with WPF, so I'm having a hard time determining the problem. The text box shown above is the custom control as mentioned in the linked post. The control is contained in a forms TableLayoutPanel. How can I get the context menu to expand and actually be visible?
Edit
First, to clarify, the context menu appears normally when displayed in a console application that opens the form. I did a little more testing and created a new web project (what the original problem project is) with a button that opens the form, and then the problem behavior occurs. What difference between the console application and the web application would cause this?
Edit 2
I tried the suggestions in this post, thinking maybe the context menu really was just being automatically sized incorrectly (to fit the parent control). But none of those solutions seemed to make any difference either.
Edit 3
After using snoop to look at the differences between the working context menu (windows application) and the not-working one (xbap) the only difference I could see was that the DesiredSize of the xbap instance's context menu has a smaller size, I just have no idea why.
The wpf control is a control which is hosted in winforms via the ElementHost.
Hence it lives within the area provided by the parent.
The only way is to expand it, is to change the size of the parent. Give it a try by creating a winform supersized textbox as a quick debug example. Note whether the size change works on not. If it does can the GUI accommodate that change? I can't answer that.

dynamically changing a non-rectangular window in c#?

I am working on an application using WPF and visual c#, and I am trying to implement an annotation feature which allows for drawing on the desktop.
My current way of accomplishing this is by making a total screen-sized window with a transparent background, and putting this over the desktop, allowing me to "write" on this invisible window.
However, I have another set of windows that constitute a floating menu of sorts on the screen, and I want this menu to always be interactable, even when the user is annotating (for example, this is where I want to put some annotation options).
When I show my transparent window, however, my menu windows are all behind it, which means i draw over these windows instead of clicking on them. I have tried things like setting topmost on the menu windows, but this does not put them over the transparency.
My current idea now is to make a non-rectangular window out of the transparent window and basically just cut out the region the floating menu is in, and dynamically update this if the user drags the menu to another place on the screen. Is this feasible/possible, and how can I dynamically make these window changes? if this is not possible, is there a better way of forcing all my menu windows to always be on top of the annotation transparency?
edit: as an additional note, is there any way at all to set a z-index of sorts on these windows? that would resolve this, I think, if I could z-index the menu windows all the way to the front and then index the transparency to one behind that, but I was unable to find a way to do this.
edit: someone commented and suggested I re-set topmost = true for my menu windows once I create my transparent window, but this ended up having no effect
FINAL EDIT: I fixed the issue using David Edey's suggestion of setting topmost; turns out I had a rogue line of code setting the transparency as topmost=true, causing this issue when setting the window topmost properties, but now it works like a charm. Thank you so much for your help!
I have built a replica version myself, and setting:
Application.Current.MainWindow.Topmost = false;
Application.Current.MainWindow.Topmost = true;
Brings that window to the front without fail (note that you do indeed need the false call first to actually refresh it sometimes... Not entirely sure why this happens, but it works!) :)
(Obviously replace the call to Application.Current.MainWindow with a reference to your Menu window).
...another set of windows that constitute a floating menu of sorts on
the screen, and I want this menu to always be interactable,
My recommendation is to place the Menu window controls in question into a single control with dependency properties which expose the business logic of the menu. Make sure the aforementioned properties are two-way bound to INotify properties on a viewmodel which ultimately will be accessed/shared with the annotation window.
When the annotation window comes up, it also creates its own menu window control which has the same zorder as the annotation window. The old original menu is hidden(?) or the new one comes up in its location(?).
By design the new menu window properties are also two-way bound to the shared VM. Hence allowing for changes from the annotation menu to go back to the original window via the shared properties.
This slight of hand with a new control, behaves to the user like the other menu control and also changes the data dynamically, for both annotation and the original. Also the second menu doesn't have the problematic z-order issues which you are running into.

Categories