hWnd = gethwnd(hFig)
HWnd = GETHWND retrieves the current figure (gcf)'s underlying Windows handle, thus enabling integration of Matlab GUI in C/C++/C# programs.
HWnd = GETHWND(hFig) retrieves a specific figure's underlying Windows handle. hFig is a Matlab handle, or a list of handles (not necessarily figure handle(s) - the handles' containing figure is used).
Windows-only (duh!); Java must be enabled
This code heavily relies on undocumented and unsupported Matlab functionality. It works on Matlab 7+, but use at your own risk!
Bugs and suggestions:
Please send to Yair Altman (altmany at gmail dot com)
I have been using this function successfully for years. With R2019b I now get the a warning that the JavaFrame figure property will be removed in a future release.
@Raym - no
Hi, Yair Altman
Is it possible to get handle to any window such as a word/pdf/older/text editor window... outside matlab with little modification of this function?
@Adam - I haven't updated this utility since 2011 so when MathWorks updated the entire GUI infrastructure in R2014b (HG2), this utility stopped working for new figures. Nobody complained until you did today, so I never noticed that this utility needs to be updated.
Anyway, I now uploaded a fixed version.
Seems to need a
jframe = jf.fHG2Client.getWindow;
option for supporting the post R2014b HG2 graphics now, but with that added in it seems to work fine for me. Haven't actually used the value it returns with this yet to see if it is correct, but it returns a value instead of erroring.
Is it possible to interact with a Window Handle (HWnd) directly from Matlab.
get(HWnd) does not return anything, as Matlab is not recognizing HWnd as an object.
This would be very useful in several cases like when trying to control an external application using the COM interfaces. A good case is controlling the Google Earth Window (see http://www.mathworks.com/matlabcentral/fileexchange/20031-google-earth-com-api-example)
Fix for R2014b onward (HG2)
Fix for R2011b