Jim Lawless' Blog


Switching a Console Window to Full Screen or Windowed Mode

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

Please note! If you're having difficulties compiling the C source code presented below, please see my post: Compiling C from the Command Line with Pelles C

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:

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



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

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


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

An Embedded Mini-Interpreter

Choose your own Adventure with Sinatra

Invoking the Default Windows Screen-Saver

My Journey Began with BASIC

Blog Posts by Category

BPL: Batch Programming Language Interpreter

Computers I Have Known

Auto Save Images from the Clipboard

Screen Capture from Multiple Monitors in Java

WSH2EXE part 1


Full List of Posts

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


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service