File Exchange

image thumbnail


version 1.4 (27.6 KB) by

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



View License

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:

Comments and Ratings (27)

Hello, I am glad that this works for the most part with the new figures, even the ones that are part of App Designer, it solves the problem with the main GUI getting shoved to the bottom of the stack when using the uigetfile dialog. The problem that I'm running into is that I was not able to create the mex file, and I seem to only have one of the compilers available for c, the MinGW64 Compiler (C), I wasn't sure if this was one of the ones that caused problems or not.
I then tried one of your precompiled versions, and that seemed to work, but the uTest function failed on get/set position and outer position. ( I changed your errors to Warnings, so I can see what tests failed without it stopping). I wasn't sure if this has something to do with something that happened in Matlab 2017b or not.
Any suggestions would be great.
p.s. If I can get it so that these two problems get solved i would be happy to give this a 5-star glowing rating.


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


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');

Udit Gupta

Excellent! Saved me hours of coding this myself.


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

cause it did`nt find file

Jan Simon

Jan Simon (view profile)

@Katharina: What about:

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


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


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


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.


Jeff (view profile)


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.


AMVR (view profile)


Daniel (view profile)

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.

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');

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.


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.



Wang Jian


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.

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.

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...)


nsbd (view profile)



HG1/HG2 compatibility.


Lock cursor, tested with Win7/64 also.


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


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


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

MATLAB Release
MATLAB 7.8 (R2009a)

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

» Watch video

Win prizes and improve your MATLAB skills

Play today