Jump to content


Problem with windows in C# & Window Forms


9 replies to this topic

#1 dohtem

    New Member

  • Members
  • PipPip
  • 18 posts

Posted 06 September 2004 - 08: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 with syntax highlighting and better formatting.

#2 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 06 September 2004 - 10:30 AM

i guess, just from looking at it, that you resize it correctly, but the placement is wrong
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....

#3 dohtem

    New Member

  • Members
  • PipPip
  • 18 posts

Posted 06 September 2004 - 10:42 AM

davepermen said:

i guess, just from looking at it, that you resize it correctly, but the placement is wrong

View Post

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

#4 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 06 September 2004 - 11: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
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....

#5 snickkers

    New Member

  • Members
  • Pip
  • 1 posts

Posted 10 September 2004 - 01:08 PM

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;


#6 dohtem

    New Member

  • Members
  • PipPip
  • 18 posts

Posted 11 September 2004 - 08:24 PM

Holy crap... all i needed was one line. Thanks guys, Works great now.

DesktopBounds = Screen.PrimaryScreen.Bounds;


#7 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 12 September 2004 - 07:05 PM

nice you got it worked out!
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....

#8 rossfeld

    New Member

  • Members
  • Pip
  • 1 posts

Posted 30 January 2005 - 12:13 PM

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();
   }
  }


#9 anubis

    Senior Member

  • Members
  • PipPipPipPip
  • 2225 posts

Posted 30 January 2005 - 04:55 PM

in this case i don't mind... permission for resurection granted :)
If Prolog is the answer, what is the question ?

#10 apostille

    New Member

  • Members
  • Pip
  • 1 posts

Posted 12 April 2009 - 11: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;





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users