天天看点

dsoframer控件的学习

根据自己对dsoframer控件的学习,想把dsoframer控件进行简单的包装为C#的usercontrol,大体需要作如下:

1.使用前注册该dsoframer控件,我把该dso控件当作嵌入资源,用学习笔记1中的方法注册即可

dsoframer控件的学习
dsoframer控件的学习

Code

 /// <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出来之前是不能进行初始化操作的,很奇怪为什 

        //么作者这样考虑

dsoframer控件的学习

..

    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>

  /// 添加控件

  private void AddOfficeControl()

    this.m_Panel_Control.Controls.Add(m_axFramerControl);

    m_axFramerControl.Dock = DockStyle.Fill;

3.初始化dsoframer控件 ,我这里用已经有的文件进行dso初始化,

  /// 初始化office控件

  /// <param name="_sFilePath">本地文档路径</param>

  private void InitOfficeControl(string _sFilePath)

    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;

     下面这个方法是dso打开文件时需要的一个参数,代表office文件类型

  /// 根据后缀名得到打开方式

  /// <param name="_sExten"></param>

  /// <returns></returns>

  private string LoadOpenFileType(string _sExten)

    string sOpenType = "";

    switch (_sExten.ToLower())

     case "xls":

      sOpenType = "Excel.Sheet";

      break;

     case "doc":

      sOpenType = "Word.Document";

     case "ppt":

      sOpenType = "PowerPoint.Show";

     case "vsd":

      sOpenType = "Visio.Drawing";

     default:

    return sOpenType;

   catch (Exception ex)

4.我觉的最重要的一步,就是公布dso当前的活动对象,因为自己做这个usercontrol功能不强,但是不能把人家dso功能给杀掉,给使用者留一个更大的空间。。。。

  /// 获取当前操作的文档

  public object ActiveDocument

   get

    return this.m_axFramerControl.ActiveDocument;

  /// 获取当前控件对象

  public AxDSOFramer.AxFramerControl OfficeObject

    return this.m_axFramerControl;

  /// 保存

  public void Save()

    //先保存

    this.m_axFramerControl.Save(true,true,"","");

  } 

  /// 另存为

  public void SaveAs()

    //另存为

    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));

  /// 关闭当前界面

  public void Close()

    if(this.m_axFramerControl != null)

     this.m_axFramerControl.Close();

版权说明

  如果标题未标有<转载、转>等字则属于作者原创,欢迎转载,其版权归作者和博客园共有。

  作      者:温景良

dsoframer控件的学习
dsoframer控件的学习

<a href="http://home.cnblogs.com/u/wenjl520/">温景良(Jason)</a>

<a href="http://home.cnblogs.com/u/wenjl520/followees">关注 - 32</a>

<a href="http://home.cnblogs.com/u/wenjl520/followers">粉丝 - 161</a>

<a>+加关注</a>

<a></a>

Post Comment

正在给头儿逼着看这个控件的代码,不知道从哪儿入手,看得人一头雾水,能否指点一二,谢谢!!

<a href="http://www.ucancode.com/index.htm" target="_blank">【推荐】超50万VC++源码: 大型工控、组态\仿真、建模CAD源码2018!</a>

<a href="https://dnspod.cloud.tencent.com/act/yearendsales?from=IT&amp;fromSource=gwzcw.741110.741110.741110" target="_blank">【推荐】腾讯云新用户域名抢购1元起,抓紧抢购</a>

dsoframer控件的学习

<b>最新IT新闻</b>:

dsoframer控件的学习

<b>最新知识库文章</b>: