RibbonX的Globalization和Outlook中RibbonX的应用

实现环境:Visual Studio 2010,Office 2010, VSTO 4.0

在Ribbon的Globalization中第一步是要建立相应语言的资源文件,这些资源文件的命名方式是"Resources.[语言代码].resx"如图:


这此资源文件必须方在项目的“Properties”文件夹下(你刚创建是是在项目的根目录下,需要你手动把它移到"Properties"文件夹下)。Resources.resx理论上不存在也是没关系的,不过当系统找不到你指定的语言资源是系统会使用这个资源。所以我认为还是有保留的必要。如图:


在这此资源文件中我们对同下个资源名使用不同的语言进行定义,这样我们就达到了Globalization的目的。

在Outlook针对不同Inspector可以定以不同的Ribbon。其方法是在项目中加入定义的xml文件。请在加入这些文件后将Build Action改成Embedded Resource否则就不能通过程序找到它。如图

Ribbon的控制代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
using System.Globalization;

// TODO:  Follow these steps to enable the Ribbon (XML) item:

// 1: Copy the following code block into the ThisAddin, ThisWorkbook, or 
//  ThisDocument class.

//  protected override Microsoft.Office.Core.IRibbonExtensibility 
//CreateRibbonExtensibilityObject()
//  {
//      return new OutlookAddIn25Ribbon();
//  }

// 2. Create callback methods in the "Ribbon Callbacks" region of this class 
//  to handle user
//    actions, such as clicking a button. Note: if you have exported this Ribbon
//    from the Ribbon designer,
//    move your code from the event handlers to the callback methods and modify 
//    the code to work with the
//    Ribbon extensibility (RibbonX) programming model.

// 3. Assign attributes to the control tags in the Ribbon XML file to identify 
//    the appropriate callback methods in your code.  

// For more information, see the Ribbon XML documentation in the Visual Studio 
// Tools for Office Help.


namespace OutlookAddIn25
{
    [ComVisible(true)]
    public class OutlookAddIn25Ribbon : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;
        private bool flag;          //用来标识初始状态
        private string tempText;    //用来记录用户的选择

        public OutlookAddIn25Ribbon()
        {
            flag = false;
        }

        #region IRibbonExtensibility Members

        public string GetCustomUI(string ribbonID)
        {
            string result = null;
/*
 * Ribbon ID									Message class 
 * Microsoft.OMS.MMS.Compose					IPM.Note.Mobile.MMS.*
 * Microsoft.OMS.MMS.Read						IPM.Note.Mobile.MMS.*
 * Microsoft.OMS.SMS.Compose					IPM.Note.Mobile.SMS.*
 * Microsoft.OMS.SMS.Read						IPM.Note.Mobile.SMS.*
 * Microsoft.Outlook.Appointment				IPM.Appointment.*
 * Microsoft.Outlook.Contact 					IPM.Contact.*
 * Microsoft.Outlook.DistributionList 			IPM.DistList.*
 * Microsoft.Outlook.Journal 					IPM.Activity.*
 * Microsoft.Outlook.Mail.Compose 				IPM.Note.*
 * Microsoft.Outlook.Mail.Read 					IPM.Note.*
 * Microsoft.Outlook.MeetingRequest.Read 		IPM.Schedule.Meeting.Request
 * 												IPM.Schedule.Meeting.Canceled
 * Microsoft.Outlook.MeetingRequest.Send 		IPM.Schedule.Meeting.Request
 * Microsoft.Outlook.Post.Compose 				IPM.Post.*
 * Microsoft.Outlook.Post.Read 					IPM.Post.*
 * Microsoft.Outlook.Report 					IPM.Report.*
 * Microsoft.Outlook.Resend 					IPM.Resend.*
 * Microsoft.Outlook.Response.Compose 			IPM.Schedule.Meeting.Resp.*
 * Microsoft.Outlook.Response.CounterPropose 	IPM.Schedule.Meeting.Resp.*
 * Microsoft.Outlook.Response.Read 				IPM.Schedule.Meeting.Resp.*
 * Microsoft.Outlook.RSS 						IPM.Post.Rss.*
 * Microsoft.Outlook.Sharing.Compose 			IPM.Sharing.*
 * Microsoft.Outlook.Sharing.Read 				IPM.Sharing.*
 * Microsoft.Outlook.Task 						IPM.Task.*
 * 												IPM.TaskRequest.*
 * Microsoft.Outlook.Explorer 					Not applicable. Use this ribbon 
 * 												ID to return XML markup for 
 * 												explorer ribbons, context menus,
 *												and Backstage view.
 * Reference Url: http://msdn.microsoft.com/en-us/library/ee692172.aspx for 2010
 * Reference Url: http://msdn.microsoft.com/en-us/library/bb226712.aspx for 2007
 */
            switch (ribbonID)
            {
                case "Microsoft.Outlook.Explorer":
                    result = GetResourceText(
"OutlookAddIn25.OutlookAddIn25Ribbon.xml");
                    break;
                case "Microsoft.Outlook.Mail.Compose":
                    result = GetResourceText("OutlookAddIn25.Mail_Compose.xml");
                    break;
            }
            return result;
        }

        #endregion

        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding 
        //callback methods, select the Ribbon XML item in Solution Explorer 
        //and then press F1

        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
        }

        #endregion

        #region Helpers

        private static string GetResourceText(string resourceName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] resourceNames = asm.GetManifestResourceNames();
            for (int i = 0; i < resourceNames.Length; ++i)
            {
                if (string.Compare(resourceName, resourceNames[i], 
StringComparison.OrdinalIgnoreCase) == 0)
                {
                    using (StreamReader resourceReader = new StreamReader(
asm.GetManifestResourceStream(resourceNames[i])))
                    {
                        if (resourceReader != null)
                        {
                            return resourceReader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }

        #endregion

        public string GetLabel(Office.IRibbonControl Control)
        {
            string result = null;
            switch (Control.Id)
            {
                case "Button1":
                    result = Properties.Resources.ButtonLabel1;
                    break;
                case "Button2":
                    result = Properties.Resources.ButtonLabel2;
                    break;
            }
            return result;
        }

        public string DefaultVaule(Office.IRibbonControl Control)
        {
            if (!flag)
            {
                flag = true;
                return Properties.Resources.ComboBoxItem1;  //初始状态
            }
            else
                return tempText;    //使用用户的选择
        }

        public string GetItemLabel(Office.IRibbonControl Control, int index)
        {
            string result = null;
            switch (index)
            {
                case 0:
                    result = Properties.Resources.ComboBoxItem1;
                    break;
                case 1:
                    result = Properties.Resources.ComboBoxItem2;
                    break;
                case 2:
                    result = Properties.Resources.ComboBoxItem3;
                    break;
            }
            return result;
        }

        public int GetItemCount(Office.IRibbonControl Control)
        {
            return 3;
        }

        public void CultureSelectorChange(Office.IRibbonControl Control, 
string Text)
        {
            if (Text.Equals(Properties.Resources.ComboBoxItem1))
                ChangeCulture("zh-CN");
            if (Text.Equals(Properties.Resources.ComboBoxItem2))
                ChangeCulture("en-US");
            if (Text.Equals(Properties.Resources.ComboBoxItem3))
                ChangeCulture("ja-JP");
        }

        private void ChangeCulture(string CultureName)
        {
            CultureInfo objCultureInfo = new CultureInfo(CultureName);
            Properties.Resources.Culture = objCultureInfo;
            switch (CultureName)
            {
                case "zh-CN":
                    tempText = Properties.Resources.ComboBoxItem1;
                    break;
                case "en-US":
                    tempText = Properties.Resources.ComboBoxItem2;
                    break;
                case "ja-JP":
                    tempText = Properties.Resources.ComboBoxItem3;
                    break;
            }
            this.ribbon.Invalidate();   //重画ribbon用来刷新Ribbon
        }

        /// <summary>
        /// 意义不大可省略
        /// </summary>
        /// <param name="Control"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public string GetItemID(Office.IRibbonControl Control, int index)
        {
            string result = null;
            switch (index)
            {
                case 0:
                    result = "zh-CN";
                    break;
                case 1:
                    result = "en-US";
                    break;
                case 2:
                    result = "ja-JP";
                    break;
            }
            return result;
        }
    }
}

OutlookAddIn25Ribbon.xml

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" 
onLoad="Ribbon_Load">
  <ribbon>
    <tabs>
      <tab idMso="TabAddIns">
        <group id="OutlookAddIn25Group"
               label="OutlookAddIn25">
          <comboBox id="CultureSelector" getItemLabel="GetItemLabel"
getText ="DefaultVaule" getItemCount="GetItemCount" getItemID ="GetItemID" 
onChange ="CultureSelectorChange">
          </comboBox>
          <button id="Button1" getLabel ="GetLabel"/>
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

Mail_Compose.xml

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" 
onLoad="Ribbon_Load">
  <ribbon>
    <tabs>
      <tab idMso="TabAddIns">
        <group id="OutlookAddIn25Group"
               label="OutlookAddIn25">
          <button id="Button2" getLabel ="GetLabel"/>
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

相关资源:http://download.csdn.net/detail/tx_officedev/3924316

posted @ 2011-12-15 13:54  许阳 无锡  阅读(301)  评论(0编辑  收藏  举报