Tải bản đầy đủ - 0 (trang)
Chapter 15. Working with the Actions Pane

Chapter 15. Working with the Actions Pane

Tải bản đầy đủ - 0trang

IntroductiontotheActionsPane

DevelopingasolutionthatrunswithinanOfficeapplication

providesconsiderablebenefitsbecauseyoucantakeadvantage

ofthefunctionalitythatalreadyexistsinOffice.Sometimes,

however,itishardtodesignauserinterfacethatmeetsyour

needs,asmostoftheuserinterfacespaceiscontrolledbythe

Officeapplication.Office2003andVSTOintroduceanumberof

newuserinterfacecapabilities,includingtheabilitytouse

WindowsFormscontrolsonthedocument.(SeeChapter14,

"UsingWindowsFormsinVSTO,"formoreinformationonthis

capability.)

Placingacontrolonthedocumentisnotalwaystheright

paradigmfortheuserinterfaceofyourapplication.Puttinga

controlontothedocumentcanoftenleadtoissueswithlayout

whenthecontrolsarelaidoutrelativetoarangeorparagraph,

forexample.IfyouuseabuttononaWorddocument,by

default,itwillbeinlinewiththetext.Thismeansthatwhenyou

reformatthedocument,thebuttonwillmovewiththetext.

Obviously,beingabletomoveacontrolwiththetextis

somethingthatyouwouldwantifyouaredevelopingaflowbaseduserinterface.Butthismodelquicklybecomesdifficult

whenyouaredevelopingmoretraditionaluserinterfaces.

Thingsgetevenmorecomplexifyoustarttoconsiderwhat

typeofbehavioryouwantwhentheuserprintsadocument.Do

youwantyourWindowsFormscontrolstobeprintedwiththe

restofthedocument,forexample?

Toaddresstheseuserinterfacechallenges,Office2003

introducedtheabilitytoputyourowncustomuserinterface

intotheDocumentActionstaskpaneofWordandExcel.The

taskpaneisdesignedtoprovideacontextualuserinterface

thatiscomplementarytothedocument.Word,forexample,

providesataskpanethatshowsthestylesandformats

availableinthecurrentdocumentanddisplaysthestyleofthe



currentselectioninthedocument,asshowninFigure15.1.To

displaythetaskpane,chooseTaskPanefromtheViewmenu.



Figure15.1.TheStylesandFormattingtaskpane

inWord.



[Viewfullsizeimage]



Theactivetaskpanecanbechangedbymakingaselection

fromthedrop-downlistofavailabletaskpanesatthetopofthe

taskpane,asshowninFigure15.2.Theactivetaskpaneisa

per-documentsetting.Youcanhaveonlyonetaskpanevisible

atatimeperdocument.Thedrop-downlistshowsseveraltask

panesthatarebuiltintoOffice.Thetaskpaneactslikeatoolbar



whenyoudragittomoveittoanotherlocation.Itcanfloat

abovethedocument.Itcanalsobedockedtotheleft,top,

right,orbottomoftheapplicationwindowspace.



Figure15.2.SelectingataskpaneinWord.



Figure15.2listsseveralofthebuilt-intaskpanesavailablein

Word,includingGettingStarted,Help,andClipArt.Thetask

paneinthelistthatiscustomizablebyyourVSTOWordorExcel

applicationiscalledtheDocumentActionstaskpane.InVSTO

andinthisbook,weoftenrefertotheDocumentActionstask

paneastheactionspane,askindofacontractionbetweenthe

DocumentActionsandthetaskpane.ActionsPaneisthename

ofthecontrolintheVSTOprogrammingmodelthatyouwilluse

toputyourowncontentintheDocumentActionstaskpane.

NotethattheDocumentActionstaskpaneislistedasan



availabletaskpaneforadocumentthathasaVSTO

customizationassociatedwithitthatusestheActionsPane

control.

Listing15.1showsasimpleVSTOExcelcustomizationthat

displaysaWindowsFormsbuttoncontrolintheDocument

Actionstaskpane.InExcel,theActionsPanecontrolisa

memberoftheThisWorkbookclass.Becausethiscodeiswritten

inSheet1,weusetheGlobalsobjecttoaccessthe

ThisWorkbookclassand,fromtheThisWorkbookclass,to

accesstheActionsPanecontrol.TheActionsPanecontrolhasa

Controlscollectionthatcontainsthecontrolsthatwillbeshown

intheDocumentActionstaskpane.Weaddtothiscollectionof

controlsaWindowsFormsbuttoncontrolwecreatedpreviously.

NotethatjusttheactionofaddingacontroltotheControls

collectioncausestheDocumentActionstaskpanetobeshown

atstartup.



Listing15.1.AVSTOExcelCustomizationThat

AddsaButtontotheActionsPane



PublicClassSheet1

PublicmyButtonAsNewButton

PrivateSubSheet1_Startup(ByValsenderAsObject,_

ByValeAsSystem.EventArgs)HandlesMe.Startup

myButton.Text="HelloWorld"

Globals.ThisWorkbook.ActionsPane.Controls.Add(myButton)

EndSub

EndClass



Figure15.3showstheresultofrunningListing15.1.The

DocumentActionstaskpaneisshownwithaWindowsForms



buttondisplayedinthepane.



Figure15.3.TheresultofrunningListing15.1.



Listing15.2showsasimilarVSTOWordcustomizationthat

displaysaWindowsFormsButtoncontrolintheDocument

Actionstaskpane.InWord,theActionsPanecontrolisa

memberoftheThisDocumentclass.



Listing15.2.AVSTOWordCustomizationThat

UsestheActionsPane



PublicClassThisDocument



PublicmyButtonAsNewButton

PrivateSubThisDocument_Startup(ByValsenderAsObject,_

ByValeAsSystem.EventArgs)HandlesMe.Startup

myButton.Text="HelloWorld"

ActionsPane.Controls.Add(myButton)

EndSub

EndClass



TheDocumentActiontaskpaneisactuallypartofalarger

applicationdevelopmentplatformprovidedinOffice2003called

SmartDocuments.ThevisionwasthatSmartDocumentswould

integratethenewXMLfeaturesavailableinWordandExceland

intheDocumentActionstaskpane.ThiscombinationofXML

andtheDocumentActionstaskpaneprovidesanapplication

developmentplatformthatmakesiteasiertobuilddocuments

thatare"smart"abouttheircontentandprovidethe

appropriateuserinterface.

SmartDocumentsweredesignedprimarilyfortheCOMworld.

SoalthoughSmartDocumentsprovidedapowerfulplatform,

theydidnotfiteasilyintothe.NETdevelopmentmethodology.

Why?

1. ThewayyoucreateaSmartDocumentisfirsttocreatea

componentthatimplementstheISmartDocumentinterface.

ThisinterfaceisratherCOM-centric.

2. TouseaSmartDocument,youmusthaveXMLschema

mappedinyourdocument.AlthoughXMLmappingprovides

considerablefunctionalitytoyourapplicationprogramming

(seeChapter21,"WorkingwithXMLinExcel,"andChapter

22,"WorkingwithXMLinWord"),notalldocumentsneedor

wanttouseXMLmapping.

3. TheDocumentActionstaskpanesupportsonlyasmallset



ofbuilt-incontrolsandActiveXcontrols.TouseaWindows

Formscontrol,youwouldhavetoregisteritasanActiveX

controlandthenattempttogetthattoworkwithinthe

DocumentActionstaskpane.ThisrequiresCOMregistration

andCOMinterop.

4. TheSmartDocumentsinfrastructurerequiresyoutocreate

anexpansionpack,whichincludesthefollowing:

-Manifest.xml,whichcontainslinkstoallthe

componentswithintheexpansionpack

-Documenttobeused

-SchemafortheSmartDocument

-ConfigurationXMLfile,whichcontainsthedefinitionof

thecontrolstobeused

VSTOprovidestheActionsPanecontroltogiveyouaccesstoall

thefeaturesprovidedbySmartDocumentswithamuchmore

.NETdevelopmentexperience.Youdonothavetoimplement

theISmartDocumentinterfaceoruseschemamappinginthe

document.YoudonothavetoregisterWindowsFormscontrols

intheregistrysothattheycanactasActiveXcontrols.Youdo

nothavetocreateanexpansionpack.Becauseusingthe

ActionsPanecontrolissomuchsimplerthanusingSmart

Documentsandprovidesallthebenefits,thisbookdoesnot

considerbuildingSmartDocumentsintheoldCOMway.

TheActionsPanefeatureofVSTOisactuallyimplementedunder

thecoversasaspecializedSmartDocumentsolution;whenyou

lookatacustomizedVSTOdocumentandexaminetheattached

XMLschemas,youwillseethataschemacalledActionsPaneis

attachedautomatically.Thisschemaprovidestheplumbingto

connectVSTO'sActionsPanecontroltotheSmartDocument

platform.WhenyouinstalltheVSTOruntime(seeChapter20,

"Deployment"),theActionsPaneschemaisalsoinstalledand



registeredwithExcelandWord,enablingtheActionsPane

controltoaccesstheDocumentActionstaskpane.



WorkingwiththeActionsPaneControl

AfirststepinunderstandinghowVSTO'sActionsPanecontrol

worksisdelvingalittleintothearchitectureofVSTO's

ActionsPanesupport.



TheActionsPaneArchitecture

TheDocumentActionstaskpaneisawindowprovidedbyOffice

thatcanhostActiveXcontrols,asshowninFigure15.4.VSTO

placesaspecialinvisibleActiveXcontrolintheDocument

ActionstaskpanethatinturnhostsasingleWindowsForms

UserControl.ThisUserControlisrepresentedintheVSTO

programmingmodelbytheActionsPanecontrolaccessiblein

WordviaDocument.ActionsPaneandaccessibleinExcelvia

Globals.ThisWorkbook.ActionsPane.



Figure15.4.ThefourlayersoftheActionsPane

architecture.



[Viewfullsizeimage]



AlthoughtheDocumentActionstaskpanecanhostmultiple

ActiveXcontrols,VSTOneedstoputonlyasingleActiveX

controlandasingleUserControlintheDocumentActionstask

panewindow,becausetheUserControlcanhostmultiple

WindowsFormscontrolsviaitsControlscollection

(ActionsPane.Controls).YoucanaddWindowsFormscontrolsto

theActionsPanebyusingtheActionsPane.Controls.Addmethod.

TheUserControlplacedintheActionsPanewindowissetto

expandtofittheareaprovidedbytheActionsPanewindow.If

theareaoftheDocumentActionstaskpaneisnotbigenough

todisplayallthecontrolshostedbytheUserControl,itis

possibletoscrolltheUserControlbysettingtheAutoScroll

propertyofActionsPanetotrue.

TheActionsPanecontrolisawrapperaroundSystem.Windows

.Forms.UserControlwithmostoftheproperties,methods,and

eventsofaUserControl.Italsoaddssomeproperties,events,

andmethodsspecifictoActionsPane.Whenyouunderstandthe

architectureinFigure15.4,youwillnotbetoosurprisedto

knowthatsomepropertiesfromUserControlthatareexposed

byActionsPanesuchasposition-relatedproperties,methods,

andeventsdonotdoanything.Becausethepositionofthe

ActionsPaneUserControlisforcedtofillthespaceprovidedby

theActionsPanewindow,forexample,youcannotrepositionthe

UserControltoarbitrarypositionswithintheDocumentActions

taskpanewindow.



AddingWindowsFormsControlstotheActions

Pane

ThebasicwayyouaddyourcustomUItotheactionspaneisto

addWindowsFormscontrolstotheactionspane'sControls



collection.Listing15.1illustratesthisapproach.First,itdeclares

andcreatesaninstanceofaSystem.Windows.Forms.Button

control.Thenthiscontrolisaddedtotheactionspanebycalling

theAddmethodoftheControlscollectionassociatedwiththe

actionspaneandpassingthebuttoninstanceasaparameterto

theAddmethod.

Theactionspaneissmartaboutarrangingcontrolswithinthe

ActionsPane.IfmultiplecontrolsareaddedtotheControls

collection,theactionspanecanautomaticallystackandarrange

thecontrols.Thestackingorderiscontrolledbythe

ActionsPane.StackOrderproperty,whichisoftype

Microsoft.Office.Tools.StackStyle.ItcanbesettoNoneforno

automaticpositioning,oritcanbesettoFromTop,FromBottom,

FromLeft,orFromRight.Figure15.5showstheeffectsofthe

variousStackOrdersettings.



Figure15.5.Theresultsofchangingthe

ActionsPaneStackOrdersetting,fromtop

left:None,FromLeft,FromBottom,FromTop,and

FromRight.



[Viewfullsizeimage]



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 15. Working with the Actions Pane

Tải bản đầy đủ ngay(0 tr)

×