image thumbnail

WindowAPI

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

5.2K Downloads

Updated 16 Apr 2018

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)
INPUT:
FigH: Matlab's figure handle or Windows HWND
Command:
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.
NOTES:
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.

EXAMPLES:
Maximize the current figure:
WindowAPI(gcf, 'maximize')
A semi-transparent figure with through-looking background:
FigH = figure('Color', ones(1,3));
plot(1:10);
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:
http://www.mathworks.com/matlabcentral/fileexchange/3407
Window Manipulation, Phil Goddard:
http://www.mathworks.com/matlabcentral/fileexchange/3434
api_showwindow, Mihai Moldovan:
http://www.mathworks.com/matlabcentral/fileexchange/2041
maxfig, Mihai Moldovan:
http://www.mathworks.com/matlabcentral/fileexchange/6913
setFigTransparency, Yair Altman:
http://www.mathworks.com/matlabcentral/fileexchange/30583

Cite As

Jan (2021). WindowAPI (https://www.mathworks.com/matlabcentral/fileexchange/31437-windowapi), MATLAB Central File Exchange. Retrieved .

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!