WalkLtDemo设计实例.cpp 175 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790
  1. //Download by http://www.NewXing.com
  2. // WalkLtDemo设计实例.cpp : implementation of the CWalkLtDemoView class
  3. //
  4. #include "stdafx.h"
  5. #include "WalkLtDemo.h"
  6. #include "WalkLtDemoDoc.h"
  7. #include "WalkLtDemoView.h"
  8. #include "WalkLtDemoHelp.h"
  9. #include "math.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. //屏蔽 4244 编译警告
  16. #pragma warning( disable : 4244 )
  17. extern Rect MyClient;
  18. void menuItem2_Click()
  19. {
  20. Graphics &g=GetGraphics();
  21. g.Clear(Color::Black);
  22. Bitmap bitmap(L"demo.bmp");
  23. int iWidth = bitmap.GetWidth();
  24. int iHeight = bitmap.GetHeight();
  25. //初始化色彩变换矩阵
  26. ColorMatrix colorMatrix=
  27. {
  28. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  29. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  30. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  31. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  32. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
  33. };
  34. ImageAttributes imageAtt;
  35. //从0到1进行修改色彩变换矩阵主对角线上的数值
  36. //使三种基准色的饱和度渐增
  37. for(float i=0.0f;i<=1.0f;i+=0.02f)
  38. {
  39. colorMatrix.m[0][0]=i;
  40. colorMatrix.m[1][1]=i;
  41. colorMatrix.m[2][2]=i;
  42. colorMatrix.m[3][3]=i;
  43. //设置色彩校正矩阵
  44. imageAtt.SetColorMatrix(&colorMatrix,
  45. ColorMatrixFlagsDefault, ColorAdjustTypeBitmap);
  46. //绘制图片
  47. g.DrawImage(
  48. &bitmap, Rect(0, 0, iWidth, iHeight),
  49. 0,0,
  50. iWidth,iHeight,
  51. UnitPixel,
  52. &imageAtt);
  53. }
  54. MyDrawString(g, "下面演示淡出效果...", Font(L"Arial",12),
  55. SolidBrush(Color::White), Point(20,iHeight+20));
  56. //从1到0进行修改色彩变换矩阵主对角线上的数值
  57. //依次减少每种色彩分量
  58. for(i=1.0f;i>=0.0f;i-=0.02f)
  59. {
  60. colorMatrix.m[0][0]=i;
  61. colorMatrix.m[1][1]=i;
  62. colorMatrix.m[2][2]=i;
  63. colorMatrix.m[3][3]=i;
  64. //设置色彩校正矩阵
  65. imageAtt.SetColorMatrix(&colorMatrix,
  66. ColorMatrixFlagsDefault,ColorAdjustTypeBitmap);
  67. //绘制图片
  68. g.DrawImage(
  69. &bitmap, Rect(0, 0, iWidth, iHeight),
  70. 0,0,
  71. iWidth,iHeight,
  72. UnitPixel,
  73. &imageAtt);
  74. }
  75. MyDrawString(g, "完毕", Font(L"Arial",12),
  76. SolidBrush(Color::White), Point(20,iHeight+40));
  77. }
  78. //
  79. void GrayScale_Click()
  80. {
  81. Graphics &g=GetGraphics();
  82. g.Clear(Color::White);
  83. Bitmap image(L"head.bmp");
  84. int Width =image.GetWidth()-1;
  85. int Height =image.GetHeight()-1;
  86. //绘制原图
  87. g.DrawImage(&image,0, 0);
  88. g.TranslateTransform((REAL)image.GetWidth(),0.f);
  89. /*image2、image3分别用来保存最大值法
  90. 和加权平均法处理的灰度图像*/
  91. Bitmap *image2=image.Clone(Rect(0,0,image.GetWidth(), image.GetHeight()),PixelFormatDontCare);
  92. Bitmap *image3=image.Clone(Rect(0,0,image.GetWidth(), image.GetHeight()),PixelFormatDontCare);
  93. Color color;
  94. //使用平均值进行灰度处理
  95. for(int i=Width; i>=0;i--)
  96. for( int j=Height; j>=0;j--)
  97. {
  98. image.GetPixel(i,j, &color);
  99. //求出平均三个色彩分量的平均值
  100. int middle=(color.GetR()+color.GetG()+color.GetB())/3;
  101. Color colorResult(255,middle,middle,middle);
  102. image.SetPixel(i,j, colorResult);
  103. }
  104. //重新绘制灰度化图
  105. g.DrawImage(&image, Rect(0, 0, Width, Height));
  106. //在新位置显示最大值法进行灰度处理的结果
  107. g.TranslateTransform((REAL)image.GetWidth(),0.f);
  108. //使用最大值法进行灰度处理
  109. for(i=Width; i>=0;i--)
  110. {
  111. for(int j=Height; j>=0;j--)
  112. {
  113. image2->GetPixel(i,j, &color);
  114. int tmp=color.GetR()>color.GetG()? color.GetR():color.GetG();
  115. int maxcolor=tmp>color.GetB()? tmp:color.GetB();
  116. Color colorResult(255,maxcolor,maxcolor,maxcolor);
  117. //设置处理后的灰度信息
  118. image2->SetPixel(i, j, colorResult);
  119. }
  120. }
  121. //重新绘制灰度化图
  122. g.DrawImage(image2, Rect(0, 0, Width, Height));
  123. //在第二行绘制图片
  124. g.ResetTransform();
  125. g.TranslateTransform(0.f, (REAL)image.GetHeight());
  126. //使用加权平均法进行灰度处理
  127. for(i=Width; i>=0;i--)
  128. {
  129. for(int j=Height; j>=0;j--)
  130. {
  131. image3->GetPixel(i, j, &color);
  132. int R=(int)(0.3f*color.GetR());
  133. int G=(int)(0.59f*color.GetG());
  134. int B=(int)(0.11f*color.GetB());
  135. Color colorResult(255,R,G,B);
  136. //设置处理后的灰度信息
  137. image3->SetPixel(i, j, colorResult);
  138. }
  139. }
  140. //重新绘制灰度化图
  141. g.DrawImage(image3, Rect(0, 0, Width, Height));
  142. g.TranslateTransform((REAL)image.GetWidth(),0.f);
  143. //灰度的还原演示,还原使用最大值法处理的灰度图像image2
  144. for(i=Width; i>0;i--)
  145. {
  146. for(int j=Height; j>0;j--)
  147. {
  148. image2->GetPixel(i,j, &color);
  149. int R=color.GetR();
  150. int G=color.GetG();
  151. int B=color.GetB();
  152. //分别对RGB三种色彩分量进行伪彩色还原
  153. //进行红色分量的还原
  154. if(R<127)
  155. R=0;
  156. if(R>=192)
  157. R=255;
  158. if(R<=191&&R>=128)
  159. R=4*R-510;
  160. /*进行绿色分量的还原,为了还原后的绿色分量再次参加比较,
  161. 这里设置一个变量YES表示G是否已经参加了比较*/
  162. bool yes;
  163. yes=false;
  164. if(G<=191&&G>=128&&(!yes))
  165. {
  166. G=255;
  167. yes=true;
  168. }
  169. if(G>=192&&(!yes))
  170. {
  171. G=1022-4*G;
  172. yes=true;
  173. }
  174. if(G<=63&&(!yes))
  175. {
  176. G=254-4*G;
  177. yes=true;
  178. }
  179. if(G<=127&&G>=67&&(!yes))
  180. G=4*G-257;
  181. //进行蓝色分量的还原
  182. if(B<=63)
  183. B=255;
  184. if(B>=128)
  185. B=0;
  186. if(B>=67&&B<=127)
  187. B=510-4*B;
  188. //还原后的伪彩色
  189. Color colorResult(255,R,G,B);
  190. //将还原后的RGB信息重新写入位图
  191. image2->SetPixel(i, j, colorResult);
  192. }
  193. }
  194. //重新绘制还原后的伪彩色位图
  195. //重新绘制灰度化图
  196. g.DrawImage(image2, Rect(0, 0, Width, Height));
  197. delete image2;
  198. delete image3;
  199. }
  200. //
  201. void Inverse_Click()
  202. {
  203. Graphics &graphics=GetGraphics();
  204. graphics.Clear(Color::White);
  205. graphics.ScaleTransform(0.7f,0.7f);
  206. Bitmap image(L"head.bmp");
  207. int Width =image.GetWidth();
  208. int Height =image.GetHeight();
  209. Color colorTemp,color2;
  210. Color color;
  211. //绘制原图
  212. graphics.DrawImage(&image, Rect(0, 0, Width, Height));
  213. for(int i=0;i<Width;i++)
  214. {
  215. for( int j=0; j<Height;j++)
  216. {
  217. image.GetPixel(i, j, &color);
  218. //将色彩进行反转,获得底片效果
  219. int r=255-color.GetRed();
  220. int g=255-color.GetGreen();
  221. int b=255-color.GetBlue();
  222. Color colorResult(255,r,g,b);
  223. //将还原后的RGB信息重新写入位图
  224. image.SetPixel(i, j, colorResult);
  225. }
  226. //动态绘制底片滤镜效果图
  227. graphics.DrawImage(&image, Rect(Width, 0, Width, Height));
  228. }
  229. //将已经实现了底片效果的位图再反色(恢复到原图)
  230. for(i=0;i<Width;i++)
  231. {
  232. for( int j=0; j<Height;j++)
  233. {
  234. image.GetPixel(i, j, &color);
  235. int r=255-color.GetRed();
  236. int g=255-color.GetGreen();
  237. int b=255-color.GetBlue();
  238. Color colorResult(255,r,g,b);
  239. //将还原后的RGB信息重新写入位图
  240. image.SetPixel(i, j, colorResult);
  241. }
  242. //绘制经过两次反色的位图
  243. graphics.DrawImage(&image, Rect(Width*2, 0, Width, Height));
  244. }
  245. }
  246. //
  247. void Emboss_Click()
  248. {
  249. Graphics &graphics=GetGraphics();
  250. graphics.Clear(Color::White);
  251. graphics.ScaleTransform(0.7f,0.7f);
  252. Bitmap image(L"head.bmp");
  253. int Width =image.GetWidth();
  254. int Height =image.GetHeight();
  255. //image2:进行雕刻处理
  256. Bitmap *image2=image.Clone(Rect(0,0,Width,Height), PixelFormatDontCare);
  257. //绘制原图
  258. graphics.DrawImage(&image, Rect(0, 0, Width, Height));
  259. Color color, colorTemp,colorLeft;
  260. //进行图片的浮雕处理
  261. //依次访问每个像素的RGB值
  262. for(int i=Width-1; i>0;i--)
  263. {
  264. for( int j=Height-1; j>0;j--)
  265. {
  266. //获取相邻两个像素的R、G、B值
  267. image.GetPixel(i, j, &color);
  268. image.GetPixel(i-1, j-1, &colorLeft);
  269. //计算与左上角像素的RGB分量之差
  270. //67:控制图片的最低灰度,128:常量,更改这两个值会得到不同的效果
  271. int r=max(67,min(255,
  272. abs(color.GetRed()-colorLeft.GetRed()+128)));
  273. int g=max(67,min(255,
  274. abs(color.GetGreen()-colorLeft.GetGreen()+128)));
  275. int b=max(67,min(255,
  276. abs(color.GetBlue()-colorLeft.GetBlue()+128)));
  277. Color colorResult(255,r,g,b);
  278. //将计算后的RGB值回写到位图
  279. image.SetPixel(i, j,colorResult);
  280. }
  281. //绘制浮雕图
  282. graphics.DrawImage(&image, Rect(Width+10, 0, Width, Height));
  283. }
  284. //进行图片的雕刻处理
  285. for(i=0; i<Height-1;i++)
  286. {
  287. for( int j=0; j<Width-1;j++)
  288. {
  289. image2->GetPixel(j, i, &color);
  290. image2->GetPixel(j+1, i+1, &colorLeft);
  291. //计算与右下角像素的分量之差
  292. //67:控制图片的最低灰度,128:常量,更改这两个值会得到不同的效果
  293. int r=max(67,min(255,abs(color.GetRed()-colorLeft.GetRed()+128)));
  294. int g=max(67,min(255,abs(color.GetGreen()-colorLeft.GetGreen()+128)));
  295. int b=max(67,min(255,abs(color.GetBlue()-colorLeft.GetBlue()+128)));
  296. Color colorResult(255,r,g,b);
  297. image2->SetPixel(j, i,colorResult);
  298. }
  299. //绘制雕刻图
  300. graphics.DrawImage(image2, Rect(Width*2+20, 0, Width, image.GetHeight()));
  301. }
  302. delete image2;
  303. }
  304. void CreatePenFromBrush_Click()
  305. {
  306. Graphics &g=GetGraphics();
  307. g.Clear(Color::White);
  308. //构造线性渐变画刷
  309. LinearGradientBrush LineargradientBrush(Rect(0,0,10,10),
  310. Color::Blue,Color::Red, LinearGradientModeBackwardDiagonal);
  311. //从线性渐变画刷中构造画笔
  312. Pen pen(&LineargradientBrush);
  313. pen.SetWidth(10);
  314. //绘制矩形
  315. g.DrawRectangle(&pen,10,10,100,100);
  316. //装入纹理图片
  317. Bitmap image(L"butterfly.bmp");
  318. //构造纹理画刷
  319. TextureBrush tBrush(&image);
  320. //将画刷传入画笔的构造函数
  321. Pen texturedPen(&tBrush,42);
  322. //设置贝塞尔曲线的起止点及控制点
  323. Point p1(10, 100);
  324. Point c1= Point(100, 10);
  325. Point c2= Point(150, 150);
  326. Point p2= Point(200, 100);
  327. g.TranslateTransform(130,0);
  328. //绘制贝塞尔曲线
  329. g.DrawBezier(&texturedPen, p1, c1, c2, p2);
  330. }
  331. //画笔的线形演示
  332. void DashStyle_Custom_Click()
  333. {
  334. Graphics &graphics=GetGraphics();
  335. graphics.Clear(Color::White);
  336. Pen pen(Color::Blue, 5);
  337. //设置文本输出对齐方式及字体
  338. StringFormat fmt;
  339. fmt.SetAlignment(StringAlignmentNear);
  340. fmt.SetLineAlignment(StringAlignmentCenter);
  341. //字体
  342. Font font(L"Arial",12);
  343. SolidBrush sBrush(Color::Black);
  344. graphics.TranslateTransform(0,30);
  345. int i=0;
  346. //分别使用常见的五种线型绘制直线
  347. for(;i<5;i++)
  348. {
  349. //设置线型
  350. pen.SetDashStyle((DashStyle)i);
  351. graphics.DrawLine(&pen, 10, 30*i, 260, 30*i);
  352. //输出当前线型的名称
  353. CString s; s.Format(_T("%d"), i+1);
  354. MyDrawString(graphics, s, font, sBrush, PointF(260.f,30.f*i), fmt);
  355. s.ReleaseBuffer();
  356. }
  357. //使用自定义义线型
  358. float dashVals[]=
  359. {
  360. 5.0f, // 线长5个像素
  361. 2.0f, // 间断2个像素
  362. 15.0f, // 线长15个像素
  363. 4.0f // 间断4个像素
  364. };
  365. pen.SetDashPattern(dashVals, 4);
  366. pen.SetColor(Color::Red);
  367. graphics.DrawLine(&pen, 10, 30*i, 260, 30*i);
  368. CString s; s.Format(_T("%d"), i+1);
  369. MyDrawString(graphics, s, font, sBrush, PointF(260.f,30.f*i), fmt);
  370. }
  371. void Pen_Align_Click()
  372. {
  373. Graphics &graphics=GetGraphics();
  374. graphics.Clear(Color::White);
  375. Pen pen(Color::Gray, 1.0f);
  376. Pen pen2(Color(155,255,0,0),10);
  377. int i=0;
  378. for(; i<5;i++)
  379. {
  380. pen2.SetAlignment((PenAlignment)i);
  381. graphics.DrawLine(&pen2, Point(0,10), Point(60,10));
  382. graphics.TranslateTransform(70,0);
  383. }
  384. graphics.ResetTransform();
  385. graphics.DrawLine(&pen,0,10,600,10);
  386. }
  387. void Pen_Tranform_Click()
  388. {
  389. Graphics &graphics=GetGraphics();
  390. graphics.Clear(Color::White);
  391. //构造一支宽度为5的红色画笔
  392. Pen pen(Color::Red, 3.0f);
  393. //将画笔从垂直方向扩充6倍,水平方向保持不变
  394. pen.ScaleTransform(1, 6);
  395. //使用未经旋转处理的画笔画圆
  396. graphics.DrawEllipse(&pen, 0, 50, 80, 80);
  397. //60°旋转
  398. graphics.TranslateTransform(100,0);
  399. pen.RotateTransform(60, MatrixOrderAppend);
  400. graphics.DrawEllipse(&pen, 0, 50, 80, 80);
  401. //120°旋转
  402. graphics.TranslateTransform(100,0);
  403. pen.RotateTransform(60, MatrixOrderAppend);
  404. graphics.DrawEllipse(&pen, 0, 50, 80, 80);
  405. //180°旋转
  406. graphics.TranslateTransform(100,0);
  407. pen.RotateTransform(60, MatrixOrderAppend);
  408. graphics.DrawEllipse(&pen, 0, 50, 80, 80);
  409. }
  410. //线帽演示
  411. void Pen_LineCap_Click()
  412. {
  413. Graphics &graphics=GetGraphics();
  414. graphics.Clear(Color::White);
  415. //设置文本输出对齐方式及字体
  416. StringFormat fmt;
  417. fmt.SetAlignment(StringAlignmentNear);
  418. fmt.SetLineAlignment(StringAlignmentCenter);
  419. //字体
  420. Font font(L"Arial",12);
  421. SolidBrush sBrush(Color::Black);
  422. //创建宽度为15的画笔
  423. Pen pen(Color::Black,15);
  424. //分别使用不同的线帽
  425. LineCap lincap[]=
  426. {
  427. LineCapFlat,
  428. LineCapSquare,
  429. LineCapRound,
  430. LineCapTriangle,
  431. LineCapNoAnchor,
  432. LineCapSquareAnchor,
  433. LineCapRoundAnchor,
  434. LineCapDiamondAnchor,
  435. LineCapArrowAnchor,
  436. LineCapCustom,
  437. LineCapAnchorMask
  438. };
  439. for (int i=0; i<sizeof(lincap)/sizeof(LineCap); i++)
  440. {
  441. pen.SetStartCap(lincap[i]);//起点
  442. pen.SetEndCap(lincap[i]);//终点
  443. graphics.DrawLine(&pen,50,10,300,10);
  444. //输出当前线帽类型(LineCap枚举成员名)
  445. CString s; s.Format(_T("%d"), lincap[i]);
  446. MyDrawString(graphics, s, font, sBrush, PointF(320.f,10.f), fmt);
  447. //平移绘图平面
  448. graphics.TranslateTransform(0,30);
  449. }
  450. }
  451. //画笔的透明度支持
  452. void Pen_TransColor_Click()
  453. {
  454. Graphics &graphics=GetGraphics();
  455. graphics.Clear(Color::White);
  456. //分别构造蓝色、红色画笔
  457. Pen blue(Color::Blue);
  458. Pen red(Color::Red);
  459. //绘制网线
  460. int y = 256;
  461. for (int x = 0; x < 256; x += 5)
  462. {
  463. graphics.DrawLine(&blue, 0, y, x, 0);
  464. graphics.DrawLine(&red, 256, x, y, 256);
  465. y -= 5;
  466. //延时以查看动态效果
  467. ::Sleep(20);
  468. }
  469. //使用绿色画笔绘制不同透明度的线条
  470. //透明度由上到下依次递减
  471. for (y = 0; y < 256; y++)
  472. {
  473. Pen pen(_MakeA_RGB(y,Color::Green));
  474. graphics.DrawLine(&pen, 0, y, 256, y);
  475. //延时以便查看动态效果
  476. ::Sleep(20);
  477. }
  478. //使用绿色画笔绘制不同透明度的线条
  479. //透明度由左到右依次递减
  480. for (x = 0; x < 256; x++)
  481. {
  482. Pen pen(_MakeA_RGB(x, Color::Blue));
  483. graphics.DrawLine(&pen, x, 100, x, 200);
  484. //延时以查看动态效果
  485. ::Sleep(20);
  486. }
  487. }
  488. //简单的单色画刷示意
  489. void Brush_SolidBrush_Click()
  490. {
  491. Graphics &graphics=GetGraphics();
  492. graphics.Clear(Color::White);
  493. //创建绿色画刷
  494. SolidBrush greenBrush(Color::Green);
  495. //定义曲线、多边形端点坐标
  496. PointF point1(100.0f, 100.0f);
  497. PointF point2(200.0f, 50.0f);
  498. PointF point3(250.0f, 200.0f);
  499. PointF point4(50.0f, 150.0f);
  500. PointF point5(100.0f, 100.0f);
  501. PointF points[]={point1, point2, point3, point4};
  502. //填充闭合曲线
  503. graphics.FillClosedCurve(&greenBrush, points, 4, FillModeAlternate, 1.0f);
  504. //构造多边形(闭合)
  505. PointF poly[]={point1, point2, point3, point4,point5};
  506. //在另一个位置填充多边形
  507. graphics.TranslateTransform(300,0);
  508. graphics.FillPolygon(&greenBrush, poly, 5, FillModeAlternate);
  509. }
  510. //填充正叶曲线
  511. void Brush_FillVurve_Click()
  512. {
  513. Graphics &graphics=GetGraphics();
  514. graphics.Clear(Color::White);
  515. //创建绿色画刷
  516. SolidBrush greenBrush(Color::Green);
  517. int cx,cy;
  518. //以当前窗口的中心点绘制正叶曲线
  519. cx=MyClient.Width/2;
  520. cy=MyClient.Height/2;
  521. //设置"叶"长
  522. int LeafLength=80;
  523. //设置叶片数量=2* LeafNum
  524. int LeafNum=5;
  525. double x,y,x2,y2,r;
  526. //创建一个图形路径对象,用以容纳正叶曲线的边界线
  527. GraphicsPath tmpPath(FillModeAlternate);
  528. //生成曲线边界数据:角度变化为一周PI*2
  529. for(float i=0.0f;i<PI*2+0.1f;i+=(float)PI/180)
  530. {
  531. r=(int)abs(LeafLength*cos(double(LeafNum*i)));
  532. x=r*cos(i);
  533. y=r*sin(i);
  534. x2=cx+x;
  535. y2=cy+y;
  536. /*将曲线的边界信息存入临时路径,如果想要查看这些信息所构成的区域
  537. ,可以在此加入graphics.DrawLine(&pen,x2,y2,x2-1,y2-1);*/
  538. tmpPath.AddLine((int)x2,(int)y2,(int)x2,(int)y2);
  539. }
  540. //填充路径
  541. graphics.FillPath(&greenBrush, &tmpPath);
  542. //绘制中心坐标轴
  543. Pen pen(Color::Gray);
  544. graphics.DrawLine(&pen,0,cy,cx*2,cy);
  545. graphics.DrawLine(&pen,cx,0,cx,cy*2);
  546. }
  547. //影线画刷示意
  548. void Brush_HatchBrush_Click()
  549. {
  550. Graphics &graphics=GetGraphics();
  551. graphics.Clear(Color::White);
  552. //构造影线画刷的前后色彩
  553. Color black=Color::Black;
  554. Color white=Color::White;
  555. //使用第一种风格的影线画刷
  556. HatchBrush brush(HatchStyleHorizontal, black, white);
  557. graphics.FillRectangle(&brush, 20, 20, 100, 50);
  558. //使用第二种风格的影线画刷
  559. HatchBrush brush1(HatchStyleVertical, black, white);
  560. graphics.FillRectangle(&brush1, 120, 20, 100, 50);
  561. //使用第三种风格的影线画刷
  562. HatchBrush brush2(HatchStyleForwardDiagonal, black, white);
  563. graphics.FillRectangle(&brush2, 220, 20, 100, 50);
  564. //使用第四种风格的影线画刷
  565. HatchBrush brush3(HatchStyleBackwardDiagonal, black, white);
  566. graphics.FillRectangle(&brush3, 320, 20, 100, 50);
  567. //使用第五种风格的影线画刷
  568. HatchBrush brush4(HatchStyleCross, black, white);
  569. graphics.FillRectangle(&brush4, 420, 20, 100, 50);
  570. //使用第六种风格的影线画刷
  571. HatchBrush brush5(HatchStyleDiagonalCross, black, white);
  572. graphics.FillRectangle(&brush5, 520, 20, 100, 50);
  573. }
  574. //列举出所有风格的影线画刷
  575. void Brush_EnumAllStyle_Click()
  576. {
  577. Graphics &graphics=GetGraphics();
  578. graphics.Clear(Color::White);
  579. //设定画刷的前景色为黑色,背景色为白色
  580. Color black=Color::Black;
  581. Color white=Color::White;
  582. //预定义填充区域的宽度及高度
  583. int WIDTH=140;
  584. int HEIGHT=40;
  585. //设定输出文本所需信息
  586. SolidBrush redBrush(Color::Red);
  587. Font myFont(L"Arial", 10);
  588. //column_count表明在每一行能够绘制矩形的总数
  589. int column_count=MyClient.Width/WIDTH;
  590. int rol=0;
  591. int column=0;
  592. //在当前窗口使用所有的影线画刷种风格填充矩形
  593. Pen pen(Color::Blue, 1);
  594. for (int i=HatchStyleHorizontal; i<HatchStyleTotal; i++)
  595. {
  596. HatchStyle style=(HatchStyle)i;
  597. //如果一行已经绘制完毕,换行
  598. if (rol>column_count-1)
  599. {
  600. column+=2;
  601. rol=0;
  602. }
  603. //创建临时画刷
  604. HatchBrush brush_tmp(style, black, white);
  605. //填充矩形:设置宽度为WIDTH-20的目的是让矩形之间留出间隔
  606. graphics.FillRectangle(&brush_tmp, rol*WIDTH, column*HEIGHT, WIDTH-20, HEIGHT);
  607. //绘制矩形边框
  608. graphics.DrawRectangle(&pen, rol*WIDTH, column*HEIGHT, WIDTH-20, HEIGHT);
  609. //显示每种画刷风格的枚举名称
  610. //计算文本输出区域
  611. RectF layoutRect(rol*WIDTH, (column+1)*HEIGHT, WIDTH, HEIGHT);
  612. StringFormat format;
  613. //设置文本输出格式:水平、垂直居中
  614. format.SetAlignment(StringAlignmentNear);
  615. format.SetLineAlignment(StringAlignmentCenter);
  616. //在矩形框中央输出枚举值
  617. CString s; s.Format(_T("%d"), style);
  618. MyDrawString(graphics, s, myFont, redBrush, layoutRect, format);
  619. rol+=1;
  620. }
  621. }
  622. //设置绘制原点
  623. void Brush_SetRenderingOrigin_Click()
  624. {
  625. Graphics &graphics=GetGraphics();
  626. graphics.Clear(Color::White);
  627. //设定画刷的前景色为黑色,背景色为白色
  628. Color black=Color::Black;
  629. Color white=Color::White;
  630. HatchBrush hatchBrush(HatchStyleDarkDownwardDiagonal,black,white);
  631. //在竖直方向填充8个矩形,使用默认的画刷原点
  632. for(int i=0;i<8;i++)
  633. {
  634. graphics.FillRectangle(&hatchBrush, 0, i*50, 100, 50);
  635. }
  636. //使用不同的绘制原点进行区域填充
  637. for(i=0;i<8;i++)
  638. {
  639. //设置画刷原点(水平方向递增)
  640. graphics.SetRenderingOrigin(i, 0);
  641. graphics.FillRectangle(&hatchBrush, 100, i*50, 100, 50);
  642. }
  643. }
  644. //纹理画刷的不同加载方式
  645. void Brush_Texture_Click()
  646. {
  647. Graphics &graphics=GetGraphics();
  648. graphics.Clear(Color::White);
  649. Pen pen(Color::Blue, 2);
  650. SolidBrush brush(Color::Black);
  651. Font myFont(L"宋体",12);
  652. //定义纹理画刷的不同填充区域
  653. RectF rect1(10, 10, 200,200);
  654. RectF rect2(210, 10, 200, 200);
  655. RectF rect3(410, 10, 200, 200);
  656. //装入纹理图片
  657. Bitmap image(L"nemo.bmp");
  658. //构造纹理画刷1:使用默认的方式
  659. TextureBrush tBrush(&image);
  660. //使用纹理画刷填充圆形区域
  661. graphics.FillEllipse(&tBrush,rect1);
  662. //绘制圆周
  663. graphics.DrawEllipse(&pen,rect1);
  664. MyDrawString(graphics, "图片原始大小", myFont, brush, PointF(40,220));
  665. //构造纹理画刷2:只使用给定图片的部分区域
  666. TextureBrush tBrush2(&image, Rect(55,35,55,35));
  667. graphics.FillEllipse(&tBrush2,rect2);
  668. graphics.DrawEllipse(&pen,rect2);
  669. MyDrawString(graphics, "使用部分截图", myFont, brush, PointF(240,220));
  670. //构造纹理画刷3:将使用图片的画刷进行缩放
  671. TextureBrush tBrush3(&image);
  672. //对画刷进行50%的缩放
  673. Matrix mtr(0.5f, 0.0f, 0.0f,0.5f, 0.0f, 0.0f);
  674. tBrush3.SetTransform(&mtr);
  675. graphics.FillEllipse(&tBrush3,rect3);
  676. graphics.DrawEllipse(&pen,rect3);
  677. MyDrawString(graphics, "比例缩小图片",myFont,brush, PointF(440,220));
  678. }
  679. //使用图片排列方式
  680. void Brush_Texture_WrapMode_Click()
  681. {
  682. Graphics &graphics=GetGraphics();
  683. graphics.Clear(Color::White);
  684. Pen pen(Color::Blue, 2);
  685. SolidBrush brush(Color::Black);
  686. Font myFont(L"Arial", 13);
  687. //装入纹理图片
  688. Bitmap image(L"nemo.bmp");
  689. //构造纹理画刷
  690. TextureBrush tBrush(&image);
  691. //将画刷进行缩放
  692. Matrix mtr(0.5f, 0.0f, 0.0f,0.5f, 0.0f, 0.0f);
  693. tBrush.SetTransform(&mtr);
  694. int i=0;
  695. //对图片不使用排列方式
  696. tBrush.SetWrapMode(WrapModeClamp);
  697. graphics.FillRectangle(&tBrush, Rect(i*150,0,150,150));
  698. graphics.DrawRectangle(&pen, Rect(i*150,0,150,150));
  699. MyDrawString(graphics, "Clamp", myFont,brush, PointF(0,155));
  700. i+=1;
  701. //对图片使用平铺排列方式
  702. tBrush.SetWrapMode(WrapModeTile);
  703. graphics.FillRectangle(&tBrush, Rect(i*150+10,0,150,150));
  704. graphics.DrawRectangle(&pen, Rect(i*150+10,0,150,150));
  705. MyDrawString(graphics, "Tile",myFont,brush, PointF(170,155) );
  706. //对图片使用水平翻转排列方式
  707. i+=1;
  708. tBrush.SetWrapMode(WrapModeTileFlipX);
  709. graphics.FillRectangle(&tBrush, Rect(i*150+20,0,150,150));
  710. graphics.DrawRectangle(&pen, Rect(i*150+20,0,150,150));
  711. MyDrawString(graphics, "TileFlipX",myFont,brush, PointF(320,155));
  712. //对图片使用垂直翻转排列方式
  713. tBrush.SetWrapMode(WrapModeTileFlipY);
  714. graphics.FillRectangle(&tBrush, Rect(0,180,150,150));
  715. graphics.DrawRectangle(&pen, Rect(0,180,150,150));
  716. MyDrawString(graphics, "TileFlipY",myFont,brush, PointF(0,335));
  717. //对图片使用水平、垂直同时翻转排列方式
  718. tBrush.SetWrapMode(WrapModeTileFlipXY);
  719. graphics.FillRectangle(&tBrush, Rect(160,180,150,150));
  720. graphics.DrawRectangle(&pen, Rect(160,180,150,150));
  721. MyDrawString(graphics, "TileFlipXY",myFont,brush, PointF(170,335));
  722. }
  723. //纹理画刷的变换
  724. void Brush_TextureTransform_Click()
  725. {
  726. Graphics &graphics=GetGraphics();
  727. graphics.Clear(Color::White);
  728. //为三种不同的变换方式的画刷定义填充区域
  729. RectF rect1= RectF (10, 10, 200,200);
  730. RectF rect2= RectF (210, 10, 200, 200);
  731. RectF rect3= RectF (410, 10, 200, 200);
  732. Pen pen(Color::Blue, 2);
  733. SolidBrush brush(Color::Black);
  734. Font myFont(L"宋体", 12);
  735. //装入纹理图片
  736. Bitmap image(L"nemo.bmp");
  737. //构造纹理画刷
  738. TextureBrush tBrush(&image);
  739. //将画刷旋转30度
  740. tBrush.RotateTransform(30);
  741. graphics.FillEllipse(&tBrush,rect1);
  742. graphics.DrawEllipse(&pen,rect1);
  743. MyDrawString(graphics, "旋转30度",myFont,brush, PointF(40,220));
  744. //重置变换矩阵:恢复到变化前的状态
  745. tBrush.ResetTransform();
  746. //将画刷在水平方向上扩大三倍
  747. tBrush.ScaleTransform(3, 1);
  748. graphics.FillEllipse(&tBrush, rect2);
  749. graphics.DrawEllipse(&pen,rect2);
  750. MyDrawString(graphics, "横向扩充三倍",myFont,brush, PointF(240,220));
  751. //平移变换
  752. tBrush.ResetTransform();
  753. //将画刷在水平方向上平移30个像素
  754. tBrush.TranslateTransform(30, 0, MatrixOrderAppend);
  755. graphics.FillEllipse(&tBrush, rect3);
  756. graphics.DrawEllipse(&pen,rect3);
  757. MyDrawString(graphics, "横向平移30个像素",myFont,brush, PointF(440,220));
  758. }
  759. //查询画刷的变换信息
  760. void Brush_GetTextureMatrix_Click()
  761. {
  762. Graphics &graphics=GetGraphics();
  763. graphics.Clear(Color::White);
  764. Matrix matrix;
  765. float elements[6];
  766. RectF rect1(10, 10, 200,200);
  767. Pen pen(Color::Black, 2);
  768. SolidBrush brush(Color::Black);
  769. Bitmap image(L"nemo.bmp");
  770. TextureBrush tBrush(&image);
  771. //进行三种任意变换
  772. tBrush.RotateTransform(30);
  773. tBrush.TranslateTransform(5,3);
  774. tBrush.ScaleTransform(0.5f,2.0f);
  775. //获取目前已经进行的画刷变换
  776. tBrush.SetTransform(&matrix);
  777. //从矩形到数组
  778. matrix.GetElements(elements);
  779. graphics.FillEllipse(&tBrush,rect1);
  780. graphics.DrawEllipse(&pen,rect1);
  781. //输出矩阵的元素
  782. for(int j = 0; j <6; ++j)
  783. {
  784. CString s; s.Format(_T("%d %f 矩阵元素值"), j, elements[j]);
  785. AfxMessageBox(s);
  786. }
  787. }
  788. //线性渐变画刷程序
  789. void Brush_LinearGradientBrush_Click()
  790. {
  791. Graphics &graphics=GetGraphics();
  792. graphics.Clear(Color::White);
  793. //定义一个由红色到蓝色渐变的画刷:水平变换区域的宽度为40
  794. //竖直方向不进行色彩渐变
  795. LinearGradientBrush linGrBrush(
  796. Point(0, 0),
  797. Point(40, 0),
  798. Color::Red, //起点色彩
  799. Color::Blue); //止点色彩
  800. //定义一个色彩呈对角线变换的区域,区域大小为40*40
  801. LinearGradientBrush linGrBrush2(
  802. Point(0, 0),
  803. Point(40, 40),
  804. Color::Red, //起点色彩
  805. Color::Blue); //止点色彩
  806. //分别演示不同的线性渐变画刷对目标区域的不同填充效果
  807. graphics.FillRectangle(&linGrBrush, 0, 0, 200, 200);
  808. graphics.FillRectangle(&linGrBrush2, 240, 0, 200, 200);
  809. Pen pen(Color::Gray,1);
  810. //在对角线方向上绘制单个画刷的填充区域
  811. for(int i=0;i<5;i++)
  812. {
  813. graphics.DrawRectangle(&pen, 240+i*40,i*40,40,40);
  814. }
  815. }
  816. //控制线性渐变画刷的填充方式
  817. void Brush_LinearArrange_Click()
  818. {
  819. Graphics &graphics=GetGraphics();
  820. graphics.Clear(Color::White);
  821. Pen pen(Color::Blue, 2);
  822. SolidBrush brush(Color::Black);
  823. Font myFont(L"Arial", 13);
  824. //定义一个色彩呈对角线变换的区域,区域大小为40*40
  825. LinearGradientBrush linGrBrush(
  826. Point(0, 0),
  827. Point(40, 40),
  828. Color::Red, //起点色彩
  829. Color::Blue); //止点色彩
  830. int i=0;
  831. //对渐变画刷使用平铺排列方式(默认方式)
  832. linGrBrush.SetWrapMode(WrapModeTile);
  833. //填充一个大小为160的正方形
  834. graphics.FillRectangle(&linGrBrush, Rect(i*160,0,160,160));
  835. graphics.DrawRectangle(&pen, Rect(i*160,0,160,160));
  836. MyDrawString(graphics, "Tile", myFont, brush, Point(20,165));
  837. //对渐变画刷使用水平翻转排列方式
  838. i+=1;
  839. linGrBrush.SetWrapMode(WrapModeTileFlipX);
  840. //重置绘图平面原点
  841. graphics.SetRenderingOrigin(160,0);
  842. graphics.FillRectangle(&linGrBrush, Rect(i*160,0,160,160));
  843. graphics.DrawRectangle(&pen, Rect(i*160,160,0,160));
  844. MyDrawString(graphics, "TileFlipX", myFont, brush, Point(170,165));
  845. //对渐变画刷使用垂直翻转排列方式
  846. linGrBrush.SetWrapMode(WrapModeTileFlipY);
  847. //重置绘图平面原点
  848. graphics.SetRenderingOrigin(0,200);
  849. graphics.FillRectangle(&linGrBrush, Rect(0,200,160,160));
  850. graphics.DrawRectangle(&pen, Rect(0,200,200,160));
  851. MyDrawString(graphics, "TileFlipY",myFont,brush, Point(0,375));
  852. //对渐变画刷使用水平、垂直同时翻转排列方式
  853. linGrBrush.SetWrapMode(WrapModeTileFlipXY);
  854. graphics.SetRenderingOrigin(160,200);
  855. graphics.FillRectangle(&linGrBrush, Rect(160,200,160,160));
  856. graphics.DrawRectangle(&pen, Rect(160,200,160,160));
  857. MyDrawString(graphics, "TileFlipXY",myFont,brush, Point(170,375));
  858. //标注渐变画刷渐变区域的大小:画格子
  859. Pen pen2(Color::Gray, 1);
  860. for(i=0;i<8;i++)
  861. graphics.DrawLine(&pen2,0,i*40,320,i*40);
  862. for(i=0;i<9;i++)
  863. graphics.DrawLine(&pen2,i*40,0,i*40,360);
  864. }
  865. //定义线性渐变画刷的渐变模式
  866. void Brush_LinearGradientMode_Click()
  867. {
  868. Graphics &graphics=GetGraphics();
  869. graphics.Clear(Color::White);
  870. Pen pen(Color::Blue, 2);
  871. SolidBrush brush(Color::Black);
  872. Font myFont(L"Arial", 12);
  873. //定义一个水平渐变画刷,大小40*20
  874. LinearGradientBrush linGrBrush1(
  875. Rect(0, 0, 40, 20),
  876. Color::Red,
  877. Color::Blue,LinearGradientModeHorizontal);
  878. //定义一个垂直渐变画刷
  879. LinearGradientBrush linGrBrush2(
  880. Rect(0, 0, 40, 20),
  881. Color::Red,
  882. Color::Blue,LinearGradientModeVertical);
  883. //从右上到左下的渐变画刷
  884. LinearGradientBrush linGrBrush3(
  885. Rect(0, 0, 40, 20),
  886. Color::Red,
  887. Color::Blue,LinearGradientModeForwardDiagonal);
  888. //从左上到左下的渐变画刷
  889. LinearGradientBrush linGrBrush4(
  890. Rect(0, 0, 40, 20),
  891. Color::Red,
  892. Color::Blue,LinearGradientModeBackwardDiagonal);
  893. int i=0;
  894. //使用水平渐变的画刷填充区域
  895. graphics.FillRectangle(&linGrBrush1, Rect(i*160,0,160,160));
  896. graphics.DrawRectangle(&pen, Rect(i*160,0,160,160));
  897. MyDrawString(graphics, "水平渐变",myFont,brush, PointF(20,165));
  898. i+=1;
  899. //重置绘图平面原点
  900. graphics.SetRenderingOrigin(160,0);
  901. //使用垂直渐变的画刷填充区域
  902. graphics.FillRectangle(&linGrBrush2, Rect(i*160,0,160,160));
  903. graphics.DrawRectangle(&pen, Rect(i*160,160,0,160));
  904. MyDrawString(graphics, "垂直渐变",myFont,brush, PointF(170,165));
  905. //重置绘图平面原点
  906. graphics.SetRenderingOrigin(0,200);
  907. //使用从右上到左下渐变的画刷填充区域
  908. graphics.FillRectangle(&linGrBrush3, Rect(0,200,160,160));
  909. graphics.DrawRectangle(&pen, Rect(0,200,200,160));
  910. MyDrawString(graphics, "左上->右下",myFont,brush, PointF(0,375));
  911. graphics.SetRenderingOrigin(160,200);
  912. graphics.FillRectangle(&linGrBrush4, Rect(160,200,160,160));
  913. //使用从左上到右下渐变的画刷填充区域
  914. graphics.DrawRectangle(&pen, Rect(160,200,160,160));
  915. MyDrawString(graphics, "右上->左下",myFont,brush, PointF(170,375));
  916. //在不同的区域中标注定义渐变方向的矩形
  917. Pen pen2(Color::Gray, 1);
  918. for(i=0;i<18;i++)
  919. graphics.DrawLine(&pen2,0,i*20,320,i*20);
  920. for(i=0;i<9;i++)
  921. graphics.DrawLine(&pen2,i* 40,0,i*40,360);
  922. }
  923. //渐变线偏转角度
  924. void Brush_LinearAngle_Click()
  925. {
  926. Graphics &graphics=GetGraphics();
  927. graphics.Clear(Color::White);
  928. Pen pen(Color::Blue, 2);
  929. SolidBrush brush(Color::Black);
  930. Font myFont(L"Arial", 12);
  931. //定义一个渐变线偏转角度为30度的渐变画刷,大小40*20
  932. LinearGradientBrush linGrBrush1(
  933. Rect(0, 0, 40, 20),
  934. Color::Red,
  935. Color::Blue,30.0f);
  936. //定义一个渐变线偏转角度为45度的渐变画刷
  937. LinearGradientBrush linGrBrush2(
  938. Rect(0, 0, 40, 20),
  939. Color::Red,
  940. Color::Blue,45.0f);
  941. //定义一个渐变线偏转角度为90度的渐变画刷
  942. LinearGradientBrush linGrBrush3(
  943. Rect(0, 0, 40, 20),
  944. Color::Red,
  945. Color::Blue,90.0f);
  946. //定义一个渐变线偏转角度为180度的渐变画刷
  947. LinearGradientBrush linGrBrush4(
  948. Rect(0, 0, 40, 20),
  949. Color::Red,
  950. Color::Blue,180.0f);
  951. int i=0;
  952. //使用偏转角度为30度的渐变画刷填充区域
  953. graphics.FillRectangle(&linGrBrush1, Rect(i*160,0,160,160));
  954. graphics.DrawRectangle(&pen, Rect(i*160,0,160,160));
  955. MyDrawString(graphics, "30度偏转",myFont,brush, PointF(20,165));
  956. i+=1;
  957. //重置绘图平面原点
  958. graphics.SetRenderingOrigin(160,0);
  959. //使用偏转角度为45度的渐变画刷填充区域
  960. graphics.FillRectangle(&linGrBrush2, Rect(i*160,0,160,160));
  961. graphics.DrawRectangle(&pen, Rect(i*160,160,0,160));
  962. MyDrawString(graphics, "45度偏转",myFont,brush, PointF(170,165));
  963. //重置绘图平面原点
  964. graphics.SetRenderingOrigin(0,200);
  965. //使用偏转角度为90度的渐变画刷填充区域
  966. graphics.FillRectangle(&linGrBrush3, Rect(0,200,160,160));
  967. graphics.DrawRectangle(&pen, Rect(0,200,200,160));
  968. MyDrawString(graphics, "90度偏转",myFont,brush, PointF(0,375));
  969. graphics.SetRenderingOrigin(160,200);
  970. graphics.FillRectangle(&linGrBrush4, Rect(160,200,160,160));
  971. //使用偏转角度为180度的渐变画刷填充区域
  972. graphics.DrawRectangle(&pen, Rect(160,200,160,160));
  973. MyDrawString(graphics, "180度偏转",myFont,brush, PointF(170,375));
  974. //在不同的区域中标注定义渐变方向的矩形
  975. Pen pen2(Color::Gray, 1);
  976. for(i=0;i<18;i++)
  977. graphics.DrawLine(&pen2,0,i*20,320,i*20);
  978. for(i=0;i<9;i++)
  979. graphics.DrawLine(&pen2,i* 40,0,i*40,360);
  980. }
  981. //多色渐变画刷
  982. void Brush_LinearInterpolation_Click()
  983. {
  984. Graphics &graphics=GetGraphics();
  985. graphics.Clear(Color::White);
  986. //定义三种参与渐变的色彩
  987. Color colors[] =
  988. {
  989. Color::Red, // 红色
  990. Color::Green,//过渡色为绿色
  991. Color::Blue // 蓝色
  992. };
  993. float positions[] =
  994. {
  995. 0.0f, // 由红色起
  996. 0.3f, // 绿色始于画刷长度的三分之一
  997. 1.0f // 到蓝色止
  998. };
  999. //构造一条从黑色到白色的渐变画刷
  1000. LinearGradientBrush linGrBrush(
  1001. Point(0, 0),
  1002. Point(180, 0),
  1003. Color::Black,Color::White);
  1004. //设置渐变画刷的多色渐变信息
  1005. //linGrBrush.InterpolationColors=clrBlend;
  1006. linGrBrush.SetInterpolationColors(colors, positions, 3);
  1007. //使用多色渐变画刷填充目标区域
  1008. graphics.FillRectangle(&linGrBrush, 0, 0, 180, 100);
  1009. //使用普通的方法实现多色渐变
  1010. //由红到绿,长度60
  1011. LinearGradientBrush linGrBrush1(
  1012. Point(0, 0),
  1013. Point(60, 0),
  1014. Color::Red,
  1015. Color::Green);
  1016. //由绿到蓝,长度120
  1017. LinearGradientBrush linGrBrush2(
  1018. Point(60, 0),
  1019. Point(181, 0),
  1020. Color::Green,
  1021. Color::Blue);
  1022. //分别使用两个画刷填充两个相邻区域,形成多色渐变
  1023. graphics.FillRectangle(&linGrBrush1, 0, 120, 60, 100);
  1024. graphics.FillRectangle(&linGrBrush2, 60, 120, 120, 100);
  1025. }
  1026. //自定义渐变过程:三角形
  1027. void Brush_LinearCustomize_Click()
  1028. {
  1029. Graphics &graphics=GetGraphics();
  1030. graphics.Clear(Color::White);
  1031. SolidBrush brush(Color::Black);
  1032. Font myFont(L"宋体", 12);
  1033. //定义一个双色渐变画刷
  1034. LinearGradientBrush linGrBrush(
  1035. Point(0, 0),
  1036. Point(250, 0),
  1037. Color::Red,
  1038. Color::Blue);
  1039. //绘制两条使用默认渐变方式填充的矩形,以用作比较
  1040. graphics.FillRectangle(&linGrBrush, 0, 0, 250, 15);
  1041. graphics.FillRectangle(&linGrBrush, 500, 0, 250, 15);
  1042. //依次改变合成点位置
  1043. int row=1;
  1044. for(float i=0.0f;i<1.0f;i+=0.1f)
  1045. {
  1046. linGrBrush.SetBlendTriangularShape(i);
  1047. graphics.FillRectangle(&linGrBrush, 0, row*15, 250, 15);
  1048. row++;
  1049. }
  1050. MyDrawString(graphics, "改变合成点位置",
  1051. myFont,brush, PointF(40,200));
  1052. //依次改变彩色合成因子
  1053. row=1;
  1054. for(i=0.0f;i<1.0f;i+=0.1f)
  1055. {
  1056. //将合成的相对位置设置在整个区域的50%处
  1057. linGrBrush.SetBlendTriangularShape (0.5f,i);
  1058. graphics.FillRectangle(&linGrBrush, 500, row*15, 250, 15);
  1059. row++;
  1060. }
  1061. MyDrawString(graphics, "改变色彩合成因子",
  1062. myFont,brush, PointF(540,200));
  1063. }
  1064. //基于钟形曲线的渐变画刷
  1065. void Brush_LinearGradientBrush_BellShape_Click()
  1066. {
  1067. Graphics &graphics=GetGraphics();
  1068. graphics.Clear(Color::White);
  1069. Rect myRect(10, 10, 150, 75);
  1070. //定义线性渐变画刷
  1071. LinearGradientBrush myLGBrush(
  1072. myRect,Color::Blue,Color::Red,0.0f,true);
  1073. //使用默认的线性渐变画刷填充椭圆
  1074. graphics.FillEllipse(&myLGBrush, myRect);
  1075. //将渐变过程设置成基于钟形曲线的渐变
  1076. myLGBrush.SetBlendBellShape(.5f, 1.0f);
  1077. //使用自定义渐变过程的画刷填充椭圆
  1078. graphics.TranslateTransform(160,0);
  1079. graphics.FillEllipse(&myLGBrush, myRect);
  1080. }
  1081. void Brush_PathGradientBrush_Star_Click()
  1082. {
  1083. Graphics &graphics=GetGraphics();
  1084. graphics.Clear(Color::White);
  1085. Pen pen(Color::Gray,1);
  1086. SolidBrush pthGrBrush(Color::Red);
  1087. SolidBrush blackbrush(Color::Blue);
  1088. graphics.TranslateTransform(20,20);
  1089. //构造五星的10个边的端点坐标
  1090. Point points[] =
  1091. {
  1092. Point(75, 0), Point(100, 50),
  1093. Point(150, 50), Point(112, 75),
  1094. Point(150, 150), Point(75, 100),
  1095. Point(0, 150), Point(37, 75),
  1096. Point(0, 50), Point(50, 50),
  1097. Point(75, 0)
  1098. };
  1099. // 创建路径
  1100. GraphicsPath path;
  1101. //在路径中加入直线
  1102. path.AddLines(points, sizeof(points)/sizeof(Point));
  1103. //填充路径
  1104. graphics.FillPath(&pthGrBrush, &path);
  1105. //绘制边界
  1106. graphics.DrawLines(&pen, points, sizeof(points)/sizeof(Point));
  1107. //绘制定义10个边的端点
  1108. for(int i=0;i<10;i++)
  1109. //每个圆点的直径为10
  1110. graphics.FillEllipse(&blackbrush, points[i].X-5,points[i].Y-5,10,10);
  1111. }
  1112. //使用路径渐变画刷绘制五星
  1113. void Brush_PathGradientBrush_Star2_Click()
  1114. {
  1115. Graphics &graphics=GetGraphics();
  1116. graphics.Clear(Color::White);
  1117. //构造五星的10个边的端点坐标
  1118. Point points[] =
  1119. {
  1120. Point(75, 0), Point(100, 50),
  1121. Point(150, 50), Point(112, 75),
  1122. Point(150, 150), Point(75, 100),
  1123. Point(0, 150), Point(37, 75),
  1124. Point(0, 50), Point(50, 50),
  1125. Point(75, 0)
  1126. };
  1127. // 创建路径
  1128. GraphicsPath path;
  1129. //在路径中添加直线
  1130. path.AddLines(points, sizeof(points)/sizeof(Point));
  1131. //创建路径渐变画刷
  1132. PathGradientBrush pthGrBrush(&path);
  1133. //设置中心点色彩(终点色)
  1134. pthGrBrush.SetCenterColor(Color::Red);
  1135. //设置每个端点的色彩(终点色)
  1136. Color colors[] =
  1137. {
  1138. Color::Black, Color::Green,
  1139. Color::Blue, Color::White,
  1140. Color::Black, Color::Green,
  1141. Color::Blue, Color::White,
  1142. Color::Black, Color::Green
  1143. };
  1144. //设置路径渐变画刷的边缘色
  1145. int clrCount=sizeof(colors)/sizeof(Color);
  1146. pthGrBrush.SetSurroundColors(colors, &clrCount);
  1147. // 填充目标路径
  1148. graphics.FillPath(&pthGrBrush, &path);
  1149. //将中心色及边界色都设置成随机色,查看渐变效果
  1150. for(int z=0;z<10;z++)
  1151. {
  1152. //在水平方向上平移绘图平面
  1153. graphics.TranslateTransform(200.0f, 0.0f);
  1154. //设置中心点色彩为随机色
  1155. pthGrBrush.SetCenterColor(_MakeA_RGB(rand_Next(255)%155,rand_Next(255)%255,rand_Next(255)%255));
  1156. //使用原有的边缘色
  1157. pthGrBrush.SetSurroundColors(colors, &clrCount);
  1158. graphics.FillPath(&pthGrBrush, &path);
  1159. }
  1160. }
  1161. //使用多个路径渐变画刷
  1162. void Brush_Using_MorePathGradientBrush_Click()
  1163. {
  1164. Graphics &graphics=GetGraphics();
  1165. graphics.Clear(Color::White);
  1166. //定义一个六边形,边长为50
  1167. float fHalf = 50 * (float)sin(120.0f/360.0f*PI);
  1168. PointF pts[] =
  1169. {
  1170. PointF( 50, 0),
  1171. PointF( 50 * 1.5f, 0),
  1172. PointF( 50, 0),
  1173. PointF( 50 / 2, -fHalf),
  1174. PointF(-50 / 2, -fHalf),
  1175. PointF(-50, 0),
  1176. PointF(-50 * 1.5f, 0),
  1177. PointF(-50, 0),
  1178. PointF(-50 / 2, fHalf),
  1179. PointF( 50 / 2, fHalf)
  1180. };
  1181. //构造六边形渐变画刷
  1182. PathGradientBrush pgbrush1(pts, 10);
  1183. //在水平和垂直方向上平移六边形的顶点
  1184. for (int i = 0; i <10; i++)
  1185. {
  1186. pts[i].X += 50*1.5f;
  1187. pts[i].Y += fHalf;
  1188. }
  1189. //根据改变坐标后的点重新生成路径渐变画刷
  1190. PathGradientBrush pgbrush2(pts, 10);
  1191. //设置路径渐变画刷的翻转方式为平铺
  1192. pgbrush1.SetWrapMode(WrapModeTile);
  1193. pgbrush2.SetWrapMode(WrapModeTile);
  1194. //分别设置两个画刷的中心点色彩为红、绿色
  1195. pgbrush1.SetCenterColor(Color::Red);
  1196. pgbrush2.SetCenterColor(Color::Green);
  1197. //填充当前窗口
  1198. graphics.FillRectangle(&pgbrush1, 0, 0,MyClient.Width,MyClient.Height);
  1199. //在上次未被填充的区域中再次填充当前窗口的空白部份
  1200. graphics.FillRectangle(&pgbrush2, 0, 0,MyClient.Width,MyClient.Height);
  1201. }
  1202. //路径渐变画刷的填充方式
  1203. void Brush_PathGradientBrush_WrapMode_Click()
  1204. {
  1205. Graphics &graphics=GetGraphics();
  1206. graphics.Clear(Color::White);
  1207. Pen pen(Color::Blue, 2);
  1208. SolidBrush brush(Color::Black);
  1209. Font myFont(L"Arial", 12);
  1210. //定义五星的边线坐标,为演示翻转效果,将五星的一角拉长
  1211. Point points[] =
  1212. {
  1213. Point(75, 0), Point(100, 50),
  1214. Point(150, 50), Point(112, 75),
  1215. Point(150, 150), Point(75, 100),
  1216. Point(0, 190), Point(37, 75),
  1217. Point(10, 50), Point(50, 50)
  1218. };
  1219. GraphicsPath path;
  1220. path.AddLines(points, sizeof(points)/sizeof(Point));
  1221. // 构造路径渐变画刷
  1222. PathGradientBrush pthGrBrush(&path);
  1223. //设置中心点色彩(终点色)
  1224. pthGrBrush.SetCenterColor(Color::Red);
  1225. //设置每个端点的色彩(终点色)
  1226. Color colors[]=
  1227. {
  1228. Color::Black, Color::Green,
  1229. Color::Blue, Color::White,
  1230. Color::Black, Color::Green,
  1231. Color::Blue, Color::White,
  1232. Color::Black, Color::Green
  1233. };
  1234. int clrCount=sizeof(colors)/sizeof(Color);
  1235. pthGrBrush.SetSurroundColors(colors, &clrCount);
  1236. //缩小画刷
  1237. pthGrBrush.ScaleTransform(0.2f,0.2f);
  1238. int i=0;
  1239. //对渐变画刷使用平铺排列方式(默认方式)
  1240. pthGrBrush.SetWrapMode(WrapModeTile);
  1241. graphics.FillRectangle(&pthGrBrush, Rect(i*120,0,120,120));
  1242. graphics.DrawRectangle(&pen, Rect(i*120,0,120,120));
  1243. MyDrawString(graphics, "Tile",myFont,brush, PointF(20,125));
  1244. i+=1;
  1245. pthGrBrush.SetWrapMode(WrapModeTileFlipX);
  1246. graphics.FillRectangle(&pthGrBrush, Rect(i*120,0,120,120));
  1247. graphics.DrawRectangle(&pen, Rect(i*120,0,120,120));
  1248. MyDrawString(graphics, "TileFlipX",myFont,brush, PointF(170,125));
  1249. //对渐变画刷使用垂直翻转排列方式
  1250. pthGrBrush.SetWrapMode(WrapModeTileFlipY);
  1251. graphics.FillRectangle(&pthGrBrush, Rect(0,200,120,120));
  1252. graphics.DrawRectangle(&pen, Rect(0,200,120,120));
  1253. MyDrawString(graphics, "TileFlipY",myFont,brush, PointF(0,325));
  1254. //对渐变画刷使用水平、垂直同时翻转排列方式
  1255. pthGrBrush.SetWrapMode(WrapModeTileFlipXY);
  1256. graphics.FillRectangle(&pthGrBrush, Rect(120,200,120,120));
  1257. graphics.DrawRectangle(&pen, Rect(120,200,120,120));
  1258. MyDrawString(graphics, "TileFlipXY",myFont,brush, PointF(170,325));
  1259. //输出路径的约束矩形、中心点信息
  1260. RectF rect;
  1261. //获取画刷的约束矩形对象
  1262. pthGrBrush.GetRectangle(&rect);
  1263. PointF CenterPoint;
  1264. //获取画刷的中心点信息
  1265. pthGrBrush.GetCenterPoint(&CenterPoint);
  1266. CString tmp;
  1267. //格式化字符串
  1268. tmp.Format("当前约束矩形的左上坐标为(%f,%f),宽度=%f 高度=%f\n当前渐变路径的中心点坐标为(%.2f,%.2f)",
  1269. rect.X,rect.Y,rect.Height,rect.Width,
  1270. CenterPoint.X,CenterPoint.Y);
  1271. //输出中心点及约束矩形对象的信息
  1272. MyDrawString(graphics, tmp,myFont,brush, PointF(0,395));
  1273. }
  1274. //更改路径渐变画刷的中心点
  1275. void Brush_PathGradientBrush_CenterPoint_Click()
  1276. {
  1277. Graphics &graphics=GetGraphics();
  1278. graphics.Clear(Color::White);
  1279. //构造一个圆形区域
  1280. GraphicsPath path;
  1281. path.AddEllipse(0, 0, 200, 200);
  1282. SolidBrush brush(_MakeA_RGB(155,Color::Red));
  1283. //构造一个圆形路径渐变画刷
  1284. PathGradientBrush pthGrBrush(&path);
  1285. //设置中心点色彩
  1286. pthGrBrush.SetCenterColor(_MakeA_RGB(155,Color::White));
  1287. Color colors[] ={_MakeA_RGB(55, Color::Blue)};
  1288. //设置边缘色
  1289. int clrCount=1;
  1290. pthGrBrush.SetSurroundColors(colors, &clrCount);
  1291. //填充区域,使用单色画刷
  1292. graphics.FillEllipse(&pthGrBrush, 0, 0, 200, 200);
  1293. //获取中心点色彩
  1294. PointF center(0,0);
  1295. pthGrBrush.GetCenterPoint(&center);
  1296. //更改画刷的中心点沿圆周的上半部分平移
  1297. for(int i=0;i<200;i++)
  1298. {
  1299. center.X=(REAL)i;
  1300. center.Y=10.f;
  1301. pthGrBrush.SetCenterPoint(center);
  1302. graphics.FillEllipse(&pthGrBrush, 0, 0, 200, 200);
  1303. //标记当前中心点
  1304. graphics.FillEllipse(&brush, center.X, center.Y, 2.f, 2.f);
  1305. }
  1306. //更改画刷的中心点沿圆周的下半部分平移
  1307. for(i=200;i>0;i--)
  1308. {
  1309. center.X=(REAL)i;
  1310. center.Y=190;
  1311. pthGrBrush.SetCenterPoint(center);
  1312. graphics.FillEllipse(&pthGrBrush, 0, 0, 200, 200);
  1313. //标记当前中心点
  1314. graphics.FillEllipse(&brush, center.X, center.Y, 2.f, 2.f);
  1315. }
  1316. }
  1317. //对路径渐变画刷使用多色渐变
  1318. void Brush_PathGradientBrush_InterpolationColors_Click()
  1319. {
  1320. Graphics &graphics=GetGraphics();
  1321. graphics.Clear(Color::White);
  1322. SolidBrush brush(Color::Red);
  1323. //设置三角形的三个点
  1324. Point points[]=
  1325. {
  1326. Point(100, 0),
  1327. Point(200, 200),
  1328. Point(0, 200)
  1329. };
  1330. //创建一个三角形渐变画刷
  1331. PathGradientBrush pthGrBrush(points, 3);
  1332. //-定义参与渐变的色彩
  1333. Color colors[]=
  1334. {
  1335. Color::Red, //红
  1336. Color::Green, //绿
  1337. Color::Blue // 蓝
  1338. };
  1339. //设置合成点的位置
  1340. float pos[] =
  1341. {
  1342. 0.0f, // 红色在区域边界为红色
  1343. 0.4f, //在距离中心40%的位置处使用绿色
  1344. 1.0f //中心点使用蓝色
  1345. };
  1346. //设置渐变的过渡色
  1347. pthGrBrush.SetInterpolationColors(colors, pos, 3);
  1348. //填充区域
  1349. graphics.FillRectangle(&pthGrBrush, 0, 0, 300, 300);
  1350. //标记中心点
  1351. PointF centerpoint(0,0);;
  1352. pthGrBrush.GetCenterPoint(&centerpoint);
  1353. graphics.FillEllipse(&brush, centerpoint.X-5.f, centerpoint.Y-5.f, 10.f, 10.f);
  1354. }
  1355. //更改路径渐变画刷的焦点缩放比例
  1356. void Brsuh_PathGradietBrush_Focus_Click()
  1357. {
  1358. Graphics &graphics=GetGraphics();
  1359. graphics.Clear(Color::White);
  1360. Pen pen(Color::Black,3);
  1361. SolidBrush brush(Color::Black);
  1362. Font myFont(L"Arial", 12);
  1363. //指定三角形三点坐标,创建三角形渐变画
  1364. Point points[]=
  1365. {
  1366. Point(100, 0),
  1367. Point(200, 200),
  1368. Point(0, 200)
  1369. };
  1370. PathGradientBrush pthGrBrush(points, 3);
  1371. //指明渐变色
  1372. Color colors[] = {Color::Red, Color::Blue};
  1373. //指明色彩合成位置
  1374. float relativePositions[]=
  1375. {
  1376. 0.0f, // 红色做边界
  1377. 1.0f // 蓝色为中心
  1378. };
  1379. //设置渐变色
  1380. pthGrBrush.SetInterpolationColors(colors, relativePositions, 2);
  1381. //使用默认缩放因子进行填充
  1382. graphics.FillRectangle(&pthGrBrush, 0, 0, 200, 200);
  1383. //获取默认缩放因子
  1384. PointF FocusScales(0,0);
  1385. pthGrBrush.GetFocusScales(&FocusScales.X, &FocusScales.Y);
  1386. //输出缩放信息
  1387. CString tmp;
  1388. tmp.Format("水平:x=%.2f\n垂直:y=%.2f",FocusScales.X,FocusScales.Y);
  1389. MyDrawString(graphics, tmp,myFont,brush, PointF(0,210));
  1390. //平移绘图平面
  1391. graphics.TranslateTransform(200,0);
  1392. //更改缩放因子
  1393. FocusScales.X=0.6f;
  1394. FocusScales.Y=0.6f;
  1395. pthGrBrush.SetFocusScales(FocusScales.X,FocusScales.Y);
  1396. graphics.FillRectangle(&pthGrBrush, 0, 0, 200, 200);
  1397. MyDrawString(graphics, "水平:x=0.6\n垂直:y=0.6", myFont,brush, PointF(0,210));
  1398. //水平缩放不等于垂直缩放时特例
  1399. graphics.TranslateTransform(200,0);
  1400. //更改缩放因子
  1401. FocusScales.X=0.1f;
  1402. FocusScales.Y=0.8f;
  1403. pthGrBrush.SetFocusScales(FocusScales.X,FocusScales.Y);
  1404. graphics.FillRectangle(&pthGrBrush,0, 0, 200, 200);
  1405. MyDrawString(graphics, "水平:x=0.1\n垂直:y=0.8", myFont,brush, PointF(0,210));
  1406. //水平缩放=垂直缩放=1.0
  1407. graphics.TranslateTransform(200,0);
  1408. FocusScales.X=1.0f;
  1409. FocusScales.Y=1.0f;
  1410. pthGrBrush.SetFocusScales(FocusScales.X,FocusScales.Y);
  1411. graphics.FillRectangle(&pthGrBrush, 0, 0, 200, 200);
  1412. MyDrawString(graphics, "水平:x=1.0\n垂直:y=1.0", myFont,brush, PointF(0,210));
  1413. //水平缩放不等于垂直缩放时特例:放大原有区域
  1414. graphics.TranslateTransform(200,0);
  1415. FocusScales.X=2.0f;
  1416. FocusScales.Y=1.5f;
  1417. pthGrBrush.SetFocusScales(FocusScales.X,FocusScales.Y);
  1418. graphics.FillRectangle(&pthGrBrush, 0, 0, 200, 200);
  1419. MyDrawString(graphics, "水平:x=2.0\n垂直:y=1.5", myFont,brush, PointF(0,210));
  1420. }
  1421. //路径渐变画刷的变换
  1422. void Brush_PathGradientBrush_Transform_Click()
  1423. {
  1424. Graphics &graphics=GetGraphics();
  1425. graphics.Clear(Color::White);
  1426. // 定义一个三角形路径渐变画刷
  1427. Point pts[]={Point(50, 0), Point(100, 100), Point(0, 100)};
  1428. PathGradientBrush pthGrBrush(pts, 3);
  1429. //对画刷使用垂直翻转方式
  1430. pthGrBrush.SetWrapMode(WrapModeTileFlipY);
  1431. //缩小画刷
  1432. pthGrBrush.ScaleTransform(0.5f, 0.5f);
  1433. //填充当前窗口
  1434. graphics.FillRectangle(&pthGrBrush, 0, 0, MyClient.Width,MyClient.Height);
  1435. //将画刷旋转90度
  1436. pthGrBrush.RotateTransform(90.0f, MatrixOrderAppend);
  1437. //再次填充当前窗口
  1438. graphics.FillRectangle(&pthGrBrush, 0, 0, MyClient.Width,MyClient.Height);
  1439. }
  1440. void Brsuh_LinearGradientBrush_UsingGamma_Click()
  1441. {
  1442. Graphics &graphics=GetGraphics();
  1443. graphics.Clear(Color::White);
  1444. LinearGradientBrush linGrBrush(
  1445. Point(0, 10),
  1446. Point(200, 10),
  1447. Color::Red,
  1448. Color::Blue);
  1449. graphics.FillRectangle(&linGrBrush, 0, 0, 200, 50);
  1450. linGrBrush.SetGammaCorrection(true);
  1451. graphics.FillRectangle(&linGrBrush, 0, 60, 200, 50);
  1452. }
  1453. //简单的使用字体示意
  1454. void Font_UsingFontInGDIPlus_Click()
  1455. {
  1456. Graphics &graphics=GetGraphics();
  1457. graphics.Clear(Color::White);
  1458. //新建一个字体对话框
  1459. CFontDialog dlg;
  1460. //允许对字体进行色彩设置
  1461. //dlg.ShowColor=true;
  1462. //调用字体选择对话框
  1463. if(dlg.DoModal()!=IDOK)
  1464. return;
  1465. //获取在字体对话框中的字体信息
  1466. LOGFONT lf; dlg.GetCurrentFont(&lf);
  1467. Font myFont(CWideCharString(dlg.GetFaceName()), lf.lfHeight);
  1468. //根据字体色彩创建画刷
  1469. SolidBrush brush(dlg.GetColor());
  1470. //设置文本输出格式:在当前窗口中居中显示
  1471. StringFormat fmt;
  1472. fmt.SetAlignment(StringAlignmentCenter);
  1473. fmt.SetLineAlignment(StringAlignmentCenter);
  1474. //输出文本
  1475. graphics.DrawString(L"输出文本", 4, &myFont, RectF(0,0,MyClient.Width,MyClient.Height),&fmt, &brush);
  1476. }
  1477. //枚举所有的字体系列
  1478. void Font_EnumAllFonts_Click()
  1479. {
  1480. Graphics &graphics=GetGraphics();
  1481. graphics.Clear(Color::White);
  1482. SolidBrush solidBrush(Color::Black);
  1483. FontFamily fontFamily(L"Arial");
  1484. Font font(&fontFamily, 8, FontStyleRegular, UnitPoint);
  1485. //设置文本输出格式
  1486. StringFormat fmt;
  1487. fmt.SetAlignment(StringAlignmentNear);
  1488. fmt.SetLineAlignment(StringAlignmentNear);
  1489. CString tmp="";
  1490. //获取所有已经安装的字体系列
  1491. InstalledFontCollection installedFontCollection;
  1492. int ffcount=installedFontCollection.GetFamilyCount();
  1493. FontFamily *fontfamily = ::new FontFamily[ffcount];
  1494. installedFontCollection.GetFamilies(ffcount, fontfamily, &ffcount);
  1495. int index=0;
  1496. //访问fontfamily数组的每一个成员
  1497. for( int i=0; i<ffcount; i++)
  1498. {
  1499. //获取当前字体系列名称
  1500. WCHAR name[LF_FACESIZE];
  1501. fontfamily[i].GetFamilyName(name);
  1502. CString t=name;
  1503. CString s; s.Format(_T("%s%s"), (i? _T(", "):_T("")), t);
  1504. tmp+=s;
  1505. index++;
  1506. }
  1507. ::delete []fontfamily;
  1508. //在窗口中输出所有的字体系列名
  1509. RectF r(0,10,MyClient.Width,MyClient.Height);
  1510. MyDrawString(graphics, tmp, font, solidBrush, r, fmt);
  1511. //输出字体系列信息
  1512. tmp.Format("在你的系统中已经安装的字体有%d种,其名称分别为:\n",index);
  1513. MyDrawString(graphics, tmp,font,solidBrush, Point(0,0));
  1514. }
  1515. //启用增强型选择对话框表单
  1516. void Font_EnhanceFontDialog_Click()
  1517. {
  1518. MyDrawString(GetGraphics(), "无增强型选择对话框表单",
  1519. Font(L"Arial",12), SolidBrush(Color::Red), Point(0,0));
  1520. /*
  1521. Form3 f= Form3();
  1522. f.ShowDialog();
  1523. */
  1524. }
  1525. //使用不同的字体边缘处理方式
  1526. void Font_UsingTextRenderHint_Click()
  1527. {
  1528. Graphics &graphics=GetGraphics();
  1529. graphics.Clear(Color::White);
  1530. SolidBrush brs(Color::Black);
  1531. FontFamily fontFamily(L"Arial");
  1532. Font font(&fontFamily, 60, FontStyleRegular, UnitPixel);
  1533. //使用不同的边缘处理方式输出六行文本
  1534. for (int i=TextRenderingHintSystemDefault; i<=TextRenderingHintClearTypeGridFit; i++)
  1535. {
  1536. //设置边缘处理方式
  1537. graphics.SetTextRenderingHint((TextRenderingHint)i);
  1538. //输出文本
  1539. MyDrawString(graphics, "Render",font,brs, PointF(0, 0));
  1540. //绘图平面下移地行
  1541. graphics.TranslateTransform(0, font.GetHeight(&graphics));
  1542. }
  1543. }
  1544. //使用私有字体集合
  1545. void Font_Privatefontcollection_Click()
  1546. {
  1547. Graphics &graphics=GetGraphics();
  1548. graphics.Clear(Color::White);
  1549. PointF pointF(0.0f, 10.0f);
  1550. SolidBrush solidBrush(Color::Red);
  1551. //创建私有字体集
  1552. PrivateFontCollection privateFontCollection;
  1553. //在私有字体集中追迷你繁启体字库文件"繁启体.TTF"
  1554. WCHAR fontfile[]= L"c:\\繁启体.TTF";
  1555. if (Ok != privateFontCollection.AddFontFile(fontfile))
  1556. {
  1557. AfxMessageBox("字体文件加载失败\n出错啦!");
  1558. return;
  1559. }
  1560. //从私有字体集合中构造繁启体,大小为35像素
  1561. FontFamily pFontFamily(L"迷你繁启体", &privateFontCollection);
  1562. Font tmpFont(&pFontFamily, 35);
  1563. //输出繁启体文字
  1564. MyDrawString(graphics, "沉舟侧畔千帆过", tmpFont,solidBrush, pointF);
  1565. //垂直坐标一移一行,行高为 字体的高度
  1566. pointF.Y +=tmpFont.GetHeight(&graphics);
  1567. MyDrawString(graphics, "病树前头万木春", tmpFont,solidBrush, pointF);
  1568. }
  1569. //在私有字体集合中使用多种字体
  1570. void Font_Privatefontcollection2_Click()
  1571. {
  1572. Graphics &graphics=GetGraphics();
  1573. graphics.Clear(Color::White);
  1574. PointF pointF(0.0f, 10.0f);
  1575. SolidBrush solidBrush(Color::Red);
  1576. //创建私有字体集
  1577. PrivateFontCollection privateFontCollection;
  1578. //在私有字体集中追加三个不同的字体文件
  1579. privateFontCollection.AddFontFile(L"C:\\WINDOWS\\Fonts\\STCAIYUN.TTF");
  1580. privateFontCollection.AddFontFile(L"C:\\WINDOWS\\Fonts\\SIMLI.TTF");
  1581. privateFontCollection.AddFontFile(L"C:\\WINDOWS\\Fonts\\Arial.ttf");
  1582. FontFamily fontfamily[200];
  1583. int ffcount=privateFontCollection.GetFamilyCount();
  1584. if (ffcount>200) ffcount=200;
  1585. privateFontCollection.GetFamilies(ffcount, fontfamily, &ffcount);
  1586. int index=0;
  1587. //访问私有字体集合中的所有字体系列
  1588. for(int i=0; i<ffcount; i++)
  1589. {
  1590. Font tmpFont(&fontfamily[i], 35);
  1591. //获取当前字体系列名称
  1592. WCHAR name[LF_FACESIZE];
  1593. fontfamily[i].GetFamilyName(name);
  1594. CString s; s.Format(_T("字体名: %s"), name);
  1595. //输出繁启体文字
  1596. MyDrawString(graphics, s, tmpFont, solidBrush, pointF);
  1597. graphics.TranslateTransform(0, tmpFont.GetHeight(&graphics));
  1598. }
  1599. /*
  1600. //通过访问PrivateFontCollection类的Families数组成员来字体
  1601. for(int index=0;index< privateFontCollection..Families.Length;index++)
  1602. {
  1603. Font tmpFont(privateFontCollection.Families[index].Name,35);
  1604. //输出繁启体文字
  1605. MyDrawString(graphics, "字体名: "+privateFontCollection.Families[index].Name,
  1606. tmpFont,solidBrush, pointF);
  1607. graphics.TranslateTransform(0,tmpFont.Height);
  1608. }
  1609. */
  1610. }
  1611. //检查字体风格是否可用
  1612. void Font_IsStyleAvailable_Click()
  1613. {
  1614. Graphics &graphics=GetGraphics();
  1615. graphics.Clear(Color::White);
  1616. Pen pen(Color::Gray);
  1617. PointF pointF(10.0f, 0.0f);
  1618. SolidBrush solidBrush(Color::Black);
  1619. //信息输出所使用的字体
  1620. Font msgfont(L"Arial",12);
  1621. //创建私有字体集
  1622. PrivateFontCollection privateFontCollection;
  1623. //在私有字体集中追加三个字体文件
  1624. //黑体
  1625. privateFontCollection.AddFontFile(L"C:\\windows\\Fonts\\simhei.ttf");
  1626. //Courier New字体
  1627. privateFontCollection.AddFontFile(L"C:\\windows\\Fonts\\CourBI.ttf");
  1628. //华文隶书
  1629. privateFontCollection.AddFontFile(L"C:\\windows\\Fonts\\STLITI.ttf");
  1630. CString familyName;
  1631. CString tmpmsg;
  1632. //查询私有字体集中字体系列数量
  1633. int count=privateFontCollection.GetFamilyCount();
  1634. //获取字体系列数组
  1635. FontFamily *pFontFamily = ::new FontFamily[count];
  1636. //将字体系列中的字体名中加入pFontFamily中
  1637. privateFontCollection.GetFamilies(count, pFontFamily, &count);
  1638. /*对私有字体集中每一个字体系列进行五种字体风格
  1639. 的查询,如果字体风格可用,使用该风格进行文本绘制
  1640. */
  1641. for(int j = 0; j< count; ++j)
  1642. {
  1643. //从字体系列中获取字体名
  1644. WCHAR name[LF_FACESIZE];
  1645. pFontFamily[j].GetFamilyName(name);
  1646. familyName=name;
  1647. // 常规风格是否可用
  1648. if(pFontFamily[j].IsStyleAvailable(FontStyleRegular))
  1649. {
  1650. tmpmsg=" 字体的常规风格可用";
  1651. //构造字体
  1652. FontFamily fm(CWideCharString(familyName), &privateFontCollection);
  1653. Font tmpFont(&fm, 12, FontStyleRegular, UnitPixel);
  1654. //输出使用常规风格的字体
  1655. MyDrawString(graphics, familyName+tmpmsg,tmpFont,solidBrush,pointF );
  1656. //垂直坐标一移一行,行高为 字体的高度
  1657. pointF.Y += tmpFont.GetHeight(&graphics);
  1658. }
  1659. else
  1660. {
  1661. MyDrawString(graphics, familyName+tmpmsg+
  1662. CString(L" 字体的常规风格不可用"),msgfont,solidBrush,pointF);
  1663. pointF.Y += msgfont.GetHeight(&graphics);
  1664. }
  1665. tmpmsg.Empty();
  1666. // 粗体风格是否可用
  1667. if(pFontFamily[j].IsStyleAvailable(FontStyleBold))
  1668. {
  1669. tmpmsg=" 字体的粗体风格可用";
  1670. //构造字体
  1671. FontFamily fm(CWideCharString(familyName), &privateFontCollection);
  1672. Font tmpFont(&fm, 12, FontStyleBold, UnitPixel);
  1673. //输出使用常规风格的字体
  1674. MyDrawString(graphics, familyName+tmpmsg,tmpFont,solidBrush,pointF );
  1675. //垂直坐标一移一行,行高为 字体的高度
  1676. pointF.Y += tmpFont.GetHeight(&graphics);
  1677. }
  1678. else
  1679. {
  1680. MyDrawString(graphics, familyName+tmpmsg+
  1681. CString(" 字体的粗体风格不可用"),msgfont,solidBrush,pointF);
  1682. pointF.Y += msgfont.GetHeight(&graphics);
  1683. }
  1684. tmpmsg.Empty();
  1685. // 斜体风格是否可用
  1686. if(pFontFamily[j].IsStyleAvailable(FontStyleItalic))
  1687. {
  1688. tmpmsg=" 字体的斜体风格可用";
  1689. //构造字体
  1690. FontFamily fm(CWideCharString(familyName),&privateFontCollection);
  1691. Font tmpFont(&fm, 12, FontStyleItalic, UnitPixel);
  1692. //输出使用常规风格的字体
  1693. MyDrawString(graphics, familyName+tmpmsg,tmpFont,solidBrush,pointF );
  1694. //垂直坐标一移一行,行高为 字体的高度
  1695. pointF.Y += tmpFont.GetHeight(&graphics);
  1696. }
  1697. else
  1698. {
  1699. MyDrawString(graphics, familyName+tmpmsg+
  1700. " 字体的斜体风格不可用",msgfont,solidBrush,pointF);
  1701. pointF.Y += msgfont.GetHeight(&graphics);
  1702. }
  1703. tmpmsg.Empty();
  1704. // 查询下划线风格是否可用
  1705. if(pFontFamily[j].IsStyleAvailable(FontStyleUnderline))
  1706. {
  1707. tmpmsg=" 字体的下划线风格可用";
  1708. //构造字体
  1709. FontFamily fm(CWideCharString(familyName),&privateFontCollection);
  1710. Font tmpFont(&fm, 12, FontStyleUnderline, UnitPixel);
  1711. //输出使用常规风格的字体
  1712. MyDrawString(graphics, familyName+tmpmsg,tmpFont,solidBrush,pointF );
  1713. //垂直坐标一移一行,行高为 字体的高度
  1714. pointF.Y += tmpFont.GetHeight(&graphics);
  1715. }
  1716. else
  1717. {
  1718. MyDrawString(graphics, familyName+tmpmsg+
  1719. " 字体的下划线风格不可用",msgfont,solidBrush,pointF);
  1720. pointF.Y += msgfont.GetHeight(&graphics);
  1721. }
  1722. tmpmsg.Empty();
  1723. // 查询强调线风格是否可用
  1724. if(pFontFamily[j].IsStyleAvailable(FontStyleStrikeout))
  1725. {
  1726. tmpmsg=" 字体的强调线风格可用";
  1727. //构造字体
  1728. FontFamily fm(CWideCharString(familyName),&privateFontCollection);
  1729. Font tmpFont(&fm, 12, FontStyleStrikeout, UnitPixel);
  1730. //输出使用常规风格的字体
  1731. MyDrawString(graphics, familyName+tmpmsg,tmpFont,solidBrush,pointF );
  1732. //垂直坐标一移一行,行高为 字体的高度
  1733. pointF.Y += tmpFont.GetHeight(&graphics);
  1734. }
  1735. else
  1736. {
  1737. MyDrawString(graphics, familyName+tmpmsg+
  1738. " 字体的强调线风格不可用",msgfont,solidBrush,pointF);
  1739. pointF.Y += msgfont.GetHeight(&graphics);
  1740. }
  1741. tmpmsg.Empty();
  1742. // 在不同的字体系列之间加上间隔线
  1743. graphics.DrawLine(&pen,0,(int )pointF.Y,400,(int)pointF.Y);
  1744. pointF.Y += 10.0f;
  1745. }
  1746. ::delete []pFontFamily;
  1747. }
  1748. //获取字体的大小
  1749. void Font_Size_Click()
  1750. {
  1751. Graphics &graphics=GetGraphics();
  1752. graphics.Clear(Color::White);
  1753. PointF pointF(0.0f, 0.0f);
  1754. SolidBrush solidBrush(Color::Black);
  1755. CString infoString;
  1756. int ascent;
  1757. float ascentPixel;
  1758. int descent;
  1759. float descentPixel;
  1760. int lineSpacing;
  1761. float lineSpacingPixel;
  1762. FontFamily fontFamily(L"Arial");
  1763. //创建一个大小为16像素的Aria字体
  1764. Font font(&fontFamily, 16, FontStyleRegular, UnitPixel);
  1765. Font font2(&fontFamily, 14, FontStyleRegular, UnitPixel);
  1766. // 显示字体大小
  1767. infoString.Format("font的Size属性值为%.2f",font.GetSize());
  1768. MyDrawString(graphics, infoString,font2,solidBrush,pointF);
  1769. // 下移一行
  1770. pointF.Y += font2.GetHeight(&graphics);
  1771. //显示字体高度:像素
  1772. infoString.Format("font的Height属性值为%.2f", font.GetHeight(&graphics));
  1773. MyDrawString(graphics, infoString,font2,solidBrush,pointF);
  1774. // 下移一行
  1775. pointF.Y += font2.GetHeight(&graphics);
  1776. // 显示字体系列的高度(设计单位)
  1777. infoString.Format("使用fontFamily.GetEmHeight函数返回的字体高度为 %d 个设计单位。"
  1778. ,fontFamily.GetEmHeight(FontStyleRegular));
  1779. MyDrawString(graphics, infoString,font2,solidBrush,pointF);
  1780. // 下移两行(Height属性的值是字体的高度,单位为像素)
  1781. pointF.Y += 2.0f*font2.GetHeight(&graphics);
  1782. // 获取字体的Ascent(上部距离)
  1783. ascent = fontFamily.GetCellAscent(FontStyleRegular);
  1784. //将上部距离的设计单位转换成像素单位
  1785. ascentPixel=
  1786. font.GetSize() * ascent / fontFamily.GetEmHeight(FontStyleRegular);
  1787. infoString.Format("上部距离为 %d个设计单位, %.2f个像素。",
  1788. ascent, ascentPixel);
  1789. MyDrawString(graphics, infoString,font2,solidBrush,pointF);
  1790. // 下移一行
  1791. pointF.Y += font2.GetHeight(&graphics);
  1792. // 获取字体的Descent(下部距离),设计单位
  1793. descent = fontFamily.GetCellDescent(FontStyleRegular);
  1794. //将下部距离的设计单位转换成像素单位
  1795. descentPixel =
  1796. font.GetSize() * descent / fontFamily.GetEmHeight(FontStyleRegular);
  1797. infoString.Format("下部距离为%d 个设计单位, %.2f个像素。",
  1798. descent, descentPixel);
  1799. MyDrawString(graphics, infoString,font2,solidBrush,pointF);
  1800. // 下移一行
  1801. pointF.Y += font2.GetHeight(&graphics);
  1802. //获取行距(设计单位)
  1803. lineSpacing = fontFamily.GetLineSpacing(FontStyleRegular);
  1804. //将行距的设计单位转换成像素单位
  1805. lineSpacingPixel =
  1806. font.GetSize() * lineSpacing / fontFamily.GetEmHeight(FontStyleRegular);
  1807. infoString.Format("行距为%d 个设计单位, %.2f 像素。",
  1808. lineSpacing, lineSpacingPixel);
  1809. MyDrawString(graphics, infoString,font2,solidBrush,pointF);
  1810. }
  1811. //设置文本输出基线
  1812. void Font_BaseLine_Click()
  1813. {
  1814. Graphics &graphics=GetGraphics();
  1815. graphics.Clear(Color::White);
  1816. FontFamily fontFamily(L"Arial");
  1817. //创建一个大小为60像素的Aria字体
  1818. Font font(&fontFamily, 60, FontStyleRegular, UnitPixel);
  1819. //获取当前窗口的矩形宽、高
  1820. int cy=MyClient.Height;
  1821. int cx=MyClient.Width;
  1822. Pen pen(Color::Blue, 1);
  1823. SolidBrush brush(Color::Blue);
  1824. //将窗口垂直方向的1/2部分作为基线
  1825. float yBaseline = cy / 2.f;
  1826. //绘制基线
  1827. graphics.DrawLine(&pen, PointF(0, yBaseline), PointF((REAL)cx,yBaseline));
  1828. //得到字体高度
  1829. float cyLineSpace = font.GetHeight(&graphics);
  1830. //获取行距
  1831. int iCellSpace = fontFamily.GetLineSpacing(FontStyleRegular);
  1832. //得到上半部分距离
  1833. int iCellAscent = fontFamily.GetCellAscent(FontStyleRegular);
  1834. //计算文本输出的起始位置相对于基线的偏移
  1835. float cyAscent = cyLineSpace * iCellAscent / iCellSpace;
  1836. //从中央向上绘制两条直线,间隔为cyAscent
  1837. graphics.DrawLine(&pen,
  1838. PointF(0, yBaseline-cyAscent), PointF(cx,yBaseline-cyAscent));
  1839. graphics.DrawLine(&pen,
  1840. PointF(0, yBaseline-2.0f*cyAscent), PointF(cx,yBaseline-2.0f*cyAscent));
  1841. //分别在两条基线上输出文本
  1842. MyDrawString(graphics, "AaFfgSs}", font,brush, PointF(0,yBaseline - cyAscent));
  1843. MyDrawString(graphics, "AaFfgSs}",font,brush, PointF(0,yBaseline - 2.0f*cyAscent));
  1844. }
  1845. void Font_DrawString_Click()
  1846. {
  1847. Graphics &graphics=GetGraphics();
  1848. graphics.Clear(Color::White);
  1849. //输出文本
  1850. CString msg="示例文本";
  1851. Font myFont(L"宋体", 16);
  1852. RectF layoutRect(50, 50, 200, 50);
  1853. //设置对齐方式
  1854. StringFormat format;
  1855. format.SetAlignment(StringAlignmentCenter);
  1856. SolidBrush blackBrush(Color::Black);
  1857. // 绘制文本
  1858. MyDrawString(graphics, msg, myFont, blackBrush, layoutRect, format);
  1859. // 绘制文本输出的矩形区域
  1860. Pen pen(Color::Black, 3);
  1861. graphics.DrawRectangle(&pen, layoutRect);
  1862. }
  1863. void Font_MeasureString_Click()
  1864. {
  1865. Graphics &graphics=GetGraphics();
  1866. graphics.Clear(Color::White);
  1867. //设置一个包含三行文本的字符串
  1868. CString txtOut="123456789\n";
  1869. txtOut+="ABCDEFGHIJKLM\n";
  1870. txtOut+="一二三四五六七八九";
  1871. FontFamily fontFamily(L"Arial");
  1872. //创建两个个大小不同的Aria字体
  1873. Font font(&fontFamily, 30, FontStyleRegular, UnitPixel);
  1874. Font font2(&fontFamily, 14, FontStyleRegular, UnitPixel);
  1875. SolidBrush brush(Color::Black);
  1876. StringFormat fmt;
  1877. fmt.SetAlignment(StringAlignmentCenter);
  1878. fmt.SetLineAlignment(StringAlignmentCenter);
  1879. SizeF stringSize, strSize(1000,1000);
  1880. //测量输出字符串所需要的矩形空间
  1881. CWideCharString txtOutW(txtOut);
  1882. graphics.MeasureString(txtOutW, txtOutW.GetLength(), &font, strSize, &fmt, &stringSize);
  1883. CString tmp;
  1884. tmp.Format("输出字符串所需要的宽度为:%2f 高度为%2f", stringSize.Width,stringSize.Height);
  1885. // 绘制输出文本的限制矩形
  1886. Pen pen(Color::Red);
  1887. graphics.DrawRectangle(&pen,
  1888. 10.0f,10.0f,stringSize.Width,stringSize.Height);
  1889. //输出字符串的测量信息
  1890. MyDrawString(graphics, txtOut, font, brush,
  1891. RectF(10.0f,10.0f,stringSize.Width,stringSize.Height),fmt);
  1892. graphics.TranslateTransform(0,10+stringSize.Height);
  1893. MyDrawString(graphics, tmp,font2,brush, PointF(0,0));
  1894. }
  1895. //计算在指定的区域中显示的字符总数及行数
  1896. void Font_MeasureString2_Click()
  1897. {
  1898. Graphics &graphics=GetGraphics();
  1899. graphics.Clear(Color::White);
  1900. CString txtOut="123456789";
  1901. txtOut+="ABCDEFGHIJKLM";
  1902. txtOut+="一二三四五六七八九";
  1903. FontFamily fontFamily(L"Arial");
  1904. //创建两个个大小不同的Aria字体
  1905. Font font(&fontFamily, 30, FontStyleRegular, UnitPixel);
  1906. Font font2(&fontFamily, 14, FontStyleRegular, UnitPixel);
  1907. //设置文本输出矩形
  1908. RectF layoutRect(10, 10,100,100);
  1909. SolidBrush brush(Color::Black);
  1910. //设置文本显示格式
  1911. StringFormat fmt;
  1912. fmt.SetAlignment(StringAlignmentCenter);
  1913. fmt.SetLineAlignment(StringAlignmentCenter);
  1914. int codepointsFitted=0;
  1915. int linesFilled=0;
  1916. SizeF stringSize;
  1917. //计算指定的区域能够显示的字符总数及行数
  1918. CWideCharString txtOutW(txtOut);
  1919. graphics.MeasureString(txtOutW, txtOutW.GetLength(),
  1920. &font, SizeF(layoutRect.Width,layoutRect.Height),
  1921. &fmt, &stringSize, &codepointsFitted, &linesFilled);
  1922. CString tmp;
  1923. tmp.Format("欲输出的字串共%d个字符\n其中,在指定的输出矩形中\n只输出了%d行共%d个字符",
  1924. txtOutW.GetLength(), linesFilled, codepointsFitted);
  1925. // 绘制指定的文本输出区域
  1926. Pen pen(Color::Blue,2);
  1927. graphics.DrawRectangle(&pen, layoutRect);
  1928. //在指定的区域矩形中显示文本
  1929. MyDrawString(graphics, txtOut, font, brush, layoutRect, fmt);
  1930. //输出字符串的测量信息
  1931. graphics.TranslateTransform(0,10+stringSize.Height);
  1932. MyDrawString(graphics, tmp, font2, brush, PointF(0,0));
  1933. }
  1934. //文本的分栏输出
  1935. void Font_ColumnTextOut_Click()
  1936. {
  1937. CString s_Filename;
  1938. //假设读取 <exePath> ../ReadMe.txt
  1939. TCHAR ModuleDirectory[_MAX_PATH];
  1940. ::GetModuleFileName(NULL, ModuleDirectory, _MAX_PATH);
  1941. TCHAR *p=::_tcsrchr(ModuleDirectory, '\\'); *p=0;
  1942. p=::_tcsrchr(ModuleDirectory, '\\'); *p=0;
  1943. ::_tcscat(ModuleDirectory, "\\ReadMe.txt");
  1944. s_Filename=ModuleDirectory;
  1945. FILE *stream = ::fopen(s_Filename, "rt");
  1946. if (!stream)
  1947. return;
  1948. CString str;
  1949. //读取所有的文件内容
  1950. char buffer[1024]="";
  1951. while (!feof(stream))
  1952. {
  1953. CString s;
  1954. ::fgets(buffer, 1024, stream);
  1955. s=buffer; s.TrimLeft(); s.TrimRight(); s+="\r\n";
  1956. str+=s;
  1957. }
  1958. //关闭文件
  1959. fclose(stream);
  1960. //定义栏宽及栏与栏之间的间隔
  1961. int COLUMWIDTH=256;
  1962. int SPACE=10;
  1963. Graphics &graphics=GetGraphics();
  1964. graphics.Clear(Color::White);
  1965. SolidBrush brush(Color::Black);
  1966. Font myFont(L"宋体", 10);
  1967. int codepointsFitted=0;
  1968. int linesFilled=0;
  1969. //设置输出格式
  1970. StringFormat format;
  1971. //禁用自动剪载
  1972. format.SetFormatFlags(StringFormatFlagsNoClip);
  1973. //输出文本以单词结束
  1974. format.SetTrimming(StringTrimmingWord);
  1975. CWideCharString strW(str);
  1976. //从左至右按列(栏)输出文本
  1977. for (int x = 0; strW.GetLength()> 0&&x<MyClient.Width; x +=(COLUMWIDTH+SPACE))
  1978. {
  1979. //设置文本对应的矩形区域(一栏)
  1980. RectF layoutRect(x, 0, COLUMWIDTH,MyClient.Height-myFont.GetHeight(&graphics));
  1981. SizeF stringSize;
  1982. //计算指定的区域能够显示的字符总数及行数
  1983. graphics.MeasureString(strW, strW.GetLength(),
  1984. &myFont, SizeF(layoutRect.Width,layoutRect.Height),
  1985. &format, &stringSize, &codepointsFitted, &linesFilled);
  1986. //显示一栏文本
  1987. graphics.DrawString(strW, strW.GetLength(), &myFont, layoutRect, &format, &brush);
  1988. //去掉已经输出的字符,以便下一栏能够正常显示
  1989. strW=strW.Mid(codepointsFitted);
  1990. }
  1991. }
  1992. //设置文本的去尾方式
  1993. void Font_StirngTrimming_Click()
  1994. {
  1995. Graphics &graphics=GetGraphics();
  1996. graphics.Clear(Color::White);
  1997. SolidBrush solidBrush(Color::Black);
  1998. FontFamily fontFamily(L"Times New Roman");
  1999. //用于文本输出的字体
  2000. Font font(&fontFamily, 25, FontStyleRegular, UnitPixel);
  2001. Pen pen(Color::Red);
  2002. StringFormat stringFormat;
  2003. //用于提示信息的字体及格式
  2004. Font font2(&fontFamily, 14, FontStyleRegular, UnitPixel);
  2005. //设置文本输出的格式
  2006. StringFormat msgFormat;
  2007. msgFormat.SetAlignment(StringAlignmentCenter);
  2008. msgFormat.SetLineAlignment(StringAlignmentCenter);
  2009. //提示信息输出区域
  2010. RectF outrect(30, 100, 160, font2.GetHeight(&graphics)*2);
  2011. //去尾方式:Character
  2012. stringFormat.SetTrimming(StringTrimmingCharacter);
  2013. //输出文本
  2014. CString text="One two three four five seven eight nine ten";
  2015. MyDrawString(graphics, text, font, solidBrush,
  2016. RectF(30, 30, 160, 60), stringFormat);
  2017. //绘制文本输出区域
  2018. graphics.DrawRectangle(&pen, 30, 30, 160, 60);
  2019. //输出提示信息
  2020. MyDrawString(graphics, "Character",font2,solidBrush,outrect,
  2021. msgFormat);
  2022. //绘图平面沿水平方向平移160个像素
  2023. graphics.TranslateTransform(160,0);
  2024. //更改去尾方式:Word
  2025. stringFormat.SetTrimming(StringTrimmingWord);
  2026. MyDrawString(graphics, text,font,solidBrush,Rect(30, 30, 160, 60), stringFormat);
  2027. graphics.DrawRectangle(&pen, 30, 30, 160, 60);
  2028. MyDrawString(graphics, "Word",font2,solidBrush,outrect, msgFormat);
  2029. //更改去尾方式:EllipsisCharacter
  2030. graphics.TranslateTransform(160,0);
  2031. stringFormat.SetTrimming(StringTrimmingEllipsisCharacter);
  2032. MyDrawString(graphics, text,font,solidBrush, Rect(30, 30, 160, 60), stringFormat);
  2033. graphics.DrawRectangle(&pen, 30, 30, 160, 60);
  2034. MyDrawString(graphics, "EllipsisCharacter",font2,solidBrush,outrect, msgFormat);
  2035. //更改去尾方式:EllipsisWord
  2036. graphics.TranslateTransform(160,0);
  2037. stringFormat.SetTrimming(StringTrimmingEllipsisWord);
  2038. MyDrawString(graphics, text,font,solidBrush, Rect(30, 30, 160, 60), stringFormat);
  2039. graphics.DrawRectangle(&pen, 30, 30, 160, 60);
  2040. MyDrawString(graphics, "EllipsisWord",font2,solidBrush,outrect, msgFormat);
  2041. //更改去尾方式:EllipsisPath
  2042. graphics.TranslateTransform(160,0);
  2043. stringFormat.SetTrimming(StringTrimmingEllipsisPath);
  2044. MyDrawString(graphics, text,font,solidBrush, Rect(30, 30, 160, 60), stringFormat);
  2045. graphics.DrawRectangle(&pen, 30, 30, 160, 60);
  2046. MyDrawString(graphics, "EllipsisPath",font2,solidBrush,outrect, msgFormat);
  2047. }
  2048. //文本输出的剪裁处理
  2049. void Font_TextOutClip_Click()
  2050. {
  2051. Graphics &graphics=GetGraphics();
  2052. graphics.Clear(Color::White);
  2053. SolidBrush solidBrush(Color::Black);
  2054. FontFamily fontFamily(L"宋体");
  2055. Font font(&fontFamily, 25, FontStyleRegular, UnitPixel);
  2056. Pen pen(Color::Red);
  2057. StringFormat stringFormat;
  2058. RectF outtext(10, 10, 120, 45);
  2059. //使用默认值输出文本
  2060. CString text="文本的剪裁示意";
  2061. MyDrawString(graphics, text,font,solidBrush,outtext,
  2062. stringFormat);
  2063. //绘制文本输出区域
  2064. graphics.DrawRectangle(&pen, outtext.X,
  2065. outtext.Y,outtext.Width,outtext.Height);
  2066. //在水平方向平移170个像素更
  2067. graphics.TranslateTransform(120,0);
  2068. RectF out2=outtext;
  2069. //将输出区域的高度调整为字体高度的整数倍
  2070. out2.Height=font.GetHeight(&graphics)*2;
  2071. MyDrawString(graphics, text,font,solidBrush,out2, stringFormat);
  2072. //绘制文本输出区域
  2073. graphics.DrawRectangle(&pen, out2.X,
  2074. out2.Y,out2.Width,out2.Height);
  2075. graphics.TranslateTransform(120,0);
  2076. //在输出文本时不使用剪截
  2077. stringFormat.SetFormatFlags(StringFormatFlagsNoClip);
  2078. MyDrawString(graphics, text,font,solidBrush,outtext, stringFormat);
  2079. //绘制文本输出区域
  2080. graphics.DrawRectangle(&pen, outtext.X,outtext.Y, outtext.Width,outtext.Height);
  2081. }
  2082. //测量文本局部输出区域
  2083. void Font_MeasureCharacterRanges_Click()
  2084. {
  2085. Graphics &graphics=GetGraphics();
  2086. graphics.Clear(Color::White);
  2087. // 设置绘制文本、填充区域的画刷
  2088. SolidBrush TextBrush(Color::Red);
  2089. SolidBrush RegionBrush(_MakeA_RGB(100, Color::Black));
  2090. Pen blackPen(Color::Black);
  2091. // 使用三个宽度不同的矩形区域绘制文本,示意不同局部区域测量结果
  2092. RectF layoutRect_A(20.0f, 20.0f, 120.0f, 180.0f);
  2093. RectF layoutRect_B(140.0f, 20.0f, 140.0f, 180.0f);
  2094. RectF layoutRect_C(280.0f, 20.0f, 180.0f, 180.0f);
  2095. //添加需要输出的字串
  2096. CString ts="秦时明月汉时关,";
  2097. ts+="万里长征人未还。";
  2098. ts+="但使龙城飞将在,";
  2099. ts+="不教胡马度阴山。";
  2100. // 在字串中需要局部测量的文本定义区域:每行诗的最后三个字
  2101. CharacterRange charRanges[] =
  2102. {
  2103. CharacterRange(4, 3),
  2104. CharacterRange(12, 3),
  2105. CharacterRange(20, 3),
  2106. CharacterRange(28, 3)
  2107. };
  2108. // 绘制文本时使用的字体及大小
  2109. FontFamily fontFamily(L"华文新魏");
  2110. Font myFont(&fontFamily, 25, FontStyleItalic, UnitPixel);
  2111. StringFormat strFormat;
  2112. //指向需要局部测量区域的数组
  2113. Region CharRangeRegions[4];
  2114. //在字体格式中设置需要局部测量的区域
  2115. strFormat.SetMeasurableCharacterRanges(4, charRanges);
  2116. //在矩形layoutRect_A中对文本进行局部测量,测量的结果保存在CharRangeRegions中
  2117. CWideCharString tsW(ts);
  2118. graphics.MeasureCharacterRanges(tsW,tsW.GetLength(),&myFont,layoutRect_A, &strFormat,4,CharRangeRegions);
  2119. //输出文本
  2120. MyDrawString(graphics, ts, myFont,TextBrush,layoutRect_A, strFormat);
  2121. //绘制文本输出边框
  2122. graphics.DrawRectangle(&blackPen, layoutRect_A.X,layoutRect_A.Y,layoutRect_A.Width,layoutRect_A.Height);
  2123. short i;
  2124. //填充由CharRangeRegions保存的区域
  2125. for ( i = 0; i < 4; i++)
  2126. {
  2127. graphics.FillRegion(&RegionBrush, CharRangeRegions+i);
  2128. }
  2129. //在矩形layoutRect_B中对文本进行局部测量,测量的结果保存在CharRangeRegions中
  2130. graphics.MeasureCharacterRanges(tsW, tsW.GetLength(), &myFont, layoutRect_B, &strFormat,4,CharRangeRegions);
  2131. //输出文本
  2132. MyDrawString(graphics, ts, myFont,TextBrush,layoutRect_B, strFormat );
  2133. graphics.DrawRectangle(&blackPen,
  2134. layoutRect_B.X,layoutRect_B.Y,layoutRect_B.Width,layoutRect_B.Height);
  2135. //填充区域
  2136. for ( i = 0; i <4; i++)
  2137. {
  2138. graphics.FillRegion(&RegionBrush, CharRangeRegions+i);
  2139. }
  2140. //在矩形layoutRect_c中对文本进行局部测量,测量的结果保存在CharRangeRegions中
  2141. graphics.MeasureCharacterRanges(tsW,tsW.GetLength(),&myFont,layoutRect_C, &strFormat,4,CharRangeRegions);
  2142. //输出文本
  2143. MyDrawString(graphics, ts, myFont,TextBrush,layoutRect_C, strFormat);
  2144. graphics.DrawRectangle(&blackPen,
  2145. layoutRect_C.X,layoutRect_C.Y,layoutRect_C.Width,layoutRect_C.Height);
  2146. //填充区域
  2147. for ( i = 0; i <4; i++)
  2148. {
  2149. graphics.FillRegion(&RegionBrush, CharRangeRegions+i);
  2150. }
  2151. }
  2152. //控件文本输出方向
  2153. void Font_TextoutDirection_Click()
  2154. {
  2155. Graphics &graphics=GetGraphics();
  2156. graphics.Clear(Color::White);
  2157. //添加需要输出的字串
  2158. CString ts="秦时明月汉时关,";
  2159. ts+="万里长征人未还。";
  2160. ts+="但使龙城飞将在,";
  2161. ts+="不教胡马度阴山。";
  2162. //设置输出字体
  2163. FontFamily fontFamily(L"幼圆");
  2164. Font myFont(&fontFamily, 20, FontStyleBold, UnitPixel);
  2165. SolidBrush brush(Color::Red);
  2166. Pen pen(Color::Black);
  2167. //设置输出区间
  2168. Rect f(10,10,120,myFont.GetHeight(&graphics)*8);
  2169. //构造StringFormat对象
  2170. StringFormat strFormat;
  2171. //从至左输出文本
  2172. int flag=StringFormatFlagsDirectionRightToLeft;
  2173. //文本在竖直方向上输出
  2174. flag|=StringFormatFlagsDirectionVertical;
  2175. //设置输出格式标志
  2176. strFormat.SetFormatFlags(flag);
  2177. //设置文本对齐方式
  2178. strFormat.SetAlignment(StringAlignmentCenter);
  2179. strFormat.SetLineAlignment(StringAlignmentCenter);
  2180. //绘制输出区间及文本
  2181. graphics.DrawRectangle(&pen,f);
  2182. MyDrawString(graphics, ts,myFont,brush,f,strFormat);
  2183. }
  2184. //设置文本对齐方式
  2185. void Font_TextAlignment_Click()
  2186. {
  2187. Graphics &graphics=GetGraphics();
  2188. graphics.Clear(Color::White);
  2189. //将当前窗口大小信息保存
  2190. Rect rect(0, 0,
  2191. MyClient.Width, MyClient.Height);
  2192. SolidBrush brush(Color::Black);
  2193. Pen pen(Color::Gray);
  2194. //以当前窗口中心点为原点绘制坐标轴
  2195. graphics.DrawLine(&pen,
  2196. PointF(rect.Width/2,0), PointF(rect.Width/2,rect.Height));
  2197. graphics.DrawLine(&pen,
  2198. PointF(0,rect.Height/2), PointF(rect.Width,rect.Height/2));
  2199. //将StringAlignment枚举的三种不同的对齐方式设置成数组成员,以便直接访问
  2200. StringAlignment Align[3]=
  2201. {
  2202. StringAlignmentNear,
  2203. StringAlignmentCenter,
  2204. StringAlignmentFar
  2205. };
  2206. CString strAlign[] ={ "近", "中", "远" };
  2207. //设置输出字体
  2208. FontFamily fontFamily(L"幼圆");
  2209. Font font(&fontFamily, 12, FontStyleRegular);
  2210. StringFormat strfmt;
  2211. //分别在垂直和水平方向上使用三种不同的对齐方式
  2212. for (int iVert = 0; iVert < 3; iVert++)
  2213. for (int iHorz = 0; iHorz < 3; iHorz++)
  2214. {
  2215. //设置垂直对齐方式
  2216. strfmt.SetLineAlignment((StringAlignment)Align[iVert]);
  2217. //设置水平对齐方式
  2218. strfmt.SetAlignment((StringAlignment)Align[iHorz]);
  2219. //将对齐信息输出
  2220. CString s;
  2221. s.Format("水平对齐:%d\n垂直对齐:%d", strAlign[iVert],strAlign[iHorz]);
  2222. //在当前区域中按照设置的对齐方式绘制文本
  2223. MyDrawString(graphics, s,font,brush,rect, strfmt);
  2224. }
  2225. }
  2226. void Font_TextAlignment2_Click()
  2227. {
  2228. Graphics &graphics=GetGraphics();
  2229. graphics.Clear(Color::White);
  2230. //将当前窗口大小信息保存
  2231. Rect rect(0, 0, MyClient.Width, MyClient.Height);
  2232. SolidBrush brush(Color::Black);
  2233. Pen pen(Color::Gray);
  2234. //以当前窗口中心点为原点绘制坐标轴
  2235. graphics.DrawLine(&pen,
  2236. PointF(rect.Width/2,0), PointF(rect.Width/2,rect.Height));
  2237. graphics.DrawLine(&pen,
  2238. PointF(0,rect.Height/2), PointF(rect.Width,rect.Height/2));
  2239. //将StringAlignment枚举的三种不同的对齐方式设置成数组成员,以便直接访问
  2240. StringAlignment Align[3]=
  2241. {
  2242. StringAlignmentNear,
  2243. StringAlignmentCenter,
  2244. StringAlignmentFar
  2245. };
  2246. CString strAlign[]={ "近", "中", "远" };
  2247. //设置输出字体
  2248. FontFamily fontFamily(L"幼圆");
  2249. Font font(&fontFamily, 12, FontStyleRegular);
  2250. StringFormat strfmt;
  2251. //分别在垂直和水平方向上使用三种不同的对齐方式
  2252. for (int iVert = 0; iVert < 3; iVert+=2)
  2253. for (int iHorz = 0; iHorz < 3; iHorz+=2)
  2254. {
  2255. //设置垂直对齐方式
  2256. strfmt.SetLineAlignment((StringAlignment)Align[iVert]);
  2257. //设置水平对齐方式
  2258. strfmt.SetAlignment((StringAlignment)Align[iHorz]);
  2259. //将对齐信息输出
  2260. CString s;
  2261. s.Format("水平对齐:%d\n垂直对齐:%d", strAlign[iVert],strAlign[iHorz]);
  2262. //在当前区域中按照设置的对齐方式绘制文本
  2263. MyDrawString(graphics, s,font,brush, PointF(MyClient.Width/2,MyClient.Height/2),strfmt);
  2264. }
  2265. }
  2266. //使用制表位
  2267. void Font_TextoutUsingTabs_Click()
  2268. {
  2269. Graphics &graphics=GetGraphics();
  2270. graphics.Clear(Color::White);
  2271. StringFormat stringFormat;
  2272. //输出文本使用的字体
  2273. FontFamily fontFamily(L"Arial");
  2274. Font font(&fontFamily, 30, FontStyleBold, UnitPixel);
  2275. SolidBrush solidBrush(Color::Black);
  2276. //不使用制表位信息的输出
  2277. CString text = "A\tB\tC\t\t\tD";
  2278. MyDrawString(graphics, text, font, solidBrush, PointF(0,10), stringFormat);
  2279. //设置制表位信息1
  2280. float tabs[]={40.0f, 30.0f,40};
  2281. stringFormat.SetTabStops(0.0f, sizeof(tabs)/sizeof(float), tabs);
  2282. //使用制表位信息的进行文本输出
  2283. graphics.TranslateTransform(0,30);
  2284. text="A\tB\tC\tD";
  2285. MyDrawString(graphics, text, font, solidBrush, PointF(0,10), stringFormat);
  2286. //设置制表位信息2
  2287. float tabs2[] = {60.0f, 60.0f, 60};
  2288. stringFormat.SetTabStops(0.0f, 3, tabs2);
  2289. graphics.TranslateTransform(0,30);
  2290. MyDrawString(graphics, text, font, solidBrush, PointF(0,10), stringFormat);
  2291. //获取制表信息
  2292. float firstTabOffset = 0;
  2293. float tabStops[3];
  2294. stringFormat.GetTabStops(3, &firstTabOffset, tabStops);
  2295. //清空制表位
  2296. for(int j = 0; j <3; ++j)
  2297. tabStops[j]=0;
  2298. stringFormat.SetTabStops(0.0f, 3, tabStops);
  2299. //在清空制表位信息后输出文本
  2300. graphics.TranslateTransform(0,30);
  2301. MyDrawString(graphics, text, font, solidBrush, PointF(0,10), stringFormat);
  2302. }
  2303. //定义商品信息结构
  2304. struct SListItem
  2305. {
  2306. public: CString name;//商品名
  2307. public: float price;//单价
  2308. public: float num;//数量
  2309. //初如化
  2310. public: SListItem(CString p1, float p2,float p3)
  2311. {
  2312. name = p1;
  2313. price=p2;
  2314. num = p3;
  2315. }
  2316. };
  2317. void Font_UsingTabs_Click()
  2318. {
  2319. //初始化商品信息
  2320. SListItem items[5]=
  2321. {
  2322. SListItem(L"MP3播放机",1228,10),
  2323. SListItem(L"笔记本电脑",17200,1),
  2324. SListItem(L"激光打印机",3200,5),
  2325. SListItem(L"喷墨打印机",620,1),
  2326. SListItem(L"数码相机",4800,5)
  2327. };
  2328. //定义表头
  2329. CString title;
  2330. title.Format("\n%s\t%s\t%s\t%s\n","商品名","单价","数量","总价");
  2331. //格式化每种商品对应的字符串
  2332. CString iteminfo[5];
  2333. for (int i=0;i<5;i++)
  2334. {
  2335. //计算总价
  2336. float total=items[i].price*items[i].num;
  2337. //定制输出格式
  2338. CString s;
  2339. s.Format("%d\t%.2f\t%.2f\t%.2f\n",
  2340. items[i].name,items[i].price,items[i].num,total);
  2341. iteminfo[i]=s;
  2342. }
  2343. //在表格每一行的内容合并
  2344. CString xxx;;
  2345. for (i=0;i<5;i++)
  2346. xxx+=iteminfo[i];
  2347. //表头+表体
  2348. title+=xxx;
  2349. //在当前窗口中输出表格
  2350. RectF rect(0.0f,0.0f,
  2351. MyClient.Width,MyClient.Height);
  2352. Graphics &graphics=GetGraphics();
  2353. graphics.Clear(Color::White);
  2354. SolidBrush solidBrush(Color::Black);
  2355. //定制输出字体
  2356. FontFamily fontFamily(L"宋体");
  2357. Font font(&fontFamily,
  2358. 16, FontStyleRegular, UnitPixel);
  2359. StringFormat stringFormat;
  2360. //设置制表位信息
  2361. float tabs[] = {120.0f, 120.0f,60.0f};
  2362. stringFormat.SetTabStops(0.0f, sizeof(tabs)/sizeof(float), tabs);
  2363. //输出表格
  2364. MyDrawString(graphics, title,font, solidBrush,rect, stringFormat);
  2365. }
  2366. void Font_TextoutHotkeyPrefix_Click()
  2367. {
  2368. Graphics &graphics=GetGraphics();
  2369. graphics.Clear(Color::White);
  2370. SolidBrush solidBrush(Color::Black);
  2371. FontFamily fontFamily(L"宋体");
  2372. Font font(&fontFamily, 20, FontStyleRegular, UnitPixel);
  2373. Pen pen(Color::Red);
  2374. StringFormat stringFormat;
  2375. //显示快捷键字符,不解释"&"号
  2376. stringFormat.SetHotkeyPrefix(HotkeyPrefixNone);
  2377. MyDrawString(graphics, "工具栏(&T )\n状态栏(&S )", font, solidBrush,
  2378. RectF(30, 30, 160, font.GetHeight(&graphics)*2),stringFormat);
  2379. //绘制边框
  2380. graphics.DrawRectangle(&pen, Rect(30, 30, 160,font.GetHeight(&graphics)*2));
  2381. //解释""号为下划线
  2382. stringFormat.SetHotkeyPrefix(HotkeyPrefixShow);
  2383. graphics.TranslateTransform(0,font.GetHeight(&graphics)*3);
  2384. MyDrawString(graphics, "工具栏(&T )\n状态栏(&S )", font, solidBrush,
  2385. RectF(30, 30, 160, font.GetHeight(&graphics)*2),stringFormat);
  2386. //绘制边框
  2387. graphics.DrawRectangle(&pen, Rect(30, 30, 160,font.GetHeight(&graphics)*2));
  2388. //隐藏""号
  2389. graphics.TranslateTransform(0,font.GetHeight(&graphics)*3);
  2390. stringFormat.SetHotkeyPrefix(HotkeyPrefixHide);
  2391. MyDrawString(graphics, "工具栏(&T )\n状态栏(&S )", font, solidBrush,
  2392. RectF(30, 30, 160, font.GetHeight(&graphics)*2),stringFormat);
  2393. graphics.DrawRectangle(&pen, Rect(30, 30, 160,font.GetHeight(&graphics)*2));
  2394. }
  2395. void Font_TextoutShadow_Click()
  2396. {
  2397. Graphics &graphics=GetGraphics();
  2398. graphics.Clear(Color::White);
  2399. //对文本使用去锯齿的边缘处理
  2400. graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
  2401. FontFamily fontFamily(L"华文楷体");
  2402. Font font(&fontFamily, 100, FontStyleBold, UnitPixel);
  2403. //文本输出框
  2404. RectF textout(font.GetHeight(&graphics),
  2405. MyClient.Height/2,MyClient.Width,MyClient.Height);
  2406. //在两个不同的位置绘制文本,形成阴影
  2407. //solidBrush的色彩透明度为100,暗黑
  2408. SolidBrush solidBrush(_MakeA_RGB(100,Color::Black));
  2409. SolidBrush redBrush(Color::Red);
  2410. MyDrawString(graphics, "阴影字",font, solidBrush,
  2411. PointF(27.0f, 27.0f));
  2412. MyDrawString(graphics, "阴影字",font, redBrush,
  2413. PointF(12.0f, 20.0f));
  2414. //另一种阴影字,阴影为线条
  2415. //构造影线画刷
  2416. HatchBrush brush_tmp(
  2417. HatchStyleDarkDownwardDiagonal,Color::Black, Color::White);
  2418. int reptime=40;
  2419. //先画背景
  2420. for(int i=0;i<reptime;i++)
  2421. MyDrawString(graphics, "阴影字",font,brush_tmp,
  2422. PointF(textout.X+i+2,textout.Y+i+2));
  2423. //再画前景
  2424. MyDrawString(graphics, "阴影字",font, redBrush, PointF(textout.X,textout.Y));
  2425. }
  2426. void Font_TextoutHashline_Click()
  2427. {
  2428. Graphics &graphics=GetGraphics();
  2429. graphics.Clear(Color::White);
  2430. //对文本使用去锯齿的边缘处理
  2431. graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
  2432. FontFamily fontFamily(L"华文楷体");
  2433. Font font(&fontFamily, 100, FontStyleBold, UnitPixel);
  2434. //文本输出框
  2435. RectF textout(font.GetHeight(&graphics),0,MyClient.Width,MyClient.Height);
  2436. //画刷样本显示框
  2437. RectF brushdemo(0,0,font.GetHeight(&graphics),font.GetHeight(&graphics));
  2438. //创建一个影线画刷
  2439. HatchBrush brush_tmp(HatchStyle75Percent,Color::Black, Color::White);
  2440. //设置文本输出格式:水平靠左、垂直居中
  2441. StringFormat format;
  2442. format.SetAlignment(StringAlignmentNear);
  2443. format.SetLineAlignment(StringAlignmentCenter);
  2444. //显示画刷样本
  2445. graphics.FillRectangle(&brush_tmp,brushdemo);
  2446. //使用画刷绘制文本
  2447. MyDrawString(graphics, "影线字",font,brush_tmp,textout,format);
  2448. }
  2449. void Font_TextoutTexture_Click()
  2450. {
  2451. Graphics &graphics=GetGraphics();
  2452. graphics.Clear(Color::White);
  2453. //对文本使用去锯齿的边缘处理
  2454. graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
  2455. FontFamily fontFamily(L"华文楷体");
  2456. Font font(&fontFamily, 70, FontStyleBold, UnitPixel);
  2457. //文本输出框
  2458. RectF textout(0,0,MyClient.Width,MyClient.Height);
  2459. //画刷样本显示框
  2460. RectF brushdemo(0,0,font.GetHeight(&graphics),font.GetHeight(&graphics));
  2461. //装入纹理图片
  2462. Bitmap image(L"TEXTURE3.bmp");
  2463. //构造纹理画刷
  2464. TextureBrush brush_tmp(&image);
  2465. //设置文本输出格式:水平、垂直居中
  2466. StringFormat format;
  2467. format.SetAlignment(StringAlignmentCenter);
  2468. format.SetLineAlignment(StringAlignmentCenter);
  2469. //显示画刷样本
  2470. graphics.FillRectangle(&brush_tmp,brushdemo);
  2471. //使用画刷绘制文本
  2472. MyDrawString(graphics, "纹理字",font,brush_tmp,textout,format);
  2473. }
  2474. void Font_TextoutGradient_Click()
  2475. {
  2476. Graphics &graphics=GetGraphics();
  2477. graphics.Clear(Color::White);
  2478. //对文本使用去锯齿的边缘处理
  2479. graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
  2480. FontFamily fontFamily(L"华文楷体");
  2481. Font font(&fontFamily, 70, FontStyleBold, UnitPixel);
  2482. RectF textout(0,0,MyClient.Width,MyClient.Height);
  2483. //画刷样本显示框
  2484. RectF brushdemo(0,0,font.GetHeight(&graphics),font.GetHeight(&graphics));
  2485. //定义一个多色渐变画刷
  2486. Color colors[] =
  2487. {
  2488. Color::Red, // 红色
  2489. Color::Green,//过度色为绿色
  2490. Color::Blue // 蓝色
  2491. };
  2492. float positions[] =
  2493. {
  2494. 0.0f, // 由红色起
  2495. 0.3f, // 绿色始于画刷长度的三分之一
  2496. 1.0f // 由蓝色止
  2497. };
  2498. //构造一条从黑色到白色的渐变画刷
  2499. LinearGradientBrush brush_tmp(
  2500. Point(0, 0),
  2501. Point(100, font.GetHeight(&graphics)),
  2502. Color::White, // 黑
  2503. Color::White); // 白
  2504. //设置渐变画刷的多色渐变信息
  2505. //ColorBlend clrBlend(3);
  2506. //clrBlend.Colors=colors;
  2507. //clrBlend.Positions=positions;
  2508. brush_tmp.SetInterpolationColors(colors, positions, 3);
  2509. //设置文本输出格式:水平靠左、垂直居中
  2510. StringFormat format;
  2511. format.SetAlignment(StringAlignmentNear);
  2512. format.SetLineAlignment(StringAlignmentCenter);
  2513. //显示画刷样本
  2514. graphics.FillRectangle(&brush_tmp,brushdemo);
  2515. //使用画刷绘制文本
  2516. MyDrawString(graphics, "渐变字",font,brush_tmp,textout,format);
  2517. }
  2518. //构造路径
  2519. void Path_Construct_Click()
  2520. {
  2521. Graphics &graphics=GetGraphics();
  2522. graphics.Clear(Color::White);
  2523. //定义构成路径的点坐标
  2524. PointF data[]=
  2525. {
  2526. PointF(40,140),
  2527. PointF(275,200),
  2528. PointF(105,225),
  2529. PointF(190,300),
  2530. PointF(50,350),
  2531. PointF(20,180)
  2532. };
  2533. //设置定义点的类型将每点都处理成直线的端点
  2534. BYTE typeline[]=
  2535. {
  2536. (BYTE)PathPointTypeLine,
  2537. (BYTE)PathPointTypeLine,
  2538. (BYTE)PathPointTypeLine,
  2539. (BYTE)PathPointTypeLine,
  2540. (BYTE)PathPointTypeLine,
  2541. (BYTE)PathPointTypeLine
  2542. };
  2543. //构造路径
  2544. GraphicsPath tmp1(data,typeline,6);
  2545. //填充路径
  2546. graphics.FillPath(&SolidBrush(Color::Red),&tmp1);
  2547. //绘制定义路径的端点
  2548. for(int i=0;i<6;i++)
  2549. graphics.FillEllipse(&SolidBrush(Color::Black),
  2550. RectF(data[i].X-5,data[i].Y-5,10,10));
  2551. //绘图平面右移
  2552. graphics.TranslateTransform(360,0);
  2553. //更改路径的端点类型
  2554. BYTE type[]=
  2555. {
  2556. (BYTE)PathPointTypeStart,
  2557. (BYTE)PathPointTypeBezier,
  2558. (BYTE)PathPointTypeBezier,
  2559. (BYTE)PathPointTypeBezier,
  2560. (BYTE)PathPointTypeLine,
  2561. (BYTE)PathPointTypeLine
  2562. };
  2563. //构造并填充路径
  2564. GraphicsPath tmp(data,type,6);
  2565. graphics.FillPath(&SolidBrush(Color::Red),&tmp);
  2566. //绘制定义路径的端点
  2567. for(i=0;i<6;i++)
  2568. graphics.FillEllipse(&SolidBrush(Color::Black),
  2569. RectF(data[i].X-5,data[i].Y-5,10,10));
  2570. }
  2571. void Path_AddLines_Click()
  2572. {
  2573. Graphics &graphics=GetGraphics();
  2574. graphics.Clear(Color::White);
  2575. Pen pen(Color::Blue,2);
  2576. //创建路径对象
  2577. GraphicsPath path;
  2578. Rect rect1(20, 20, 100, 200);
  2579. Rect rect2(40, 40, 100, 200);
  2580. //添加两段弧线
  2581. path.AddArc(rect1, 0.0f, 180.0f);
  2582. path.AddArc(rect2, 0.0f, 180.0f);
  2583. graphics.DrawPath(&pen, &path);
  2584. }
  2585. //封闭图形
  2586. void Path_CloseFigure_Click()
  2587. {
  2588. Graphics &graphics=GetGraphics();
  2589. graphics.Clear(Color::White);
  2590. Pen pen(Color::Blue,2);
  2591. //创建路径对象
  2592. GraphicsPath path;
  2593. Rect rect1(20, 20, 100, 200);
  2594. Rect rect2(40, 40, 100, 200);
  2595. GraphicsPath path2;
  2596. path2.AddArc(rect1, 0.0f, 180.0f);
  2597. //封闭弧线1
  2598. path2.CloseFigure();
  2599. path2.AddArc(rect2, 0.0f, 180.0f);
  2600. //封闭弧线2
  2601. path2.CloseFigure();
  2602. graphics.DrawPath(&pen, &path2);
  2603. }
  2604. //路径的填充
  2605. void Path_FillPath_Click()
  2606. {
  2607. Graphics &graphics=GetGraphics();
  2608. graphics.Clear(Color::White);
  2609. GraphicsPath path1;
  2610. GraphicsPath path;
  2611. Pen pen(_MakeA_RGB(128, Color::Blue), 5);
  2612. SolidBrush brush(Color::Red);
  2613. //添加三条直线:开放的图形
  2614. path1.AddLine(10,10,10,50);
  2615. path1.AddLine(10,50,50,50);
  2616. path1.AddLine(50,50,50,10);
  2617. path1.StartFigure();
  2618. //添加一个闭合的图形
  2619. RectF rect(110,10,40,40);
  2620. //开始第二个子路径的图形追加
  2621. path1.AddRectangle(rect);
  2622. //绘制、填充路径
  2623. graphics.DrawPath(&pen, &path1);
  2624. graphics.FillPath(&brush, &path1);
  2625. //添加开放的弧线
  2626. path.AddArc(0, 0, 150, 120, 30, 120);
  2627. //添加封闭的椭圆
  2628. path.AddEllipse(50, 50, 50, 100);
  2629. //使用默认的填充方式填充路径
  2630. graphics.FillPath(&brush, &path);
  2631. graphics.DrawPath(&pen, &path);
  2632. }
  2633. void Path_AddSubPath_Click()
  2634. {
  2635. Graphics &graphics=GetGraphics();
  2636. graphics.Clear(Color::White);
  2637. Pen pen(Color::Blue,2);
  2638. Rect rect(0, 0, 100, 50);
  2639. GraphicsPath path;
  2640. //在路径中按垂直方向添加五段弧线
  2641. path.AddArc(0, 0, 100, 50, 0.0f, 180.0f);
  2642. path.AddArc(0, 30, 100, 50, 0.0f, 180.0f);
  2643. path.AddArc(0, 60, 100, 50, 0.0f, 180.0f);
  2644. path.AddArc(0, 90, 100, 50, 0.0f, 180.0f);
  2645. path.AddArc(0, 120, 100, 50, 0.0f, 180.0f);
  2646. graphics.DrawPath(&pen, &path);
  2647. //获取路径所占区域
  2648. RectF rect2; path.GetBounds(&rect2);
  2649. //绘图平面右移
  2650. graphics.TranslateTransform(rect2.Width+10,0);
  2651. GraphicsPath path2;
  2652. //构造第一个子路径
  2653. path2.AddArc(0, 0, 100, 50, 0.0f, 180.0f);
  2654. path2.AddArc(0, 30, 100, 50, 0.0f, 180.0f);
  2655. path2.AddArc(0, 60, 100, 50, 0.0f, 180.0f);
  2656. //在不封闭当前子路径的情况下开始第二个子路径的绘制
  2657. path2.StartFigure();
  2658. //添加弧线
  2659. path2.AddArc(0, 90, 100, 50, 0.0f, 180.0f);
  2660. //在封闭当前子路径的情况下开始第三个子路径的绘制
  2661. path2.CloseFigure();
  2662. path2.AddArc(0, 120, 100, 50, 0.0f, 180.0f);
  2663. //绘制子路径
  2664. graphics.DrawPath(&pen, &path2);
  2665. }
  2666. void Path_GetSubPath_Click()
  2667. {
  2668. Graphics &graphics=GetGraphics();
  2669. graphics.Clear(Color::White);
  2670. Pen bluePen(Color::Blue,3);
  2671. GraphicsPath path;
  2672. // 字路径1
  2673. path.AddRectangle(Rect(20, 20, 60, 30));
  2674. // 字路径2
  2675. path.AddLine(100, 20, 160, 50);
  2676. path.AddArc(180, 20, 60, 30, 0.0f, 180.0f);
  2677. // 字路径3
  2678. path.AddRectangle(Rect(260, 20, 60, 30));
  2679. // 字路径4
  2680. path.AddLine(340, 20, 400, 50);
  2681. path.AddArc(340, 20, 60, 30, 0.0f, 180.0f);
  2682. path.CloseFigure();
  2683. // 字路径5
  2684. path.AddRectangle(Rect(420, 20, 60, 30));
  2685. //绘制路径
  2686. graphics.DrawPath(&bluePen, &path);
  2687. // 建立路径描述对象iterator
  2688. GraphicsPathIterator iterator(&path);
  2689. GraphicsPath subpath;
  2690. int isClosed=false;
  2691. int count=0;
  2692. //连续调用两次NextSubpath,获取子路径
  2693. //将路径的当前位置移动到1
  2694. count =iterator.NextSubpath(&subpath, &isClosed);
  2695. //将路径的当前位置移动到2
  2696. count = iterator.NextSubpath(&subpath, &isClosed);
  2697. //在第二行绘制提取的子路径
  2698. graphics.TranslateTransform(0,50);
  2699. graphics.DrawPath(&bluePen, &subpath);
  2700. //复位所有的位置移动信息
  2701. iterator.Rewind();
  2702. count = iterator.NextSubpath(&subpath, &isClosed);
  2703. //在第三行绘制新抽取的子路径
  2704. graphics.TranslateTransform(0,50);
  2705. Pen redPen(Color::Red,3);
  2706. graphics.DrawPath(&redPen, &subpath);
  2707. //连续调用两次NextSubpath,以获取子路径的定义点信息
  2708. int start=0;
  2709. int end=0;
  2710. CString msg;
  2711. //复位所有的位置移动信息
  2712. iterator.Rewind();
  2713. count = iterator.NextSubpath(&start, &end, &isClosed);
  2714. //保存位置索引信息
  2715. msg.Format(_T("第一次移动子路径的定义点起止位置为%d\t和\t%d\n"),start,end);
  2716. count = iterator.NextSubpath(&start, &end, &isClosed);
  2717. CString s;
  2718. s.Format(_T("第二次移动子路径的定义点起止位置为%d\t和\t%d\n"),start,end);
  2719. msg+=s;
  2720. //绘制子路径的定义点位置索引信息
  2721. FontFamily fontFamily(L"宋体");
  2722. Font font(&fontFamily,12);
  2723. graphics.TranslateTransform(0,60);
  2724. MyDrawString(graphics, msg,font,SolidBrush(Color::Black), PointF(0,0));
  2725. }
  2726. void Path_GetPoints_Click()
  2727. {
  2728. Graphics &graphics=GetGraphics();
  2729. graphics.Clear(Color::White);
  2730. //创建路径对象
  2731. GraphicsPath path;
  2732. //添加直线
  2733. path.AddLine(20, 100, 150, 120);
  2734. //添加矩形
  2735. path.AddRectangle(Rect(40, 30, 20, 60));
  2736. //添加椭圆
  2737. path.AddEllipse(Rect(70, 30, 100, 40));
  2738. //添加曲线 
  2739. Point points[] =
  2740. {
  2741. Point(300, 40),
  2742. Point(350, 60),
  2743. Point(300, 80),
  2744. Point(300, 100),
  2745. Point(350, 150)
  2746. };
  2747. path.AddCurve(points,5);
  2748. //绘制路径
  2749. Pen pen(Color::Blue);
  2750. graphics.DrawPath(&pen, &path);
  2751. // 获取定义路径的点总数
  2752. int count =path.GetPointCount();
  2753. //绘制点定义路径的点
  2754. SolidBrush brush(Color::Red);
  2755. PointF *points0 = new PointF[count];
  2756. path.GetPathPoints(points, count);
  2757. for(int j = 0; j < count; ++j)
  2758. {
  2759. graphics.FillEllipse(
  2760. &brush,
  2761. points0[j].X - 3.0f,
  2762. points0[j].Y - 3.0f,
  2763. 6.0f,
  2764. 6.0f);
  2765. }
  2766. delete points0;
  2767. /*另外一一种遍历数组的方式
  2768. foreach( PointF x in path.PathPoints)
  2769. graphics.FillEllipse(
  2770. brush,
  2771. x.X - 3.0f,
  2772. x.Y - 3.0f,
  2773. 6.0f,
  2774. 6.0f);
  2775. */
  2776. }
  2777. void Path_PathData_Click()
  2778. {
  2779. Graphics &graphics=GetGraphics();
  2780. graphics.Clear(Color::White);
  2781. //曲线的定义点
  2782. Point points[] =
  2783. {
  2784. Point(40, 60),
  2785. Point(50, 70),
  2786. Point(30, 90)
  2787. };
  2788. Pen pen(Color::Red, 2);
  2789. GraphicsPath path;
  2790. //弧线
  2791. path.AddArc(175, 50, 50, 50, 0.0f, -180.0f);
  2792. //直线
  2793. path.AddLine(100, 0, 250, 20);
  2794. path.StartFigure();
  2795. //直线
  2796. path.AddLine(50, 20, 5, 90);
  2797. //曲线
  2798. path.AddCurve(points, 3);
  2799. path.AddLine(50, 150, 150, 180);
  2800. path.CloseAllFigures();
  2801. //绘制路径
  2802. graphics.DrawPath(&pen, &path);
  2803. //获取PathData属性值
  2804. PathData pathData;
  2805. path.GetPathData(&pathData);
  2806. SolidBrush brush(Color::Red);
  2807. //绘制定义点
  2808. for (int i=0; i<pathData.Count; i++)
  2809. graphics.FillEllipse(&SolidBrush(Color::Black),
  2810. pathData.Points[i].X - 5.0f,pathData.Points[i].Y - 5.0f,10.0f,10.0f);
  2811. //输出每一个路径定义点的类型
  2812. FontFamily fontFamily(L"宋体");
  2813. Font font(&fontFamily,12);
  2814. RectF rect;
  2815. path.GetBounds(&rect);
  2816. graphics.TranslateTransform(rect.Width+10,0);
  2817. for (i=0; i<pathData.Count; i++)
  2818. {
  2819. CString msg;
  2820. msg.Format("第%d点的类型为:\t%d", i+1, pathData.Types[i]);
  2821. MyDrawString(graphics, msg,
  2822. font,SolidBrush(Color::Black), PointF(0,(i+1)*font.GetHeight(&graphics)));
  2823. }
  2824. }
  2825. void Path_ViewPointFLAG_Click()
  2826. {
  2827. Graphics &graphics=GetGraphics();
  2828. graphics.Clear(Color::White);
  2829. graphics.ScaleTransform(0.8f,0.8f);
  2830. SolidBrush brush(Color::Red);
  2831. //创建一个具有四个子路径的路径
  2832. GraphicsPath path;
  2833. path.AddRectangle(Rect(20, 20, 60, 30));
  2834. path.AddLine(100, 20, 160, 50);
  2835. path.AddArc(180, 20, 60, 30, 0, 180);
  2836. path.AddRectangle(Rect(260, 20, 60, 30));
  2837. path.AddLine(340, 20, 400, 50);
  2838. path.AddArc(340, 20, 60, 30, 0, 180);
  2839. path.CloseAllFigures();
  2840. //填充路径
  2841. graphics.FillPath(&brush, &path);
  2842. //将路径的标记信息传入GraphicsPathIterator对象
  2843. GraphicsPathIterator iterator(&path);
  2844. //获取标记点信息
  2845. int start=0;
  2846. int end=0;
  2847. int count=0;
  2848. //获取第一个标记区间
  2849. count = iterator.NextMarker(&start, &end);
  2850. //为点坐标、类型分配空间
  2851. PointF *points = new PointF[count];
  2852. BYTE *types = new BYTE[count];
  2853. //从iterator复制信息
  2854. iterator.CopyData(points, types, start, end);
  2855. //根据复制的信息新创建一个路径
  2856. GraphicsPath pathnew(points, types, count);
  2857. //在新位置绘制路径的定义点
  2858. graphics.TranslateTransform(0,60);
  2859. for(int j = 0; j < count; ++j)
  2860. graphics.FillEllipse(&SolidBrush(Color::Black),points[j].X - 2.0f,
  2861. points[j].Y - 2.0f, 4.0f,4.0f);
  2862. //填充新创建的路径
  2863. graphics.TranslateTransform(0,60);
  2864. graphics.FillPath(&brush, &pathnew);
  2865. graphics.TranslateTransform(0,60);
  2866. //输出所有的标记点信息
  2867. CString msg;
  2868. for(j = 0; j < count; ++j)
  2869. {
  2870. CString s;
  2871. s.Format("第%s的信息:X=%.2f\tY=%.2f\t类型=%d\n",
  2872. j,points[j].X,points[j].Y,types[j]);
  2873. msg+=s;
  2874. }
  2875. //绘制字符串
  2876. FontFamily fontFamily(L"宋体");
  2877. Font font(&fontFamily, 12, FontStyleRegular, UnitPixel);
  2878. MyDrawString(graphics, msg, font,SolidBrush(Color::Black), PointF(0,0));
  2879. delete types;
  2880. delete points;
  2881. }
  2882. void Path_SubPathRange_Click()
  2883. {
  2884. Graphics &graphics=GetGraphics();
  2885. graphics.Clear(Color::White);
  2886. SolidBrush brush(Color::Red);
  2887. //创建一个具有五个子路径的路径
  2888. GraphicsPath path;
  2889. // *************path.SetMarkers();**************
  2890. //添加第一个子路径
  2891. path.AddRectangle(Rect(20, 20, 60, 30));
  2892. //添加第二个子路径
  2893. path.AddLine(100, 20, 160, 50);
  2894. //第二个标记将第二个子路径从中央截断
  2895. path.SetMarker();
  2896. path.AddArc(180, 20, 60, 30, 0, 180);
  2897. //添加第三个子路径
  2898. path.AddRectangle(Rect(260, 20, 60, 30));
  2899. //第三个标记
  2900. path.SetMarker();
  2901. //添加第四个子路径
  2902. path.AddLine(340, 20, 400, 50);
  2903. path.AddArc(340, 20, 60, 30, 0, 180);
  2904. //填充路径
  2905. graphics.FillPath(&brush,&path);
  2906. //将路径的标记信息传入GraphicsPathIterator对象
  2907. GraphicsPathIterator iterator(&path);
  2908. int d=iterator.GetSubpathCount();
  2909. //获取标记点信息
  2910. int start;
  2911. int end;
  2912. int count;
  2913. //移到第二个标记
  2914. count = iterator.NextMarker(&start, &end);
  2915. //移到第三个标记
  2916. count = iterator.NextMarker(&start, &end);
  2917. //为点坐标、类型分配空间
  2918. PointF *points = new PointF[count];
  2919. BYTE *types = new BYTE[count];
  2920. //从iterator复制信息
  2921. iterator.CopyData(points, types, start, end);
  2922. //根据复制的信息新创建一个路径
  2923. GraphicsPath pathnew(points, types, count);
  2924. //在新位置绘制标记点
  2925. graphics.TranslateTransform(0,60);
  2926. for(int j = 0; j < count; ++j)
  2927. graphics.FillEllipse(&SolidBrush(Color::Black),
  2928. points[j].X - 2.0f,points[j].Y - 2.0f,
  2929. 4.0f,
  2930. 4.0f);
  2931. //填充新创建的路径
  2932. graphics.TranslateTransform(0,60);
  2933. graphics.FillPath(&brush,&pathnew);
  2934. //输出所有的标记点信息
  2935. CString msg;
  2936. for( j = 0; j < count; ++j)
  2937. {
  2938. CString s;
  2939. s.Format(_T("第%d点的信息:X=%.2f\tY=%.2f \t类型=%d\n"),
  2940. j,points[j].X,points[j].Y,types[j]);
  2941. msg+=s;
  2942. }
  2943. FontFamily fontFamily(L"宋体");
  2944. Font font(&fontFamily, 12, FontStyleRegular, UnitPixel);
  2945. //绘制字符串
  2946. graphics.TranslateTransform(0,60);
  2947. MyDrawString(graphics, msg, font,SolidBrush(Color::Black), PointF(0,0));
  2948. delete points;
  2949. delete types;
  2950. }
  2951. void Path_Flatten_Click()
  2952. {
  2953. Graphics &graphics=GetGraphics();
  2954. graphics.Clear(Color::White);
  2955. //曲线定义点
  2956. Point pts[] =
  2957. {
  2958. Point(20,50),
  2959. Point(60,70),
  2960. Point(80,10),
  2961. Point(120,50)
  2962. };
  2963. GraphicsPath path;
  2964. path.AddCurve(pts, 4);
  2965. path.AddEllipse(160, 10, 150, 80);
  2966. path.AddBezier(20, 100, 20, 150, 50, 80, 100, 60);
  2967. //按照默认的方式绘制路径
  2968. Pen pen(Color::Black,3);
  2969. graphics.DrawPath(&pen, &path);
  2970. //获取路径点信息
  2971. PathData pathData;
  2972. path.GetPathData(&pathData);
  2973. // 绘制路径定义点
  2974. SolidBrush brush(Color::Red);
  2975. for(int j = 0; j <pathData.Count; ++j)
  2976. {
  2977. graphics.FillEllipse(&brush, pathData.Points[j].X - 3.0f,
  2978. pathData.Points[j].Y - 3.0f,6.0f, 6.0f);
  2979. }
  2980. //绘图平面下移100个单位
  2981. graphics.TranslateTransform(0,120);
  2982. //进行曲线转
  2983. path.Flatten(&Matrix(), 14.0f);
  2984. pen.SetColor(Color::Green);
  2985. //绘制转换后的路径
  2986. graphics.DrawPath(&pen, &path);
  2987. }
  2988. void Path_Warp_Click()
  2989. {
  2990. Graphics &graphics=GetGraphics();
  2991. graphics.Clear(Color::White);
  2992. // 创建一个全部由线条组成的路径
  2993. PointF points[] =
  2994. {
  2995. PointF(20.0f, 60.0f),
  2996. PointF(30.0f, 90.0f),
  2997. PointF(15.0f, 110.0f),
  2998. PointF(15.0f, 145.0f),
  2999. PointF(55.0f, 145.0f),
  3000. PointF(55.0f, 110.0f),
  3001. PointF(40.0f, 90.0f),
  3002. PointF(50.0f, 60.0f)
  3003. };
  3004. GraphicsPath path;
  3005. path.AddLines(points, sizeof(points)/sizeof(PointF));
  3006. path.CloseFigure();
  3007. // 绘制扭曲前的路径
  3008. Pen bluePen(Color::Blue,3);
  3009. graphics.DrawPath(&bluePen, &path);
  3010. // 定义放置路径的源矩形
  3011. RectF srcRect(10.0f, 50.0f, 50.0f, 100.0f);
  3012. //定义矩形映射的目标点
  3013. PointF destPts[] =
  3014. {
  3015. PointF(220.0f, 10.0f),
  3016. PointF(280.0f, 10.0f),
  3017. PointF(100.0f, 150.0f),
  3018. PointF(300.0f, 150.0f)
  3019. };
  3020. //扭曲矩形
  3021. path.Warp(destPts, 4, srcRect);
  3022. // 绘制扭曲后的路径
  3023. graphics.DrawPath(&bluePen, &path);
  3024. // 绘制源矩形和目标的多边形
  3025. Pen blackPen(Color::Black,1);
  3026. graphics.DrawRectangle(&blackPen,
  3027. srcRect.X,srcRect.Y,srcRect.Width,srcRect.Height);
  3028. graphics.DrawLine(&blackPen, destPts[0], destPts[1]);
  3029. graphics.DrawLine(&blackPen, destPts[0], destPts[2]);
  3030. graphics.DrawLine(&blackPen, destPts[1], destPts[3]);
  3031. graphics.DrawLine(&blackPen, destPts[2], destPts[3]);
  3032. }
  3033. void Path_Transform_Click()
  3034. {
  3035. Graphics &graphics=GetGraphics();
  3036. graphics.Clear(Color::White);
  3037. GraphicsPath path;
  3038. //添加一个矩形子路径
  3039. path.AddRectangle(Rect(40, 10, 200, 50));
  3040. //绘制坐标变换前的路径
  3041. graphics.DrawPath(&Pen(Color::Blue,4), &path);
  3042. //旋转路径
  3043. Matrix matrix;
  3044. matrix.Rotate(15.0f);
  3045. path.Transform(&matrix);
  3046. //绘制进行坐标变换后的路径
  3047. graphics.DrawPath(&Pen(Color::Red,4), &path);
  3048. }
  3049. void Path_Widen_Click()
  3050. {
  3051. Graphics &graphics=GetGraphics();
  3052. graphics.Clear(Color::White);
  3053. Pen bluePen(Color::Blue,1);
  3054. Pen greenPen(Color::Green, 10);
  3055. //定义曲线
  3056. PointF points[] =
  3057. {
  3058. PointF(10.0f, 10.0f),
  3059. PointF(130.0f, 90.0f),
  3060. PointF(140.0f, 60.0f),
  3061. PointF(60.0f, 90.0f)
  3062. };
  3063. GraphicsPath path;
  3064. //添加四条曲线
  3065. path.AddClosedCurve(points, 4);
  3066. //复制三个相同的路径进行轮廓拓宽处理
  3067. GraphicsPath *path2=path.Clone();
  3068. GraphicsPath *path3=path.Clone();
  3069. GraphicsPath *path4=path.Clone();
  3070. //使用宽度为10的画笔绘制路径
  3071. graphics.DrawPath(&Pen(Color::Blue,10), &path);
  3072. RectF rect; path.GetBounds(&rect);
  3073. //使用绿色画笔拓宽路径轮廓
  3074. path2->Widen(&greenPen);
  3075. //绘图平面右移,绘制拓宽的路径
  3076. graphics.TranslateTransform(rect.Width+10,0);
  3077. graphics.DrawPath(&bluePen, path2);
  3078. //下移
  3079. graphics.TranslateTransform(-rect.Width,rect.Height);
  3080. //使用间断线风格的绿色画笔拓宽路径
  3081. greenPen.SetDashStyle(DashStyleDash);
  3082. path3->Widen(&greenPen);
  3083. //填充路径
  3084. graphics.FillPath(&SolidBrush(Color::Red), path3);
  3085. //对画笔进行缩放变换
  3086. Pen pentmp(Color::Blue);
  3087. pentmp.ScaleTransform(3.0f, 20.0f);
  3088. path4->Widen(&pentmp);
  3089. //绘图平面右移
  3090. graphics.TranslateTransform(rect.Width,0);
  3091. graphics.DrawPath(&bluePen, path4);
  3092. delete path2;
  3093. delete path3;
  3094. delete path4;
  3095. }
  3096. void Path_WidenDemo_Click()
  3097. {
  3098. Graphics &graphics=GetGraphics();
  3099. graphics.Clear(Color::White);
  3100. Pen bluePen(Color::Blue,1);
  3101. Pen greenPen(Color::Green, 20);
  3102. GraphicsPath path;
  3103. //添加一条直线
  3104. path.AddLine(10,20,100,20);
  3105. GraphicsPath *path2=path.Clone();
  3106. PathData pathData;
  3107. PathData pathData2;
  3108. path.GetPathData(&pathData);
  3109. int num1=path.GetPointCount();
  3110. //保存路径的定义点信息
  3111. CString msg;
  3112. for(int i=0;i<num1;i++)
  3113. {
  3114. CString s;
  3115. s.Format("点%d的坐标分别为:X=%.2f\tY=%.2f\n",
  3116. i,pathData.Points[i].X,pathData.Points[i].Y);
  3117. msg+=s;
  3118. }
  3119. //绘制路径
  3120. graphics.DrawPath(&Pen(Color::Blue), &path);
  3121. //使用绿色画笔拓宽路径轮廓
  3122. path2->Widen(&greenPen);
  3123. path2->GetPathData(&pathData2);
  3124. int num2=path2->GetPointCount();
  3125. //绘图平面右移,绘制拓宽的路径
  3126. RectF rect; path.GetBounds(&rect);
  3127. graphics.TranslateTransform(rect.Width*2,0);
  3128. graphics.DrawPath(&bluePen, path2);
  3129. graphics.ResetTransform();
  3130. //保存拓宽后路径的定义点信息
  3131. CString msg2;
  3132. for(i=0;i<num2;i++)
  3133. {
  3134. CString s;
  3135. s.Format("拓宽后的路径点%d的坐标分别为:X=%.2f\tY=%.2f\n",
  3136. i,pathData2.Points[i].X,pathData2.Points[i].Y);
  3137. msg2+=s;
  3138. }
  3139. //输出路径拓宽前后的定义点坐标位置信息
  3140. FontFamily fontFamily(L"宋体");
  3141. Font font(&fontFamily, 12, FontStyleRegular, UnitPixel);
  3142. graphics.TranslateTransform(0,120);
  3143. MyDrawString(graphics, msg,font,SolidBrush(Color::Black), PointF(0,0));
  3144. graphics.TranslateTransform(0,font.GetHeight(&graphics)*(num1+1));
  3145. MyDrawString(graphics, msg2,font,SolidBrush(Color::Black), PointF(0,0));
  3146. }
  3147. void Region_FromPath_Click()
  3148. {
  3149. Graphics &graphics=GetGraphics();
  3150. graphics.Clear(Color::White);
  3151. //设定文本输出质量
  3152. graphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit);
  3153. graphics.SetSmoothingMode(SmoothingModeAntiAlias);
  3154. FontFamily fontFamily(L"隶书");
  3155. //创建路径区域
  3156. GraphicsPath path;
  3157. //向区域中追加文本
  3158. path.AddString(L"文字区域",4,&fontFamily,
  3159. (int)FontStyleRegular,100.0f, Point(0, 0), &StringFormat() );
  3160. Pen pen(Color::Red);
  3161. //绘制路径
  3162. graphics.DrawPath(&pen, &path);
  3163. graphics.TranslateTransform(0,80);
  3164. //从路径中构造区域
  3165. Region region(&path);
  3166. //填充区域
  3167. graphics.FillRegion(&SolidBrush(Color::Red),&region);
  3168. }
  3169. void Region_Calculate_Click()
  3170. {
  3171. Graphics &graphics=GetGraphics();
  3172. graphics.Clear(Color::White);
  3173. FontFamily fontFamily(L"宋体");
  3174. Font font(&fontFamily, 16, FontStyleRegular, UnitPixel);
  3175. SolidBrush solidBrush2(Color::Red);
  3176. SolidBrush solidBrush(Color::Green);
  3177. Pen pen(Color::Cyan);
  3178. //定义多边形的端点坐标
  3179. Point points[]=
  3180. {
  3181. Point(75, 0),
  3182. Point(100, 50),
  3183. Point(150, 50),
  3184. Point(112, 75),
  3185. Point(150, 150),
  3186. Point(75, 120),
  3187. Point(10, 150),
  3188. Point(37, 75),
  3189. Point(0, 50),
  3190. Point(50, 50),
  3191. Point(75, 0)
  3192. };
  3193. //创建路径并添加线条
  3194. GraphicsPath path;
  3195. path.AddLines(points, sizeof(points)/sizeof(Point));
  3196. //缩小路径
  3197. Matrix matrix;
  3198. matrix.Scale(0.5f,0.5f);
  3199. path.Transform(&matrix);
  3200. //获取路径所占的矩形区域
  3201. RectF tmp; path.GetBounds(&tmp);
  3202. //定义区域2
  3203. RectF rect(0,10,tmp.Width,50);
  3204. //计算文本信息输出区域
  3205. RectF textout(0,tmp.Height,tmp.Width,font.GetHeight(&graphics)*2);
  3206. //设置文本输出格式:水平居中、垂直靠下
  3207. StringFormat format;
  3208. format.SetAlignment(StringAlignmentCenter);
  3209. format.SetLineAlignment(StringAlignmentFar);
  3210. //填充路径
  3211. graphics.FillPath(&solidBrush2,&path);
  3212. graphics.TranslateTransform(tmp.Width+10,0);
  3213. //填充矩形
  3214. graphics.FillRectangle(&solidBrush,rect);
  3215. //从路径中创建区域
  3216. Region region(&path);
  3217. //求交集
  3218. region.Intersect(rect);
  3219. graphics.TranslateTransform(tmp.Width+10,0);
  3220. //填充交集
  3221. graphics.FillRegion(&solidBrush2, &region);
  3222. MyDrawString(graphics, "交集",font,SolidBrush(Color::Black),textout,format);
  3223. //绘制路径所占的矩形区域
  3224. graphics.DrawRectangle(&pen,tmp.X,tmp.Y,tmp.Width,tmp.Height);
  3225. //求并集
  3226. Region region2(&path);
  3227. region2.Union(rect);
  3228. graphics.TranslateTransform(tmp.Width+10,0);
  3229. graphics.FillRegion(&solidBrush2, &region2);
  3230. MyDrawString(graphics, "并集",font,solidBrush,textout,format);
  3231. //绘制路径所占的矩形区域
  3232. graphics.DrawRectangle(&pen,tmp.X,tmp.Y,tmp.Width,tmp.Height);
  3233. //换行输出区域运算结果
  3234. graphics.TranslateTransform(-(tmp.Width+10)*3,tmp.Height+textout.Height);
  3235. //求异并集
  3236. Region region3(&path);
  3237. region3.Xor(rect);
  3238. //填充异并集
  3239. graphics.FillRegion(&solidBrush2, &region3);
  3240. MyDrawString(graphics, "异并集",font,SolidBrush(Color::Black),textout,format);
  3241. //绘制路径所占的矩形区域
  3242. graphics.DrawRectangle(&pen,tmp.X,tmp.Y,tmp.Width,tmp.Height);
  3243. //求补集
  3244. Region region4(&path);
  3245. region4.Complement(rect);
  3246. graphics.TranslateTransform(tmp.Width+10,0);
  3247. //填充补集
  3248. graphics.FillRegion(&solidBrush2, &region4);
  3249. MyDrawString(graphics, "补集",font,SolidBrush(Color::Black),textout,format);
  3250. //绘制路径所占的矩形区域
  3251. graphics.DrawRectangle(&pen,tmp.X,tmp.Y,tmp.Width,tmp.Height);
  3252. //求排斥集
  3253. Region region5(&path);
  3254. region5.Exclude(rect);
  3255. graphics.TranslateTransform(tmp.Width+10,0);
  3256. //填排斥集
  3257. graphics.FillRegion(&solidBrush2, &region5);
  3258. MyDrawString(graphics, "排斥集",font,SolidBrush(Color::Black),textout,format);
  3259. graphics.DrawRectangle(&pen,tmp.X,tmp.Y,tmp.Width,tmp.Height);
  3260. }
  3261. void Region_GetRects_Click()
  3262. {
  3263. Graphics &graphics=GetGraphics();
  3264. graphics.Clear(Color::White);
  3265. SolidBrush solidBrush(Color::Red);
  3266. Pen pen(Color::Black);
  3267. GraphicsPath path;
  3268. Matrix matrix;
  3269. //从路径中创建区域
  3270. path.AddEllipse(10, 0, 80, 120);
  3271. Region pathRegion(&path);
  3272. //填充区域
  3273. graphics.FillRegion(&solidBrush, &pathRegion);
  3274. //获取路径的矩形组成集信息(不使用变换)
  3275. int count = pathRegion.GetRegionScansCount(&matrix);
  3276. RectF *rects = new RectF[count];
  3277. pathRegion.GetRegionScans(&matrix, rects, &count);
  3278. //绘制平面右移,以绘制矩形集中所有的矩形
  3279. graphics.TranslateTransform(90,0);
  3280. for(int i=0; i<count; i++)
  3281. graphics.DrawRectangle(&pen, rects[i]);
  3282. //实施变换
  3283. matrix.Scale(1.0f,0.75f);
  3284. matrix.Shear(0.2f,0.3f);
  3285. //获取路径的矩形组成集信息(使用变换)
  3286. count = pathRegion.GetRegionScansCount(&matrix);
  3287. delete rects;
  3288. rects = new RectF[count];
  3289. pathRegion.GetRegionScans(&matrix, rects, &count);
  3290. //绘制平面右移,以绘制变换后的矩形集
  3291. graphics.TranslateTransform(90,0);
  3292. for(i=0; i<count; i++)
  3293. graphics.DrawRectangle(&pen, rects[i]);
  3294. delete rects;
  3295. }
  3296. void Transform_Click()
  3297. {
  3298. Graphics &graphics=GetGraphics();
  3299. graphics.Clear(Color::White);
  3300. SolidBrush mySolidBrush1(Color::Blue);
  3301. SolidBrush mySolidBrush2(_MakeA_RGB(155,Color::Chocolate));
  3302. GraphicsPath myGraphicsPath;
  3303. Rect myRect(60, 60, 60, 60);
  3304. //在路径中添加矩形
  3305. myGraphicsPath.AddRectangle(myRect);
  3306. //填充路径
  3307. graphics.FillPath(&mySolidBrush1, &myGraphicsPath);
  3308. // 进行坐标变换
  3309. Matrix myPathMatrix;
  3310. //水平方向扩大两倍,垂直方向保持不变
  3311. myPathMatrix.Scale(2, 1);
  3312. myPathMatrix.Rotate(30.0f);
  3313. //对路径实话坐标变换
  3314. myGraphicsPath.Transform(&myPathMatrix);
  3315. //填充变换后的路径
  3316. graphics.FillPath(&mySolidBrush2, &myGraphicsPath);
  3317. }
  3318. void TranslateTransform_Click()
  3319. {
  3320. Graphics &graphics=GetGraphics();
  3321. graphics.Clear(Color::White);
  3322. //装入图片
  3323. Bitmap image(L"nemo.bmp");
  3324. //定义图片的显示区域
  3325. Rect rect(0,0,110,70);
  3326. for(int i=0;i<10;i++)
  3327. {
  3328. //显示图片
  3329. graphics.DrawImage(&image,rect);
  3330. //绘图平面右移,每次沿水平方面右移110个像素
  3331. graphics.TranslateTransform(110,0);
  3332. }
  3333. }
  3334. void RotateTransform_Click()
  3335. {
  3336. Graphics &graphics=GetGraphics();
  3337. graphics.Clear(Color::White);
  3338. //装入图片
  3339. Bitmap image(L"nemo.bmp");
  3340. //获取当前窗口的中心点
  3341. Rect rect(0,0,MyClient.Width,MyClient.Height);
  3342. PointF center(rect.Width/2,rect.Height/2);
  3343. float offsetX=0;
  3344. float offsetY=0;
  3345. offsetX=center.X-image.GetWidth()/2;
  3346. offsetY=center.Y-image.GetHeight()/2;
  3347. //构造图片显示区域:让图片的中心点与窗口的中心点一致
  3348. RectF picRect(offsetX,offsetY,image.GetWidth(),image.GetHeight());
  3349. PointF Pcenter(picRect.X+picRect.Width/2,
  3350. picRect.Y+picRect.Height/2);
  3351. //让图片绕中心旋转一周
  3352. for(int i=0;i<361;i+=10)
  3353. {
  3354. // 绘图平面以图片的中心点旋转
  3355. graphics.TranslateTransform(Pcenter.X, Pcenter.Y);
  3356. graphics.RotateTransform(i);
  3357. //恢复绘图平面在水平和垂直方向的平移
  3358. graphics.TranslateTransform(-Pcenter.X, -Pcenter.Y);
  3359. //绘制图片并延时
  3360. graphics.DrawImage(&image,picRect);
  3361. ::Sleep(100);
  3362. //重置绘图平面的所有变换
  3363. graphics.ResetTransform();
  3364. }
  3365. }
  3366. void DrawWatch_Click()
  3367. {
  3368. Graphics &graphics=GetGraphics();
  3369. graphics.Clear(Color::White);
  3370. Rect clientrect(0,0,
  3371. MyClient.Width,MyClient.Height);
  3372. //设置路码表的高和宽
  3373. float WIDTH=clientrect.Width/2;
  3374. float HEIGHT =clientrect.Height/2;
  3375. //设置文本输出外观
  3376. graphics.SetSmoothingMode(SmoothingModeAntiAlias);
  3377. FontFamily fontFamily(L"Times New Roman");
  3378. Font font(&fontFamily, 14, FontStyleBold, UnitPixel);
  3379. //将绘图平面的坐标原点移到窗口中心
  3380. graphics.TranslateTransform(WIDTH /2,HEIGHT/2);
  3381. //绘制仪表盘
  3382. graphics.FillEllipse(&SolidBrush(Color::Black),
  3383. HEIGHT/-2,HEIGHT/-2,HEIGHT, HEIGHT);
  3384. //输出文本
  3385. MyDrawString(graphics, "公里/小时",font,SolidBrush(Color::Green),
  3386. PointF(-26,HEIGHT/-4-font.GetHeight(&graphics)));
  3387. //绘制刻度标记
  3388. graphics.RotateTransform(225);
  3389. for(int x = 0; x<55;x++)
  3390. {
  3391. graphics.FillRectangle(&SolidBrush(Color::Green),
  3392. Rect(-2, -(HEIGHT/2-2), 3, 15));
  3393. graphics.RotateTransform(5);
  3394. }
  3395. //重置绘图平面的坐标变换
  3396. graphics.ResetTransform();
  3397. graphics.TranslateTransform(WIDTH /2,HEIGHT/2);
  3398. graphics.RotateTransform(225);
  3399. int sp = 0;
  3400. CString tmp;
  3401. //绘制刻度值(整数类)
  3402. for(x = 0; x<7;x++)
  3403. {
  3404. tmp.Format("%d",sp);
  3405. //绘制红色刻度
  3406. graphics.FillRectangle(&SolidBrush(Color::Red),Rect(-3,(HEIGHT/2 -2)* -1,6,25));
  3407. //绘制数值
  3408. MyDrawString(graphics, tmp,font,SolidBrush(Color::Green), PointF(tmp.GetLength()*(-6),
  3409. (HEIGHT/-2) + 25));
  3410. //旋转45度
  3411. graphics.RotateTransform(45);
  3412. sp += 20;
  3413. }
  3414. //重置绘图平面的坐标变换
  3415. graphics.ResetTransform();
  3416. graphics.TranslateTransform(WIDTH /2,HEIGHT/2);
  3417. //绘制指针在30公里/秒的情形
  3418. float angle= 30*2.25f+ 225;
  3419. graphics.RotateTransform((float)angle);
  3420. Pen pen(Color::Blue,14);
  3421. //设置线帽
  3422. pen.SetEndCap(LineCapArrowAnchor);
  3423. pen.SetStartCap(LineCapRoundAnchor);
  3424. //画指针
  3425. graphics.DrawLine(&pen, PointF(0,0), PointF(0,(-1)*(HEIGHT/2.75F)));
  3426. graphics.ResetTransform();
  3427. graphics.TranslateTransform(WIDTH /2,HEIGHT/2);
  3428. //绘制中心点
  3429. graphics.FillEllipse(&SolidBrush(Color::Black),-6,-9,14,14);
  3430. graphics.FillEllipse(&SolidBrush(Color::Red),-7,-7,14,14);
  3431. //绘制速度极限标记
  3432. pen.SetWidth(4);
  3433. pen.SetColor(Color::Black);
  3434. pen.SetEndCap(LineCapRound);
  3435. pen.SetStartCap(LineCapFlat);
  3436. graphics.DrawLine(&pen, PointF(HEIGHT/15.75F,HEIGHT/3.95F),
  3437. PointF(HEIGHT/10.75F,HEIGHT/5.2F));
  3438. pen.SetColor(Color::Red);
  3439. graphics.DrawLine(&pen, PointF(HEIGHT/15.75F,HEIGHT/3.95F),
  3440. PointF(HEIGHT/15.75F,HEIGHT/4.6F));
  3441. }
  3442. void ScaleTransform_Click()
  3443. {
  3444. Graphics &graphics=GetGraphics();
  3445. graphics.Clear(Color::White);
  3446. Rect clientrect(0,0,
  3447. MyClient.Width,MyClient.Height);
  3448. //获取当前窗口的中心点
  3449. float width=clientrect.Width/2;
  3450. float height=clientrect.Height/2;
  3451. PointF center(width,height);
  3452. //设置矩形初始大小为30*30
  3453. float offsetX=center.X-15;
  3454. float offsetY=center.Y-15;
  3455. //构造矩形的缩放区域:让矩形的中心点与窗口的中心点一致
  3456. RectF rotateRect(offsetX,offsetY,30,30);
  3457. PointF Pcenter(rotateRect.X+rotateRect.Width/2,
  3458. rotateRect.Y+rotateRect.Height/2);
  3459. //设置初始缩放倍数
  3460. int scale=1;
  3461. //对矩形rotateRect旋转360度并不断放大矩形
  3462. for(int i=0;i<360;i+=60)
  3463. {
  3464. // 绘图平面以图片的中心点旋转
  3465. graphics.TranslateTransform(Pcenter.X, Pcenter.Y);
  3466. //在水平和垂直方向上同时扩大矩形
  3467. graphics.ScaleTransform(scale,scale);
  3468. graphics.RotateTransform(i);
  3469. //恢复绘图平面在水平和垂直方向的平移
  3470. graphics.TranslateTransform(-Pcenter.X, -Pcenter.Y);
  3471. //绘制矩形并延时
  3472. graphics.DrawRectangle(&Pen(Color::Black),
  3473. rotateRect.X,rotateRect.Y,rotateRect.Width,rotateRect.Height);
  3474. ::Sleep(100);
  3475. //重置绘图平面的所有变换
  3476. graphics.ResetTransform();
  3477. scale++;
  3478. }
  3479. }
  3480. void RectScale_Click()
  3481. {
  3482. Graphics &graphics=GetGraphics();
  3483. graphics.Clear(Color::White);
  3484. Rect clientrect(0,0,
  3485. MyClient.Width,MyClient.Height);
  3486. //获取当前窗口的中心点
  3487. float width=clientrect.Width/2;
  3488. float height=clientrect.Height/2;
  3489. PointF center(width,height);
  3490. //设置矩形初始大小为30*30
  3491. float offsetX=center.X-15;
  3492. float offsetY=center.Y-15;
  3493. //构造矩形的缩放区域:让矩形的中心点与窗口的中心点一致
  3494. RectF rotateRect(offsetX,offsetY,30,30);
  3495. PointF Pcenter(rotateRect.X+rotateRect.Width/2,
  3496. rotateRect.Y+rotateRect.Height/2);
  3497. //设置初始缩放倍数
  3498. int scale=0;
  3499. //对矩形rotateRect旋转360度并不断放大矩形
  3500. for(int i=0;i<361;i+=60)
  3501. {
  3502. // 绘图平面以图片的中心点旋转
  3503. graphics.TranslateTransform(Pcenter.X, Pcenter.Y);
  3504. //在水平和垂直方向上同时增加矩形的宽度和高度
  3505. rotateRect.Inflate(scale*15,scale*15);
  3506. graphics.RotateTransform(i);
  3507. //恢复绘图平面在水平和垂直方向的平移
  3508. graphics.TranslateTransform(-Pcenter.X, -Pcenter.Y);
  3509. //绘制图片并延时
  3510. graphics.DrawRectangle(&Pen(Color::Black),
  3511. rotateRect.X,rotateRect.Y,rotateRect.Width,rotateRect.Height);
  3512. ::Sleep(100);
  3513. //重置对矩形大小的所有变换
  3514. graphics.ResetTransform();
  3515. rotateRect.Inflate(-scale*15,-scale*15);
  3516. scale++;
  3517. }
  3518. }
  3519. void FontRotate_Click()
  3520. {
  3521. Graphics &graphics=GetGraphics();
  3522. graphics.Clear(Color::White);
  3523. FontFamily fontFamily(L"宋体");
  3524. Font myFont(&fontFamily, 20, FontStyleBold, UnitPixel);
  3525. SolidBrush redBrush(Color::Red);
  3526. //设置文本输出区域
  3527. RectF layoutRect(myFont.GetHeight(&graphics),myFont.GetHeight(&graphics),
  3528. MyClient.Width/2,MyClient.Height/2);
  3529. StringFormat format;
  3530. //设置文本输出格式
  3531. format.SetAlignment(StringAlignmentNear);
  3532. format.SetLineAlignment(StringAlignmentCenter);
  3533. //将文本的输出起点设置成窗口的中心
  3534. graphics.TranslateTransform(layoutRect.Width,layoutRect.Height);
  3535. //在旋转时每隔30度输出文本
  3536. for(int i=0;i<360;i+=30)
  3537. {
  3538. //旋转绘图平面
  3539. graphics.RotateTransform(i);
  3540. MyDrawString(graphics, " 旋转字体",myFont, redBrush,
  3541. layoutRect, format);
  3542. //恢复所做的旋转
  3543. graphics.RotateTransform(-i);
  3544. }
  3545. }
  3546. void MirrorText_Click()
  3547. {
  3548. Graphics &graphics=GetGraphics();
  3549. graphics.Clear(Color::White);
  3550. SolidBrush redBrush(Color::Red);
  3551. //构造字体,用于镜像文本输出
  3552. FontFamily fontFamily(L"华文楷体");
  3553. Font myFont(&fontFamily,
  3554. 48, FontStyleRegular, UnitPixel);
  3555. //构造英文字体,用于演示文本的输出方向
  3556. FontFamily fontFamily2(L"Courier New");
  3557. Font myFont2(&fontFamily2, 16, FontStyleRegular, UnitPixel);
  3558. //设置文本输出区域
  3559. RectF layoutRect(myFont.GetHeight(&graphics),myFont.GetHeight(&graphics),
  3560. MyClient.Width/2,MyClient.Height/2);
  3561. StringFormat format;
  3562. //设置文本输出时精确位置
  3563. //行距-下行部分 (设计单位)
  3564. float ascent =fontFamily.GetLineSpacing(FontStyleRegular)-
  3565. fontFamily.GetCellDescent(FontStyleRegular);
  3566. //行距-上行部分 (设计单位)
  3567. float ascent2 =fontFamily.GetLineSpacing(FontStyleRegular)-
  3568. fontFamily.GetCellAscent(FontStyleRegular);
  3569. //将上、下部距离的设计单位转换成像素单位
  3570. float ascentPixel = myFont.GetSize()*
  3571. ascent /(fontFamily.GetEmHeight(FontStyleRegular));
  3572. float ascentPixel2 = myFont.GetSize()*
  3573. ascent2 /(fontFamily.GetEmHeight(FontStyleRegular));
  3574. //绘图平面下移
  3575. graphics.TranslateTransform(0,MyClient.Height/4);
  3576. //绘制正常方向的文本
  3577. MyDrawString(graphics, "镜像输出",myFont,redBrush,
  3578. PointF(0,-myFont.GetHeight(&graphics)+ascentPixel2), format);
  3579. //绘图平面右移,演示当前文本输出文向
  3580. graphics.TranslateTransform(200,0);
  3581. CString msg;
  3582. for(int i=0;i<3;i++)
  3583. {
  3584. msg.Format("Line %d...........",i);
  3585. MyDrawString(graphics, msg,myFont2,redBrush,
  3586. PointF(0,myFont2.GetHeight(&graphics)*i),format);
  3587. }
  3588. //恢复对绘图平面的右移
  3589. graphics.TranslateTransform(-200,0);
  3590. //绘图平面在垂直方向倒置
  3591. graphics.ScaleTransform(1,-1);
  3592. //输出镜像文本
  3593. MyDrawString(graphics, "镜像输出",myFont,SolidBrush(Color::Gray),
  3594. PointF(0,-ascentPixel), format);
  3595. //绘图平面再次右移,演示当前文本输出文向
  3596. graphics.TranslateTransform(200,0);
  3597. for(i=0;i<3;i++)
  3598. {
  3599. msg.Format("Line %d...........",i);
  3600. MyDrawString(graphics, msg,myFont2, SolidBrush(Color::Gray),
  3601. PointF(0,myFont2.GetHeight(&graphics)*i), format);
  3602. }
  3603. //重置在绘图平面上进行的所有变换:演示水平镜像文本输出
  3604. graphics.ResetTransform ( ) ;
  3605. //绘图平面下移
  3606. graphics.TranslateTransform(MyClient.Width/2,
  3607. MyClient.Height/2+myFont.GetHeight(&graphics)*2);
  3608. //绘制正常方向的文本
  3609. MyDrawString(graphics, "镜像输出",myFont, redBrush,
  3610. PointF(0,0), format);
  3611. //绘图平面在水平方向倒置
  3612. graphics.ScaleTransform(-1,1);
  3613. //输出水平镜像文本
  3614. MyDrawString(graphics, "镜像输出",myFont, SolidBrush(Color::Gray),
  3615. PointF(0,0), format);
  3616. }
  3617. void Matrix_ListElements_Click_1()
  3618. {
  3619. Graphics &graphics=GetGraphics();
  3620. graphics.Clear(Color::White);
  3621. SolidBrush redBrush(Color::Red);
  3622. FontFamily fontFamily(L"华文楷体");
  3623. Font myFont(&fontFamily,20,
  3624. FontStyleRegular, UnitPixel);
  3625. //进行平移变换
  3626. graphics.TranslateTransform(10,10);
  3627. graphics.TranslateTransform(20,10);
  3628. Matrix m;;
  3629. graphics.GetTransform(&m);
  3630. //获取已经实施在绘图平面的坐标变换
  3631. CString msg;
  3632. float x[6]; m.GetElements(x);
  3633. //输出矩阵的每一个元素
  3634. for(int i=0; i<6; i++)
  3635. {
  3636. msg.Format("第%d个矩阵元素为%.2f",i,x[i]);
  3637. MyDrawString(graphics, msg,myFont, redBrush,
  3638. PointF(0, myFont.GetHeight(&graphics)*(i+1)));
  3639. }
  3640. }
  3641. void MatrixPos_Click()
  3642. {
  3643. Graphics &graphics=GetGraphics();
  3644. graphics.Clear(Color::White);
  3645. Pen pen(Color::Blue,4);
  3646. Pen pen2(Color::Gray);
  3647. FontFamily fontFamily(L"宋体");
  3648. Font myFont(&fontFamily,
  3649. 20, FontStyleRegular, UnitPixel);
  3650. //获取当前窗口的大小
  3651. Rect rect(0,0,
  3652. MyClient.Width,MyClient.Height);
  3653. //设置定义椭圆的矩形
  3654. Rect r(-40, -65,80, 130);
  3655. Matrix matrix;
  3656. graphics.TranslateTransform(rect.Width/2, rect.Height/2);
  3657. //使用默认的旋转顺序(MatrixOrderPrepend)
  3658. graphics.RotateTransform(20);
  3659. //获取对绘图平面已经进行的坐标变换
  3660. graphics.GetTransform(&matrix);
  3661. //绘制椭圆
  3662. graphics.DrawEllipse(&pen, r);
  3663. //使用MatrixOrderAppend旋转顺序
  3664. graphics.ResetTransform();
  3665. graphics.TranslateTransform(rect.Width/2, rect.Height/2);
  3666. graphics.RotateTransform(20,MatrixOrderAppend);
  3667. //使用红色画笔绘制另一个椭圆
  3668. graphics.DrawEllipse(&Pen(Color::Red,1), r);
  3669. Matrix matrix2;
  3670. graphics.GetTransform(&matrix2);
  3671. //重置坐标变换、以窗口中心点为原点绘制坐标轴
  3672. graphics.ResetTransform();
  3673. graphics.DrawLine(&pen2, 0, rect.Height/2,
  3674. rect.Width, rect.Height/2);
  3675. graphics.DrawLine(&pen2, rect.Width/2,
  3676. 0, rect.Width/2, rect.Height);
  3677. CString msg;
  3678. //获取矩阵元素值
  3679. float x[6]; matrix.GetElements(x);
  3680. //输出矩阵元素值
  3681. int i=0;
  3682. for (i=0; i<6; i++)
  3683. {
  3684. msg.Format("%8.2f",x[i]);
  3685. //输出行向量
  3686. if(i==1||i==3||i==5)
  3687. {
  3688. //垂直方向上下移一行
  3689. graphics.TranslateTransform(0,myFont.GetHeight(&graphics));
  3690. MyDrawString (graphics, msg, myFont, SolidBrush(Color::Black), PointF(0,0));
  3691. }
  3692. }
  3693. }
  3694. void Martrix_Invert_Click()
  3695. {
  3696. Graphics &graphics=GetGraphics();
  3697. graphics.Clear(Color::White);
  3698. Pen myPen(Color::Blue,4);
  3699. Matrix matrix(1.0f, 0.0f, 0.0f, 1.0f, 30.0f, 20.0f);
  3700. //检查矩阵是否可逆?
  3701. if(!matrix.IsInvertible())
  3702. {
  3703. AfxMessageBox("该矩形为不可逆矩形,无法演示");
  3704. return;
  3705. }
  3706. graphics.SetTransform(&matrix);
  3707. for(int i=0;i<200;i++)
  3708. {
  3709. //计算逆矩形
  3710. matrix.Invert();
  3711. //对绘图平面进行坐标变换
  3712. graphics.SetTransform(&matrix);
  3713. graphics.DrawRectangle(&myPen, 35, 25, 200, 100);
  3714. }
  3715. }
  3716. void Matrix_Multiply_Click()
  3717. {
  3718. Graphics &graphics=GetGraphics();
  3719. graphics.Clear(Color::White);
  3720. SolidBrush brush(Color::Blue);
  3721. FontFamily fontFamily(L"Arial");
  3722. Font myFont(&fontFamily,
  3723. 16, FontStyleRegular, UnitPixel);
  3724. //加载图片
  3725. Bitmap image(L"jieba.bmp");
  3726. //缩放
  3727. Matrix matrix1(0.80f, 0.0f, 0.0f, 0.8f, 0.0f, 0.0f);
  3728. //位移
  3729. Matrix matrix2(1.0f, 0.0f, 0.0f, 1.0f, 20.0f, 10.0f);
  3730. //从右上往左下翻转图片:X轴与Y轴对调
  3731. Matrix matrix3(0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
  3732. //第一次追加变换
  3733. matrix1.Multiply(&matrix2, MatrixOrderAppend);
  3734. //第二次追加变换
  3735. matrix1.Multiply(&matrix3,MatrixOrderAppend);
  3736. //进行复合变换
  3737. graphics.SetTransform(&matrix1);
  3738. //绘制图片
  3739. graphics.DrawImage(&image, 0, 0);
  3740. //获取已经作用在绘图平面上的坐标变换
  3741. Matrix matrix;
  3742. graphics.GetTransform(&matrix);
  3743. graphics.ResetTransform();
  3744. //在新位置上查看变换矩阵信息
  3745. graphics.TranslateTransform(image.GetHeight(),0);
  3746. //获取矩阵元素值
  3747. float x[6]; matrix.GetElements(x);
  3748. //输出每一个矩阵元素的值
  3749. CString msg;
  3750. int i=0;
  3751. for(i=0; i<6; i++)
  3752. {
  3753. msg.Format("%6.2f",x[i]);
  3754. //输出行向量
  3755. if(i==1||i==3||i==5)
  3756. {
  3757. //垂直方向上下移一行
  3758. graphics.TranslateTransform(0,myFont.GetHeight(&graphics));
  3759. MyDrawString(graphics, msg,myFont,SolidBrush(Color::Black),
  3760. PointF(0,0));
  3761. }
  3762. }
  3763. }
  3764. void Matrix_TransformPoints_Click()
  3765. {
  3766. Graphics &graphics=GetGraphics();
  3767. graphics.Clear(Color::White);
  3768. SolidBrush redBrush(Color::Red);
  3769. SolidBrush blueBrush(Color::Blue);
  3770. Pen pen1(Color::Blue,2);
  3771. Pen pen2(Color::Red,1);
  3772. //定义构成曲线的点坐标
  3773. Point points[] =
  3774. {
  3775. Point(50, 100),
  3776. Point(100, 50),
  3777. Point(150, 125),
  3778. Point(200, 100),
  3779. Point(250, 125),
  3780. };
  3781. //绘制曲线(未使用变换时)
  3782. graphics.DrawCurve(&pen1, points, 5);
  3783. //绘制曲线定义点信息
  3784. for(int i=0;i<5;i++)
  3785. {
  3786. graphics.FillEllipse(&blueBrush,
  3787. points[i].X-5,points[i].Y-5,10,10);
  3788. }
  3789. //定义一个在竖直方向上缩放因子为0.5的矩阵
  3790. Matrix matrix(1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f);
  3791. //对points数据中的每一个成员进行矩形运算
  3792. matrix.TransformPoints(points);
  3793. //绘制曲线(使用变换后)
  3794. graphics.DrawCurve(&pen2, points,5);
  3795. //再次绘制曲线定义点信息
  3796. for(i=0;i<5;i++)
  3797. {
  3798. graphics.FillEllipse(&redBrush,
  3799. points[i].X-5,points[i].Y-5,10,10);
  3800. }
  3801. }
  3802. void Matrix_TransformPoints2_Click()
  3803. {
  3804. Graphics &graphics=GetGraphics();
  3805. graphics.Clear(Color::White);
  3806. //构造字体系列
  3807. FontFamily fontFamily(L"隶书");
  3808. SolidBrush redBrush(Color::Red);
  3809. //设置文本输出质量
  3810. graphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit);
  3811. graphics.SetSmoothingMode(SmoothingModeAntiAlias);
  3812. //创建路径区域
  3813. GraphicsPath path;
  3814. //向区域中追加文本,字体大小为60
  3815. path.AddString(L"文字特效",4 ,&fontFamily,(int)FontStyleRegular,
  3816. 60, Point(0, 0), &StringFormat());
  3817. //获取路径的点信息
  3818. //获取路径的点类型信息
  3819. PathData pathdata;
  3820. path.GetPathData(&pathdata);
  3821. //将文本在水平方向上缩小,在垂直方向上拉伸
  3822. Matrix matrix(0.50f, 0.0f, 0.0f, 3.5f, 0.0f,0.0f);
  3823. //对points数据中的每一个成员进行矩形运算
  3824. matrix.TransformPoints(pathdata.Points, pathdata.Count);
  3825. //根据计算后的点重新构造路径
  3826. GraphicsPath newpath(pathdata.Points,pathdata.Types,pathdata.Count);
  3827. //填充路径
  3828. graphics.FillPath(&redBrush,&newpath);
  3829. //第二种特效
  3830. Matrix matrix2(0.6f, 0.5f, 0.20f, 1.5f, 160.0f, -40.0f);
  3831. PathData pathdata2;
  3832. path.GetPathData(&pathdata2);
  3833. //对所有的点进行计算
  3834. matrix2.TransformPoints(pathdata2.Points, pathdata2.Count);
  3835. //根据计算后的点重新构造路径
  3836. GraphicsPath newpath2(pathdata2.Points,pathdata2.Types,pathdata2.Count);
  3837. graphics.FillPath(&redBrush,&newpath2);
  3838. }
  3839. void Matrix_TransformVectors_Click()
  3840. {
  3841. Graphics &graphics=GetGraphics();
  3842. graphics.Clear(Color::White);
  3843. Pen pen(Color::Blue, 7);
  3844. pen.SetEndCap(LineCapArrowAnchor);
  3845. SolidBrush brush(Color::Blue);
  3846. // 定义一个点和一个向量
  3847. PointF point[]={PointF(100, 50)};
  3848. PointF vector[]={PointF(100, 50)};
  3849. // 绘制点point
  3850. graphics.FillEllipse(&brush, RectF(point[0].X-5, point[0].Y-5, 10, 10));
  3851. //将点(10,10)与vector表示的位置连接起来
  3852. graphics.DrawLine(&pen, PointF(10, 10), vector[0]);
  3853. // 定义变换矩阵
  3854. Matrix matrix(0.8f, 0.6f, -0.6f, 0.8f, 100.0f, 0.0f);
  3855. //对点point的位置信息进行变换
  3856. matrix.TransformPoints(point);
  3857. //对点vector的位置信息进行二维向量变换
  3858. matrix.TransformVectors(vector);
  3859. // 绘制变换后的点
  3860. pen.SetColor(Color::Red);
  3861. brush.SetColor(Color::Red);
  3862. graphics.FillEllipse(&brush, RectF(point[0].X - 5, point[0].Y - 5, 10, 10));
  3863. //将点(10,10)与变换后的vector表示的位置连接起来
  3864. graphics.DrawLine(&pen, PointF(10, 10), vector[0]);
  3865. }
  3866. void Matrix_RotateAt_Click()
  3867. {
  3868. Graphics &graphics=GetGraphics();
  3869. graphics.Clear(Color::White);
  3870. Pen pen(Color::Blue,4);
  3871. Pen pen2(Color::Gray);
  3872. //获取当前窗口区域
  3873. RectF rect(0,0,MyClient.Width,MyClient.Height);;
  3874. //构造并平移矩阵到窗口中心点
  3875. Matrix matrix;
  3876. matrix.Translate(rect.Width/2, rect.Height/2);
  3877. //将绘图平面依次旋转一周
  3878. for(int i=0;i<360;i+=30)
  3879. {
  3880. //旋转矩阵,相对于窗口中心点
  3881. matrix.RotateAt(i, PointF(rect.Width/2,
  3882. rect.Height/2), MatrixOrderAppend);
  3883. //将旋转后的矩阵作用于绘图平面
  3884. graphics.SetTransform(&matrix);
  3885. //绘制椭圆
  3886. graphics.DrawEllipse(&pen, -80, -30, 160, 60);
  3887. //重置作用在绘图平面是所有的变换
  3888. graphics.ResetTransform();
  3889. }
  3890. //以窗口中心点为原点绘制坐标轴
  3891. graphics.DrawLine(&pen2, PointF(0, rect.Height/2),
  3892. PointF(rect.Width, rect.Height/2));
  3893. graphics.DrawLine(&pen2, PointF(rect.Width/2, 0),
  3894. PointF(rect.Width/2, rect.Height));
  3895. }
  3896. void Matrix_Shear_Click()
  3897. {
  3898. Graphics &graphics=GetGraphics();
  3899. graphics.Clear(Color::White);
  3900. //装入图片
  3901. Bitmap image(L"jieba.bmp");
  3902. //定义图片的显示区域
  3903. Rect rect(0,0,100,150);
  3904. //绘制源图
  3905. graphics.DrawImage(&image,rect);
  3906. Matrix matrix;
  3907. //设置水平投射因子
  3908. matrix.Shear(0.8f,0.0f);
  3909. //对绘图平面使用投射变换
  3910. graphics.SetTransform(&matrix);
  3911. graphics.TranslateTransform(100,0);
  3912. //绘制投射变换后的图片
  3913. graphics.DrawImage(&image,rect);
  3914. graphics.ResetTransform();
  3915. Matrix matrix2;
  3916. //设置垂直投射因子
  3917. matrix2.Shear(0.0f,0.8f);
  3918. //对绘图平面使用投射变换
  3919. graphics.SetTransform(&matrix2);
  3920. graphics.TranslateTransform(200,0);
  3921. //绘制投射变换后的图片
  3922. graphics.DrawImage(&image,rect);
  3923. }
  3924. void Matrix_TextoutShear_Click()
  3925. {
  3926. Graphics &graphics=GetGraphics();
  3927. graphics.Clear(Color::White);
  3928. SolidBrush brush(Color::Blue);
  3929. FontFamily fontFamily(L"华文楷体");
  3930. Font myFont(&fontFamily, 20, FontStyleRegular, UnitPixel);
  3931. Font myfont(L"Times New Roman", 100);
  3932. Matrix mymat;
  3933. //投射
  3934. mymat.Shear( -1.4f, 0.0f ) ;
  3935. //缩放
  3936. mymat.Scale( 1, 0.5f ) ;
  3937. //平移
  3938. mymat.Translate ( 236, 170 ) ;
  3939. //对绘图平面实施坐标变换、、
  3940. graphics.SetTransform(&mymat);
  3941. SolidBrush mybrush(Color::Gray);
  3942. SolidBrush redbrush(Color::Red);
  3943. //绘制阴影
  3944. MyDrawString(graphics, "Hello",myfont,mybrush, PointF(0, 50));
  3945. graphics.ResetTransform ( ) ;
  3946. //绘制前景
  3947. MyDrawString(graphics, "Hello",myfont,redbrush, PointF(0, 50));
  3948. CString msg;
  3949. //获取矩阵元素值
  3950. float x[6]; mymat.GetElements(x);
  3951. //输出每一个矩阵元素的值
  3952. int i=0;
  3953. for(; i<6; i++)
  3954. {
  3955. msg.Format("{0,%6.2f}",x[i]);
  3956. //输出行向量
  3957. if(i==1||i==3||i==5)
  3958. {
  3959. MyDrawString(graphics, msg,myFont,SolidBrush(Color::Black),
  3960. PointF(0,0));
  3961. //垂直方向上下移一行
  3962. graphics.TranslateTransform(0,myFont.GetHeight(&graphics));
  3963. }
  3964. }
  3965. }
  3966. void Matrix_ChangeFontHeight_Click()
  3967. {
  3968. Graphics &graphics=GetGraphics();
  3969. graphics.Clear(Color::White);
  3970. //构造字体系列
  3971. FontFamily fontFamily(L"隶书");
  3972. //创建路径区域
  3973. GraphicsPath path;
  3974. SolidBrush redBrush(Color::Red);
  3975. //设置文本输出质量
  3976. graphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit);
  3977. graphics.SetSmoothingMode(SmoothingModeAntiAlias);
  3978. //向区域中追加文本,字体大小为80
  3979. path.AddString(L"大小渐变",4,&fontFamily,(int)FontStyleRegular,
  3980. 80, Point(0, 0), &StringFormat());
  3981. //获取路径所占的矩形区域
  3982. RectF bound; path.GetBounds(&bound);
  3983. //获取路径区域的中心点
  3984. float halfH=bound.Height/2;
  3985. float halfW=bound.Width/2;
  3986. //对路径实施变换,更改路径区域的中心点
  3987. Matrix pathMartrix(1,0,0,1,-halfW,-halfH);
  3988. path.Transform(&pathMartrix);
  3989. //获取路径的点信息
  3990. //获取路径的点类型信息
  3991. PathData pathdata;
  3992. path.GetPathData(&pathdata);
  3993. //依次对路径的定义点的Y值进行缩放
  3994. for(int i=0;i<pathdata.Count;i++)
  3995. {
  3996. //根据该点距路径起点的距离占整个路径长度的比例更改Y值
  3997. pathdata.Points[i].Y*=2*(bound.Width-abs(pathdata.Points[i].X))/bound.Width;
  3998. }
  3999. //根据更改后的路径定义点重新构造路径
  4000. GraphicsPath newpath(pathdata.Points,pathdata.Types,pathdata.Count);
  4001. //将绘图平面的原点移到窗口中心
  4002. graphics.TranslateTransform(MyClient.Width/2,MyClient.Height/2-40);
  4003. //填充路径
  4004. graphics.FillPath(&redBrush,&newpath);
  4005. }
  4006. void ColorMatrix_Start_Click()
  4007. {
  4008. Graphics &graphics=GetGraphics();
  4009. graphics.Clear(Color::White);
  4010. //加载图片
  4011. Bitmap image(L"ColorInput.bmp");
  4012. int width = image.GetWidth();
  4013. int height = image.GetHeight();
  4014. ImageAttributes imageAttributes;
  4015. //定义色彩变换矩阵
  4016. ColorMatrix colorMatrix=
  4017. {
  4018. 2.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  4019. 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
  4020. 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  4021. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  4022. 1.0f, 0.0f, 0.0f, 0.0f, 1.0f
  4023. };
  4024. //启用色彩变换矩阵
  4025. imageAttributes.SetColorMatrix(
  4026. &colorMatrix,
  4027. ColorMatrixFlagsDefault,
  4028. ColorAdjustTypeBitmap);
  4029. //绘制源图
  4030. graphics.DrawImage(&image, 0, 0);
  4031. //使用色彩变换矩阵输出图片
  4032. graphics.TranslateTransform(width+10,0);
  4033. graphics.DrawImage(
  4034. &image,
  4035. Rect(0, 0, width, height),
  4036. 0, 0,
  4037. width, height,UnitPixel,
  4038. &imageAttributes);
  4039. }
  4040. void TranslateColor_Click()
  4041. {
  4042. Graphics &graphics=GetGraphics();
  4043. graphics.Clear(Color::White);
  4044. //加载图片
  4045. Bitmap image(L"ColorBar.bmp");
  4046. ImageAttributes imageAttributes;
  4047. int width = image.GetWidth();
  4048. int height = image.GetHeight();
  4049. //定义色彩变换矩阵
  4050. ColorMatrix colorMatrix=
  4051. {
  4052. 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  4053. 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
  4054. 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  4055. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  4056. 0.75f, 0.0f, 0.0f, 0.0f, 1.0f
  4057. };
  4058. //启用色彩变换矩阵
  4059. imageAttributes.SetColorMatrix(&colorMatrix,
  4060. ColorMatrixFlagsDefault,ColorAdjustTypeBitmap);
  4061. //绘制源图
  4062. graphics.DrawImage(&image, 0, 0);
  4063. //使用色彩变换矩阵输出图片
  4064. graphics.TranslateTransform(width+10,0);
  4065. graphics.DrawImage(&image, Rect(0, 0, width, height),
  4066. 0, 0,width, height,UnitPixel,&imageAttributes);
  4067. }
  4068. void ScaleColor_Click()
  4069. {
  4070. Graphics &graphics=GetGraphics();
  4071. graphics.Clear(Color::White);
  4072. //加载图片
  4073. Bitmap image(L"ColorBar.bmp");
  4074. ImageAttributes imageAttributes;
  4075. int width = image.GetWidth();
  4076. int height = image.GetHeight();
  4077. //定义色彩变换矩阵1
  4078. ColorMatrix colorMatrix=
  4079. {
  4080. 0.5f, 0.0f, 0.0f, 0.0f, 0.0f,
  4081. 0.0f, 0.5f, 0.0f, 0.0f, 0.0f,
  4082. 0.0f, 0.0f, 0.5f, 0.0f, 0.0f,
  4083. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  4084. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
  4085. };
  4086. //定义色彩变换矩阵2
  4087. ColorMatrix colorMatrix2=
  4088. {
  4089. 0.5f, 0.0f, 0.0f, 0.0f, 0.0f,
  4090. 0.0f, 0.5f, 0.0f, 0.0f, 0.0f,
  4091. 0.0f, 0.0f, 500.5f, 0.0f, 0.0f,
  4092. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  4093. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
  4094. };
  4095. //启用色彩变换矩阵1
  4096. imageAttributes.SetColorMatrix(
  4097. &colorMatrix,
  4098. ColorMatrixFlagsDefault,
  4099. ColorAdjustTypeBitmap);
  4100. //绘制源图
  4101. graphics.DrawImage(&image, 0, 0);
  4102. //使用色彩变换矩阵输出图片
  4103. graphics.TranslateTransform(width+10,0);
  4104. graphics.DrawImage(
  4105. &image,
  4106. Rect(0, 0, width, height),
  4107. 0, 0,
  4108. width, height,UnitPixel,
  4109. &imageAttributes);
  4110. //清除已经采取的色彩变换
  4111. imageAttributes.ClearColorMatrix(ColorAdjustTypeBitmap);
  4112. //重新加载另一变换矩阵Matrix2
  4113. imageAttributes.SetColorMatrix(
  4114. &colorMatrix2,
  4115. ColorMatrixFlagsDefault,
  4116. ColorAdjustTypeBitmap);
  4117. //演示使用Matrix2的色彩调整情况
  4118. graphics.TranslateTransform(width+10,0);
  4119. graphics.DrawImage(
  4120. &image,
  4121. Rect(0, 0, width, height),
  4122. 0, 0,
  4123. width, height,UnitPixel,
  4124. &imageAttributes);
  4125. }
  4126. void RotateColor_Click()
  4127. {
  4128. Graphics &graphics=GetGraphics();
  4129. graphics.Clear(Color::White);
  4130. //加载图片
  4131. Bitmap image(L"Colorinput.bmp");
  4132. int width = image.GetWidth();
  4133. int height = image.GetHeight();
  4134. float degrees = 90;
  4135. //从角度到弧度
  4136. double r = degrees * PI/ 180.0f;
  4137. ImageAttributes imageAttributes;
  4138. //绘制源图
  4139. graphics.DrawImage(&image, 0, 0);
  4140. //红色绕着蓝色旋转
  4141. ColorMatrix colorMatrix=
  4142. {
  4143. (float)cos(r), (float)sin(r), 0.0f, 0.0f, 0.0f,
  4144. -(float)sin(r), (float)cos(r), 0.0f, 0.0f, 0.0f,
  4145. 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  4146. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  4147. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
  4148. };
  4149. //使用色彩变换矩阵输出图片(R->B)
  4150. graphics.TranslateTransform(width+10,0);
  4151. //设置R->B色彩变换矩阵
  4152. imageAttributes.SetColorMatrix(
  4153. &colorMatrix,
  4154. ColorMatrixFlagsDefault,
  4155. ColorAdjustTypeBitmap);
  4156. graphics.DrawImage(
  4157. &image,
  4158. Rect(0, 10, width, height),
  4159. 0, 0,width,height,
  4160. UnitPixel,
  4161. &imageAttributes);
  4162. //清除已经采取的色彩变换
  4163. imageAttributes.ClearColorMatrix(ColorAdjustTypeBitmap);
  4164. //重新加载另一变换矩阵Matrix2
  4165. //绿色绕着红色旋转
  4166. ColorMatrix colorMatrix2=
  4167. {
  4168. 1, 0, 0.0f, 0.0f, 0.0f,
  4169. 0, (float)cos(r), (float)sin(r), 0.0f, 0.0f,
  4170. 0.0f, -(float)sin(r), (float)cos(r), 0.0f, 0.0f,
  4171. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  4172. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
  4173. };
  4174. imageAttributes.SetColorMatrix(
  4175. &colorMatrix2,
  4176. ColorMatrixFlagsDefault,
  4177. ColorAdjustTypeBitmap);
  4178. //在第二行输出
  4179. graphics.ResetTransform();
  4180. graphics.TranslateTransform(0,height+10);
  4181. graphics.DrawImage(&image,
  4182. Rect(0, 0, width, height),
  4183. 0, 0,width, height,UnitPixel,
  4184. &imageAttributes);
  4185. //清除已经采取的色彩变换
  4186. imageAttributes.ClearColorMatrix(ColorAdjustTypeBitmap);
  4187. //蓝色绕着红色旋
  4188. ColorMatrix colorMatrix3=
  4189. {
  4190. (float)cos(r), 0, -(float)sin(r), 0.0f, 0.0f,
  4191. 0, 1, 0.0f, 0.0f, 0.0f,
  4192. (float)sin(r), 0, (float)cos(r), 0.0f, 0.0f,
  4193. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  4194. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
  4195. };
  4196. //重新加载另一变换矩阵Matrix3
  4197. imageAttributes.SetColorMatrix(
  4198. &colorMatrix3,
  4199. ColorMatrixFlagsDefault,
  4200. ColorAdjustTypeBitmap);
  4201. graphics.TranslateTransform(width+10,0);
  4202. graphics.DrawImage(
  4203. &image,
  4204. Rect(0, 0, width, height),
  4205. 0, 0,
  4206. width, height,UnitPixel,
  4207. &imageAttributes);
  4208. }
  4209. void ColorShear_Click()
  4210. {
  4211. Graphics &graphics=GetGraphics();
  4212. graphics.Clear(Color::White);
  4213. //加载图片
  4214. Bitmap image(L"Colorinput.bmp");
  4215. ImageAttributes imageAttributes;
  4216. int width = image.GetWidth();
  4217. int height = image.GetHeight();
  4218. //定义色彩变换矩阵
  4219. ColorMatrix colorMatrix=
  4220. {
  4221. 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  4222. 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
  4223. 0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
  4224. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  4225. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
  4226. };
  4227. //启用色彩变换矩阵
  4228. imageAttributes.SetColorMatrix(
  4229. &colorMatrix,
  4230. ColorMatrixFlagsDefault,
  4231. ColorAdjustTypeBitmap);
  4232. //绘制源图
  4233. graphics.DrawImage(&image, 0, 0);
  4234. //使用色彩变换矩阵输出图片
  4235. graphics.TranslateTransform(width+10,0);
  4236. graphics.DrawImage(&image, Rect(0, 0, width, height),
  4237. 0, 0,width, height,UnitPixel,&imageAttributes);
  4238. }
  4239. void ColorRemap_Click()
  4240. {
  4241. Graphics &graphics=GetGraphics();
  4242. graphics.Clear(Color::White);
  4243. //加载蓝色背景的图片
  4244. Bitmap image(L"Nemo_Blue.bmp");
  4245. ImageAttributes imageAttributes;
  4246. int width = image.GetWidth();
  4247. int height = image.GetHeight();
  4248. //将蓝色替换成白色,以达到抠除的效果
  4249. ColorMap colorMap;
  4250. colorMap.oldColor = Color(255, 0, 0, 255);
  4251. colorMap.newColor = Color(255, 255,255,255);
  4252. //设置色彩转换表
  4253. ColorMap remapTable[1]; remapTable[0]=colorMap;
  4254. //设置图片的色彩信息
  4255. imageAttributes.SetRemapTable(1, remapTable, ColorAdjustTypeBitmap);
  4256. //绘制原始图像
  4257. graphics.DrawImage(&image, 0, 0, width, height);
  4258. //绘制已经抠除背景色的新图像
  4259. graphics.DrawImage(&image,
  4260. Rect(width+10, 0, width, height), //目标区域
  4261. 0, 0, // 源图左上角坐标
  4262. width, // 源图宽
  4263. height, // 源图宽
  4264. UnitPixel,
  4265. //图片的色彩信息
  4266. &imageAttributes);
  4267. }
  4268. void SetRGBOutputChannel_Click()
  4269. {
  4270. Graphics &graphics=GetGraphics();
  4271. graphics.Clear(Color::White);
  4272. //加载图片
  4273. Bitmap image(L"jieba.bmp");
  4274. //绘制源图
  4275. graphics.DrawImage(&image, 0, 0);
  4276. int width = image.GetWidth();
  4277. int height = image.GetHeight();
  4278. ImageAttributes imageAttributes;
  4279. //设置红色通道
  4280. ColorMatrix colorMatrix=
  4281. {
  4282. 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  4283. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  4284. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  4285. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  4286. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
  4287. };
  4288. //启用色彩变换矩阵
  4289. imageAttributes.SetColorMatrix(
  4290. &colorMatrix,
  4291. ColorMatrixFlagsDefault,
  4292. ColorAdjustTypeBitmap);
  4293. //使用色彩变换矩阵输出图片
  4294. graphics.TranslateTransform(width+10,0);
  4295. graphics.DrawImage(&image, Rect(0, 0, width, height),
  4296. 0, 0,width, height,UnitPixel,&imageAttributes);
  4297. //清除已经采取的色彩变换
  4298. imageAttributes.ClearColorMatrix(ColorAdjustTypeBitmap);
  4299. //设置绿色通道
  4300. ColorMatrix colorMatrix2=
  4301. {
  4302. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  4303. 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
  4304. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  4305. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  4306. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
  4307. };
  4308. //启用色彩变换矩阵
  4309. imageAttributes.SetColorMatrix(
  4310. &colorMatrix2,
  4311. ColorMatrixFlagsDefault,
  4312. ColorAdjustTypeBitmap);
  4313. //使用色彩变换矩阵输出图片
  4314. graphics.ResetTransform();
  4315. graphics.TranslateTransform(0,height+10);
  4316. graphics.DrawImage(&image, Rect(0, 0, width, height),
  4317. 0, 0,width, height,UnitPixel,&imageAttributes);
  4318. //清除已经采取的色彩变换
  4319. imageAttributes.ClearColorMatrix(ColorAdjustTypeBitmap);
  4320. //设置蓝色通道
  4321. ColorMatrix colorMatrix3=
  4322. {
  4323. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  4324. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  4325. 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  4326. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  4327. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
  4328. };
  4329. //启用色彩变换矩阵
  4330. imageAttributes.SetColorMatrix(
  4331. &colorMatrix3,
  4332. ColorMatrixFlagsDefault,
  4333. ColorAdjustTypeBitmap);
  4334. //使用色彩变换矩阵输出图片
  4335. graphics.TranslateTransform(width+10,0);
  4336. graphics.DrawImage(&image, Rect(0, 0, width, height),
  4337. 0, 0,width, height,UnitPixel,&imageAttributes);
  4338. }
  4339. void Metafile_Click()
  4340. {
  4341. // Note: To record a metafile, you must construct a Graphics
  4342. //object based on a Metafile object. The recording of the metafile
  4343. //ends when that Graphics object is deleted or goes out of scope.
  4344. //新建一个图元文件
  4345. HDC hdc =GetGraphics().GetHDC();
  4346. Metafile metaFile1(L"图元文件示例.emf", hdc);
  4347. {
  4348. //使用Metafile对象的地址做为绘图平面
  4349. Graphics graphics(&metaFile1);
  4350. //定义一个由红到蓝的渐变色画刷
  4351. LinearGradientBrush RtoBBrush(
  4352. Point(0, 10),
  4353. Point(200, 10),
  4354. Color::Red,
  4355. Color::Blue);
  4356. //定义一个由蓝到黄的渐变色画刷
  4357. LinearGradientBrush BtoYBrush(
  4358. Point(0, 10),
  4359. Point(200, 10),
  4360. Color::Blue,
  4361. Color::Yellow);
  4362. Pen bluePen(Color::Blue);
  4363. // 以下的操作是往屏幕上绘制一八卦图形
  4364. Rect ellipseRect(0, 0, 200, 200);
  4365. Rect left(0, 50, 100, 100);
  4366. graphics.DrawArc(&bluePen,left,180.0f,180.0f);
  4367. Rect right(100, 50, 100, 100);
  4368. graphics.FillPie(&RtoBBrush, ellipseRect,0.0f,180.0f);
  4369. graphics.FillPie(&BtoYBrush, ellipseRect,180.0f,180.0f);
  4370. graphics.FillPie(&RtoBBrush, left,180.0f,180.0f);
  4371. graphics.FillPie(&BtoYBrush, right,0.0f,180.0f);
  4372. //文本输出
  4373. SolidBrush solidBrush(Color::Black);
  4374. FontFamily fontFamily(L"隶书");
  4375. Font font(&fontFamily, 27,
  4376. FontStyleRegular, UnitPixel);
  4377. MyDrawString(graphics, "图元文件示例", font, solidBrush,
  4378. PointF(20.0f, 80.0f));
  4379. //到此,GDI+进行的只是往硬盘中存放图片信息的操作
  4380. }
  4381. //将上面的绘图信息进行回放
  4382. Graphics playbackGraphics(hdc);
  4383. playbackGraphics.Clear(Color::White);
  4384. //打开并显示图元文件
  4385. Metafile metaFile2(L"图元文件示例.emf");
  4386. playbackGraphics.DrawImage(&metaFile2, Point(0,0));
  4387. //释放HDC
  4388. playbackGraphics.ReleaseHDC(hdc);
  4389. }
  4390. void CroppingAndScaling_Click()
  4391. {
  4392. Graphics &graphics=GetGraphics();
  4393. graphics.Clear(Color::White);
  4394. //加载图片
  4395. Bitmap image(L"nemo.bmp");
  4396. int width = image.GetWidth();
  4397. int height = image.GetHeight();
  4398. // 目标显示区域在源图大小的基础上放大1.4倍
  4399. RectF destinationRect(
  4400. width+10, 0.0f, 1.4f* width, 1.4f* height);
  4401. //绘制源图
  4402. graphics.DrawImage(&image, 0, 0);
  4403. //在目标区域内输出位图
  4404. graphics.DrawImage(
  4405. &image,
  4406. destinationRect,
  4407. 0.f, 0.f, // 原图左上角
  4408. 0.65f*width, // 仅显示原图宽度的65%部分
  4409. 0.65f*height, // 仅显示原图高度的65%部分
  4410. UnitPixel);
  4411. }
  4412. void UsingInterpolationMode_Click()
  4413. {
  4414. Graphics &graphics=GetGraphics();
  4415. graphics.Clear(Color::White);
  4416. //装入图片
  4417. Bitmap image(L"eagle.bmp");
  4418. int width = image.GetWidth();
  4419. int height = image.GetHeight();
  4420. //绘制源图
  4421. graphics.DrawImage(
  4422. &image,
  4423. Rect(0, 0, width, height), //目标区域
  4424. 0, 0, //源图左上角坐标
  4425. width, //源图宽度
  4426. height, //源图高
  4427. UnitPixel);
  4428. //绘图平面右移
  4429. graphics.TranslateTransform( width+10,0);
  4430. //最临近插值法(低质量)
  4431. graphics.SetInterpolationMode(InterpolationModeNearestNeighbor);
  4432. graphics.DrawImage(
  4433. &image,
  4434. RectF(0.0f, 0.0f, 0.6f*width, 0.6f*height), //目标区域
  4435. 0, 0, //源图左上角坐标
  4436. width, //源图宽度
  4437. height, //源图高
  4438. UnitPixel);
  4439. //绘图平面右移
  4440. graphics.TranslateTransform( 0.6f*width+10,0);
  4441. // 高质量双线性插值法
  4442. graphics.SetInterpolationMode(InterpolationModeHighQualityBilinear);
  4443. graphics.DrawImage(
  4444. &image,
  4445. RectF(0, 0, 0.6f * width, 0.6f * height), //目标区域
  4446. 0, 0, //源图左上角坐标
  4447. width, //源图宽度
  4448. height, //源图高
  4449. UnitPixel);
  4450. //绘图平面右移
  4451. graphics.TranslateTransform(width*0.6f+10, 0.f);
  4452. // 高质量双三次插值法
  4453. graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
  4454. graphics.DrawImage(
  4455. &image,
  4456. RectF(0, 0, 0.6f * width, 0.6f * height), //目标区域
  4457. 0, 0, //源图左上角坐标
  4458. width, //源图宽度
  4459. height, //源图高
  4460. UnitPixel);
  4461. }
  4462. void RotateFlip_Click()
  4463. {
  4464. Graphics &graphics=GetGraphics();
  4465. graphics.Clear(Color::White);
  4466. //加载图片
  4467. Bitmap photo(L"nemo2.bmp");
  4468. //得到图片尺寸
  4469. int iWidth = photo.GetWidth();
  4470. int iHeight = photo.GetHeight();
  4471. //绘制原始图片
  4472. graphics.DrawImage(&photo, 10+photo.GetWidth()+2,
  4473. 10, photo.GetWidth(), photo.GetHeight());
  4474. //水平翻转图片
  4475. photo.RotateFlip(RotateNoneFlipX);
  4476. //旋转后的图片
  4477. graphics.DrawImage(&photo, 10, 10, photo.GetWidth(), photo.GetHeight());
  4478. }
  4479. void ImageSkewing_Click()
  4480. {
  4481. Graphics &graphics=GetGraphics();
  4482. graphics.Clear(Color::White);
  4483. //定义图形的目标显示区域
  4484. Point destination[]=
  4485. {
  4486. Point(200, 20), //原始图像左上角映射后的坐标
  4487. Point(110, 100), //原始图像右上角映射后的坐标
  4488. Point(250, 30) //原始图像左下角映射后的坐标
  4489. };
  4490. Bitmap image(L"Stripes.bmp");
  4491. // 绘制原始图像
  4492. graphics.DrawImage(&image, 0, 0);
  4493. // 绘制基于平行四边形映射后的图像
  4494. graphics.TranslateTransform(image.GetWidth(),0);
  4495. graphics.DrawImage(&image, destination, 3);
  4496. }
  4497. void Cubeimage_Click()
  4498. {
  4499. int WIDTH=200;
  4500. int LEFT=200;
  4501. int TOP=200;
  4502. Graphics &graphics=GetGraphics();
  4503. //使用蓝色做背景色清屏
  4504. graphics.Clear(Color::Blue);
  4505. //设置插值模式:高质量双三次插值法
  4506. graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
  4507. //分别装入张贴在立方体三面的图片
  4508. Bitmap face(L"rose.bmp");
  4509. Bitmap top(L"flower.bmp");
  4510. Bitmap right(L"yujinxiang.bmp");
  4511. //重新定义用于张贴在正面的图片坐标
  4512. Point destinationFace[] =
  4513. {
  4514. Point(LEFT,TOP),
  4515. Point(LEFT+WIDTH, TOP),
  4516. Point(LEFT, TOP+WIDTH)
  4517. };
  4518. //张贴正面图像
  4519. graphics.DrawImage(&face,destinationFace,3);
  4520. //重新定义用于张贴在顶部的图片坐标
  4521. PointF destinationTop[]=
  4522. {
  4523. PointF(LEFT+WIDTH/2, TOP-WIDTH/2),
  4524. PointF(LEFT+WIDTH/2+WIDTH, TOP-WIDTH/2),
  4525. PointF(LEFT, TOP)
  4526. };
  4527. //张贴顶部面图像
  4528. graphics.DrawImage(&top, destinationTop, 3);
  4529. //重新定义用于张贴在右侧的图片坐标
  4530. Point destinationRight[]=
  4531. {
  4532. Point(LEFT+WIDTH, TOP),
  4533. Point(LEFT+WIDTH/2+WIDTH, TOP-WIDTH/2),
  4534. Point(LEFT+WIDTH,TOP+WIDTH)
  4535. };
  4536. //张贴右侧面图像
  4537. graphics.DrawImage(&right, destinationRight, 3);
  4538. }
  4539. void ThumbnailImage_Click()
  4540. {
  4541. Graphics &graphics=GetGraphics();
  4542. graphics.Clear(Color::White);
  4543. //设置插值模式:高质量双三次插值法
  4544. graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
  4545. //加载欲查看缩略图的图片
  4546. Bitmap image(L"flower.bmp");
  4547. //获取当前窗口大小
  4548. Rect client(0,0,
  4549. MyClient.Width,MyClient.Height);
  4550. float width=image.GetWidth();
  4551. float height=image.GetHeight();
  4552. //获取指定大小的缩略图
  4553. Image *pThumbnail = image.GetThumbnailImage(40,40, NULL, NULL);
  4554. //将缩略图作为画刷
  4555. TextureBrush picBrush(pThumbnail);
  4556. //填充窗口
  4557. graphics.FillEllipse(&picBrush,client);
  4558. }
  4559. void Clone_Click()
  4560. {
  4561. Graphics &graphics=GetGraphics();
  4562. graphics.Clear(Color::White);
  4563. Bitmap image(L"head.bmp");
  4564. int Height=image.GetHeight();
  4565. int Width=image.GetWidth();
  4566. //定义将图片切分成四个部分的区域
  4567. RectF block[4]=
  4568. {
  4569. RectF(0,0,Width/2,Height/2),
  4570. RectF(Width/2,0,Width/2,Height/2),
  4571. RectF(0,Height/2,Width/2,Height/2),
  4572. RectF(Width/2,Height/2,Width/2,Height/2)
  4573. };
  4574. //分别克隆图片的四个部分
  4575. Bitmap *s[4];
  4576. s[0]=image.Clone(block[0],PixelFormatDontCare);
  4577. s[1]=image.Clone(block[1],PixelFormatDontCare);
  4578. s[2]=image.Clone(block[2],PixelFormatDontCare);
  4579. s[3]=image.Clone(block[3],PixelFormatDontCare);
  4580. //绘制图片的四个部分,各部分绘制时间间隔为1秒
  4581. graphics.DrawImage(s[0],0,0);
  4582. //延时,以达到分块显示的效果
  4583. ::Sleep(1000);
  4584. graphics.DrawImage(s[1],Width/2,0);
  4585. ::Sleep(1000);
  4586. graphics.DrawImage(s[3],Width/2,Height/2);
  4587. ::Sleep(1000);
  4588. graphics.DrawImage(s[2],0,Height/2);
  4589. delete s[0];
  4590. delete s[1];
  4591. delete s[2];
  4592. delete s[3];
  4593. }
  4594. void Picturescale_Click()
  4595. {
  4596. Graphics &graphics=GetGraphics();
  4597. graphics.Clear(Color::White);
  4598. //装入图片
  4599. Bitmap image(L"photo.bmp");
  4600. //定义图片的显示区域
  4601. Rect rect(0,0,image.GetWidth(),image.GetHeight());
  4602. graphics.DrawImage(&image,rect);
  4603. //局部缩小的区域大小为80*80
  4604. graphics.TranslateTransform(image.GetWidth()+10,0);
  4605. Rect smallrect(0,0,80,80);
  4606. //局部缩小
  4607. graphics.DrawImage(&image,smallrect,80,10,106,112,UnitPixel);
  4608. graphics.TranslateTransform(0,100);
  4609. //局部放大的区域大小为80*80
  4610. Rect largerect(0,0,80,80);
  4611. //绘制放大后的局部图像
  4612. graphics.DrawImage(&image,largerect,56,101,35,40,UnitPixel);
  4613. }
  4614. void ImageAttributesSetNoOp_Click()
  4615. {
  4616. Graphics &graphics=GetGraphics();
  4617. graphics.Clear(Color::White);
  4618. Bitmap image(L"ColorTable.bmp");
  4619. int width=image.GetWidth();
  4620. //绘制标准图片
  4621. graphics.DrawImage(&image,0,0);
  4622. graphics.TranslateTransform(image.GetWidth()+10,0);
  4623. ImageAttributes imAtt;
  4624. //构造一个红色转换到绿色的变换矩阵
  4625. ColorMatrix brushMatrix=
  4626. {
  4627. 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
  4628. 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
  4629. 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  4630. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  4631. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
  4632. };
  4633. //设置色彩校正
  4634. imAtt.SetColorMatrix(
  4635. &brushMatrix,
  4636. ColorMatrixFlagsDefault,
  4637. ColorAdjustTypeBitmap);
  4638. //使用色彩校正绘制图片
  4639. graphics.DrawImage(
  4640. &image,
  4641. Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标位置
  4642. 0, 0, image.GetWidth(), image.GetHeight(), //源位置
  4643. UnitPixel,
  4644. &imAtt);
  4645. //临时关闭色彩校正
  4646. imAtt.SetNoOp(ColorAdjustTypeBitmap);
  4647. //不使用色彩校正绘制图片红色->红色
  4648. graphics.TranslateTransform(width+10,0);
  4649. graphics.DrawImage(
  4650. &image,
  4651. Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标位置
  4652. 0, 0, image.GetWidth(), image.GetHeight(), //源位置
  4653. UnitPixel,
  4654. &imAtt);
  4655. //撤消对色彩校正的关闭
  4656. imAtt.ClearNoOp(ColorAdjustTypeBitmap);
  4657. //使用色彩校正绘制图片:红色->绿色
  4658. graphics.TranslateTransform(width+10,0);
  4659. graphics.DrawImage(
  4660. &image,
  4661. Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标位置
  4662. 0, 0, image.GetWidth(), image.GetHeight(), //源位置
  4663. UnitPixel,
  4664. &imAtt);
  4665. }
  4666. void CreateMetaFile()
  4667. {
  4668. Graphics &metagraph=GetGraphics();
  4669. //新建一个图元文件
  4670. HDC hdc =metagraph.GetHDC();
  4671. Metafile metaFile1(CWideCharString("ddd.emf"), hdc);
  4672. //使用Metafile对象的地址做为绘图平面
  4673. Graphics *graphics=Graphics::FromImage(&metaFile1);
  4674. graphics->ScaleTransform(0.8f,0.8f);
  4675. //在沿水平方向输出三个椭圆并填充
  4676. graphics->SetSmoothingMode(SmoothingModeHighQuality);
  4677. //红色椭圆
  4678. graphics->DrawEllipse(&Pen(Color.Red,10), Rect(0, 0, 75, 95));
  4679. graphics->FillEllipse(&SolidBrush(Color::Red), Rect(0, 0, 75, 95));
  4680. //绿色椭圆
  4681. graphics->DrawEllipse(&Pen(Color::Green,10), Rect(40, 0, 75, 95));
  4682. graphics->FillEllipse(&SolidBrush(Color::Green), Rect(40, 0, 75, 95));
  4683. //蓝色椭圆
  4684. graphics->DrawEllipse(&Pen(Color.Blue,10), Rect(80, 0, 75, 95));
  4685. graphics->FillEllipse(&SolidBrush(Color::Blue), Rect(80, 0, 75, 95));
  4686. // 追加三种色彩的文本
  4687. FontFamily fontFamily(CWideCharString("Arial"));
  4688. Font font(&fontFamily, 24, FontStyleRegular, UnitPixel);
  4689. graphics->SetTextRenderingHint(TextRenderingHintAntiAlias);
  4690. //红色文字
  4691. MyDrawString(*graphics, "GDI+",font, SolidBrush(Color::Red), PointF(-80.0f, 0.0f));
  4692. //绿色文字
  4693. MyDrawString(*graphics, "GDI+",font, SolidBrush(Color::Green), PointF(-80.0f, font.GetHeight(graphics)));
  4694. //蓝色文字
  4695. MyDrawString(*graphics, "GDI+",font, SolidBrush(Color::Blue), PointF(-80.0f, font.GetHeight(graphics)*2));
  4696. //释放所有资源。
  4697. //graphics->Dispose();
  4698. //metaFile1.Dispose();
  4699. metagraph.ReleaseHDC(hdc);
  4700. //metagraph.Dispose();
  4701. delete graphics;
  4702. }
  4703. void SetColorMatrices_Click()
  4704. {
  4705. Graphics &graphics=GetGraphics();
  4706. graphics.Clear(Color::White);
  4707. //加载图元文件
  4708. Metafile image(L"ddd.emf");
  4709. Unit unit=UnitPixel;
  4710. //获取图片区间
  4711. RectF rect; image.GetBounds(&rect, &unit);
  4712. //不使用任何色彩校正输出图片
  4713. graphics.DrawImage(&image, 0.0f, 0.0f, rect.Width, rect.Height);
  4714. ImageAttributes imAtt;
  4715. //定义一个使红色分量递增1.5的矩阵
  4716. ColorMatrix defaultColorMatrix=
  4717. {
  4718. 1.5f, 0.0f, 0.0f, 0.0f, 0.0f,
  4719. 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
  4720. 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  4721. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  4722. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
  4723. };
  4724. //定义一个使绿色分量递增1.5的矩阵
  4725. ColorMatrix defaultGrayMatrix=
  4726. {
  4727. 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  4728. 0.0f, 1.5f, 0.0f, 0.0f, 0.0f,
  4729. 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  4730. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  4731. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
  4732. };
  4733. //画笔的彩色色彩信息较校正矩阵:蓝色分量递增1.5的矩阵
  4734. ColorMatrix penColorMatrix=
  4735. {
  4736. 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  4737. 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
  4738. 0.0f, 0.0f, 1.5f, 0.0f, 0.0f,
  4739. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  4740. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
  4741. };
  4742. //画笔的灰度色矩阵:所有分量递增1.5的矩阵
  4743. ColorMatrix penGrayMatrix=
  4744. {
  4745. 1.5f, 0.0f, 0.0f, 0.0f, 0.0f,
  4746. 0.0f, 1.5f, 0.0f, 0.0f, 0.0f,
  4747. 0.0f, 0.0f, 1.5f, 0.0f, 0.0f,
  4748. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  4749. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
  4750. };
  4751. // 设置默认的彩色及灰度校正矩阵.
  4752. //ColorAdjustTypeDefault:修改所有的色彩信息
  4753. imAtt.SetColorMatrices(
  4754. &defaultColorMatrix,
  4755. &defaultGrayMatrix,
  4756. ColorMatrixFlagsAltGray,
  4757. ColorAdjustTypeDefault);
  4758. //使用校正矩阵绘制图元文件:校正所有的色彩
  4759. graphics.TranslateTransform(image.GetWidth()+10,0);
  4760. graphics.DrawImage(
  4761. &image,
  4762. Rect(0, 0, (int)rect.Width,(int)rect.Height),
  4763. rect.X, rect.Y,
  4764. rect.Width,
  4765. rect.Height,
  4766. UnitPixel,
  4767. &imAtt);
  4768. //设置画笔的彩色及灰度色彩校正矩阵
  4769. //ColorAdjustTypePen:修正画笔色彩
  4770. imAtt.SetColorMatrices(
  4771. &penColorMatrix,
  4772. &penGrayMatrix,
  4773. ColorMatrixFlagsAltGray,
  4774. ColorAdjustTypePen);
  4775. //在第二行绘制
  4776. graphics.ResetTransform();
  4777. graphics.TranslateTransform(0,image.GetHeight());
  4778. //使用修正后的画笔绘制图片
  4779. graphics.DrawImage(
  4780. &image,
  4781. Rect(0, 0, (int)rect.Width,(int)rect.Height),
  4782. rect.X, rect.Y,
  4783. rect.Width,
  4784. rect.Height,
  4785. UnitPixel,
  4786. &imAtt);
  4787. graphics.TranslateTransform(image.GetWidth()+10,0);
  4788. //清除在画笔上的所有变换
  4789. imAtt.ClearColorMatrix(ColorAdjustTypePen);
  4790. graphics.DrawImage(
  4791. &image,
  4792. Rect(0, 0, (int)rect.Width,(int)rect.Height),
  4793. rect.X, rect.Y,
  4794. rect.Width,
  4795. rect.Height,
  4796. UnitPixel,
  4797. &imAtt);
  4798. }
  4799. void SetOutputChannelColorProfile_Click()
  4800. {
  4801. Graphics &graphics=GetGraphics();
  4802. graphics.Clear(Color::White);
  4803. Bitmap image(L"car.bmp");
  4804. ImageAttributes imAtt;
  4805. int width = image.GetWidth();
  4806. int height = image.GetHeight();
  4807. Rect rect(0, 0, width, height);
  4808. //绘制原始图片
  4809. graphics.DrawImage(&image, rect);
  4810. graphics.TranslateTransform(width,0);
  4811. //设置色彩配置文件
  4812. imAtt.SetOutputChannelColorProfile(
  4813. CWideCharString("kodak_dc.ICM"), ColorAdjustTypeBitmap);
  4814. //使用色彩配置文件输出图片
  4815. graphics.DrawImage(
  4816. &image,
  4817. rect,
  4818. 0, 0, width, height,
  4819. UnitPixel,
  4820. &imAtt);
  4821. }
  4822. void Gammaadjust_Click()
  4823. {
  4824. Graphics &graphics=GetGraphics();
  4825. graphics.Clear(Color::White);
  4826. //装入原始图片
  4827. Bitmap image(L"warrior.bmp");
  4828. int width = image.GetWidth();
  4829. int height = image.GetHeight();
  4830. // 设置字体信息
  4831. Font myFont(L"宋体", 12);
  4832. //设置提示信息的显示区域
  4833. PointF origin(width+10, height+20);
  4834. SolidBrush blackBrush(Color::Black);
  4835. ImageAttributes imAtt;
  4836. CString msg;
  4837. //从0-3依次调整输出图片时所使用的Gamma值
  4838. for(float i=0.0f;i<3.0f;i+=0.1f)
  4839. {
  4840. //绘制原始图片
  4841. graphics.DrawImage(&image,0,0);
  4842. //设置Gamma值
  4843. imAtt.SetGamma(i,ColorAdjustTypeBitmap);
  4844. //使用修改后Gamma值进行图片输出
  4845. graphics.DrawImage(
  4846. &image,
  4847. Rect(width+10, 0, width, height), //目标区域
  4848. 0, 0, width, height, //源区域
  4849. UnitPixel,
  4850. &imAtt);
  4851. msg.Format("正在修改Gamma值,Gamma=%.2f",i);
  4852. //显示当前的Gamma值信息
  4853. MyDrawString(graphics, msg,myFont,blackBrush,origin);
  4854. //延时一秒以便观看效果
  4855. ::Sleep(100);
  4856. graphics.Clear(Color::White);
  4857. imAtt.ClearGamma();
  4858. }
  4859. }
  4860. void SetOutputChannel_Click()
  4861. {
  4862. Graphics &graphics=GetGraphics();
  4863. graphics.Clear(Color::White);
  4864. //装入图片
  4865. Bitmap image(L"jieba.bmp");
  4866. //图片的高度
  4867. int width = image.GetWidth();
  4868. int height = image.GetHeight();
  4869. //绘制原始图片
  4870. graphics.DrawImage(&image, RectF(0, 0, width, height));
  4871. ImageAttributes imAtt;
  4872. //设置色彩输出通道cyan
  4873. imAtt.SetOutputChannel(ColorChannelFlagsC,
  4874. ColorAdjustTypeBitmap);
  4875. //右移,绘制图片
  4876. graphics.TranslateTransform(width, 0);
  4877. graphics.DrawImage(
  4878. &image,
  4879. Rect(0, 0, width, height),
  4880. 0, 0, width, height,
  4881. UnitPixel,
  4882. &imAtt);
  4883. //设置色彩输出通道:magenta
  4884. imAtt.SetOutputChannel(ColorChannelFlagsM,
  4885. ColorAdjustTypeBitmap);
  4886. //右移,绘制图片
  4887. graphics.TranslateTransform(width, 0);
  4888. graphics.DrawImage(
  4889. &image,
  4890. Rect(0, 0, width, height),
  4891. 0, 0, width, height,
  4892. UnitPixel,
  4893. &imAtt);
  4894. //设置色彩输出通道:yellow
  4895. imAtt.SetOutputChannel(ColorChannelFlagsY,
  4896. ColorAdjustTypeBitmap);
  4897. //右移,绘制图片
  4898. graphics.TranslateTransform(width, 0);
  4899. graphics.DrawImage(
  4900. &image,
  4901. Rect(0, 0, width, height),
  4902. 0, 0, width, height,
  4903. UnitPixel,
  4904. &imAtt);
  4905. //设置色彩输出通道:black
  4906. imAtt.SetOutputChannel(ColorChannelFlagsK,
  4907. ColorAdjustTypeBitmap);
  4908. //右移,绘制图片
  4909. graphics.TranslateTransform(width, 0);
  4910. graphics.DrawImage(
  4911. &image,
  4912. Rect(0, 0, width, height),
  4913. 0, 0, width, height,
  4914. UnitPixel,
  4915. &imAtt);
  4916. }
  4917. void Colorkey_Click()
  4918. {
  4919. Graphics &graphics=GetGraphics();
  4920. graphics.Clear(Color::White);
  4921. //装入前后背景图片
  4922. Bitmap forground(L"grid.bmp");
  4923. Bitmap background(L"snike.bmp");
  4924. int width=background.GetWidth();
  4925. int height=background.GetHeight();
  4926. Rect rect(0,0,width,height);
  4927. //将红色设置成关键色
  4928. ImageAttributes imAtt;
  4929. imAtt.SetColorKey(
  4930. Color::Red,
  4931. Color::Red,
  4932. ColorAdjustTypeBitmap);
  4933. //绘制背景
  4934. graphics.DrawImage(&background,0,0);
  4935. //绘制前景
  4936. graphics.DrawImage(
  4937. &forground,
  4938. rect,
  4939. 0, 0, forground.GetWidth(), forground.GetHeight(),
  4940. UnitPixel,
  4941. &imAtt);
  4942. graphics.TranslateTransform(width+20,0);
  4943. graphics.DrawImage(&background,0,0);
  4944. //清除已经应用的关键色信息
  4945. imAtt.ClearColorKey(ColorAdjustTypeBitmap);
  4946. ///将蓝色设置成关键色
  4947. imAtt.SetColorKey(
  4948. Color::Blue,
  4949. Color::Blue,
  4950. ColorAdjustTypeBitmap);
  4951. graphics.DrawImage(&forground,
  4952. rect,
  4953. 0, 0, forground.GetWidth(), forground.GetHeight(),
  4954. UnitPixel,
  4955. &imAtt);
  4956. graphics.TranslateTransform(width+20,0);
  4957. //绘制源图
  4958. graphics.DrawImage(&background,0,0);
  4959. }
  4960. void Setthreshold_Click()
  4961. {
  4962. Graphics &graphics=GetGraphics();
  4963. graphics.Clear(Color::White);
  4964. Bitmap image(L"box-2.bmp");
  4965. int Width=image.GetWidth();
  4966. int Height=image.GetHeight();
  4967. //绘制原始图片
  4968. graphics.DrawImage(&image, 10, 10, Width, Height);
  4969. //将阈值从0到1依次运用
  4970. ImageAttributes imAtt;
  4971. for(float i=0.0f;i<1.0f;i+=0.1f)
  4972. {
  4973. //设置输出图片时使用的阈值
  4974. imAtt.SetThreshold(i, ColorAdjustTypeBitmap);
  4975. //绘制已经使用了阈值的图片
  4976. graphics.DrawImage(&image,
  4977. Rect(10+Width, 10, Width, Height),
  4978. 0, 0, Width, Height,
  4979. UnitPixel,
  4980. &imAtt);
  4981. //延时
  4982. ::Sleep(1000);
  4983. }
  4984. }
  4985. void AdjustedPalette_Click()
  4986. {
  4987. Graphics &graphics=GetGraphics();
  4988. graphics.Clear(Color::White);
  4989. graphics.ScaleTransform(0.7f,0.7f);
  4990. //加载图片
  4991. Bitmap image(L"lord-256.bmp");
  4992. //复制图片
  4993. Bitmap image2(L"lord-256.bmp");
  4994. //获取图片使用的调色板信息
  4995. int size=image.GetPaletteSize();
  4996. ColorPalette *palette= (ColorPalette *)new BYTE[size];
  4997. image.GetPalette(palette, size);
  4998. //获取调色板所包含的色彩总数
  4999. int count=palette->Count;
  5000. if(count<1)
  5001. {
  5002. AfxMessageBox("图片无调色板信息可用");
  5003. return;
  5004. }
  5005. //更改调色板中的每一种色彩信息
  5006. for(int i=0;i<count;i++)
  5007. {
  5008. Color u(palette->Entries[i]);
  5009. int r=u.GetR()/2;
  5010. int g=u.GetG()/2;
  5011. int b=u.GetB()/2;
  5012. if(r<1)
  5013. r=0;
  5014. if(g<1)
  5015. g=0;
  5016. if(b<1)
  5017. b=0;
  5018. palette->Entries[i]=u.MakeARGB(255, r,g,b);
  5019. }
  5020. //设置图像的新调色板
  5021. image.SetPalette(palette);
  5022. //绘制原图
  5023. graphics.DrawImage(&image2,0,0);
  5024. //绘制修改后的图片
  5025. graphics.DrawImage(&image,image.GetWidth()+10,0);
  5026. delete palette;
  5027. }
  5028. void SetWrapMode_Click()
  5029. {
  5030. Graphics &graphics=GetGraphics();
  5031. graphics.Clear(Color::White);
  5032. //加载图片
  5033. Bitmap image(L"yueru.bmp");
  5034. ImageAttributes imAtt;
  5035. //设置图片排列方式为WrapModeClamp:图片不进行平铺
  5036. imAtt.SetWrapMode(WrapModeClamp,Color::Red);
  5037. //缩小显示源图
  5038. graphics.DrawImage(&image,
  5039. Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标区域
  5040. 0, 0, 2*image.GetWidth(), 2*image.GetHeight(), //源图片区域
  5041. UnitPixel,
  5042. &imAtt);
  5043. graphics.TranslateTransform(image.GetWidth()+10,0);
  5044. //设置图片排列方式为WrapModeTileFlipXY:图片在水平和垂直方向上同时翻转
  5045. imAtt.SetWrapMode(WrapModeTileFlipXY);
  5046. graphics.DrawImage(&image,
  5047. Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标区域
  5048. 0, 0, 2*image.GetWidth(), 2*image.GetHeight(), //源图片区域
  5049. UnitPixel,
  5050. &imAtt);
  5051. graphics.TranslateTransform(image.GetWidth()+10,0);
  5052. //设置图片排列方式为WrapModeTileFlipX:图片在水平上翻转
  5053. imAtt.SetWrapMode(WrapModeTileFlipX);
  5054. graphics.DrawImage(&image,
  5055. Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标区域
  5056. 0, 0, 2*image.GetWidth(), 2*image.GetHeight(), //源图片区域
  5057. UnitPixel,
  5058. &imAtt);
  5059. graphics.TranslateTransform(image.GetWidth()+10,0);
  5060. //设置图片排列方式为WrapModeTileFlipY:图片在垂直上翻转
  5061. imAtt.SetWrapMode(WrapModeTileFlipY);
  5062. graphics.DrawImage(&image,
  5063. Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标区域
  5064. 0, 0, 2*image.GetWidth(), 2*image.GetHeight(), //源图片区域
  5065. UnitPixel,
  5066. &imAtt);
  5067. }
  5068. void ListAllImageEncoders_Click()
  5069. {
  5070. Graphics &graphics=GetGraphics();
  5071. graphics.Clear(Color::White);
  5072. SolidBrush brush(Color::Blue);
  5073. FontFamily fontFamily(L"华文楷体");
  5074. Font myFont(&fontFamily, 20, FontStyleRegular, UnitPixel);
  5075. //获取编码器信息
  5076. UINT num, size;
  5077. GetImageEncodersSize(&num, &size);
  5078. ImageCodecInfo *pImageCodecInfo = (ImageCodecInfo*)new BYTE[size];
  5079. GetImageEncoders(num, size, pImageCodecInfo);
  5080. //输出编码信息
  5081. CString msg;
  5082. for(UINT j = 0; j <num; ++j)
  5083. {
  5084. CString s;
  5085. s.Format("编码器名称:%s\t文件格式扩展名:%s\t\n",
  5086. CString(pImageCodecInfo[j].CodecName),
  5087. CString(pImageCodecInfo[j].FilenameExtension));
  5088. msg+=s;
  5089. }
  5090. MyDrawString(graphics, msg,myFont,brush, PointF(0,0));
  5091. delete pImageCodecInfo;
  5092. }
  5093. //帮助函数
  5094. CString MyStringFromGUID2(const GUID &rguid)
  5095. {
  5096. WCHAR strGuid[39];
  5097. StringFromGUID2(rguid, strGuid, 39);
  5098. return CString(strGuid);
  5099. }
  5100. void ListImageEncoder_Detail_Click()
  5101. {
  5102. Graphics &graphics=GetGraphics();
  5103. graphics.Clear(Color::White);
  5104. //获取编码器信息
  5105. UINT num, size;
  5106. GetImageEncodersSize(&num, &size);
  5107. ImageCodecInfo *pImageCodecInfo = (ImageCodecInfo*) new BYTE[size];
  5108. GetImageEncoders(num, size, pImageCodecInfo);
  5109. //查找指定格式文件的编码器信息
  5110. CString msg;
  5111. //查询所有的编码器信息
  5112. for(UINT j = 0; j <num; j++)
  5113. {
  5114. CString s;
  5115. s.Format("开始描述第%d种图形编码信息\n\n",j); msg+=s;
  5116. s.Format("编码标识: %s\n",MyStringFromGUID2(pImageCodecInfo[j].Clsid)); msg+=s;
  5117. s.Format("文件格式标识: %s\n", MyStringFromGUID2(pImageCodecInfo[j].FormatID)); msg+=s;
  5118. s.Format("编码器名称: %s\n", CString(pImageCodecInfo[j].CodecName)); msg+=s;
  5119. s.Format("编码器依存的动态连接库名: %s\n",CString(pImageCodecInfo[j].DllName)); msg+=s;
  5120. s.Format("编码描述: %s\n", CString(pImageCodecInfo[j].FormatDescription)); msg+=s;
  5121. s.Format("编码器对应的文件扩展名: %s\n",CString(pImageCodecInfo[j].FilenameExtension)); msg+=s;
  5122. s.Format("编码器的MIME类型描述: %s\n",CString(pImageCodecInfo[j].MimeType)); msg+=s;
  5123. s.Format("ImageCodecFlags枚举的标记集: %d\n",pImageCodecInfo[j].Flags); msg+=s;
  5124. s.Format("编码器版本: %d\n",pImageCodecInfo[j].Version); msg+=s;
  5125. int sigCount = pImageCodecInfo[j].SigCount;
  5126. s.Format("与编码器对应的编码器签名的数组大小:%d\n", sigCount); msg+=s;
  5127. s.Format("第%d种图形编码信息描述完毕\n\n",j); msg+=s;
  5128. AfxMessageBox(msg);
  5129. msg.Empty();
  5130. }
  5131. delete pImageCodecInfo;
  5132. //将编码器的详细信息写入文件
  5133. //StreamWriter sw= StreamWriter(@"listinfo.txt",false,System.Text.Encoding.Unicode);
  5134. //sw.Write(msg);
  5135. //sw.Close();
  5136. //AfxMessageBox("操作结束,请打开当前目录下的listinfo.txt查看编码器消息");
  5137. }
  5138. void ListImageDecoder_Click()
  5139. {
  5140. Graphics &graphics=GetGraphics();
  5141. graphics.Clear(Color::White);
  5142. SolidBrush brush(Color::Blue);
  5143. FontFamily fontFamily(L"华文楷体");
  5144. Font myFont(&fontFamily, 20, FontStyleRegular, UnitPixel);
  5145. //获取编码器信息
  5146. UINT num, size;
  5147. GetImageDecodersSize(&num, &size);
  5148. ImageCodecInfo *pImageCodecInfo = (ImageCodecInfo*) new BYTE[size];
  5149. GetImageDecoders(num, size, pImageCodecInfo);
  5150. //输出每一个解码器的详细信息
  5151. CString msg;
  5152. for(UINT j = 0; j <num; j++)
  5153. {
  5154. CString s;
  5155. s.Format("解码器名称:%s\t文件格式扩展名:%s\t\n",
  5156. CString(pImageCodecInfo[j].CodecName),CString(pImageCodecInfo[j].FilenameExtension));
  5157. msg+=s;
  5158. }
  5159. delete pImageCodecInfo;
  5160. //显示信息
  5161. MyDrawString(graphics, msg,myFont,brush, PointF(0,0));
  5162. }
  5163. GUID GetEncoderClsid(CString format)
  5164. {
  5165. GUID picGUID;
  5166. //获取编码器信息
  5167. UINT num=0, size=0;
  5168. GetImageEncodersSize(&num, &size);
  5169. ImageCodecInfo *pImageCodecInfo = (ImageCodecInfo*) new BYTE[size];
  5170. GetImageEncoders(num, size, pImageCodecInfo);
  5171. //查找指定格式文件的编码器信息
  5172. CWideCharString formatW(format);
  5173. for(UINT i = 0; i <num; i++)
  5174. { //MimeType:编码方式的具体描述
  5175. if(formatW == CWideCharString(pImageCodecInfo[i].MimeType))
  5176. {
  5177. picGUID= pImageCodecInfo[i].Clsid;
  5178. break;
  5179. }
  5180. }
  5181. delete pImageCodecInfo;
  5182. return picGUID;
  5183. }
  5184. void GetEncoderParameter_Click()
  5185. {
  5186. Graphics &graphics=GetGraphics();
  5187. graphics.Clear(Color::White);
  5188. SolidBrush brush(Color::Blue);
  5189. FontFamily fontFamily(L"宋体");
  5190. Font myFont(&fontFamily, 16, FontStyleRegular, UnitPixel);
  5191. CString msg;
  5192. // 为了查询将位图保存为JPEG格式的图片,新建一个位图
  5193. Bitmap bitmap(1,1);
  5194. //获取JPEG格式的图像编码器的标识
  5195. GUID encoderClsid;
  5196. encoderClsid=GetEncoderClsid(L"image/jpeg");
  5197. // 获取转换成JPG所需要的具体参数信息
  5198. UINT size=bitmap.GetEncoderParameterListSize(&encoderClsid);
  5199. EncoderParameters *pEncoderParameters= (EncoderParameters *)new BYTE[size];
  5200. bitmap.GetEncoderParameterList(&encoderClsid, size, pEncoderParameters);
  5201. // 查看pEncoderParameters对象中多少个EncoderParameter类
  5202. int count=pEncoderParameters->Count;
  5203. msg.Format("在编码参数数组中有%d个EncoderParameter类\n每个类的详细信息为:\n", count);
  5204. EncoderParameter *pEncoderParameter=pEncoderParameters->Parameter;
  5205. /*分别查看EncoderParameters对象中的
  5206. 每一个EncoderParameter对象的成员变量
  5207. GUID、NumberOfValues、Type*/
  5208. for(int i=0;i<count;i++)
  5209. {
  5210. CString s;
  5211. //将GUID值转换成字串
  5212. s.Format("所需设置的第%d个参数种类(GUID):%s\n", i, MyStringFromGUID2(pEncoderParameter[i].Guid));
  5213. msg+=s;
  5214. //查看在每一个参数种类下,可以设置的参数信息
  5215. s.Format("\t在该参数参数种类下,你可以进行的设置的参数分别如下\n");
  5216. msg+=s;
  5217. s.Format("\t\t变量总数=%d\n", pEncoderParameter[i].NumberOfValues);
  5218. msg+=s;
  5219. s.Format("\t\t参数类型=%d\n", pEncoderParameter[i].Type);
  5220. msg+=s;
  5221. }
  5222. //输出编码参数列表信息
  5223. MyDrawString(graphics, msg,myFont,brush, PointF(0,0));
  5224. delete pEncoderParameters;
  5225. }
  5226. HRESULT EncoderParameterCategoryFromGUID(GUID guid, WCHAR* category, UINT maxChars);
  5227. CString _EncoderParameterCategoryFromGUID(GUID guid)
  5228. {
  5229. CONST MAX_CATEGORY_LENGTH = 50;
  5230. WCHAR strParameterCategory[MAX_CATEGORY_LENGTH] = L"";
  5231. if (Ok==EncoderParameterCategoryFromGUID(guid, strParameterCategory, MAX_CATEGORY_LENGTH))
  5232. return CString(strParameterCategory);
  5233. return _T("失败");
  5234. }
  5235. CString ShowAllEncoderParameters(CString format)
  5236. {
  5237. CString outmsg;
  5238. //以位图为例,查看pImageCodecInfo的详细信息
  5239. Bitmap bitmap(L"head.bmp");
  5240. GUID encoderClsid=GetEncoderClsid(format);
  5241. UINT size=bitmap.GetEncoderParameterListSize(&encoderClsid);
  5242. if (0==size)
  5243. {
  5244. return _T("错误");
  5245. }
  5246. EncoderParameters *encodersarameters= (EncoderParameters *)new BYTE[size];
  5247. bitmap.GetEncoderParameterList(&encoderClsid, size, encodersarameters);
  5248. // 获取编码所需的参数列表
  5249. //获取EncoderParameter对象总数
  5250. int count=encodersarameters->Count;
  5251. outmsg.Format("在EncoderParameters中,有%d个 EncoderParameter对象。\n",
  5252. count);
  5253. EncoderParameter *pEncoderParameter=encodersarameters->Parameter;
  5254. // 查看每一个EncoderParameter对象信息
  5255. for(int k = 0; k <count;++k)
  5256. {
  5257. //还原GUID信息
  5258. CString strParameterCategory=_EncoderParameterCategoryFromGUID(
  5259. pEncoderParameter[k].Guid);
  5260. CString s;
  5261. s.Format("\t参数种类: %s.\n", strParameterCategory); outmsg+=s;
  5262. s.Format("\t该参数的属性值一共有 %d个\n",
  5263. pEncoderParameter[k].NumberOfValues); outmsg+=s;
  5264. s.Format("\t数据类型%d.\n", pEncoderParameter[k].Type); outmsg+=s;
  5265. }
  5266. delete encodersarameters;
  5267. //将所有信息导出outmsg
  5268. return outmsg;
  5269. }
  5270. void GetAllEncoderParameter_Click()
  5271. {
  5272. Graphics &graphics=GetGraphics();
  5273. graphics.Clear(Color::White);
  5274. //设置输出信息时使用的字体、画刷
  5275. SolidBrush brush(Color::Blue);
  5276. FontFamily fontFamily(L"宋体");
  5277. Font myFont(&fontFamily, 16, FontStyleRegular,UnitPixel);
  5278. //将参数列表详细信息保存到msg之中
  5279. CString msg=ShowAllEncoderParameters(L"image/jpeg");
  5280. //显示参数列表信息
  5281. MyDrawString(graphics, msg, myFont, SolidBrush(Color::Black), PointF(0,0));
  5282. }
  5283. void menuItem17_Click()
  5284. {
  5285. Graphics &graphics=GetGraphics();
  5286. graphics.Clear(Color::White);
  5287. Bitmap image(L"snike.bmp");
  5288. RectF rect(0,0,image.GetWidth()/2,image.GetHeight()/2);
  5289. graphics.DrawImage(&image,rect);
  5290. //将BMP保存为PNG文件,不使用编码参数
  5291. GUID pngGuid = GetEncoderClsid("image/png");
  5292. MyImageSave(image, L"snike.png", &pngGuid, NULL);
  5293. //使用第二种方法设置encoder参数
  5294. MyImageSave(image,L"snike2.png", &pngGuid);
  5295. //分别打开两种方法保存的图像
  5296. Bitmap image_png1(L"snike.png");
  5297. Bitmap image_png2(L"snike2.png");
  5298. //绘制PNG格式的图片
  5299. graphics.TranslateTransform(rect.Width,0);
  5300. graphics.DrawImage(&image_png1,rect);
  5301. graphics.TranslateTransform(rect.Width,0);
  5302. graphics.DrawImage(&image_png2,rect);
  5303. }
  5304. void SaveBmp2tif_Click()
  5305. {
  5306. Graphics &graphics=GetGraphics();
  5307. graphics.Clear(Color::White);
  5308. Bitmap myBitmap(L"jieba.bmp");
  5309. // 获取TIFF格式文件的编码信息
  5310. GUID tiffGuid = GetEncoderClsid("image/tiff");
  5311. EncoderParameters params; //其中已经包含了一个 EncoderParameter
  5312. params.Count=1;
  5313. // 使用LZW压缩方式将图图存为 TIFF文件
  5314. params.Parameter[0].Guid= EncoderCompression;
  5315. params.Parameter[0].NumberOfValues=1;
  5316. params.Parameter[0].Type=EncoderParameterValueTypeLong;
  5317. params.Parameter[0].Value=new long[1];
  5318. ((long*)params.Parameter[0].Value)[0] = (long)EncoderValueCompressionLZW;
  5319. MyImageSave(myBitmap,L"jieba.tif", &tiffGuid, &params);
  5320. delete params.Parameter[0].Value;
  5321. }
  5322. void SaveBMP2JPG_Click()
  5323. {
  5324. Graphics &graphics=GetGraphics();
  5325. graphics.Clear(Color::White);
  5326. //打开BMP文件
  5327. Bitmap myBitmap(L"car.bmp");
  5328. //获取显示图片所需要区域
  5329. Rect imgrect(0,0, myBitmap.GetWidth(),myBitmap.GetHeight());
  5330. graphics.DrawImage(&myBitmap, imgrect);
  5331. //获取JPEG格式的编码方式
  5332. GUID jpegGuid= GetEncoderClsid("image/jpeg");
  5333. //分别设置JPEG文件的图片质量
  5334. //编码参数种类为 Quality,
  5335. EncoderParameters params; //其中已经包含了一个 EncoderParameter
  5336. params.Count=1;
  5337. params.Parameter[0].Guid= EncoderQuality;
  5338. params.Parameter[0].NumberOfValues=1;
  5339. params.Parameter[0].Type=EncoderParameterValueTypeLong;
  5340. params.Parameter[0].Value=new long[1];
  5341. //设置JPEG图片质量为25级
  5342. ((long*)params.Parameter[0].Value)[0] = 25L;
  5343. MyImageSave(myBitmap,L"car025.jpg", &jpegGuid, &params);
  5344. //设置JPEG图片质量为50级
  5345. ((long*)params.Parameter[0].Value)[0] = 50L;
  5346. MyImageSave(myBitmap,L"car050.jpg", &jpegGuid, &params);
  5347. //设置JPEG图片质量为75级
  5348. ((long*)params.Parameter[0].Value)[0] = 75L;
  5349. MyImageSave(myBitmap,L"car075.jpg", &jpegGuid, &params);
  5350. delete params.Parameter[0].Value;
  5351. //分别显示不同图片质量的JPEG文件
  5352. Bitmap image01(L"car025.jpg");
  5353. //绘图平面右移
  5354. graphics.TranslateTransform(imgrect.Width+10,0);
  5355. graphics.DrawImage(&image01,imgrect);
  5356. Bitmap image02(L"car050.jpg");
  5357. //重置绘图平面,下移
  5358. graphics.ResetTransform();
  5359. graphics.TranslateTransform(0,imgrect.Height+10);
  5360. graphics.DrawImage(&image02,imgrect);
  5361. Bitmap image05(L"car075.jpg");
  5362. graphics.TranslateTransform(imgrect.Width+10,0);
  5363. graphics.DrawImage(&image02,imgrect);
  5364. }
  5365. void TransformingJPEG_Click()
  5366. {
  5367. Graphics &graphics=GetGraphics();
  5368. graphics.Clear(Color::White);
  5369. //打开JPEG文件
  5370. Bitmap myBitmap(L"car.jpg");
  5371. //获取显示图片所需要区域
  5372. Rect imgrect(0,0, myBitmap.GetWidth(), myBitmap.GetHeight());
  5373. //绘制原图
  5374. graphics.DrawImage(&myBitmap,imgrect);
  5375. //获取JPEG格式的编码方式
  5376. GUID jpegGuid= GetEncoderClsid("image/jpeg");
  5377. //分别设置JPEG文件的位置变换信息
  5378. //编码参数种类为 Transformation
  5379. EncoderParameters params; //其中已经包含了一个 EncoderParameter
  5380. params.Count=1;
  5381. params.Parameter[0].Guid= EncoderTransformation;
  5382. params.Parameter[0].NumberOfValues=1;
  5383. params.Parameter[0].Type=EncoderParameterValueTypeLong;
  5384. params.Parameter[0].Value=new long[1];
  5385. ((long*)params.Parameter[0].Value)[0] = (long)EncoderValueTransformRotate270;
  5386. // 将图片旋转90度后保存
  5387. MyImageSave(myBitmap,L"car0_rotate.jpg", &jpegGuid, &params);
  5388. delete params.Parameter[0].Value;
  5389. //绘制旋转后的图片
  5390. graphics.TranslateTransform(imgrect.Width,0);
  5391. Bitmap myBitmap2(L"car0_rotate.jpg");
  5392. //获取显示图片所需要区域
  5393. Rect imgrect2(0,0, myBitmap2.GetWidth(), myBitmap2.GetHeight());
  5394. graphics.DrawImage(&myBitmap2, imgrect2);
  5395. }
  5396. void MultipleFrameImage_Click()
  5397. {
  5398. //装入四张不同格式的图片
  5399. Bitmap multi(L"dog.bmp");
  5400. Bitmap page2(L"dog.gif");
  5401. Bitmap page3(L"cute.jpg");
  5402. Bitmap page4(L"cat.png");
  5403. //获取tiff图像格式的编码信息
  5404. GUID tiffGuid = GetEncoderClsid("image/tiff");
  5405. EncoderParameters params; //其中已经包含了一个 EncoderParameter
  5406. params.Count=1;
  5407. //参数类型:SaveFlag
  5408. params.Parameter[0].Guid= EncoderSaveFlag;
  5409. params.Parameter[0].NumberOfValues=1;
  5410. params.Parameter[0].Type=EncoderParameterValueTypeLong;
  5411. params.Parameter[0].Value=new long[1];
  5412. ((long*)params.Parameter[0].Value)[0] = (long)EncoderValueMultiFrame;
  5413. //保存第一张图片
  5414. MyImageSave(multi,L"Multiframe.tiff", &tiffGuid, &params);
  5415. //保存第二张图片
  5416. ((long*)params.Parameter[0].Value)[0] = (long)EncoderValueFrameDimensionPage;
  5417. multi.SaveAdd(&page2, &params);
  5418. //保存第三张图片
  5419. multi.SaveAdd(&page3, &params);
  5420. //保存第四张图片
  5421. multi.SaveAdd(&page4, &params);
  5422. // 关闭TIFF文件
  5423. ((long*)params.Parameter[0].Value)[0] = (long)EncoderValueFlush;
  5424. multi.SaveAdd(&params);
  5425. delete params.Parameter[0].Value;
  5426. AfxMessageBox("操作结束,请打开当前目录下的Multiframe.tiff查看图片添加结果");
  5427. }
  5428. void GetImageFromMultyFrame_Click()
  5429. {
  5430. Graphics &graphics=GetGraphics();
  5431. graphics.Clear(Color::White);
  5432. //将窗口切分成四个部分,用以显示四幅帧图片
  5433. Rect ClientRect(0,0,
  5434. MyClient.Width,MyClient.Height);
  5435. Rect r1(0,0,
  5436. ClientRect.Width/2,ClientRect.Height/2);
  5437. Rect r2(ClientRect.Width/2,0,
  5438. ClientRect.Width/2,ClientRect.Height/2);
  5439. Rect r3(0,
  5440. ClientRect.Height/2,ClientRect.Width/2,ClientRect.Height/2);
  5441. Rect r4(ClientRect.Width/2,
  5442. ClientRect.Height/2,ClientRect.Width/2,ClientRect.Height/2);
  5443. //打开TIF文件
  5444. Bitmap multi(L"Multiframe.tiff");
  5445. //FrameDimension.Page:子帧图片
  5446. GUID pageGuid = FrameDimensionPage;
  5447. GUID pngGuid = GetEncoderClsid("image/png");
  5448. //显示并保存第一帧图片
  5449. multi.SelectActiveFrame(&pageGuid, 0);
  5450. graphics.DrawImage(&multi, r1);
  5451. MyImageSave(multi,L"Page0.png", &pngGuid);
  5452. //显示并保存第二帧图片
  5453. multi.SelectActiveFrame(&pageGuid, 1);
  5454. graphics.DrawImage(&multi,r2);
  5455. MyImageSave(multi,L"Page1.png",&pngGuid);
  5456. ////显示并保存第三帧图片
  5457. multi.SelectActiveFrame(&pageGuid, 2);
  5458. graphics.DrawImage(&multi, r3);
  5459. MyImageSave(multi,L"Page2.png",&pngGuid);
  5460. ////显示并保存第四帧图片
  5461. multi.SelectActiveFrame(&pageGuid, 3);
  5462. graphics.DrawImage(&multi, r4);
  5463. MyImageSave(multi,L"Page3.png",&pngGuid);
  5464. }
  5465. void QueryImage_Click()
  5466. {
  5467. Graphics &graphics=GetGraphics();
  5468. graphics.Clear(Color::White);
  5469. SolidBrush brush(Color::Black);
  5470. FontFamily fontFamily(L"宋体");
  5471. Font myFont(&fontFamily, 16, FontStyleRegular, UnitPixel);
  5472. Bitmap image(L"car.jpg");
  5473. //获取图像的属性项
  5474. UINT size, count=0;
  5475. image.GetPropertySize(&size, &count);
  5476. PropertyItem *propItem= (PropertyItem *)new BYTE[size];
  5477. image.GetAllPropertyItems(size, count, propItem);
  5478. // 图像中共有多少属性名可供查询
  5479. if(count == 0)
  5480. {
  5481. AfxMessageBox("无属性名可供查询");
  5482. return;
  5483. }
  5484. CString tmp;
  5485. for(UINT j = 0; j < count; ++j)
  5486. {
  5487. CString s;
  5488. s.Format("第 %d 个可供查找询的属性名的标记值为(16进制):%x\n",
  5489. j,propItem[j].id); tmp+=s;
  5490. s.Format("第 %d 个属性名对应的属性项的信息为:\n",j); tmp+=s;
  5491. //获取属性项详细描述信息
  5492. s.Format("\t属性项的长度为%d\n\t数据类型为%d\n",
  5493. propItem[j].length, propItem[j].type); tmp+=s;
  5494. }
  5495. delete propItem;
  5496. //输出图像的属性信息
  5497. MyDrawString(graphics, tmp, myFont, brush, PointF(0,0));
  5498. }
  5499. void SetProp_Click()
  5500. {
  5501. //装入图片以供修改
  5502. Bitmap image(L"car.jpg");
  5503. // 设置图片的作者为Jasmine
  5504. BYTE newWriterValue[] = {(BYTE)'J', (BYTE)'a', (BYTE)'s', (BYTE)'m', (BYTE)'i', (BYTE)'e', 0};
  5505. //获取图像的属性项
  5506. UINT size, count=0;
  5507. image.GetPropertySize(&size, &count);
  5508. PropertyItem *pp= (PropertyItem *)new BYTE[size];
  5509. image.GetAllPropertyItems(size, count, pp);
  5510. //0x13b:该标记值对应图片作者
  5511. pp[0].id=0x13b;
  5512. //属性值长度
  5513. pp[0].length=strlen((const char*)newWriterValue);
  5514. //2:属性值的数据类型为字符串
  5515. pp[0].type=2;
  5516. pp[0].value=newWriterValue;
  5517. //设置图片属性
  5518. image.SetPropertyItem(pp+0);
  5519. GUID jpegGuid= GetEncoderClsid("image/jpeg");
  5520. MyImageSave(image,L"newwriter.jpg", &jpegGuid);
  5521. delete pp;
  5522. //重新加载修改后的图片
  5523. Bitmap image2(L"newwriter.jpg");
  5524. //查看图片的作者信息
  5525. size = image2.GetPropertyItemSize(0x13b);
  5526. pp= (PropertyItem *)new BYTE[size];
  5527. image2.GetPropertyItem(0x13b, size, pp);
  5528. CString msg;
  5529. msg.Format("图片的作者已经更改为\n\n%s",CString((BYTE*)pp->value));
  5530. AfxMessageBox(msg);
  5531. delete pp;
  5532. }
  5533. void OnCanvas_Click()
  5534. {
  5535. Graphics &graphics=GetGraphics();
  5536. graphics.Clear(Color::White);
  5537. graphics.ScaleTransform(0.7f,0.7f);
  5538. Bitmap image(L"box-2.bmp");
  5539. int Width = image.GetWidth();
  5540. int Height = image.GetHeight();
  5541. Color color;
  5542. graphics.DrawImage(&image,
  5543. Rect(0, 0, Width, Height));
  5544. //产生随机数序列
  5545. for(int i=0;i<Width-5;i++)
  5546. {
  5547. for( int j=0; j<Height-5;j++)
  5548. {
  5549. int a=rand_Next(1000)%5;
  5550. image.GetPixel(i+a, j+a, &color);
  5551. //将该点的RGB值设置成附近五点之内的任一点
  5552. image.SetPixel(i, j, color);
  5553. }
  5554. //动态绘制滤镜效果图
  5555. graphics.DrawImage(&image,
  5556. Rect(Width, 0, Width, Height));
  5557. }
  5558. }
  5559. void OnWood_Click()
  5560. {
  5561. Graphics &graphics=GetGraphics();
  5562. graphics.Clear(Color::White);
  5563. graphics.ScaleTransform(0.7f,0.7f);
  5564. Bitmap image(L"box-2.bmp");
  5565. int Width = image.GetWidth();
  5566. int Height = image.GetHeight();
  5567. Color colorTemp,color2;
  5568. Color color;
  5569. //绘制原图
  5570. graphics.DrawImage(&image, Rect(0, 0, Width, Height));
  5571. int tmp;
  5572. for(int i=0;i<Width;i++)
  5573. {
  5574. for( int j=0; j<Height;j++)
  5575. {
  5576. image.GetPixel(i, j, &color);
  5577. //根据该点RGB的平均值来确认该点的”明暗”
  5578. int avg;
  5579. avg=(color.GetRed()+color.GetGreen()+color.GetBlue())/3;
  5580. if(avg>=128)
  5581. tmp=255;
  5582. else
  5583. tmp=0;
  5584. colorTemp = Color(255,tmp,tmp,tmp);
  5585. //将计算后的RGB值回写到位图
  5586. image.SetPixel(i, j, colorTemp);
  5587. }
  5588. //动态绘制滤镜效果图
  5589. graphics.DrawImage(&image, Rect(Width, 0, Width, Height));
  5590. }
  5591. }
  5592. //计算两点A、B之间的绝对距离
  5593. float fDistance(Point A,Point B)
  5594. {
  5595. double i=((A.X-B.X)*(A.X-B.X))+((A.Y-B.Y)*(A.Y-B.Y));
  5596. return (float)sqrt(i);
  5597. }
  5598. void Flashligt_Click()
  5599. {
  5600. Graphics &graphics=GetGraphics();
  5601. graphics.Clear(Color::White);
  5602. Bitmap image(L"sports.bmp");
  5603. int Width = image.GetWidth();
  5604. int Height = image.GetHeight();
  5605. int A=Width/2;
  5606. int B=Height/2;
  5607. //Center:图片中心点,发亮此值会让强光中心发生偏移
  5608. Point Center= Point(A,B);
  5609. //R:强光照射面的半径,即”光晕”
  5610. int R=100;
  5611. Color colorTemp,color2;
  5612. Color color;
  5613. graphics.DrawImage(&image, Rect(0, 0, Width, Height));
  5614. //依次访问每个像素
  5615. for(int x=0;x<Width;x++)
  5616. {
  5617. for( int y=0; y<Height;y++)
  5618. {
  5619. Point tmp= Point(x,y);
  5620. //如果像素位于”光晕”之内
  5621. if(fDistance(tmp,Center)<R)
  5622. {
  5623. image.GetPixel(x, y, &color);
  5624. int r,g,b;
  5625. //根据该点距离强光中心点的距离,分别让RGB值变量
  5626. //220:亮度增加常量,该值越大,光亮度越强
  5627. float tmp_r=220.0f*(1.0f-fDistance(tmp,Center)/R);
  5628. r=color.GetRed()+(int)tmp_r;
  5629. r=max(0,min(r,255));
  5630. g=color.GetGreen()+(int)tmp_r;
  5631. g=max(0,min(g,255));
  5632. b=color.GetBlue()+(int)tmp_r;
  5633. b=max(0,min(b,255));
  5634. colorTemp = Color(255,(int)r,(int)g,(int)b);
  5635. //将增亮后的像素值回写到位图
  5636. image.SetPixel(x, y, colorTemp);
  5637. }
  5638. }
  5639. //动态绘制滤镜效果图
  5640. graphics.DrawImage(&image, Rect(Width, 0, Width, Height));
  5641. }
  5642. }
  5643. void BlurAndSharpen_Click()
  5644. {
  5645. Graphics &graphics=GetGraphics();
  5646. graphics.Clear(Color::White);
  5647. graphics.ScaleTransform(0.8f,0.8f);
  5648. Bitmap image(L"snike.bmp");
  5649. int Width = image.GetWidth();
  5650. int Height = image.GetHeight();
  5651. //image2:进行锐化处理
  5652. Bitmap *image2=image.Clone(0,0,Width,Height,PixelFormatDontCare);
  5653. Color colorTemp;
  5654. Color color[3][3];
  5655. //绘制原图
  5656. graphics.DrawImage(&image, Rect(0, 0, Width, Height));
  5657. for(int i=1;i<Width-2;i++)
  5658. {
  5659. for( int j=1; j<Height-2;j++)
  5660. {
  5661. //访问周围9个点的RGB值
  5662. image.GetPixel(i-1, j-1, &color[0][0]);
  5663. image.GetPixel(i-1, j, &color[0][1]);
  5664. image.GetPixel(i-1, j+1, &color[0][2]);
  5665. image.GetPixel(i, j-1, &color[1][0]);
  5666. image.GetPixel(i, j, &color[1][1]);
  5667. image.GetPixel(i, j+1, &color[1][2]);
  5668. image.GetPixel(i+1, j-1, &color[2][0]);
  5669. image.GetPixel(i+1, j, &color[2][1]);
  5670. image.GetPixel(i+1, j+1, &color[2][2]);
  5671. int rSum=0;
  5672. int gSum=0;
  5673. int bSum=0;
  5674. //分别求出周围9个点的R、G、B之和
  5675. for(int n=0;n<3;n++)
  5676. for(int nn=0;nn<3;nn++)
  5677. {
  5678. rSum+=color[n][nn].GetR();
  5679. gSum+=color[n][nn].GetG();
  5680. bSum+=color[n][nn].GetB();
  5681. }
  5682. //用RGB的平均值做为当前点的RGB值
  5683. colorTemp = Color(255,(int)(rSum/9),(int)(gSum/9),(int)(bSum/9));
  5684. //将计算后的RGB值回写到位图
  5685. image.SetPixel(i, j,colorTemp);
  5686. }
  5687. //绘制经过平滑处理的效果图
  5688. graphics.DrawImage(&image, Rect(Width, 0, Width, Height));
  5689. }
  5690. //进行锐化处理
  5691. Color colorLeft,colornow;
  5692. //常量dep:锐化系数,此值越大,锐化效果越明显
  5693. float dep=0.550f;
  5694. for(i=1;i<Width-1; i++)
  5695. {
  5696. for( int j=1;j<Height-1; j++)
  5697. {
  5698. image2->GetPixel(i, j, &colornow);
  5699. image2->GetPixel(i-1, j-1, &colorLeft);
  5700. float r=colornow.GetR()+(colornow.GetR()
  5701. -colorLeft.GetR()*dep);
  5702. r=min(255,max(0,r));
  5703. float g=colornow.GetG()+(colornow.GetG() -colorLeft.GetG()*dep);
  5704. g=min(255,max(0,g));
  5705. float b=colornow.GetB()+(colornow.GetB()-colorLeft.GetB()*dep);
  5706. b=min(255,max(0,b));
  5707. colorTemp = Color(255,(int)r,(int)g,(int)b);
  5708. //将计算后的RGB值回写到位图
  5709. image2->SetPixel(i, j, colorTemp);
  5710. }
  5711. graphics.DrawImage(image2, Rect(Width*2, 0, Width, Height));
  5712. }
  5713. delete image2;
  5714. }
  5715. void ShowAllEncoderParameters(ImageCodecInfo*, CString &msg);
  5716. HRESULT EncoderParameterCategoryFromGUID(GUID guid, WCHAR* category, UINT maxChars);
  5717. HRESULT ValueTypeFromULONG(ULONG index, WCHAR* strValueType, UINT maxChars);
  5718. void ShowAllEncoder()
  5719. {
  5720. UINT num; // Number of image encoders
  5721. UINT size; // Size of the image encoder array in bytes
  5722. ImageCodecInfo* pImageCodecInfo;
  5723. // How many encoders are there?
  5724. // How big (in bytes) is the array of all ImageCodecInfo obects?
  5725. GetImageEncodersSize(&num, &size);
  5726. // Create a buffer large enough to hold the array of ImageCodecInfo
  5727. // objects that will be returned by GetImageEncoders.
  5728. pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
  5729. // GetImageEncoders creates an array of ImageCodecInfo objects
  5730. // and copies that array into a previously allocated buffer.
  5731. // The third argument, imageCodecInfos, is a pointer to that buffer.
  5732. GetImageEncoders(num, size, pImageCodecInfo);
  5733. // For each ImageCodecInfo object in the array, show all parameters.
  5734. for(UINT j = 0; j < num; ++j)
  5735. {
  5736. CString msg;
  5737. ShowAllEncoderParameters(&(pImageCodecInfo[j]), msg);
  5738. AfxMessageBox(msg);
  5739. }
  5740. free(pImageCodecInfo);
  5741. }
  5742. VOID ShowAllEncoderParameters(ImageCodecInfo* pImageCodecInfo, CString &msg)
  5743. {
  5744. CONST MAX_CATEGORY_LENGTH = 50;
  5745. CONST MAX_VALUE_TYPE_LENGTH = 50;
  5746. WCHAR strParameterCategory[MAX_CATEGORY_LENGTH] = L"";
  5747. WCHAR strValueType[MAX_VALUE_TYPE_LENGTH] = L"";
  5748. CString s(pImageCodecInfo->MimeType);
  5749. s+="\n";
  5750. msg+=s;
  5751. // Create a Bitmap (inherited from Image) object so that we can call
  5752. // GetParameterListSize and GetParameterList.
  5753. Bitmap bitmap(1, 1);
  5754. // How big (in bytes) is the encoder's parameter list?
  5755. UINT listSize = 0;
  5756. listSize = bitmap.GetEncoderParameterListSize(&pImageCodecInfo->Clsid);
  5757. s.Format(" The parameter list requires %d bytes.\n", listSize);
  5758. msg+=s;
  5759. if(listSize == 0)
  5760. return;
  5761. // Allocate a buffer large enough to hold the parameter list.
  5762. EncoderParameters* pEncoderParameters = NULL;
  5763. pEncoderParameters = (EncoderParameters*)malloc(listSize);
  5764. if(pEncoderParameters == NULL)
  5765. return;
  5766. // Get the parameter list for the encoder.
  5767. bitmap.GetEncoderParameterList(
  5768. &pImageCodecInfo->Clsid, listSize, pEncoderParameters);
  5769. // pEncoderParameters points to an EncoderParameters object, which
  5770. // has a Count member and an array of EncoderParameter objects.
  5771. // How many EncoderParameter objects are in the array?
  5772. s.Format(" There are %d EncoderParameter objects in the array.\n",
  5773. pEncoderParameters->Count);
  5774. msg+=s;
  5775. // For each EncoderParameter object in the array, list the
  5776. // parameter category, data type, and number of values.
  5777. for(UINT k = 0; k < pEncoderParameters->Count; ++k)
  5778. {
  5779. EncoderParameterCategoryFromGUID(
  5780. pEncoderParameters->Parameter[k].Guid, strParameterCategory, MAX_CATEGORY_LENGTH);
  5781. ValueTypeFromULONG(
  5782. pEncoderParameters->Parameter[k].Type, strValueType, MAX_VALUE_TYPE_LENGTH);
  5783. s.Format(" Parameter[%d]\n", k); msg+=s;
  5784. s.Format(" The category is %s.\n", CString(strParameterCategory)); msg+=s;
  5785. s.Format(" The data type is %s.\n", strValueType); msg+=s;
  5786. s.Format(" The number of values is %d.\n",
  5787. pEncoderParameters->Parameter[k].NumberOfValues); msg+=s;
  5788. } // for
  5789. free(pEncoderParameters);
  5790. } // ShowAllEncoderParameters
  5791. HRESULT MyStringCchCopy(LPWSTR pszDest, size_t cchDest, LPCWSTR pszSrc)
  5792. {
  5793. return wcsncpy(pszDest, pszSrc, cchDest)? Ok: E_FAIL;
  5794. }
  5795. HRESULT EncoderParameterCategoryFromGUID(GUID guid, WCHAR* category, UINT maxChars)
  5796. {
  5797. HRESULT hr = E_FAIL;
  5798. if(guid == EncoderCompression)
  5799. hr = MyStringCchCopy(category, maxChars, L"Compression");
  5800. else if(guid == EncoderColorDepth)
  5801. hr = MyStringCchCopy(category, maxChars, L"ColorDepth");
  5802. else if(guid == EncoderScanMethod)
  5803. hr = MyStringCchCopy(category, maxChars, L"ScanMethod");
  5804. else if(guid == EncoderVersion)
  5805. hr = MyStringCchCopy(category, maxChars, L"Version");
  5806. else if(guid == EncoderRenderMethod)
  5807. hr = MyStringCchCopy(category, maxChars, L"RenderMethod");
  5808. else if(guid == EncoderQuality)
  5809. hr = MyStringCchCopy(category, maxChars, L"Quality");
  5810. else if(guid == EncoderTransformation)
  5811. hr = MyStringCchCopy(category, maxChars, L"Transformation");
  5812. else if(guid == EncoderLuminanceTable)
  5813. hr = MyStringCchCopy(category, maxChars, L"LuminanceTable");
  5814. else if(guid == EncoderChrominanceTable)
  5815. hr = MyStringCchCopy(category, maxChars, L"ChrominanceTable");
  5816. else if(guid == EncoderSaveFlag)
  5817. hr = MyStringCchCopy(category, maxChars, L"SaveFlag");
  5818. else
  5819. hr = MyStringCchCopy(category, maxChars, L"Unknown category");
  5820. return hr;
  5821. } // EncoderParameterCategoryFromGUID
  5822. HRESULT ValueTypeFromULONG(ULONG index, WCHAR* strValueType, UINT maxChars)
  5823. {
  5824. HRESULT hr = E_FAIL;
  5825. WCHAR* valueTypes[] = {
  5826. L"Nothing", // 0
  5827. L"ValueTypeByte", // 1
  5828. L"ValueTypeASCII", // 2
  5829. L"ValueTypeShort", // 3
  5830. L"ValueTypeLong", // 4
  5831. L"ValueTypeRational", // 5
  5832. L"ValueTypeLongRange", // 6
  5833. L"ValueTypeUndefined", // 7
  5834. L"ValueTypeRationalRange"}; // 8
  5835. hr = MyStringCchCopy(strValueType, maxChars, valueTypes[index]);
  5836. return hr;
  5837. } // ValueTypeFromULONG