function dotnet_control(HandlePanel,update)
% DOTNET_CONTROL, This function starts a .NET Form, which will float
% above a matlab panel or another control. Which allows the usage of .NET
% controls like they where activex controls.
%
% dotnet_control(HandlePanel)
%
% Example,
% fig_handle=dotnet_control_example
% handles=guidata(fig_handle);
% dotnet_control(handles.uipanel1)
%
% Function is written by D.Kroon University of Twente (April 2009)
if(exist('update','var'))
% HandlePanel is now the ID of the control
updateFormPosition(HandlePanel)
return;
end
if(~ishandle(HandlePanel))
error('dotnet_control:inputs', 'The input is not a valid handle');
end
HandleFigure=get(HandlePanel,'Parent');
% Add the needed Windows Assemblies
NET.addAssembly('System');
NET.addAssembly('System.Windows.Forms');
% Make A new Form (.NET window)
Form1=System.Windows.Forms.Form;
Form1.Visible=true;
Form1.FormBorderStyle=System.Windows.Forms.FormBorderStyle.None;
Form1.ShowInTaskbar=false;
Form1.BackColor = System.Drawing.Color.Black;
% Get number of .NET controls
nid=getappdata(0,'dotnet_nid'); if(isempty(nid)), setappdata(0,'dotnet_nid',0); nid=0; end
% Update +1 the number of .Net controls
nid=nid+1; setappdata(0,'dotnet_nid',nid);
% Current ID is number of .NET controls
id=nid;
% Data needed in update steps
data.HandleFigure=HandleFigure;
data.HandlePanel=HandlePanel;
data.HandleTimer=timer('TimerFcn',['dotnet_control(' num2str(id) ',true)'], 'Period', 0.2,'ExecutionMode','fixedDelay');
data.Position_fig=[0 0 0 0];
data.Position_panel=[0 0 0 0];
% Store all .NET control data
StoreName=['dotnet_control' num2str(id)];
setappdata(0,[StoreName '_form'],Form1);
setappdata(0,[StoreName '_data'],data);
start(data.HandleTimer)
% t = timer('TimerFcn',@mycallback, 'Period', 1.0);
function updateFormPosition(id)
% Get data struct stored in figure
StoreName=['dotnet_control' num2str(id)];
Form1=getappdata(0,[StoreName '_form']);
data=getappdata(0,[StoreName '_data']);
HandleFigure=data.HandleFigure;
HandlePanel=data.HandlePanel;
if((~ishandle(HandleFigure))||(~ishandle(HandlePanel)));
stop(data.HandleTimer);
Form1.Close;
delete(data.HandleTimer);
rmappdata(0,[StoreName '_form']);
rmappdata(0,[StoreName '_data']);
return
end
% Check for change in position
Position_fig=get(HandleFigure,'Position');
Position_panel=get(HandlePanel,'Position');
check=(sum(abs(Position_fig-data.Position_fig))+sum(abs(Position_panel-data.Position_panel)))>0;
if(~check), return, end
% Store current position
data.Position_fig=Position_fig;
data.Position_panel=Position_panel;
data.TopMost=true;
setappdata(0,[StoreName '_data'],data);
% Get the size of the desktop
ScreenSize=get(0,'ScreenSize');
% Get Normalized Figure Position
storeUnits=get(HandleFigure,'Units');
set(HandleFigure,'Units','normalized')
Position_fig=get(HandleFigure,'Position');
set(HandleFigure,'Units',storeUnits);
% Get Normalized Panel Positon
storeUnits=get(HandlePanel,'Units');
set(HandlePanel,'Units','normalized')
Position_panel=get(HandlePanel,'Position');
set(HandlePanel,'Units',storeUnits)
% Calculate Figure position in pixels
Fig.Top=(1-Position_fig(2)-Position_fig(4))*ScreenSize(4);
Fig.Left=Position_fig(1)*ScreenSize(3);
Fig.Width=Position_fig(3)*ScreenSize(3);
Fig.Height=Position_fig(4)*ScreenSize(4);
% Calculate Panel position in pixels
Panel.Left=Position_panel(1)*Fig.Width;
Panel.Top=(1-Position_panel(2)-Position_panel(4))*Fig.Height;
Panel.Width=Position_panel(3)*Fig.Width;
Panel.Height=Position_panel(4)*Fig.Height;
% Update .NET Form position
Form1.TopMost=true;
Form1.Top=round(Fig.Top+Panel.Top);
Form1.Left=round(Fig.Left+Panel.Left);
Form1.Width=round(Panel.Width);
Form1.Height=round(Panel.Height);
figure(HandleFigure);