dsoframer控件的学习
根据自己对dsoframer控件的学习,想把dsoframer控件进行简单的包装为C#的usercontrol,大体需要作如下:
1.使用前注册该dsoframer控件,我把该dso控件当作嵌入资源,用学习笔记1中的方法注册即可
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/// <summary>
/// usercontrol控件初始化
/// </summary>
/// <param name="_sFilePath">本地文件全路径</param>
public void Init(string _sFilePath)
{
try
{
RegControl();//注册控件
if(!CheckFile(_sFilePath))//判断是否为所支持的office文件
{
throw new ApplicationException("文件不存在或未标识的文件格式!");
}
AddOfficeControl();//这里一定要先把dso控件加到界面上才能初始化dso控件,这个dso控件在没有被show出来之前是不能进行初始化操作的,很奇怪为什
//么作者这样考虑
![](https://www.cnblogs.com/Images/dot.gif)
InitOfficeControl(_sFilePath);
}
catch(Exception ex)
{
throw ex;
}
}
public bool RegControl()
{
try
{
Assembly thisExe = Assembly.GetExecutingAssembly();
System.IO.Stream myS = thisExe.GetManifestResourceStream("NameSpaceName.dsoframer.ocx");
string sPath = “该ocx文件的实际路径”+ @"/dsoframer.ocx";
ProcessStartInfo psi = new ProcessStartInfo("regsvr32","/s " +sPath);
Process.Start(psi);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
return true;
}
2.动态向usercontrol添加dsoframer实例
private AxDSOFramer.AxFramerControl m_axFramerControl = new AxDSOFramer.AxFramerControl();
/// <summary>
/// 添加控件
/// </summary>
private void AddOfficeControl()
{
try
{
this.m_Panel_Control.Controls.Add(m_axFramerControl);
m_axFramerControl.Dock = DockStyle.Fill;
}
catch(Exception ex)
{
throw ex;
}
}
3.初始化dsoframer控件 ,我这里用已经有的文件进行dso初始化,
/// <summary>
/// 初始化office控件
/// </summary>
/// <param name="_sFilePath">本地文档路径</param>
private void InitOfficeControl(string _sFilePath)
{
try
{
if(m_axFramerControl == null)
{
throw new ApplicationException("请先初始化office控件对象!");
}
string sExt = System.IO.Path.GetExtension(_sFilePath).Replace(".","");
//this.m_axFramerControl.CreateNew(this.LoadOpenFileType(sExt));//创建新的文件
this.m_axFramerControl.Open(_sFilePath,false,this.LoadOpenFileType(sExt),"","");//打开文件
//隐藏标题
this.m_axFramerControl.Titlebar = false;
}
catch(Exception ex)
{
throw ex;
}
}
下面这个方法是dso打开文件时需要的一个参数,代表office文件类型
/// <summary>
/// 根据后缀名得到打开方式
/// </summary>
/// <param name="_sExten"></param>
/// <returns></returns>
private string LoadOpenFileType(string _sExten)
{
try
{
string sOpenType = "";
switch (_sExten.ToLower())
{
case "xls":
sOpenType = "Excel.Sheet";
break;
case "doc":
sOpenType = "Word.Document";
break;
case "ppt":
sOpenType = "PowerPoint.Show";
break;
case "vsd":
sOpenType = "Visio.Drawing";
break;
default:
sOpenType = "Word.Document";
break;
}
return sOpenType;
}
catch (Exception ex)
{
throw ex;
}
}
4.我觉的最重要的一步,就是公布dso当前的活动对象,因为自己做这个usercontrol功能不强,但是不能把人家dso功能给杀掉,给使用者留一个更大的空间。。。。
/// <summary>
/// 获取当前操作的文档
/// </summary>
public object ActiveDocument
{
get
{
return this.m_axFramerControl.ActiveDocument;
}
}
/// <summary>
/// 获取当前控件对象
/// </summary>
public AxDSOFramer.AxFramerControl OfficeObject
{
get
{
return this.m_axFramerControl;
}
}
/// <summary>
/// 保存
/// </summary>
public void Save()
{
try
{
//先保存
this.m_axFramerControl.Save(true,true,"","");
}
catch(Exception ex)
{
throw ex;
}
}
/// <summary>
/// 另存为
/// </summary>
public void SaveAs()
{
try
{
//另存为
SaveFileDialog sfd = new SaveFileDialog();
string sExt = System.IO.Path.GetExtension(this.m_sFilePath).Replace(".","");
sfd.Filter = sExt;
if(sfd.ShowDialog() == DialogResult.OK)
{
string sSavePath = sfd.FileName;
if(System.IO.File.Exists(sSavePath))
{
System.IO.File.Delete(sSavePath);
}
this.m_axFramerControl.SaveAs(sSavePath,this.LoadOpenFileType(sExt));
}
}
catch(Exception ex)
{
throw ex;
}
}
/// <summary>
/// 关闭当前界面
/// </summary>
public void Close()
{
try
{
if(this.m_axFramerControl != null)
{
this.m_axFramerControl.Close();
}
}
catch(Exception ex)
{
throw ex;
}
}