Code covered by the BSD License  

Highlights from

4.9 | 10 ratings Rate this file 41 Downloads (last 30 days) File Size: 22.4 KB File ID: #31437
image thumbnail



Jan Simon


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

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.
  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, 2001b/64, 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 Cmd Win Tool.

MATLAB release MATLAB 7.8 (R2009a)
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 (22)
25 Jan 2015 Vishnu


check my way of load library... undocumented

12 Jan 2015 Patrik Andersson

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

@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


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

Philip Ohnewein

18 Dec 2013 Leenman


16 Nov 2012 Jan Simon

Jan Simon

@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


20 Sep 2012 Jeff


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


14 Aug 2011 Daniel


01 Jul 2011 Jan Simon

Jan Simon

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

@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

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

Wang Jian


Comment only
24 May 2011 Marc Lalancette

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

@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

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


27 May 2011

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

29 Jun 2011

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

01 Jul 2011

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

04 Nov 2011

Lock cursor, tested with Win7/64 also.

Contact us