Developping Ribbon(设计Ribbon)
by Charlie on Jun.12, 2008, under VSTO, Hits 1450
Nearly the end of the term, coupled with last weekend’s Northeast ACM,
I am afraid not much time to write the VSTO. So taking advantage of this two-day
idle, wrote this article.
While developping a software, we always want a human surface, VSTO projects versa. And I have said before, these articles is mainly for the users of VS2005, since VS2008 users do not have to charge too much effort, VS2008 enables them design a Ribbon easily, Figure:As an example to VS2005 + PowerPoint2007 (the same to Word, Excel),user just need to create a Add New Item menu to create a new Ribbon. This creates two files in your project:one for Ribbon definition and one for the Ribbon codebehind.Just simply edit the methods generated in the Ribbon1 class to handle these callbacks,you will see a new Ribbon, a lovely big smiles.
Follow are the content of the XML file:
<customUI xmlns=
"http://schemas.microsoft.com/office/2006/01/customui"
onLoad="OnLoad">
<ribbon>
<tabs>
<tab id="myOwnTab" label="PowerPlugin" >
<group id="Group_Options" label="PowerPlugin">
<toggleButton id="ButtonPassOn" size="large"
label="PowerPlugin On" screentip="我的按钮屏幕提示"
onAction="OnToggleButton" imageMso="HappyFace"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Here, it is not difficult to see the structure
of our Ribbon:a tab whose id is myOwnTab, a group
whose id is Group_Options and a toggleButton whose
id is ButtonPassOn.
<toggleButton id="ButtonPassOn" size="large"
label="PowerPlugin On" screentip="我的按钮屏幕提示"
onAction="OnToggleButton" imageMso="HappyFace"/>
Analysising this statement, we can easily guess the
role of onAction. However, from where could we see
the statement of OnToggleButton?
Back to our source, we can easily find this function.
public void OnToggleButton(Office.IRibbonControl control, bool isPressed){
}
Thus, the whole process is relatively clear of.
In addition, I would like to introduce several of my control commonly used.
|
name |
Ope1 |
Ope2 |
| Box | OnAction | |
| dropDown | onAction=
“HandleIndexedAction” |
|
| labelControl | onAction | |
| editBox | getText=”getText” | onChange=
“HandleOnChange” |
| button | onAction=”getText” |
public string getText(Office.IRibbonControl control){
}
public void HandleOnChange(Office.IRibbonControl control, string text){
}
public void HandleIndexedAction( Office.IRibbonControl control,
string selectedId, int index){
}
期末将近,再加上周末的ACM东北赛,恐怕无太多时间再写VSTO了。于是
趁着这两天的空闲,写下这篇文章。
开发一款软件,我们往往希望能有一个人性化的界面,VSTO的项目亦是
如此。当然,我以前说过,这些文章主要是针对VS2005的用户的,用VS2008
的用户则不必费太多功夫,VS2008中能直接设计器扩展Ribbon,如图:
以VS2005 + PowerPoint2007为例(Word、Excel等类似),用
户倘若要新建一个加载项只需新建类中新建一个功能区支持。这时,将会产生
两个文件,一个Ribbon XML文件,一个Ribbon的源文件。只要按照提示将部
分代码注释取消便能在我们的powerpoint中看到一个新的加载项,一个可爱
的大笑脸。
Ribbon XML文件的大致内容如下:
<customUI xmlns=
"http://schemas.microsoft.com/office/2006/01/customui"
onLoad="OnLoad">
<ribbon>
<tabs>
<tab id="myOwnTab" label="PowerPlugin" >
<group id="Group_Options" label="PowerPlugin">
<toggleButton id="ButtonPassOn" size="large"
label="PowerPlugin On" screentip="我的按钮屏幕提示"
onAction="OnToggleButton" imageMso="HappyFace"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
从这儿,我们不难看到我们的Ribbon的构造,是由一个
id为myOwnTab的tab,一个id为Group_Options的group及一个
id为ButtonPassOn的toggleButton构成。其中,tab就是一个
选项卡,计上图中标为的PowerPlugin的选项卡;group也是一个
容器,便是陈放大笑脸的那个容器了;而toggleButton便是那个
大笑脸。
<toggleButton id="ButtonPassOn" size="large"
label="PowerPlugin On" screentip="我的按钮屏幕提示"
onAction="OnToggleButton" imageMso="HappyFace"/>
分析这个语句,我们可以很轻松地猜测到onAction的作用。
但是,OnToggleButton这个事件又是在哪儿声明的呢?
回到我们的Ribbon的源文件,在这我们不难找到这个函数。
public void OnToggleButton(Office.IRibbonControl control,
bool isPressed){
}
于是,整个过程也相对明晰了。
另外,我再介绍几个我常用的控件及其处理函数。至于其他
控件,用户可自己尝试或查阅相关资料。
|
name |
Ope1 |
Ope2 |
| Box | OnAction | |
| dropDown | onAction=
“HandleIndexedAction” |
|
| labelControl | onAction | |
| editBox | getText=”getText” | onChange=
“HandleOnChange” |
| button | onAction=”getText” |
public string getText(Office.IRibbonControl control){
}
public void HandleOnChange(Office.IRibbonControl control, string text){
}
public void HandleIndexedAction( Office.IRibbonControl control,
string selectedId, int index){
}











