qg777钱柜误乐

热门关键词: qg777钱柜误乐

[分享]外部exe窗体嵌入winform

图片 1

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Data;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Diagnostics;usingSystem.Runtime.InteropServices;usingSystem.IO;usingSystem.Drawing.Design;namespaceHCDL{publicclassShowForm{//-------------------//Actionobject,EventArgsappIdleAction=null;EventHandlerappIdleEvent=null;ControlParentCon=null;stringstrGUID="";publicShowForm(ControlC,stringTitlestr){appIdleEvent=newEventHandler(Application_Idle);ParentCon=C;strGUID=Titlestr;}///summary///将属性codeAppFilename/code指向的应用程序打开并嵌入此容器////summarypublicIntPtrStart(stringFileNameStr){if(m_AppProcess!=null){Stop();}try{ProcessStartInfoinfo=newProcessStartInfo(FileNameStr);info.UseShellExecute=true;info.WindowStyle=ProcessWindowStyle.Minimized;m_AppProcess=System.Diagnostics.Process.Start(info);m_AppProcess.WaitForInputIdle();Application.Idle+=appIdleEvent;}catch{if(m_AppProcess!=null){if(!m_AppProcess.HasExited)m_AppProcess.Kill();m_AppProcess=null;}}returnm_AppProcess.Handle;}///summary///确保应用程序嵌入此容器////summary///paramname="sender"/param///paramname="e"/paramvoidApplication_Idle(objectsender,EventArgse){if(this.m_AppProcess==null||this.m_AppProcess.HasExited){this.m_AppProcess=null;Application.Idle-=appIdleEvent;return;}if(m_AppProcess.MainWindowHandle==IntPtr.Zero)return;Application.Idle-=appIdleEvent;EmbedProcess(m_AppProcess,ParentCon);}///summary///应用程序结束运行时要清除这里的标识////summary///paramname="sender"/param///paramname="e"/paramvoidm_AppProcess_Exited(objectsender,EventArgse){m_AppProcess=null;}///summary///将属性codeAppFilename/code指向的应用程序关闭////summarypublicvoidStop(){if(m_AppProcess!=null)//m_AppProcess.MainWindowHandle!=IntPtr.Zero){try{if(!m_AppProcess.HasExited)m_AppProcess.Kill();}catch(Exception){}m_AppProcess=null;}}#region属性///summary///applicationprocess////summaryProcessm_AppProcess=null;///summary///标识内嵌程序是否已经启动////summarypublicboolIsStarted{get{return(this.m_AppProcess!=null);}}#endregion属性#regionWin32API[DllImport("user32.dll",EntryPoint="GetWindowThreadProcessId",SetLastError=true,CharSet=CharSet.Unicode,ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]privatestaticexternlongGetWindowThreadProcessId(longhWnd,longlpdwProcessId);[DllImport("user32.dll",SetLastError=true)]privatestaticexternIntPtrFindWindow(stringlpClassName,stringlpWindowName);[DllImport("user32.dll",SetLastError=true)]privatestaticexternlongSetParent(IntPtrhWndChild,IntPtrhWndNewParent);[DllImport("user32.dll",EntryPoint="GetWindowLongA",SetLastError=true)]privatestaticexternlongGetWindowLong(IntPtrhwnd,intnIndex);publicstaticIntPtrSetWindowLong(HandleRefhWnd,intnIndex,intdwNewLong){if(IntPtr.Size==4){returnSetWindowLongPtr32(hWnd,nIndex,dwNewLong);}returnSetWindowLongPtr64(hWnd,nIndex,dwNewLong);}[DllImport("user32.dll",EntryPoint="SetWindowLong",CharSet=CharSet.Auto)]publicstaticexternIntPtrSetWindowLongPtr32(HandleRefhWnd,intnIndex,intdwNewLong);[DllImport("user32.dll",EntryPoint="SetWindowLongPtr",CharSet=CharSet.Auto)]publicstaticexternIntPtrSetWindowLongPtr64(HandleRefhWnd,intnIndex,intdwNewLong);[DllImport("user32.dll",SetLastError=true)]privatestaticexternlongSetWindowPos(IntPtrhwnd,longhWndInsertAfter,longx,longy,longcx,longcy,longwFlags);[DllImport("user32.dll",SetLastError=true)]privatestaticexternboolMoveWindow(IntPtrhwnd,intx,inty,intcx,intcy,boolrepaint);[DllImport("user32.dll",EntryPoint="PostMessageA",SetLastError=true)]privatestaticexternboolPostMessage(IntPtrhwnd,uintMsg,uintwParam,uintlParam);[DllImport("user32.dll",SetLastError=true)]privatestaticexternIntPtrGetParent(IntPtrhwnd);[DllImport("user32.dll",EntryPoint="ShowWindow",SetLastError=true)]staticexternboolShowWindow(IntPtrhWnd,intnCmdShow);privateconstintSWP_NOOWNERZORDER=0x200;privateconstintSWP_NOREDRAW=0x8;privateconstintSWP_NOZORDER=0x4;privateconstintSWP_SHOWWINDOW=0x0040;privateconstintWS_EX_MDICHILD=0x40;privateconstintSWP_FRAMECHANGED=0x20;privateconstintSWP_NOACTIVATE=0x10;privateconstintSWP_ASYNCWINDOWPOS=0x4000;privateconstintSWP_NOMOVE=0x2;privateconstintSWP_NOSIZE=0x1;privateconstintGWL_STYLE=(-16);privateconstintWS_VISIBLE=0x10000000;privateconstintWM_CLOSE=0x10;privateconstintWS_CHILD=0x40000000;privateconstintSW_HIDE=0;//{隐藏,并且任务栏也没有最小化图标}privateconstintSW_SHOWNORMAL=1;//{用最近的大小和位置显示,激活}privateconstintSW_NORMAL=1;//{同SW_SHOWNORMAL}privateconstintSW_SHOWMINIMIZED=2;//{最小化,激活}privateconstintSW_SHOWMAXIMIZED=3;//{最大化,激活}privateconstintSW_MAXIMIZE=3;//{同SW_SHOWMAXIMIZED}privateconstintSW_SHOWNOACTIVATE=4;//{用最近的大小和位置显示,不激活}privateconstintSW_SHOW=5;//{同SW_SHOWNORMAL}privateconstintSW_MINIMIZE=6;//{最小化,不激活}privateconstintSW_SHOWMINNOACTIVE=7;//{同SW_MINIMIZE}privateconstintSW_SHOWNA=8;//{同SW_SHOWNOACTIVATE}privateconstintSW_RESTORE=9;//{同SW_SHOWNORMAL}privateconstintSW_SHOWDEFAULT=10;//{同SW_SHOWNORMAL}privateconstintSW_MAX=10;//{同SW_SHOWNORMAL}#endregionWin32API///summary///将指定的程序嵌入指定的控件////summaryprivatevoidEmbedProcess(Processapp,Controlcontrol){//Getthemainhandleif(app==null||app.MainWindowHandle==IntPtr.Zero||control==null)return;try{//PutitintothisformSetParent(app.MainWindowHandle,control.Handle);}catch(Exception){}try{//RemoveborderandwhatnotSetWindowLong(newHandleRef(this,app.MainWindowHandle),GWL_STYLE,WS_VISIBLE);SendMessage(app.MainWindowHandle,WM_SETTEXT,IntPtr.Zero,strGUID);}catch(Exception){}try{//MovethewindowtooverlayitonthiswindowMoveWindow(app.MainWindowHandle,-20,-20,control.Width,control.Height-10,true);}catch(Exception){}}[DllImport("User32.dll",EntryPoint="SendMessage")]privatestaticexternintSendMessage(IntPtrhWnd,intMsg,IntPtrwParam,stringlParam);constintWM_SETTEXT=0x000C;}}

PS:以前是3.5版本。现在改成2.0也能用的了用法

ShowFormSf=newShowForm(this,"仅供内部使用"+System.Guid.NewGuid().ToString());ProxHandle=Sf.Start(Application.StartupPath+"\ProxyThorn.exe");

第三方exewinform效果图

本文由qg777发布于操作系统,转载请注明出处:[分享]外部exe窗体嵌入winform

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。