Maximize window in XNA 4.0 - c#

I am currently using the following code to allow the user to resize and maximize the window:
Window.AllowUserResizing = true;
Window.ClientSizeChanged += Window_ClientSizeChanged;
with the Window_ClientSizeChanged event handling the changes and re-scaling the drawn images etc.
This allows me to resize the window however much I want and also maximize it using the standard button in the window's handle. I would like to be able to start the application with the window in its maximized mode but can't figure out how to do so. I know I can use graphics.IsFullScreen = true; to show it in full screen mode but I would like to be able to run the program in a maximized, windowed mode. Any ideas?

You can put this code in the constructor to do what you want:
Form form = (Form)Control.FromHandle(Window.Handle);
form.WindowState = FormWindowState.Maximized;
This requires that you add a reference to System.Windows.Forms to your project.
This does have the downside that it actually sets up the graphics device twice on startup. Once in the normal way, and then once again because the window was resized. (Although this all happens before the form is first displayed.)
It has the advantage of being extremely simple to implement.
I'm going to poke around for awhile and see if the initialisation order can be changed...
There really is no simple way to get around this problem, it would seem.

Related

"Tint" an entire window, or alternative method of making window look inactive

I have a winforms C# application that opens multiple dialog boxes. To suit the style of the application, I have removed the default title bars for each window and created my own (with control buttons and drag-to-move function).
The problem that now faces me is that without a titlebar, the user has no way of telling which window is the 'active' window when they are manually moved apart (so they are not overlapping).
In any windows application (that uses titlebars), when you try to navigate away from a dialog box back to the main program (without closing the dialog box) - it wont let you. The border of the dialog box flashes and you hear a windows error sound. Some kind of equivalent visual feedback would be great without needing to have the default titlebars - and tinting an entire window darker seems like it would do the trick nicely.
Something like this in pseudo-code, which would nicely tint the parent window whilst a dialog is open:
// tint window now
window.ShowDialog();
// un-tint window
I have tried to place a panel covering everything with colour set to 'transparent' (with the intention of later controlling the opacity of the panel) but the transparency does not seem to work. Any other ideas of accomplishing this? Or does anyone have a better suggestion to achieve the same level of visual feedback?
Summary:
Is there any way to tint an entire window, or overlay it with a colour? If not, could anyone suggest an alternate method of making the window appear 'inactive'?
I would suggest you to create a method in forms you want to disable:
void DisableForm()
{
//some fancy color
this.BackColor = System.Drawing.Color.Khaki;
//and disable all controls owned by form, just to be sure
foreach (var s in this.Controls)
{
((Control)s).Enabled = false;
}
}
and functions which enables back those forms of course.
edit.
also you can set visibility property of controls to false

Skip Aero Window Animation

How can I disable the "opening" animation of a window under Aero programatically?
When opening a new Form it "pops in" (fade in + a slight scaling transformation).
I want to stop this animation and show the window instantly.
I already tried to set the Location property of the Form to somewhere offscreen, then calling Show(), and then moving it at its correct location.
But that doesn't help, the animation will continue.
Maybe there is some hidden property I can set?
I don't want to disable open/close/minimize/maximize animations globally!
I just want to skip the "window-open" animation of my window.
I already played around with single and multiple calls ShowWindow(...) directly after Form.Show(). But no matter what parameters I pass, doesn't abort the opening-animation.
I've got it! After some trying around with ShowWindow, BorderStyles I found my exact solution:
Change the initial "FormBorderStyle" property of the form to one of those:
None
FixedToolWindow
SizeableToolWindow
Add a eventhandler to the Forms "Shown" event.
Inside the eventhandler, change the FormBorderStyle to "Sizeable" (or any other).
Now the trick is that "none" and "*toolwindow" borderstyles will suppress the opening/popup animation for that form. Than, as soon as the form is being displayed the borderstyle is changed, giving it the original functionality (Icon in the Control-Bar, Minimize/Maximize Buttons etc...)
Edit: For everyone who might want to try this too, I have to point out that this can will screw with the actual size of the window when done with PInvoke commands.
If you rely on the size of the window being correct, be sure to resize the the window to its intended size after you done this.
This is part of windows visual effects and can be adjusted using the SystemParametersInfo Method.
I found that the animation is only take place when the form is shown for the first time.
So here is the trick:
var form = new Form();
form.Show();
form.Hide();
form.Show();
I tested it only in Windows 8
You can change the style before and after, like this, which will prevent the fade-out animation.
this.FormBorderStyle = FormBorderStyle.Sizable;
this.Show();
// Do whatever
this.FormBorderStyle = FormBorderStyle.Sizable;
this.Show();

Get window handle underneath mouse pointer, while ignoring a semi-transparent window

I've created a semi-transparent form (60% opacity with black background color) that my app launches, maximized, over the entire screen. Basically, it casts a gray color on the entire desktop.
When the user mouses-over a window on the desktop, I want to get that window's handle (hWnd).
The easy way to do this, which is working for me, is:
Temporarily hide my form (OR, temporarily set my form's opacity to 0.0)
Call [GetCursorPos][1]
Call [WindowFromPoint][2]
Show my form again
The problem with this approach is that my form / the screen blinks, which I don't like.
I've tried to fix this in two ways:
I figure there should be a way to get the hWnd of the window directly underneath my form by calling ChildWindowFromPointEx (passing-in the hWnd of the desktop and CWP_SKIPTRANSPARENT), but it doesn't seem to work. I also played with [ChildWindowFromPoint][4] and [RealChildWindowFromPoint][5] with no success. (P.S. Raymond Chen discusses the differences between these calls, here and it seems to me that ChildWindowFromPointEx is designed to do exactly what I need)
I tried preventing the entire desktop from refreshing (kind of "freezing" the screen momentarily) by using (1) SendMessage(GetDesktopWindow(), WM_SETREDRAW, false, 0) before I hide my form and (2) SendMessage(GetDesktopWindow(), WM_SETREDRAW, true, 0) after I hide my form. This didn't work quite right: some areas of the screen would freeze, some weird black blocks would appear, etc. I do know, however, that (1) does work, because one time I called (1) and didn't call (2) and my desktop appeared completely frozen (had to reboot, even TaskMgr didn't render correctly). I also tried using SuspendLayout and ResumeLayout on my form, but I don't think they are meant to handle my case.
Any help would be greatly appreciated.
You can do the checking yourself since your need to customise beyond that what the standard functions offer.
Call EnumWindows() to get a list of top-level windows.
Remove your semi-transparent window from this list.
For each window in the list use PtInRegion() to determine whether or not the mouse is over the window. Remove any windows that don't fit the bill.
Use GetNextWindow(), starting from one of the remaining windows to walk the z-order and find out which of the candidates is at the top.

C# Fullscreen, hiding the taskbar

I have recently written an application for my daughter, which is a kid-free zone where she has all unnecessary key presses ignored (windows key, Esc etc) but the problem I am having is that when I use the following code:
targetForm.WindowState = FormWindowState.Maximized;
targetForm.FormBorderStyle = FormBorderStyle.None;
targetForm.TopMost = true;
I am able to HIDE the taskbar, but it is not truly overlayed. When I move the mouse to where the taskbar would be, and click, it pops up, also, using this code and running external applications withing my windows form, I am left with this windows form keeping itself on top.
If anyone could help me with a proper way to display my windows form as a true fullscreen application, and be able to run external applications from within the form and have them prioritize themselves on top, that would be greatly appreciated.
In case you missed it, I am using VS2010, C# and winforms.
Thanks in advance!
The proper way to make a full-screen app is to just put something like Bounds = Screen.PrimaryScreen.Bounds; in your main form. Then when your app has focus it will cover the task bar.
You also probably want FormBorderStyle = FormBorderStyle.None;
The order of the performed actions is incorrect.
You should first hide the border (FormBorderStyle=None), and then set the window state to maximized. You even don't have to set TopMost to true.

How to bring up a message popup in C#?

Most recently, while at my mom's house, a phone call came in and the caller ID popped up in a banner on her TV (Comcast). I've seen a similar functionality when the McAfee brings up a virus warning. It was a translucent popup window with the company logo, message and a button or two.
I'd like to mimic this behavior (via C#). This will event driven. My experience in C# is pretty limited, so I'm still feeling out the different libraries. Are there any ideas on where I should start?
I recommended to use WPF. Create new window, that will popup and set next properties:
WindowStyle="None"
AllowsTransparency="True"
Opacity="0.5" //50% transparent
Topmost="True"
Background property will set color of window.
Place on window any controls what you need.
Create this window and show when some event happens:
YourWindow popup = new YourWindow(/*possible args for message on popup, for example*/);
popup.Show();
To place your window in bottom-right corner, as all popups, use next code in windows Loaded event:
this.Left = SystemParameters.WorkArea.Width - this.Width;
this.Top = SystemParameters.WorkArea.Height - this.Height;
How to make animation of window movement you can read in other questions.
If your app is running in the background, you can simply pop up a window and set it to topmost.
Exactly what you do beyond that is going to depend on what type of UI are using (WPF/WinForms.) WPF makes it easier to build a transparent form, as described here:
http://blogs.interknowlogy.com/2007/06/20/transparent-windows-in-wpf-2/
Transparency in WinForms is a little bit harder, but there are some posts about it:
Partial transparency with C# .NET 3.5 WinForms?
A couple of things you will want to do with your pop-up window:
Disable the minimize/maximize/close buttons
Disable the borders
Just put those in your form so it looks better.
-- Dan

Categories