Jim Lawless' Blog


Switching a Console Window to Full Screen or Windowed Mode

Originally published on: Tue, 20 Apr 2010 03:00:23 +0000

About a month ago, I wrote about using Windows Console functions to change the active text color of the console window.

See http://www.mailsend-online.com/blog?p=78.

I now present some Console API code that will allow one to change the active console screen to full-screen mode or to windowed mode.

fullscreen.c


// Switch a console window to full screen or windowed mode.
//
// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include <windows.h>
#include <stdio.h>
#include <string.h>

void *getConsoleFunction(char *name);

BOOL (WINAPI *doSetConsoleDisplayMode)(HANDLE hConsoleOutput,DWORD dwFlags, void *coord);
void syntax();

int main(int argc,char **argv) {
   HANDLE hCon;
   int mode;
   
   if(argc<2) {
      syntax();
      return 1;
   }

   if(!stricmp(argv[1],"-f"))
      mode=1;
   else
   if(!stricmp(argv[1],"-w"))
      mode=2;
   else {
      syntax();
      return 1;
   }
   
   hCon=GetStdHandle(STD_OUTPUT_HANDLE);
   
   doSetConsoleDisplayMode=getConsoleFunction("SetConsoleDisplayMode");

   if(doSetConsoleDisplayMode==NULL) {
      fprintf(stderr,"Sorry! fullscreen.exe is incompatible with this version of Windows.");
      return 1;
   }
   (*doSetConsoleDisplayMode)(hCon,mode,NULL);

   return 0;
}

void *getConsoleFunction(char *name) {
   static HMODULE kernel32=(HMODULE)0xffffffff;
   if(kernel32==0)
      return NULL;
   if(kernel32==(HMODULE)0xffffffff) {
      kernel32=LoadLibrary("kernel32.dll");
      if(kernel32==0)
         return 0;
   }
   return GetProcAddress(kernel32,name);
}

void syntax() {
   fprintf(stderr,"fullscreen - Change console window to full screen or windowed mode\n");
   fprintf(stderr,"by Jim Lawless\n\n");
   fprintf(stderr,"Syntax:\n\tfullscreen -f\n");
   fprintf(stderr,"or\n");
   fprintf(stderr,"\tfullscreen -w\n\n");
   fprintf(stderr,"Where -f will force the console window into full-screen mode\nand -w will force the console window into windowed mode.\n");
   fprintf(stderr,"\nSee:\nhttp://wp.me/pvgM9-d1\nfor usage instructions and C source code.\n");
}

The SetConsoleDisplayMode() function accepts three parameters:

  • A handle to the console screen buffer
  • A code indicating which mode to set... 1 sets full-screen mode, 2 sets windowed mode.
  • An optional pointer to a COORD data structure

I omit the COORD data structure reference in the above code so that the given program will simply change between modes, but will not attempt to change to a custom size.

To change a console screen to full-screen mode, enter the following at a command-prompt or in a batch file: fullscreen -f

To change to windowed mode, enter the following: fullscreen -w

The source code and executable file for fullscreen can be downloaded from a single archive at: http://www.mailsend-online.com/wp/fullscreen.zip

Unless otherwise noted, all code and text entries are Copyright ©2010 by James K. Lawless

del_icio_us Save to del.icio.us
stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks



Previous post: CP/M Days
Next post:Blogoversary


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

Auto Save Clipboard Images Redux

Extending SpiderMonkey JavaScript on Windows

Rhino JavaScript to EXE with launch4j

Compiling Rhino JavaScript to Java

Directory Traversal in Rhino JavaScript

Taking Shape

We've Moved!


Popular Posts

A Command-Line MP3 Player for Windows

Auto Save Images from the Clipboard

Java in a Windows EXE with launch4j

An Interview with Tom Zimmer: Forth System Developer

Setting Windows Console Text Colors in C


Random Posts

Tracing XSLT with a Tiny Java Web Server

An Interview with Game Developer James Hague

Blogoversary

Internet Protocols and Rhino JavaScript

Rhino JavaScript to EXE with launch4j

Invoking the Default Windows Screen-Saver

A Quine in C

A Scrolling Banner using Canvas and JavaScript

Changing the C64 Text Color in C

An Interview with the Creator of the BDS C Compiler


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer