娟斌心 发表于 2015-9-14 08:48:10

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
{

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 IDMessage class
* Microsoft.OMS.MMS.ComposeIPM.Note.Mobile.MMS.*
* Microsoft.OMS.MMS.ReadIPM.Note.Mobile.MMS.*
* Microsoft.OMS.SMS.ComposeIPM.Note.Mobile.SMS.*
* Microsoft.OMS.SMS.ReadIPM.Note.Mobile.SMS.*
* Microsoft.Outlook.AppointmentIPM.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,
StringComparison.OrdinalIgnoreCase) == 0)
{
using (StreamReader resourceReader = new StreamReader(
asm.GetManifestResourceStream(resourceNames)))
{
if (resourceReader != null)
{
return resourceReader.ReadToEnd();
}
}
}
}
return null;
}
#endregion
public string GetLabel(Office.IRibbonControl Control)
{
string result = null;
switch (Control.Id)
{
case &quot;Button1&quot;:
result = Properties.Resources.ButtonLabel1;
break;
case &quot;Button2&quot;:
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(&quot;zh-CN&quot;);
if (Text.Equals(Properties.Resources.ComboBoxItem2))
ChangeCulture(&quot;en-US&quot;);
if (Text.Equals(Properties.Resources.ComboBoxItem3))
ChangeCulture(&quot;ja-JP&quot;);
}
private void ChangeCulture(string CultureName)
{
CultureInfo objCultureInfo = new CultureInfo(CultureName);
Properties.Resources.Culture = objCultureInfo;
switch (CultureName)
{
case &quot;zh-CN&quot;:
tempText = Properties.Resources.ComboBoxItem1;
break;
case &quot;en-US&quot;:
tempText = Properties.Resources.ComboBoxItem2;
break;
case &quot;ja-JP&quot;:
tempText = Properties.Resources.ComboBoxItem3;
break;
}
this.ribbon.Invalidate();   //重画ribbon用来刷新Ribbon
}
/// <summary>
/// 意义不大可省略
/// </summary>
/// <param name=&quot;Control&quot;></param>
/// <param name=&quot;index&quot;></param>
/// <returns></returns>
public string GetItemID(Office.IRibbonControl Control, int index)
{
string result = null;
switch (index)
{
case 0:
result = &quot;zh-CN&quot;;
break;
case 1:
result = &quot;en-US&quot;;
break;
case 2:
result = &quot;ja-JP&quot;;
break;
}
return result;
}
}
}

OutlookAddIn25Ribbon.xml
  
  

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

Mail_Compose.xml
  
  

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

相关资源:http://download.iyunv.com/detail/tx_officedev/3924316
  
页: [1]
查看完整版本: RibbonX的Globalization和Outlook中RibbonX的应用