View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

4.9 | 12 ratings Rate this file 19 Downloads (last 30 days) File Size: 27.6 KB File ID: #31437 Version: 1.4
image thumbnail



Jan Simon (view profile)


13 May 2011 (Updated )

Set figure size, top-most, transparency and pixel mask, multi-monitor etc by Windows API

| Watch this File

File Information

WINDOWAPI - Set window properties using the Windows API
There are a lot of functions in the FEX to set the figure properties, e.g. maximized, minimized, always-on-top, level of transparency (using Java).
This submission adds 4 new features:
1. Maximize a window with or without visible taskbar or menu bar.
2. Set transparency level of figure and stencil-color: pixels of a this color are 100% transparent.
3. Placing figures on multi-monitor setups.
4. Clip region: draw only pixles inside a specified rectangle (e.g. splash screen).
5. Lock cursor: Especially for games it can be useful to keep the mouse cursor in a specified rectangle.
WindowAPI(FigH, Command)
  FigH: Matlab's figure handle or Windows HWND
    TopMost: Window is topmost even if the focus is lost
    NoTopMost: Disable the topmost state
    Front: Move window on toptemporarily
    Minimize: Minimize the window
    Restore: Restore to former size
    Maximize: Full screen, taskbar and menubar visible
    XMax, YMax: Maximize figure horizontally or vertically only
    Position: Set inner figure position to 'work' (taskbar visible), 'full' (taskbar hidden),
                or [X,Y,Width,Height].
                Monitor index can be specified also
    OuterPosition: As Position, but with figure border and titlebar
    Flash: Short flashing
    Alpha: Set the figure's transparency between 0.0 and 1.0.
                4th input [R,G,B]: Pixels with this color are 100% transparent in addition
    Opaque: Release memory needed for alpha blending
    Clip: Draw only pixels inside a rectangle
    ToScreen: Move figure completely to nearest monitor
    SetFocus: Gain keyboard focus to figure (figure(FigH) is buggy)
    LockCursor: Limit the cursor motion to a rectangle
    SetFocus: Activate figure
    Enable: Enable or disable all GUI elements
    Hide, Show: Hide the window without changing its Os handle
    Button: Set visibility of Max, Min, Close buttons in the window border

Reply = WindowAPI(FigureHandle, Command)
    GetStatus: Replies 'maximized', 'minimized' or 'restored'.
    GetHWnd: Reply the Windows handle of the figure.
    Position/OuterPosition: If used with 2 inputs, the position relative to current monitor and the monitor index is replied.
    Monitor: Get index and position of monitor.

  This function calls Windows-API functions => No Linux, no MacOS - sorry.
  The function is written in C and must be compiled before using by the mex command. See WindowAPI.c for instructions.
  Depending on the Matlab version, the MEX is compiled for HG1 or HG2 automatically. Both versions can exist
  in parallel.
  Suggestion for other OS are are very appreciated!

Maximize the current figure:
  WindowAPI(gcf, 'maximize')
A semi-transparent figure with through-looking background:
  FigH = figure('Color', ones(1,3));
  WindowAPI(FigH, 'Alpha', 0.5, [255,255,255]);

Draw a button without surrounding figure ("splash button"):
  FigH = figure;
  ButtonH = uicontrol('Style', 'PushButton', 'String', 'Bye', ...
      'Units', 'pixels', 'Callback', 'delete(gcbf)');
  WindowAPI(FigH, 'Clip', get(ButtonH, 'Position'));

Tested: Matlab 6.5, 2008a, 2009a, 2011b/64, 2016b, WinXP/32, Win7/64, MSVC 2008/2010, BCC5.5
Assumed compatibility: Higher Matlab versions.

See also:
ShowWindow, Matthew Simoneau:
Window Manipulation, Phil Goddard:
api_showwindow, Mihai Moldovan:
maxfig, Mihai Moldovan:
setFigTransparency, Yair Altman:


Api Showwindow and Set Fig Transparency Set Figure Transparency / Fading inspired this file.

This file inspired Matlab Loadlibrary User32.Dll For Windows Functions In One Class Working Video Proof and Cmd Win Tool.

MATLAB release MATLAB 7.8 (R2009a)
MATLAB Search Path
Other requirements C-Mex needs to be compiled, not compatible with LCC. Precompiled Mex files:
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (25)
24 Mar 2016 JP

JP (view profile)

I could not get the mex file to compile. Here is what I tried along with the errors I got. (I'm currently using MATLAB 2015b)

>> WindowAPI
== WindowAPI: Cannot find the compiled mex file
== Start compilation of WindowAPI:
Compilation failed:
Error using mex
C:\Users\Jonathan\Documents\MATLAB\WindowAPI_04Nov2011\WindowAPI not found; check that you are in the correct current folder, and check the spelling of 'C:\Users\Jonathan\Documents\MATLAB\WindowAPI_04Nov2011\WindowAPI'.
MSVC or Intel compiler is needed - not working with LCC!
Perhaps you must setup the compiler at first:
mex -setup
>> mex('-O','WindowAPI')
Error using mex
C:\Users\Jonathan\Documents\MATLAB\WindowAPI_04Nov2011\WindowAPI not found; check that you are in the correct current folder,
and check the spelling of 'C:\Users\Jonathan\Documents\MATLAB\WindowAPI_04Nov2011\WindowAPI'.

>> mex('-O','WindowAPI.c')
Building with 'MinGW64 Compiler (C)'.
Error using mex
Deprecated MEX function mexGet|mexSet was called. Either update the source code to use mxGetProperty|mxSetProperty, OR rerun
MEX with the -DMEX_DOUBLE_HANDLE added to the command line to enter compatibility mode.

>> mex('-O','-DMEX_DOUBLE_HANDLE','WindowAPI.c')
Building with 'MinGW64 Compiler (C)'.
Error using mex
C:\Users\Jonathan\AppData\Local\Temp\mex_52468892045512_7732\WindowAPI.obj:WindowAPI.c:(.text+0x829): undefined reference to
C:\Users\Jonathan\AppData\Local\Temp\mex_52468892045512_7732\WindowAPI.obj:WindowAPI.c:(.text+0x847): undefined reference to
collect2.exe: error: ld returned 1 exit status

Comment only
19 Aug 2015 Xy

Xy (view profile)

Thanks, works nicely.

Note that from 2014b, Matlab has changed the format of figure handles:

Therefore, WindowAPI needs to be called with
WindowAPI(figure_handle.Number, 'Position', 'full');
instead of
WindowAPI(figure_handle, 'Position', 'full');

04 Feb 2015 Udit Gupta

Excellent! Saved me hours of coding this myself.

25 Jan 2015 Vishnu

Vishnu (view profile)

check my way of load library... undocumented

12 Jan 2015 Patrik Andersson

17 Nov 2014 Abdunnoer

Thanks for the great work
mex('-O', 'WindowAPI');
mex('-O', 'WindowAPI.c');

cause it did`nt find file

Comment only
01 Nov 2014 Jan Simon

Jan Simon (view profile)

@Katharina: What about:

nMonitor = size(get(0, 'MonitorPositions'), 1)

Comment only
27 Oct 2014 Katharina

Is there a way to find out the number of monitors? Something that returns something like:
1: Monitor A
2: Monitor B

Comment only
18 Oct 2014 Tim

Tim (view profile)

With the change to figure handle objects in R2014b, one needs to get the integer figure handle to pass to WindowAPI for it to work with that version:
h = figure; % object handle
FigH = h.Number; % integer handle

09 Jul 2014 Philip Ohnewein

18 Dec 2013 Leenman

16 Nov 2012 Jan Simon

Jan Simon (view profile)

@Jeff: I do not work with GUIDE, therefore I'm not sure what setting the resize behaviour to "proportional" exactly does. I will try some experiments to see, if I can reproduce the behaviour you describe.

Comment only
20 Sep 2012 Jeff

Jeff (view profile)

20 Sep 2012 Jeff

Jeff (view profile)

Hi Jan,
This looks perfect for my purposes. I have tried using WindowAPI(fig,'Position','full') to completely maximize a gui I've created with GUIDE. The API has different effects depending on whether the gui's resize behaviour is set to proportional or non-resizable. Taskbar is hidden for non-resizeable, but neither taskbar or title bar are hidden for proportional. I'd like to have them both hidden somehow. The demo works completely fine with WindowAPI(FigH,'Position','full'), hiding both the taskbar and title bar. Using Win 7 x32 here.

Comment only
17 Aug 2011 AMVR

AMVR (view profile)

14 Aug 2011 Daniel

Daniel (view profile)

01 Jul 2011 Jan Simon

Jan Simon (view profile)

Small bug in the test function uTest_WindowAPI: 'Crop' must be changed to 'Clip'.
The function demo_WindowAPI is added to decrease the large help section a little bit.
The update is submitted and will appear soon.

Comment only
29 Jun 2011 Jan Simon

Jan Simon (view profile)

@Gaszton: The new version is comming in the next days with multi-monitor handling. Then this shows your IMSHOW picture on the 2nd monitor:
axes('Position', [0,0,1,1]); imshow(pic); WindowAPI(gcf, 'Position', 'work', 2); WindowAPI(gcf, 'Clip');

Comment only
30 May 2011 Jan Simon

Jan Simon (view profile)

Unfortunately I cannot test the code for multiple monitors yet. I will try to expand the submission to support this in the future.

Comment only
30 May 2011 Gaszton

Is it possible to automatically move the figure in fullscreen to the secondary monitor?
I would like to diplay an image with imshow, that is the same size as the resolution of the secondary monitor, so i want the image to cover the full screen.



Comment only
26 May 2011 Wang Jian


Comment only
24 May 2011 Marc Lalancette

Hi Jan, I use Windows 7 x64. Unfortunately, I know nothing about these Windows things, sorry. Which is why I really appreciate your work. Cheers.

Comment only
24 May 2011 Jan Simon

Jan Simon (view profile)

@Marc: Thanks for the valuable comment. Which OS do you use? Your improved PosCorrect method works well under Win2k and XP also - I'll include it soon.
Do you know a method to set the window in the 'screen' mode on top of the taskbar without setting the TOPMOST flag? To my surprise it works in the 'fullscreen' mode without the TOPMOST flag, but not in the 'screen' mode.

Comment only
23 May 2011 Marc Lalancette

Works well, thanks!
I had to modify the fullscreen numbers, as it was not exactly right, in particular I could still grab the edge of the window at the bottom. With these, it works perfectly:
SetWindowPos(hWnd, HWND_NOTOPMOST,
Rect.left + PosCorrect[0], - PosCorrect[3] - PosCorrect[1],
Rect.right + PosCorrect[2],
Rect.bottom + PosCorrect[3],
(Note I also removed the topmost property, otherwise I didn't know how to close the figure or switch to another window without calling the task manager...)

20 May 2011 nsbd

nsbd (view profile)

27 May 2011 1.1

FullScreen: improved placement and not topmost anymore (thanks Marc!)

29 Jun 2011 1.2

Multi-monitor handling, Clip region, ToScreen, XMax, YMax
Position command replaces Screen and FullScreen commands.

01 Jul 2011 1.3

Bug in uTest_WindowAPI: 'crop' -> 'clip'. No changes in the main function.

04 Nov 2011 1.4

Lock cursor, tested with Win7/64 also.

16 Dec 2016 1.4

HG1/HG2 compatibility.

Contact us