PDA

View Full Version : Problem with windows in C# & Window Forms


dohtem
09-06-2004, 02:33 AM
Hi guys, I was following a tutorial on the Drunken Hyena website and I decided to do things differently (make my own mistakes and learn). Well I have hit a brick wall.

All this code does is create a window, It askes the user whether they want fullscreen or windowed, then makes it. They can also hit 'f' or 'w' to toggle between windowed and fullscreen after the window has been created. My problem is that when you choose windowed first, then hit 'f' to move to fullscreen mode, it makes a fullscreen sized window but it doesnt fill the screen with it. Hard to explain, please take a look. Any ideas?



using System;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

public class WindowsPrimer : System.Windows.Forms.Form
{
// full screen or windowed
protected bool m_fullscreen = false;

// window size
protected const int m_width = 800;
protected const int m_height = 600;

// used for window title and other prompts
protected const string m_name = "Windows Primer";

public WindowsPrimer()
{
// ask user fullscreen or windowed mode
//m_fullscreen = Utility.AskFullscreen(m_name);
if (MessageBox.Show("Do you want to run in fullscreen mode?", m_name,
MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk)
== System.Windows.Forms.DialogResult.Yes)
{
m_fullscreen = true;
}

//Set the window up based on the user's choice above
ConfigureWindow();
}

protected void ConfigureWindow()
{
AutoScale = false;
Name = m_name;
Text = m_name;
BackColor = System.Drawing.Color.Black;

// we dont want to see the minimize or maximize buttons
MaximizeBox = false;
MinimizeBox = false;

// center the form
StartPosition = FormStartPosition.CenterScreen;

// necessary for the first time
FullScreen(m_fullscreen);
}

private void FullScreen(bool status)
{
System.Drawing.Rectangle rect;

if (status)
{
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
rect = System.Windows.Forms.Screen.PrimaryScreen.Bounds;

// ClientSize (member of Form) is just the drawable area
ClientSize = rect.Size;
Cursor.Hide();
}
else
{
ClientSize = new System.Drawing.Size(m_width,m_height);
FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
//Fixed3D means we cant resize the window
}
}

protected override void Dispose(bool disposing)
{
if(disposing)
{
//If we had stuff to dispose, we'd do it here
}
base.Dispose(disposing);
}

protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
switch(e.KeyCode)
{
case Keys.F:
m_fullscreen = true;
FullScreen(m_fullscreen);
break;
case Keys.W:
m_fullscreen = false;
FullScreen(m_fullscreen);
break;
case Keys.Escape:
this.Close();
break;
default:
base.OnKeyDown(e);
break;
}
}

protected override void OnClick(System.EventArgs e)
{
this.Close();
}

static int Main()
{
Application.Run(new WindowsPrimer());
return 0;
}
}

Any help would be appreciated. Thanks.

edit: Same code is posted here (http://muer.njoerdba.com/paste/view.aspx?id=7a93921d-2644-4ad8-ba84-8589534108ce) with syntax highlighting and better formatting.

davepermen
09-06-2004, 04:30 AM
i guess, just from looking at it, that you resize it correctly, but the placement is wrong

dohtem
09-06-2004, 04:42 AM
i guess, just from looking at it, that you resize it correctly, but the placement is wrong
10664

It is and I realize that. I just don't know how to fix it.

davepermen
09-06-2004, 05:00 AM
when going to fullscreen, you can do like that:

if (status)
{
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
DesktopBounds = Screen.PrimaryScreen.Bounds;
Cursor.Hide();
}


the rest is more complicated, as you have to correctly recalculate size, and position, to have a window in the middle of your screen.. have fun :D

snickkers
09-10-2004, 07:08 AM
I've just been trying to do the exact same thing as you (well, I dont want windowed mode, I only want full-screen). The way your doing it is perfectly fine, however, you MUST set "borderstyle=none" BEFORE you assign the size/bounds of the form...

Here's the simple gist of it...
private void MainWindow_Load(object sender, System.EventArgs e)
{
// 1. Hide the border & titlebar
FormBorderStyle = FormBorderStyle.None;

// 2. Set our window bounds to the edge of the monitor's bounds
DesktopBounds = Screen.PrimaryScreen.Bounds;
}
Also, I wanted to mention something that took me a million years to discover, even though it should have been quite obvious. I use 2 monitors, and I wanted to make this fullscreen in my second monitor. So, to use my second monitor, I actually used this code
// 2. Set our window bounds to the edge of the 2nd monitor's bounds
// (well, actually, the *last* monitor's bounds)
DesktopBounds = Screen.AllScreens[Screen.AllScreens.Length-1].Bounds;

dohtem
09-11-2004, 02:24 PM
Holy crap... all i needed was one line. Thanks guys, Works great now.


DesktopBounds = Screen.PrimaryScreen.Bounds;

davepermen
09-12-2004, 01:05 PM
nice you got it worked out!

rossfeld
01-30-2005, 05:13 AM
Hi guys. I found this on google and it was actually one of the more useful examples I could find. I appreciate the help. I might have to stick around for a bit to see what else you guys are talking about. :p

Anyway, I realize this thread is a bit old, but I figured I would add a clean, working example that I'm using and so far seems to work well in case others stumble across here. Hope nobody minds. The code sets the form back to normal mode if it's in maximized state so that you don't lose those values. The flicker isn't so bad since I suspend the layout during the operation.


// these are the member variables I'm using
private FormWindowState _oldWindowState;
private System.Drawing.Rectangle _oldDesktopBounds;
private System.Drawing.Size _oldClientSize;
private bool _isFullScreenEnabled = false;

/// <summary>
/// Sets or Unsets Full-Screen mode for this form, saving the old state values.
/// Note, the order of calls in this function is important.
/// </summary>
/// <param name="bFullScreen">set to fullscreen if true, unset if false</param>
void SetFullScreenMode(bool bFullScreen)
{
// enable full screen mode only if we're NOT in fullscreen
if (bFullScreen && !_isFullScreenEnabled)
{
this.SuspendLayout();

// get current window state
_oldWindowState = this.WindowState;

// get the normal window state so we don't lose those values
if (this.WindowState == FormWindowState.Maximized)
this.WindowState = FormWindowState.Normal;

// get the normal state values
_oldClientSize = this.ClientSize;
_oldDesktopBounds = this.DesktopBounds;

// jump to full screen
this.FormBorderStyle = FormBorderStyle.None;
this.DesktopBounds = Screen.PrimaryScreen.Bounds;

_isFullScreenEnabled = true;

this.ResumeLayout();
}

// disable full screen mode only if we're in fullscreen
if (!bFullScreen && _isFullScreenEnabled)
{
this.SuspendLayout();

// reset the old state
this.DesktopBounds = _oldDesktopBounds;
this.ClientSize = _oldClientSize;
FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
this.WindowState = _oldWindowState;

_isFullScreenEnabled = false;

this.ResumeLayout();
}
}

anubis
01-30-2005, 09:55 AM
in this case i don't mind... permission for resurection granted :)

apostille
04-12-2009, 05:09 AM
I've just been trying to do the exact same thing as you (well, I dont want windowed mode, I only want full-screen). The way your doing it is perfectly fine, however, you MUST set "borderstyle=none" BEFORE you assign the size/bounds of the form...
Here's the simple gist of it...
private void MainWindow_Load(object sender, System.EventArgs e){
// 1. Hide the border & titlebar
FormBorderStyle = FormBorderStyle.None;
// 2. Set our window bounds to the edge of the monitor's bounds
DesktopBounds = Screen.PrimaryScreen.Bounds;}
Also, I wanted to mention something that took me a million years to discover, even though it should have been quite obvious. I use 2 monitors, and I wanted to make this fullscreen in my second monitor. So, to use my second monitor, I actually used this code
// 2. Set our window bounds to the edge of the 2nd monitor's bounds
// (well, actually, the *last* monitor's bounds)
DesktopBounds = Screen.AllScreens[Screen.AllScreens.Length-1].Bounds;