Tải bản đầy đủ (.pdf) (7 trang)

Addison Wesley Windows Forms Programming In C Sharp Sep 2003 ISBN 0321116208

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (381.52 KB, 7 trang )

Shapes
Nowthatyouknowhowtoframeandfillshapeswithpensand
brushes,youmightbeinterestedintheshapesthatare
available.Figure4.15showsthem.

Figure4.15.TheBasicShapes

AlltheshapesinFigure4.15wereedgedusingaDrawXxx
functionfromtheGraphicsobjectfortheformforexample,
DrawArcandDrawBezier.Theshapesthatcanbefilledwere
drawnusingaFillXxxfunction,suchasFillClosedCurveand
FillEllipse.Notalloftheshapescouldbefilledbecausenotallof
themareclosedshapes;forexample,thereisnoFillCurve.
However,alltheopenshapes(excepttheBezier)haveclosedshapeequivalents;forexample,afilledarciscalledapie.
Also,noticetheuseoftheLinesshape.Thisshapecouldbe
drawnusingmultiplecallstotheDrawLinefunction,butthreeof
theshapesline,rectangle,andBezierhavehelpersthatdraw
moreofthematonce.Inadditiontobeingconvenient,these


helpershandletheappropriatemiteringatintersectionsthat
you'dotherwisehavetodobyhand.Forexample,theGraphics
objectprovidesallthefollowingfunctionsfordrawing
rectangles:DrawRectangle,DrawRectangles,FillRectangle,and
FillRectangles.

Curves
Mostoftheshapesarespecifiedasyou'dexpect.Youspecify
therectangleandtheellipseusinganx,y,width,andheight,or
aRectangleobject.Youspecifythearcandthepieaswitha
rectangle,butyoualsoincludeastartandalengthofsweep,


bothspecifiedindegrees(theshownarcandpiestartat180
degreesandsweepfor180degrees).Thelinesandpolygonare
specifiedwithanarrayofpoints,asarethecurves,butthe
curvesarealittledifferent.
Thecurve(alsoknownasacardinalspline)actsjustlikeaset
oflines,exceptasapointisapproached,there'sacurveinstead
ofasharppoint.Inadditiontoasetofpoints,thecurveis
specifiedusingatension,whichisavaluethatdetermineshow
"curvy"thecurveisaroundthepoints.Atensionof0indicates
nocurve,andatensionof0.5isthedefault.Thetensioncan
getashighasallowedbythefloatingpointtype.Figure4.16
showssomecommonvariations.

Figure4.16.CurvesDrawnwithVariousValuesof
Tension


Figure4.16showsthesamesetofpoints(asindicatedbythe
blackdotsandindexnumber)drawnusingtheDrawCurve
functionwiththreedifferentvaluesoftension.Asthetension
increases,sodoestheamountofcurveateachpoint.
Unlikenormalcurves,Beziercurvesarespecifiedwithexactly
fourpoints:onestartpoint,followedbytwocontrolpoints,
followedbyanendpoint.IftheDrawBeziersfunctionisusedto
drawmultiplecurves,theendpointoftheprecedingcurve
becomesthestartpointofthenext.Figure4.17showsthree
Beziercurvesdrawnusingthesamesetofpoints,butin
differentorders.

Figure4.17.ThreeBezierCurvesDrawnUsingthe

SameSetofPointsinDifferentOrders


Ineachcase,theBezierisdrawnbetweenthestartpointand
theendpoint,butthetwocontrolpointsareusedtodetermine
theshapeofthecurvebyexertingmore"control"overthe
curveastheygetfartheraway.

SmoothingModes
Whendrawingshapes,youmaywantthesmoothrendering
you'veseeninthereallycoolapplications.Theshapesin
Figures4.15,4.16,and4.17werealldrawnwithoutanykindof
"smoothing,"asevidencedbythejaggededges.Thejagged
edgesarecausedbytheswifttransitionbetweenthecolorof
theshapebeingdrawnandthecolorofthebackground.A
techniqueknownasantialiasingusesasmoothertransition
betweentheshapecolorandthebackgroundcolor,inmuchthe
samewaythatagradientbrushprovidesasmoothtransition
fromonecolortoanother.Toturnonantialiasingforshapes
subsequentlydrawnontheGraphicsobject,yousetthe
SmoothingModeproperty:
g.SmoothingMode=SmoothingMode.AntiAlias;
ThedefaultvalueoftheSmoothingModepropertyis
SmoothingMode.None.InadditiontotheAntiAliasvalue,
SmoothingModehasthreeothervalues:Default,HighSpeed,
andHighQuality.ThesearemerelyaliasesforNone,None,and
AntiAlias,dependingonyoursystemsettings.Figure4.18
showsthedifferencebetweenusingandnotusingantialiasing.

Figure4.18.TheEffectofChangingthe

SmoothingModefromAntiAliastoNone


NoticethatsettingtheSmoothingModehasnoeffectonthetext
drawnontheGraphicsobject.Yousettherenderingeffectsof
textusingtheTextRenderingHintproperty,whichIdiscussin
Chapter5:DrawingText.

SavingandRestoringGraphicsSettings
SettingtheSmoothingModeintheprecedingsectionisthefirst
timewe'vechangedapropertyontheGraphicsobjectthat
affectssubsequentoperations.Youcanalsosetotherproperties
thataffectsubsequentoperations,andwe'llcoverthosetopics
asappropriate.WhenyouchangeapropertyofaGraphics
objectinamethodotherthanthePainteventhandleritself,it's
agoodideatoresetitontheGraphicsobjectbeforethe
methodreturns:
voidDrawSomething(Graphicsg){
//Saveoldsmoothingmode
SmoothingModeoldMode=g.SmoothingMode;
//Makethingsdrawsmoothly
g.SmoothingMode=SmoothingMode.AntiAlias;
//Drawthings...
//Restoresmoothingmode
g.SmoothingMode=oldMode;
}


Thiscanquicklybecomepainfulwhentherearemultiple
propertiestorestore.Luckily,youcansaveyourselfthetrouble

bytakingasnapshotofaGraphicsobjectstateina
GraphicsStateobjectfromtheSystem.Drawing.Drawing2D
namespace:
voidDrawSomething(Graphicsg){
//Saveoldgraphicsstate
GraphicsStateoldState=g.Save();
//Madethingsdrawsmoothly
g.SmoothingMode=SmoothingMode.AntiAlias;
//Drawthings...
//Restoreoldgraphicsstate
g.Restore(oldState);
}
TheSavemethodontheGraphicsclassreturnsthecurrent
stateofthepropertiesintheGraphicsobject.Thecallto
RestoretakesaGraphicsStateobjectandsetstheGraphics
objecttothestatecachedinthatobject.Thecodeshowsapair
ofcallstoSaveandRestore,butit'snotnecessarytokeep
theminbalance,somethingthat'shandyforswitchingalot
betweenacoupleofstates.


Chapter5.DrawingText
Probablythemostusefulthingtodrawinanyapplicationis
text.Sometimesyou'lldrawtextyourself,andsometimesitwill
bedrawnforyoubythecontrolsyou'reusing.Nomatterwho
doesthedrawing,youoftencanspecifythefontusedtodraw
thetext,andthat'swhatthefirstpartofthischapterisabout.
ThesecondpartdealswithdrawingtextyourselfintoaGraphics
objectoraGraphicsPath.




×