File Exchange

image thumbnail


version (28 KB) by Jan
Set figure size, top-most, transparency and pixel mask, multi-monitor etc by Windows API


Updated 16 Apr 2018

View Version History

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.

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:

Cite As

Jan (2021). WindowAPI (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (46)

Ryan Krensel

Very useful! Noticed that the description of the double vector [X,Y,Width,Height] for the 'position' call is incorrect in the WindowsAPI.m file as [Left, Top, Right, Bottom] which confused me for some time until I checked the WindowsAPI.c file and the overview on this webpage.

Luc Masset

Excellent graphic tool!! Works like a charm! Just one small side effect: when removing the Min/Max/Close buttons with

>> WindowAPI(fig, 'Button', false)

the figure icon is also removed. Is there a possibility to keep the icon?

Bertram Dumböck

Joe Thibedeau

It appears this works with the 'figure' function, but not the 'uifigure' function. I am just trying to hide the window borders for an About box in the app designer.

f = uifigure

Executes without error, but the title bar remains on the uifigure.

Waddah Moghram

Exactly what I was looking for! Using setting size code, MATLAB would automatically resize the height to less.

ScreenWorkArea = images.internal.getWorkArea;
set(figHandle, 'Position', [50, -(ImageSizePixels(2) - ,ImageSizePixels(1), ImageSizePixels(2)]);
matlab.ui.internal.PositionUtils.setDevicePixelPosition(figHandle, [50, -(ImageSizePixels(2) - ,ImageSizePixels(1), ImageSizePixels(2)]);
matlab.ui.internal.PositionUtils.setDevicePixelPosition(gca, [0,0, ImageSizePixels(1), ImageSizePixels(2)]);

Peter Cook

@Jan Thank you for your reply. If you upload the source code to GitHub I can fork it and add the changes I mentioned. I did discover a workaround that may work in some similar cases (Windows 10, high DPI monitor scaling): Find the path to java.exe used by MATLAB, right-click>"Properties", goto "Compatibility" tab, [x] "Override High DPI Scaling Behavior", choose "Scaling Performed By">"System". After some googling it seems the issue caused by javax.swing


@Peter Cook: Thanks for this valuable suggestion. The scaling problem is severe and I struggle a lot with rounding effects.
It is intended, that WindowAPI replies the real pixel positions, because the unscaled positions are replied by the built-in |get(h, 'Position')| already. A fair solution would be to include both outputs in WindowAPI, for setting and getting the positions.

Peter Cook

Jan, a small suggestion for a future update. In Windows 10 (and maybe others?) Microsoft imposes a scaling factor on high DPI monitors (e.g. which distorts some of the values returned by MATLAB w.r.t. figure size/position. e.g. Suppose I create a MATLAB figure with 'Position' property of [64, 64, 512, 1024] on a monitor with a scaling factor of 250% - the actual pixel position on screen is rendered by Windows as [148, 148, 1300, 2684] (which I can only retrieve w/ WindowAPI(h, 'Position') and not get(h,'Position')). I guess that makes it more of a MATLAB problem than a WindowAPI problem, but outputting position coordinates in the same pixel mapping as MATLAB would be useful... It could potentially be rectified with this ( Windows API function.


@Lei: I guess, that you are using the MinGW64 Compiler (C) compiler? There seems like MinGW fails to include the Windows libraries. Please download the pre-compiled files instead, which have been created by the MSVC compilers.


*** Compilation failed:
C:\Users\Lei\AppData\Local\Temp\mex_107417481032953_13660\WindowAPI.obj:WindowAPI.c:(.text+0x83e): undefined reference to `__imp_CreateRectRgn'
C:\Users\Lei\AppData\Local\Temp\mex_107417481032953_13660\WindowAPI.obj:WindowAPI.c:(.text+0x85c): undefined reference to `__imp_DeleteObject'

SeokHyun Shin

Dear Jan,
Thanks to your help, I was able to finish my work perfectly.
I am so sorry on the one hand and really appreciate on the other.
Have a nice day~!
Sincerely yours.

Youshu Zhou

Hi Jan,
This bug has been fixed. Thank you for your reply and help!
Best regards,


Thanks Youshu Zhou to find this bug. Please test if this is fixed now.

Youshu Zhou

Hi Jan,
I am very glad to see such birlliant work that is likely to help me a lot. However I encountered a problem when running the demo with Matlab2014b. And the error message is something like this:

Error WindowAPI (line 181)
persistent MexFcn hasHG2
The output parameter "Reply" (possibly other parameters) is not assigned when calling “(...the file path)\WindowAPI_20161126\WindowAPI.m>WindowAPI”
Error demo_WindowAPI (line 21)
MexVersion = WindowAPI();

(The message below is not copied from Matlab exactly because the language of my Matlab is not English so it is translated by me. I hope the interpretation could give you the key point.)

I am wondering whether there is any prerequisite before running the demo.

Best regards,




@SeokHyun Shin: I cannot reproduce the problem on my R2016b. Maybe it is a typo only, but "WindowAPI(hFig,'Alpha',1,[0,0,0])" sets the full opacity. Values < 1.0 make the selected color more transparent. Feel free to contact me by email for a debugging.

SeokHyun Shin

I tested according to your advice. but, It still turn back to black background and never turn to transparent until delete patch object.
I wish if you ever solve this problem someday.
Sincerely yours.


@Timothy Plummer: Can you provide more details about the failing compilation? What happens if you use get/set the Position or OuterPosition? Do you get an error message? I had problems with the determination of the size of the window border, if they have non integer values due to the scaling of the operating system. Please use the email address from the code to send me more details. Thanks.

@SeokHyun Shin: The patch command can set the renderer to OpenGL. I did not observe this yet, but maybe this changes the view of the window. What happens for:
hFig = figure('Renderer', OpenGL');
objPath = patch('faces', faces, 'vertices', vertices);

SeokHyun Shin

I'm currently using Matlab R2014a and all WindowAPI functions goes well except when I add patch function on my GUI.
This is simplified code that I applied.
hFig = figure;
WindowAPI(hFig,'Alpha',1,[0,0,0]); % transparent background
objPath = patch('faces', faces, 'vertices', vertices); % turn back to opaque black background
Please help me.
Sincerely yours.

Timothy Plummer

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.

Daiichiro Matsunaga


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


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.

Patrik Andersson


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

cause it did`nt find file


@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


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

Philip Ohnewein



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



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.




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.


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


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


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.


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

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


MATLAB Release Compatibility
Created with R2016b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!