How do you resize the Flash Player or to be more precise the width of window showing your animation? Whereas with AIR this is done with few lines, the Flash Player will not allow you to resize its window. The first idea when trying to solve this issue was to write something like : stage.width = 1024; . Yes, this could work but this value can’t be overwritten and the Flash Player won’t be resized. But how to realize this, let’s say if you have several screen resolution to target and want to automatically resize the Flash Player? Here is my solution
Overview
Here is the solution in form of an architecture picture. To simplify: we will trick the Flash Player by calling a .bat file placed in a directory named fscommand. This .bat will then call an .exe that will catch the window handle (over C#) of your Flash Player and then resize the Flash Player window. Tricky, isn’t it? It’s the only way to realize this and here is the step to step tutorial.
Step 1: fscommand
One command that is available in Flash Player to communicate with the operating system is fscommand. With fscommand you can start any .bat (not exe) placed in a directory named fscommand. So now we can write following AS3 code:
function resizeFlashPlayer(aHeight:int,aWidth:int):void { trace("Now resizing to: " + aHeight + " // " + aWidth); var executeCommand:String = "resize_"+ aHeight + "x" + aWidth + ".bat"; fscommand("exec", executeCommand); }
But why using a concatenated string to build the path to .bat ?
The problem with fscommand is that you cannot pass a variable to .bat. It also means that you will need to generate a .bat for each screen resolution. But come on, it’s not a major issue 😉 Here is the swf we are going to build to test the whole workflow. You can download the whole project at the end of this post.
Step 2: Setting the .bat file
The .bat will help us to launch an exe. Why using a .bat and not directly the exe? fscommand doesn’t allow to directly launch the exe, so we are using a .bat as what can be called a proxy. Here is the content of the .bat file (named rezise_800x600.bat):
@echo off start /b ResizeFlashPlayer.exe 800 600
Step 3: Creating the ResizeFlashPlayer.exe with C#
Here comes the trickiest part. In order to be able to control the Flash player we are going to use C# and the SetWindowPos function to resize our window. In order to find the Flash Player we are going to crawl over the processes of Windows and catch every window title name. When we have found the window with the name “Adobe Flash Player 10” we have found the currently running Flash Player instance. Once found we can then use a pointer on it and apply the SetWindowPos function that will allow to set the width (cx) and height (cy) of the window.
In order to have some more flexibility, I’ve choosen to pack it as a ConsoleApplication and to use two arguments (remember that we are calling ResizeFlashPlayer.exe over a .bat).
namespace ConsoleApplication { class Program { [DllImport("user32.dll", EntryPoint = "SetWindowPos")] public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags); static void Main(string[] args) { Process[] processes = Process.GetProcesses(); foreach (Process p in processes) { if (p.MainWindowTitle.IndexOf("Adobe Flash Player 10") != -1) { IntPtr pFoundWindow = p.MainWindowHandle; Debug.WriteLine("Flash Player was found"); if (1
Step 4: Publish and build with the Flash Player Projector
Last step: Open the Flash Player (http://www.adobe.com/support/flashplayer/downloads.html), select your .swf and click on create a Projector. This will generate a projector for your swf: press one of the buttons and you will see how the Flash Player will directly resize!
That’s it! Enjoy the code and if you want to test it, download the code below
💾Download the code of the C# and the Flash example application