| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790 |
- //Download by http://www.NewXing.com
- // WalkLtDemo设计实例.cpp : implementation of the CWalkLtDemoView class
- //
- #include "stdafx.h"
- #include "WalkLtDemo.h"
- #include "WalkLtDemoDoc.h"
- #include "WalkLtDemoView.h"
- #include "WalkLtDemoHelp.h"
- #include "math.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- //屏蔽 4244 编译警告
- #pragma warning( disable : 4244 )
- extern Rect MyClient;
- void menuItem2_Click()
- {
- Graphics &g=GetGraphics();
- g.Clear(Color::Black);
- Bitmap bitmap(L"demo.bmp");
- int iWidth = bitmap.GetWidth();
- int iHeight = bitmap.GetHeight();
- //初始化色彩变换矩阵
- ColorMatrix colorMatrix=
- {
- 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- ImageAttributes imageAtt;
- //从0到1进行修改色彩变换矩阵主对角线上的数值
- //使三种基准色的饱和度渐增
- for(float i=0.0f;i<=1.0f;i+=0.02f)
- {
- colorMatrix.m[0][0]=i;
- colorMatrix.m[1][1]=i;
- colorMatrix.m[2][2]=i;
- colorMatrix.m[3][3]=i;
- //设置色彩校正矩阵
- imageAtt.SetColorMatrix(&colorMatrix,
- ColorMatrixFlagsDefault, ColorAdjustTypeBitmap);
- //绘制图片
- g.DrawImage(
- &bitmap, Rect(0, 0, iWidth, iHeight),
- 0,0,
- iWidth,iHeight,
- UnitPixel,
- &imageAtt);
- }
- MyDrawString(g, "下面演示淡出效果...", Font(L"Arial",12),
- SolidBrush(Color::White), Point(20,iHeight+20));
- //从1到0进行修改色彩变换矩阵主对角线上的数值
- //依次减少每种色彩分量
- for(i=1.0f;i>=0.0f;i-=0.02f)
- {
- colorMatrix.m[0][0]=i;
- colorMatrix.m[1][1]=i;
- colorMatrix.m[2][2]=i;
- colorMatrix.m[3][3]=i;
- //设置色彩校正矩阵
- imageAtt.SetColorMatrix(&colorMatrix,
- ColorMatrixFlagsDefault,ColorAdjustTypeBitmap);
- //绘制图片
- g.DrawImage(
- &bitmap, Rect(0, 0, iWidth, iHeight),
- 0,0,
- iWidth,iHeight,
- UnitPixel,
- &imageAtt);
- }
- MyDrawString(g, "完毕", Font(L"Arial",12),
- SolidBrush(Color::White), Point(20,iHeight+40));
- }
-
- //
- void GrayScale_Click()
- {
- Graphics &g=GetGraphics();
- g.Clear(Color::White);
- Bitmap image(L"head.bmp");
- int Width =image.GetWidth()-1;
- int Height =image.GetHeight()-1;
- //绘制原图
- g.DrawImage(&image,0, 0);
- g.TranslateTransform((REAL)image.GetWidth(),0.f);
- /*image2、image3分别用来保存最大值法
- 和加权平均法处理的灰度图像*/
- Bitmap *image2=image.Clone(Rect(0,0,image.GetWidth(), image.GetHeight()),PixelFormatDontCare);
- Bitmap *image3=image.Clone(Rect(0,0,image.GetWidth(), image.GetHeight()),PixelFormatDontCare);
- Color color;
- //使用平均值进行灰度处理
- for(int i=Width; i>=0;i--)
- for( int j=Height; j>=0;j--)
- {
- image.GetPixel(i,j, &color);
- //求出平均三个色彩分量的平均值
- int middle=(color.GetR()+color.GetG()+color.GetB())/3;
- Color colorResult(255,middle,middle,middle);
- image.SetPixel(i,j, colorResult);
- }
- //重新绘制灰度化图
- g.DrawImage(&image, Rect(0, 0, Width, Height));
- //在新位置显示最大值法进行灰度处理的结果
- g.TranslateTransform((REAL)image.GetWidth(),0.f);
- //使用最大值法进行灰度处理
- for(i=Width; i>=0;i--)
- {
- for(int j=Height; j>=0;j--)
- {
- image2->GetPixel(i,j, &color);
- int tmp=color.GetR()>color.GetG()? color.GetR():color.GetG();
- int maxcolor=tmp>color.GetB()? tmp:color.GetB();
- Color colorResult(255,maxcolor,maxcolor,maxcolor);
- //设置处理后的灰度信息
- image2->SetPixel(i, j, colorResult);
- }
- }
- //重新绘制灰度化图
- g.DrawImage(image2, Rect(0, 0, Width, Height));
- //在第二行绘制图片
- g.ResetTransform();
- g.TranslateTransform(0.f, (REAL)image.GetHeight());
- //使用加权平均法进行灰度处理
- for(i=Width; i>=0;i--)
- {
- for(int j=Height; j>=0;j--)
- {
- image3->GetPixel(i, j, &color);
- int R=(int)(0.3f*color.GetR());
- int G=(int)(0.59f*color.GetG());
- int B=(int)(0.11f*color.GetB());
- Color colorResult(255,R,G,B);
- //设置处理后的灰度信息
- image3->SetPixel(i, j, colorResult);
- }
- }
- //重新绘制灰度化图
- g.DrawImage(image3, Rect(0, 0, Width, Height));
-
- g.TranslateTransform((REAL)image.GetWidth(),0.f);
- //灰度的还原演示,还原使用最大值法处理的灰度图像image2
- for(i=Width; i>0;i--)
- {
- for(int j=Height; j>0;j--)
- {
- image2->GetPixel(i,j, &color);
- int R=color.GetR();
- int G=color.GetG();
- int B=color.GetB();
- //分别对RGB三种色彩分量进行伪彩色还原
- //进行红色分量的还原
- if(R<127)
- R=0;
- if(R>=192)
- R=255;
- if(R<=191&&R>=128)
- R=4*R-510;
- /*进行绿色分量的还原,为了还原后的绿色分量再次参加比较,
- 这里设置一个变量YES表示G是否已经参加了比较*/
- bool yes;
- yes=false;
- if(G<=191&&G>=128&&(!yes))
- {
- G=255;
- yes=true;
- }
- if(G>=192&&(!yes))
- {
- G=1022-4*G;
- yes=true;
- }
- if(G<=63&&(!yes))
- {
- G=254-4*G;
- yes=true;
- }
- if(G<=127&&G>=67&&(!yes))
- G=4*G-257;
-
- //进行蓝色分量的还原
- if(B<=63)
- B=255;
- if(B>=128)
- B=0;
- if(B>=67&&B<=127)
- B=510-4*B;
-
- //还原后的伪彩色
- Color colorResult(255,R,G,B);
- //将还原后的RGB信息重新写入位图
- image2->SetPixel(i, j, colorResult);
-
- }
- }
- //重新绘制还原后的伪彩色位图
- //重新绘制灰度化图
- g.DrawImage(image2, Rect(0, 0, Width, Height));
- delete image2;
- delete image3;
- }
- //
- void Inverse_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- graphics.ScaleTransform(0.7f,0.7f);
-
- Bitmap image(L"head.bmp");
- int Width =image.GetWidth();
- int Height =image.GetHeight();
- Color colorTemp,color2;
- Color color;
- //绘制原图
- graphics.DrawImage(&image, Rect(0, 0, Width, Height));
-
- for(int i=0;i<Width;i++)
- {
- for( int j=0; j<Height;j++)
- {
- image.GetPixel(i, j, &color);
- //将色彩进行反转,获得底片效果
- int r=255-color.GetRed();
- int g=255-color.GetGreen();
- int b=255-color.GetBlue();
- Color colorResult(255,r,g,b);
- //将还原后的RGB信息重新写入位图
- image.SetPixel(i, j, colorResult);
- }
- //动态绘制底片滤镜效果图
- graphics.DrawImage(&image, Rect(Width, 0, Width, Height));
- }
- //将已经实现了底片效果的位图再反色(恢复到原图)
- for(i=0;i<Width;i++)
- {
- for( int j=0; j<Height;j++)
- {
- image.GetPixel(i, j, &color);
- int r=255-color.GetRed();
- int g=255-color.GetGreen();
- int b=255-color.GetBlue();
- Color colorResult(255,r,g,b);
- //将还原后的RGB信息重新写入位图
- image.SetPixel(i, j, colorResult);
- }
- //绘制经过两次反色的位图
- graphics.DrawImage(&image, Rect(Width*2, 0, Width, Height));
- }
- }
- //
- void Emboss_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- graphics.ScaleTransform(0.7f,0.7f);
- Bitmap image(L"head.bmp");
- int Width =image.GetWidth();
- int Height =image.GetHeight();
- //image2:进行雕刻处理
- Bitmap *image2=image.Clone(Rect(0,0,Width,Height), PixelFormatDontCare);
- //绘制原图
- graphics.DrawImage(&image, Rect(0, 0, Width, Height));
- Color color, colorTemp,colorLeft;
- //进行图片的浮雕处理
- //依次访问每个像素的RGB值
- for(int i=Width-1; i>0;i--)
- {
- for( int j=Height-1; j>0;j--)
- {
- //获取相邻两个像素的R、G、B值
- image.GetPixel(i, j, &color);
- image.GetPixel(i-1, j-1, &colorLeft);
- //计算与左上角像素的RGB分量之差
- //67:控制图片的最低灰度,128:常量,更改这两个值会得到不同的效果
- int r=max(67,min(255,
- abs(color.GetRed()-colorLeft.GetRed()+128)));
- int g=max(67,min(255,
- abs(color.GetGreen()-colorLeft.GetGreen()+128)));
- int b=max(67,min(255,
- abs(color.GetBlue()-colorLeft.GetBlue()+128)));
- Color colorResult(255,r,g,b);
- //将计算后的RGB值回写到位图
- image.SetPixel(i, j,colorResult);
- }
- //绘制浮雕图
- graphics.DrawImage(&image, Rect(Width+10, 0, Width, Height));
- }
- //进行图片的雕刻处理
- for(i=0; i<Height-1;i++)
- {
- for( int j=0; j<Width-1;j++)
- {
- image2->GetPixel(j, i, &color);
- image2->GetPixel(j+1, i+1, &colorLeft);
- //计算与右下角像素的分量之差
- //67:控制图片的最低灰度,128:常量,更改这两个值会得到不同的效果
- int r=max(67,min(255,abs(color.GetRed()-colorLeft.GetRed()+128)));
- int g=max(67,min(255,abs(color.GetGreen()-colorLeft.GetGreen()+128)));
- int b=max(67,min(255,abs(color.GetBlue()-colorLeft.GetBlue()+128)));
- Color colorResult(255,r,g,b);
- image2->SetPixel(j, i,colorResult);
- }
- //绘制雕刻图
- graphics.DrawImage(image2, Rect(Width*2+20, 0, Width, image.GetHeight()));
- }
- delete image2;
- }
- void CreatePenFromBrush_Click()
- {
- Graphics &g=GetGraphics();
- g.Clear(Color::White);
- //构造线性渐变画刷
- LinearGradientBrush LineargradientBrush(Rect(0,0,10,10),
- Color::Blue,Color::Red, LinearGradientModeBackwardDiagonal);
- //从线性渐变画刷中构造画笔
- Pen pen(&LineargradientBrush);
- pen.SetWidth(10);
- //绘制矩形
- g.DrawRectangle(&pen,10,10,100,100);
- //装入纹理图片
- Bitmap image(L"butterfly.bmp");
- //构造纹理画刷
- TextureBrush tBrush(&image);
- //将画刷传入画笔的构造函数
- Pen texturedPen(&tBrush,42);
- //设置贝塞尔曲线的起止点及控制点
- Point p1(10, 100);
- Point c1= Point(100, 10);
- Point c2= Point(150, 150);
- Point p2= Point(200, 100);
- g.TranslateTransform(130,0);
- //绘制贝塞尔曲线
- g.DrawBezier(&texturedPen, p1, c1, c2, p2);
- }
- //画笔的线形演示
- void DashStyle_Custom_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen pen(Color::Blue, 5);
- //设置文本输出对齐方式及字体
- StringFormat fmt;
- fmt.SetAlignment(StringAlignmentNear);
- fmt.SetLineAlignment(StringAlignmentCenter);
- //字体
- Font font(L"Arial",12);
- SolidBrush sBrush(Color::Black);
- graphics.TranslateTransform(0,30);
- int i=0;
- //分别使用常见的五种线型绘制直线
- for(;i<5;i++)
- {
- //设置线型
- pen.SetDashStyle((DashStyle)i);
- graphics.DrawLine(&pen, 10, 30*i, 260, 30*i);
- //输出当前线型的名称
- CString s; s.Format(_T("%d"), i+1);
- MyDrawString(graphics, s, font, sBrush, PointF(260.f,30.f*i), fmt);
- s.ReleaseBuffer();
- }
- //使用自定义义线型
- float dashVals[]=
- {
- 5.0f, // 线长5个像素
- 2.0f, // 间断2个像素
- 15.0f, // 线长15个像素
- 4.0f // 间断4个像素
- };
- pen.SetDashPattern(dashVals, 4);
- pen.SetColor(Color::Red);
- graphics.DrawLine(&pen, 10, 30*i, 260, 30*i);
- CString s; s.Format(_T("%d"), i+1);
- MyDrawString(graphics, s, font, sBrush, PointF(260.f,30.f*i), fmt);
- }
- void Pen_Align_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
-
- Pen pen(Color::Gray, 1.0f);
- Pen pen2(Color(155,255,0,0),10);
- int i=0;
- for(; i<5;i++)
- {
- pen2.SetAlignment((PenAlignment)i);
- graphics.DrawLine(&pen2, Point(0,10), Point(60,10));
- graphics.TranslateTransform(70,0);
- }
- graphics.ResetTransform();
- graphics.DrawLine(&pen,0,10,600,10);
- }
- void Pen_Tranform_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //构造一支宽度为5的红色画笔
- Pen pen(Color::Red, 3.0f);
- //将画笔从垂直方向扩充6倍,水平方向保持不变
- pen.ScaleTransform(1, 6);
- //使用未经旋转处理的画笔画圆
- graphics.DrawEllipse(&pen, 0, 50, 80, 80);
- //60°旋转
- graphics.TranslateTransform(100,0);
- pen.RotateTransform(60, MatrixOrderAppend);
- graphics.DrawEllipse(&pen, 0, 50, 80, 80);
- //120°旋转
- graphics.TranslateTransform(100,0);
- pen.RotateTransform(60, MatrixOrderAppend);
- graphics.DrawEllipse(&pen, 0, 50, 80, 80);
- //180°旋转
- graphics.TranslateTransform(100,0);
- pen.RotateTransform(60, MatrixOrderAppend);
- graphics.DrawEllipse(&pen, 0, 50, 80, 80);
- }
- //线帽演示
- void Pen_LineCap_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //设置文本输出对齐方式及字体
- StringFormat fmt;
- fmt.SetAlignment(StringAlignmentNear);
- fmt.SetLineAlignment(StringAlignmentCenter);
- //字体
- Font font(L"Arial",12);
- SolidBrush sBrush(Color::Black);
- //创建宽度为15的画笔
- Pen pen(Color::Black,15);
- //分别使用不同的线帽
- LineCap lincap[]=
- {
- LineCapFlat,
- LineCapSquare,
- LineCapRound,
- LineCapTriangle,
- LineCapNoAnchor,
- LineCapSquareAnchor,
- LineCapRoundAnchor,
- LineCapDiamondAnchor,
- LineCapArrowAnchor,
- LineCapCustom,
- LineCapAnchorMask
- };
- for (int i=0; i<sizeof(lincap)/sizeof(LineCap); i++)
- {
- pen.SetStartCap(lincap[i]);//起点
- pen.SetEndCap(lincap[i]);//终点
- graphics.DrawLine(&pen,50,10,300,10);
- //输出当前线帽类型(LineCap枚举成员名)
- CString s; s.Format(_T("%d"), lincap[i]);
- MyDrawString(graphics, s, font, sBrush, PointF(320.f,10.f), fmt);
- //平移绘图平面
- graphics.TranslateTransform(0,30);
- }
- }
- //画笔的透明度支持
- void Pen_TransColor_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //分别构造蓝色、红色画笔
- Pen blue(Color::Blue);
- Pen red(Color::Red);
- //绘制网线
- int y = 256;
- for (int x = 0; x < 256; x += 5)
- {
- graphics.DrawLine(&blue, 0, y, x, 0);
- graphics.DrawLine(&red, 256, x, y, 256);
- y -= 5;
- //延时以查看动态效果
- ::Sleep(20);
- }
- //使用绿色画笔绘制不同透明度的线条
- //透明度由上到下依次递减
- for (y = 0; y < 256; y++)
- {
- Pen pen(_MakeA_RGB(y,Color::Green));
- graphics.DrawLine(&pen, 0, y, 256, y);
- //延时以便查看动态效果
- ::Sleep(20);
- }
- //使用绿色画笔绘制不同透明度的线条
- //透明度由左到右依次递减
- for (x = 0; x < 256; x++)
- {
- Pen pen(_MakeA_RGB(x, Color::Blue));
- graphics.DrawLine(&pen, x, 100, x, 200);
- //延时以查看动态效果
- ::Sleep(20);
- }
- }
- //简单的单色画刷示意
- void Brush_SolidBrush_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //创建绿色画刷
- SolidBrush greenBrush(Color::Green);
- //定义曲线、多边形端点坐标
- PointF point1(100.0f, 100.0f);
- PointF point2(200.0f, 50.0f);
- PointF point3(250.0f, 200.0f);
- PointF point4(50.0f, 150.0f);
- PointF point5(100.0f, 100.0f);
- PointF points[]={point1, point2, point3, point4};
- //填充闭合曲线
- graphics.FillClosedCurve(&greenBrush, points, 4, FillModeAlternate, 1.0f);
- //构造多边形(闭合)
- PointF poly[]={point1, point2, point3, point4,point5};
- //在另一个位置填充多边形
- graphics.TranslateTransform(300,0);
- graphics.FillPolygon(&greenBrush, poly, 5, FillModeAlternate);
- }
- //填充正叶曲线
- void Brush_FillVurve_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //创建绿色画刷
- SolidBrush greenBrush(Color::Green);
- int cx,cy;
- //以当前窗口的中心点绘制正叶曲线
- cx=MyClient.Width/2;
- cy=MyClient.Height/2;
- //设置"叶"长
- int LeafLength=80;
- //设置叶片数量=2* LeafNum
- int LeafNum=5;
- double x,y,x2,y2,r;
- //创建一个图形路径对象,用以容纳正叶曲线的边界线
- GraphicsPath tmpPath(FillModeAlternate);
- //生成曲线边界数据:角度变化为一周PI*2
- for(float i=0.0f;i<PI*2+0.1f;i+=(float)PI/180)
- {
- r=(int)abs(LeafLength*cos(double(LeafNum*i)));
- x=r*cos(i);
- y=r*sin(i);
- x2=cx+x;
- y2=cy+y;
- /*将曲线的边界信息存入临时路径,如果想要查看这些信息所构成的区域
- ,可以在此加入graphics.DrawLine(&pen,x2,y2,x2-1,y2-1);*/
- tmpPath.AddLine((int)x2,(int)y2,(int)x2,(int)y2);
- }
- //填充路径
- graphics.FillPath(&greenBrush, &tmpPath);
- //绘制中心坐标轴
- Pen pen(Color::Gray);
- graphics.DrawLine(&pen,0,cy,cx*2,cy);
- graphics.DrawLine(&pen,cx,0,cx,cy*2);
- }
- //影线画刷示意
- void Brush_HatchBrush_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //构造影线画刷的前后色彩
- Color black=Color::Black;
- Color white=Color::White;
- //使用第一种风格的影线画刷
- HatchBrush brush(HatchStyleHorizontal, black, white);
- graphics.FillRectangle(&brush, 20, 20, 100, 50);
- //使用第二种风格的影线画刷
- HatchBrush brush1(HatchStyleVertical, black, white);
- graphics.FillRectangle(&brush1, 120, 20, 100, 50);
- //使用第三种风格的影线画刷
- HatchBrush brush2(HatchStyleForwardDiagonal, black, white);
- graphics.FillRectangle(&brush2, 220, 20, 100, 50);
- //使用第四种风格的影线画刷
- HatchBrush brush3(HatchStyleBackwardDiagonal, black, white);
- graphics.FillRectangle(&brush3, 320, 20, 100, 50);
- //使用第五种风格的影线画刷
- HatchBrush brush4(HatchStyleCross, black, white);
- graphics.FillRectangle(&brush4, 420, 20, 100, 50);
- //使用第六种风格的影线画刷
- HatchBrush brush5(HatchStyleDiagonalCross, black, white);
- graphics.FillRectangle(&brush5, 520, 20, 100, 50);
- }
- //列举出所有风格的影线画刷
- void Brush_EnumAllStyle_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //设定画刷的前景色为黑色,背景色为白色
- Color black=Color::Black;
- Color white=Color::White;
- //预定义填充区域的宽度及高度
- int WIDTH=140;
- int HEIGHT=40;
- //设定输出文本所需信息
- SolidBrush redBrush(Color::Red);
- Font myFont(L"Arial", 10);
- //column_count表明在每一行能够绘制矩形的总数
- int column_count=MyClient.Width/WIDTH;
- int rol=0;
- int column=0;
- //在当前窗口使用所有的影线画刷种风格填充矩形
- Pen pen(Color::Blue, 1);
- for (int i=HatchStyleHorizontal; i<HatchStyleTotal; i++)
- {
- HatchStyle style=(HatchStyle)i;
- //如果一行已经绘制完毕,换行
- if (rol>column_count-1)
- {
- column+=2;
- rol=0;
- }
- //创建临时画刷
- HatchBrush brush_tmp(style, black, white);
- //填充矩形:设置宽度为WIDTH-20的目的是让矩形之间留出间隔
- graphics.FillRectangle(&brush_tmp, rol*WIDTH, column*HEIGHT, WIDTH-20, HEIGHT);
- //绘制矩形边框
- graphics.DrawRectangle(&pen, rol*WIDTH, column*HEIGHT, WIDTH-20, HEIGHT);
- //显示每种画刷风格的枚举名称
- //计算文本输出区域
- RectF layoutRect(rol*WIDTH, (column+1)*HEIGHT, WIDTH, HEIGHT);
- StringFormat format;
- //设置文本输出格式:水平、垂直居中
- format.SetAlignment(StringAlignmentNear);
- format.SetLineAlignment(StringAlignmentCenter);
- //在矩形框中央输出枚举值
- CString s; s.Format(_T("%d"), style);
- MyDrawString(graphics, s, myFont, redBrush, layoutRect, format);
- rol+=1;
- }
- }
- //设置绘制原点
- void Brush_SetRenderingOrigin_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //设定画刷的前景色为黑色,背景色为白色
- Color black=Color::Black;
- Color white=Color::White;
- HatchBrush hatchBrush(HatchStyleDarkDownwardDiagonal,black,white);
- //在竖直方向填充8个矩形,使用默认的画刷原点
- for(int i=0;i<8;i++)
- {
- graphics.FillRectangle(&hatchBrush, 0, i*50, 100, 50);
- }
- //使用不同的绘制原点进行区域填充
- for(i=0;i<8;i++)
- {
- //设置画刷原点(水平方向递增)
- graphics.SetRenderingOrigin(i, 0);
- graphics.FillRectangle(&hatchBrush, 100, i*50, 100, 50);
- }
- }
- //纹理画刷的不同加载方式
- void Brush_Texture_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen pen(Color::Blue, 2);
- SolidBrush brush(Color::Black);
- Font myFont(L"宋体",12);
- //定义纹理画刷的不同填充区域
- RectF rect1(10, 10, 200,200);
- RectF rect2(210, 10, 200, 200);
- RectF rect3(410, 10, 200, 200);
- //装入纹理图片
- Bitmap image(L"nemo.bmp");
- //构造纹理画刷1:使用默认的方式
- TextureBrush tBrush(&image);
- //使用纹理画刷填充圆形区域
- graphics.FillEllipse(&tBrush,rect1);
- //绘制圆周
- graphics.DrawEllipse(&pen,rect1);
- MyDrawString(graphics, "图片原始大小", myFont, brush, PointF(40,220));
- //构造纹理画刷2:只使用给定图片的部分区域
- TextureBrush tBrush2(&image, Rect(55,35,55,35));
- graphics.FillEllipse(&tBrush2,rect2);
- graphics.DrawEllipse(&pen,rect2);
- MyDrawString(graphics, "使用部分截图", myFont, brush, PointF(240,220));
- //构造纹理画刷3:将使用图片的画刷进行缩放
- TextureBrush tBrush3(&image);
- //对画刷进行50%的缩放
- Matrix mtr(0.5f, 0.0f, 0.0f,0.5f, 0.0f, 0.0f);
- tBrush3.SetTransform(&mtr);
- graphics.FillEllipse(&tBrush3,rect3);
- graphics.DrawEllipse(&pen,rect3);
- MyDrawString(graphics, "比例缩小图片",myFont,brush, PointF(440,220));
- }
- //使用图片排列方式
- void Brush_Texture_WrapMode_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen pen(Color::Blue, 2);
- SolidBrush brush(Color::Black);
- Font myFont(L"Arial", 13);
- //装入纹理图片
- Bitmap image(L"nemo.bmp");
- //构造纹理画刷
- TextureBrush tBrush(&image);
- //将画刷进行缩放
- Matrix mtr(0.5f, 0.0f, 0.0f,0.5f, 0.0f, 0.0f);
- tBrush.SetTransform(&mtr);
- int i=0;
- //对图片不使用排列方式
- tBrush.SetWrapMode(WrapModeClamp);
- graphics.FillRectangle(&tBrush, Rect(i*150,0,150,150));
- graphics.DrawRectangle(&pen, Rect(i*150,0,150,150));
- MyDrawString(graphics, "Clamp", myFont,brush, PointF(0,155));
- i+=1;
- //对图片使用平铺排列方式
- tBrush.SetWrapMode(WrapModeTile);
- graphics.FillRectangle(&tBrush, Rect(i*150+10,0,150,150));
- graphics.DrawRectangle(&pen, Rect(i*150+10,0,150,150));
- MyDrawString(graphics, "Tile",myFont,brush, PointF(170,155) );
- //对图片使用水平翻转排列方式
- i+=1;
- tBrush.SetWrapMode(WrapModeTileFlipX);
- graphics.FillRectangle(&tBrush, Rect(i*150+20,0,150,150));
- graphics.DrawRectangle(&pen, Rect(i*150+20,0,150,150));
- MyDrawString(graphics, "TileFlipX",myFont,brush, PointF(320,155));
- //对图片使用垂直翻转排列方式
- tBrush.SetWrapMode(WrapModeTileFlipY);
- graphics.FillRectangle(&tBrush, Rect(0,180,150,150));
- graphics.DrawRectangle(&pen, Rect(0,180,150,150));
- MyDrawString(graphics, "TileFlipY",myFont,brush, PointF(0,335));
- //对图片使用水平、垂直同时翻转排列方式
- tBrush.SetWrapMode(WrapModeTileFlipXY);
- graphics.FillRectangle(&tBrush, Rect(160,180,150,150));
- graphics.DrawRectangle(&pen, Rect(160,180,150,150));
- MyDrawString(graphics, "TileFlipXY",myFont,brush, PointF(170,335));
- }
- //纹理画刷的变换
- void Brush_TextureTransform_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //为三种不同的变换方式的画刷定义填充区域
- RectF rect1= RectF (10, 10, 200,200);
- RectF rect2= RectF (210, 10, 200, 200);
- RectF rect3= RectF (410, 10, 200, 200);
- Pen pen(Color::Blue, 2);
- SolidBrush brush(Color::Black);
- Font myFont(L"宋体", 12);
- //装入纹理图片
- Bitmap image(L"nemo.bmp");
- //构造纹理画刷
- TextureBrush tBrush(&image);
- //将画刷旋转30度
- tBrush.RotateTransform(30);
- graphics.FillEllipse(&tBrush,rect1);
- graphics.DrawEllipse(&pen,rect1);
- MyDrawString(graphics, "旋转30度",myFont,brush, PointF(40,220));
- //重置变换矩阵:恢复到变化前的状态
- tBrush.ResetTransform();
- //将画刷在水平方向上扩大三倍
- tBrush.ScaleTransform(3, 1);
- graphics.FillEllipse(&tBrush, rect2);
- graphics.DrawEllipse(&pen,rect2);
- MyDrawString(graphics, "横向扩充三倍",myFont,brush, PointF(240,220));
- //平移变换
- tBrush.ResetTransform();
- //将画刷在水平方向上平移30个像素
- tBrush.TranslateTransform(30, 0, MatrixOrderAppend);
- graphics.FillEllipse(&tBrush, rect3);
- graphics.DrawEllipse(&pen,rect3);
- MyDrawString(graphics, "横向平移30个像素",myFont,brush, PointF(440,220));
- }
- //查询画刷的变换信息
- void Brush_GetTextureMatrix_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Matrix matrix;
- float elements[6];
- RectF rect1(10, 10, 200,200);
- Pen pen(Color::Black, 2);
- SolidBrush brush(Color::Black);
- Bitmap image(L"nemo.bmp");
- TextureBrush tBrush(&image);
- //进行三种任意变换
- tBrush.RotateTransform(30);
- tBrush.TranslateTransform(5,3);
- tBrush.ScaleTransform(0.5f,2.0f);
- //获取目前已经进行的画刷变换
- tBrush.SetTransform(&matrix);
- //从矩形到数组
- matrix.GetElements(elements);
- graphics.FillEllipse(&tBrush,rect1);
- graphics.DrawEllipse(&pen,rect1);
- //输出矩阵的元素
- for(int j = 0; j <6; ++j)
- {
- CString s; s.Format(_T("%d %f 矩阵元素值"), j, elements[j]);
- AfxMessageBox(s);
- }
- }
- //线性渐变画刷程序
- void Brush_LinearGradientBrush_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //定义一个由红色到蓝色渐变的画刷:水平变换区域的宽度为40
- //竖直方向不进行色彩渐变
- LinearGradientBrush linGrBrush(
- Point(0, 0),
- Point(40, 0),
- Color::Red, //起点色彩
- Color::Blue); //止点色彩
- //定义一个色彩呈对角线变换的区域,区域大小为40*40
- LinearGradientBrush linGrBrush2(
- Point(0, 0),
- Point(40, 40),
- Color::Red, //起点色彩
- Color::Blue); //止点色彩
- //分别演示不同的线性渐变画刷对目标区域的不同填充效果
- graphics.FillRectangle(&linGrBrush, 0, 0, 200, 200);
- graphics.FillRectangle(&linGrBrush2, 240, 0, 200, 200);
- Pen pen(Color::Gray,1);
- //在对角线方向上绘制单个画刷的填充区域
- for(int i=0;i<5;i++)
- {
- graphics.DrawRectangle(&pen, 240+i*40,i*40,40,40);
- }
- }
- //控制线性渐变画刷的填充方式
- void Brush_LinearArrange_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen pen(Color::Blue, 2);
- SolidBrush brush(Color::Black);
- Font myFont(L"Arial", 13);
- //定义一个色彩呈对角线变换的区域,区域大小为40*40
- LinearGradientBrush linGrBrush(
- Point(0, 0),
- Point(40, 40),
- Color::Red, //起点色彩
- Color::Blue); //止点色彩
- int i=0;
- //对渐变画刷使用平铺排列方式(默认方式)
- linGrBrush.SetWrapMode(WrapModeTile);
- //填充一个大小为160的正方形
- graphics.FillRectangle(&linGrBrush, Rect(i*160,0,160,160));
- graphics.DrawRectangle(&pen, Rect(i*160,0,160,160));
- MyDrawString(graphics, "Tile", myFont, brush, Point(20,165));
- //对渐变画刷使用水平翻转排列方式
- i+=1;
- linGrBrush.SetWrapMode(WrapModeTileFlipX);
- //重置绘图平面原点
- graphics.SetRenderingOrigin(160,0);
- graphics.FillRectangle(&linGrBrush, Rect(i*160,0,160,160));
- graphics.DrawRectangle(&pen, Rect(i*160,160,0,160));
- MyDrawString(graphics, "TileFlipX", myFont, brush, Point(170,165));
- //对渐变画刷使用垂直翻转排列方式
- linGrBrush.SetWrapMode(WrapModeTileFlipY);
- //重置绘图平面原点
- graphics.SetRenderingOrigin(0,200);
- graphics.FillRectangle(&linGrBrush, Rect(0,200,160,160));
- graphics.DrawRectangle(&pen, Rect(0,200,200,160));
- MyDrawString(graphics, "TileFlipY",myFont,brush, Point(0,375));
- //对渐变画刷使用水平、垂直同时翻转排列方式
- linGrBrush.SetWrapMode(WrapModeTileFlipXY);
- graphics.SetRenderingOrigin(160,200);
- graphics.FillRectangle(&linGrBrush, Rect(160,200,160,160));
- graphics.DrawRectangle(&pen, Rect(160,200,160,160));
- MyDrawString(graphics, "TileFlipXY",myFont,brush, Point(170,375));
- //标注渐变画刷渐变区域的大小:画格子
- Pen pen2(Color::Gray, 1);
- for(i=0;i<8;i++)
- graphics.DrawLine(&pen2,0,i*40,320,i*40);
- for(i=0;i<9;i++)
- graphics.DrawLine(&pen2,i*40,0,i*40,360);
- }
- //定义线性渐变画刷的渐变模式
- void Brush_LinearGradientMode_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen pen(Color::Blue, 2);
- SolidBrush brush(Color::Black);
- Font myFont(L"Arial", 12);
- //定义一个水平渐变画刷,大小40*20
- LinearGradientBrush linGrBrush1(
- Rect(0, 0, 40, 20),
- Color::Red,
- Color::Blue,LinearGradientModeHorizontal);
- //定义一个垂直渐变画刷
- LinearGradientBrush linGrBrush2(
- Rect(0, 0, 40, 20),
- Color::Red,
- Color::Blue,LinearGradientModeVertical);
- //从右上到左下的渐变画刷
- LinearGradientBrush linGrBrush3(
- Rect(0, 0, 40, 20),
- Color::Red,
- Color::Blue,LinearGradientModeForwardDiagonal);
- //从左上到左下的渐变画刷
- LinearGradientBrush linGrBrush4(
- Rect(0, 0, 40, 20),
- Color::Red,
- Color::Blue,LinearGradientModeBackwardDiagonal);
- int i=0;
- //使用水平渐变的画刷填充区域
- graphics.FillRectangle(&linGrBrush1, Rect(i*160,0,160,160));
- graphics.DrawRectangle(&pen, Rect(i*160,0,160,160));
- MyDrawString(graphics, "水平渐变",myFont,brush, PointF(20,165));
- i+=1;
- //重置绘图平面原点
- graphics.SetRenderingOrigin(160,0);
- //使用垂直渐变的画刷填充区域
- graphics.FillRectangle(&linGrBrush2, Rect(i*160,0,160,160));
- graphics.DrawRectangle(&pen, Rect(i*160,160,0,160));
- MyDrawString(graphics, "垂直渐变",myFont,brush, PointF(170,165));
- //重置绘图平面原点
- graphics.SetRenderingOrigin(0,200);
- //使用从右上到左下渐变的画刷填充区域
- graphics.FillRectangle(&linGrBrush3, Rect(0,200,160,160));
- graphics.DrawRectangle(&pen, Rect(0,200,200,160));
- MyDrawString(graphics, "左上->右下",myFont,brush, PointF(0,375));
- graphics.SetRenderingOrigin(160,200);
- graphics.FillRectangle(&linGrBrush4, Rect(160,200,160,160));
- //使用从左上到右下渐变的画刷填充区域
- graphics.DrawRectangle(&pen, Rect(160,200,160,160));
- MyDrawString(graphics, "右上->左下",myFont,brush, PointF(170,375));
- //在不同的区域中标注定义渐变方向的矩形
- Pen pen2(Color::Gray, 1);
- for(i=0;i<18;i++)
- graphics.DrawLine(&pen2,0,i*20,320,i*20);
- for(i=0;i<9;i++)
- graphics.DrawLine(&pen2,i* 40,0,i*40,360);
- }
- //渐变线偏转角度
- void Brush_LinearAngle_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen pen(Color::Blue, 2);
- SolidBrush brush(Color::Black);
- Font myFont(L"Arial", 12);
- //定义一个渐变线偏转角度为30度的渐变画刷,大小40*20
- LinearGradientBrush linGrBrush1(
- Rect(0, 0, 40, 20),
- Color::Red,
- Color::Blue,30.0f);
- //定义一个渐变线偏转角度为45度的渐变画刷
- LinearGradientBrush linGrBrush2(
- Rect(0, 0, 40, 20),
- Color::Red,
- Color::Blue,45.0f);
- //定义一个渐变线偏转角度为90度的渐变画刷
- LinearGradientBrush linGrBrush3(
- Rect(0, 0, 40, 20),
- Color::Red,
- Color::Blue,90.0f);
- //定义一个渐变线偏转角度为180度的渐变画刷
- LinearGradientBrush linGrBrush4(
- Rect(0, 0, 40, 20),
- Color::Red,
- Color::Blue,180.0f);
- int i=0;
- //使用偏转角度为30度的渐变画刷填充区域
- graphics.FillRectangle(&linGrBrush1, Rect(i*160,0,160,160));
- graphics.DrawRectangle(&pen, Rect(i*160,0,160,160));
- MyDrawString(graphics, "30度偏转",myFont,brush, PointF(20,165));
- i+=1;
- //重置绘图平面原点
- graphics.SetRenderingOrigin(160,0);
- //使用偏转角度为45度的渐变画刷填充区域
- graphics.FillRectangle(&linGrBrush2, Rect(i*160,0,160,160));
- graphics.DrawRectangle(&pen, Rect(i*160,160,0,160));
- MyDrawString(graphics, "45度偏转",myFont,brush, PointF(170,165));
- //重置绘图平面原点
- graphics.SetRenderingOrigin(0,200);
- //使用偏转角度为90度的渐变画刷填充区域
- graphics.FillRectangle(&linGrBrush3, Rect(0,200,160,160));
- graphics.DrawRectangle(&pen, Rect(0,200,200,160));
- MyDrawString(graphics, "90度偏转",myFont,brush, PointF(0,375));
- graphics.SetRenderingOrigin(160,200);
- graphics.FillRectangle(&linGrBrush4, Rect(160,200,160,160));
- //使用偏转角度为180度的渐变画刷填充区域
- graphics.DrawRectangle(&pen, Rect(160,200,160,160));
- MyDrawString(graphics, "180度偏转",myFont,brush, PointF(170,375));
- //在不同的区域中标注定义渐变方向的矩形
- Pen pen2(Color::Gray, 1);
- for(i=0;i<18;i++)
- graphics.DrawLine(&pen2,0,i*20,320,i*20);
- for(i=0;i<9;i++)
- graphics.DrawLine(&pen2,i* 40,0,i*40,360);
- }
- //多色渐变画刷
- void Brush_LinearInterpolation_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //定义三种参与渐变的色彩
- Color colors[] =
- {
- Color::Red, // 红色
- Color::Green,//过渡色为绿色
- Color::Blue // 蓝色
- };
- float positions[] =
- {
- 0.0f, // 由红色起
- 0.3f, // 绿色始于画刷长度的三分之一
- 1.0f // 到蓝色止
- };
- //构造一条从黑色到白色的渐变画刷
- LinearGradientBrush linGrBrush(
- Point(0, 0),
- Point(180, 0),
- Color::Black,Color::White);
- //设置渐变画刷的多色渐变信息
- //linGrBrush.InterpolationColors=clrBlend;
- linGrBrush.SetInterpolationColors(colors, positions, 3);
- //使用多色渐变画刷填充目标区域
- graphics.FillRectangle(&linGrBrush, 0, 0, 180, 100);
- //使用普通的方法实现多色渐变
- //由红到绿,长度60
- LinearGradientBrush linGrBrush1(
- Point(0, 0),
- Point(60, 0),
- Color::Red,
- Color::Green);
- //由绿到蓝,长度120
- LinearGradientBrush linGrBrush2(
- Point(60, 0),
- Point(181, 0),
- Color::Green,
- Color::Blue);
- //分别使用两个画刷填充两个相邻区域,形成多色渐变
- graphics.FillRectangle(&linGrBrush1, 0, 120, 60, 100);
- graphics.FillRectangle(&linGrBrush2, 60, 120, 120, 100);
- }
- //自定义渐变过程:三角形
- void Brush_LinearCustomize_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush brush(Color::Black);
- Font myFont(L"宋体", 12);
- //定义一个双色渐变画刷
- LinearGradientBrush linGrBrush(
- Point(0, 0),
- Point(250, 0),
- Color::Red,
- Color::Blue);
- //绘制两条使用默认渐变方式填充的矩形,以用作比较
- graphics.FillRectangle(&linGrBrush, 0, 0, 250, 15);
- graphics.FillRectangle(&linGrBrush, 500, 0, 250, 15);
- //依次改变合成点位置
- int row=1;
- for(float i=0.0f;i<1.0f;i+=0.1f)
- {
- linGrBrush.SetBlendTriangularShape(i);
- graphics.FillRectangle(&linGrBrush, 0, row*15, 250, 15);
- row++;
- }
- MyDrawString(graphics, "改变合成点位置",
- myFont,brush, PointF(40,200));
- //依次改变彩色合成因子
- row=1;
- for(i=0.0f;i<1.0f;i+=0.1f)
- {
- //将合成的相对位置设置在整个区域的50%处
- linGrBrush.SetBlendTriangularShape (0.5f,i);
- graphics.FillRectangle(&linGrBrush, 500, row*15, 250, 15);
- row++;
- }
- MyDrawString(graphics, "改变色彩合成因子",
- myFont,brush, PointF(540,200));
- }
- //基于钟形曲线的渐变画刷
- void Brush_LinearGradientBrush_BellShape_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Rect myRect(10, 10, 150, 75);
- //定义线性渐变画刷
- LinearGradientBrush myLGBrush(
- myRect,Color::Blue,Color::Red,0.0f,true);
- //使用默认的线性渐变画刷填充椭圆
- graphics.FillEllipse(&myLGBrush, myRect);
- //将渐变过程设置成基于钟形曲线的渐变
- myLGBrush.SetBlendBellShape(.5f, 1.0f);
- //使用自定义渐变过程的画刷填充椭圆
- graphics.TranslateTransform(160,0);
- graphics.FillEllipse(&myLGBrush, myRect);
- }
- void Brush_PathGradientBrush_Star_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen pen(Color::Gray,1);
- SolidBrush pthGrBrush(Color::Red);
- SolidBrush blackbrush(Color::Blue);
- graphics.TranslateTransform(20,20);
- //构造五星的10个边的端点坐标
- Point points[] =
- {
- Point(75, 0), Point(100, 50),
- Point(150, 50), Point(112, 75),
- Point(150, 150), Point(75, 100),
- Point(0, 150), Point(37, 75),
- Point(0, 50), Point(50, 50),
- Point(75, 0)
- };
- // 创建路径
- GraphicsPath path;
- //在路径中加入直线
- path.AddLines(points, sizeof(points)/sizeof(Point));
- //填充路径
- graphics.FillPath(&pthGrBrush, &path);
- //绘制边界
- graphics.DrawLines(&pen, points, sizeof(points)/sizeof(Point));
- //绘制定义10个边的端点
- for(int i=0;i<10;i++)
- //每个圆点的直径为10
- graphics.FillEllipse(&blackbrush, points[i].X-5,points[i].Y-5,10,10);
- }
- //使用路径渐变画刷绘制五星
- void Brush_PathGradientBrush_Star2_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //构造五星的10个边的端点坐标
- Point points[] =
- {
- Point(75, 0), Point(100, 50),
- Point(150, 50), Point(112, 75),
- Point(150, 150), Point(75, 100),
- Point(0, 150), Point(37, 75),
- Point(0, 50), Point(50, 50),
- Point(75, 0)
- };
- // 创建路径
- GraphicsPath path;
- //在路径中添加直线
- path.AddLines(points, sizeof(points)/sizeof(Point));
- //创建路径渐变画刷
- PathGradientBrush pthGrBrush(&path);
- //设置中心点色彩(终点色)
- pthGrBrush.SetCenterColor(Color::Red);
- //设置每个端点的色彩(终点色)
- Color colors[] =
- {
- Color::Black, Color::Green,
- Color::Blue, Color::White,
- Color::Black, Color::Green,
- Color::Blue, Color::White,
- Color::Black, Color::Green
- };
- //设置路径渐变画刷的边缘色
- int clrCount=sizeof(colors)/sizeof(Color);
- pthGrBrush.SetSurroundColors(colors, &clrCount);
- // 填充目标路径
- graphics.FillPath(&pthGrBrush, &path);
- //将中心色及边界色都设置成随机色,查看渐变效果
- for(int z=0;z<10;z++)
- {
- //在水平方向上平移绘图平面
- graphics.TranslateTransform(200.0f, 0.0f);
- //设置中心点色彩为随机色
- pthGrBrush.SetCenterColor(_MakeA_RGB(rand_Next(255)%155,rand_Next(255)%255,rand_Next(255)%255));
- //使用原有的边缘色
- pthGrBrush.SetSurroundColors(colors, &clrCount);
- graphics.FillPath(&pthGrBrush, &path);
- }
- }
- //使用多个路径渐变画刷
- void Brush_Using_MorePathGradientBrush_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //定义一个六边形,边长为50
- float fHalf = 50 * (float)sin(120.0f/360.0f*PI);
- PointF pts[] =
- {
- PointF( 50, 0),
- PointF( 50 * 1.5f, 0),
- PointF( 50, 0),
- PointF( 50 / 2, -fHalf),
- PointF(-50 / 2, -fHalf),
- PointF(-50, 0),
- PointF(-50 * 1.5f, 0),
- PointF(-50, 0),
- PointF(-50 / 2, fHalf),
- PointF( 50 / 2, fHalf)
- };
- //构造六边形渐变画刷
- PathGradientBrush pgbrush1(pts, 10);
- //在水平和垂直方向上平移六边形的顶点
- for (int i = 0; i <10; i++)
- {
- pts[i].X += 50*1.5f;
- pts[i].Y += fHalf;
- }
- //根据改变坐标后的点重新生成路径渐变画刷
- PathGradientBrush pgbrush2(pts, 10);
- //设置路径渐变画刷的翻转方式为平铺
- pgbrush1.SetWrapMode(WrapModeTile);
- pgbrush2.SetWrapMode(WrapModeTile);
- //分别设置两个画刷的中心点色彩为红、绿色
- pgbrush1.SetCenterColor(Color::Red);
- pgbrush2.SetCenterColor(Color::Green);
- //填充当前窗口
- graphics.FillRectangle(&pgbrush1, 0, 0,MyClient.Width,MyClient.Height);
- //在上次未被填充的区域中再次填充当前窗口的空白部份
- graphics.FillRectangle(&pgbrush2, 0, 0,MyClient.Width,MyClient.Height);
- }
- //路径渐变画刷的填充方式
- void Brush_PathGradientBrush_WrapMode_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen pen(Color::Blue, 2);
- SolidBrush brush(Color::Black);
- Font myFont(L"Arial", 12);
- //定义五星的边线坐标,为演示翻转效果,将五星的一角拉长
- Point points[] =
- {
- Point(75, 0), Point(100, 50),
- Point(150, 50), Point(112, 75),
- Point(150, 150), Point(75, 100),
- Point(0, 190), Point(37, 75),
- Point(10, 50), Point(50, 50)
- };
- GraphicsPath path;
- path.AddLines(points, sizeof(points)/sizeof(Point));
- // 构造路径渐变画刷
- PathGradientBrush pthGrBrush(&path);
- //设置中心点色彩(终点色)
- pthGrBrush.SetCenterColor(Color::Red);
- //设置每个端点的色彩(终点色)
- Color colors[]=
- {
- Color::Black, Color::Green,
- Color::Blue, Color::White,
- Color::Black, Color::Green,
- Color::Blue, Color::White,
- Color::Black, Color::Green
- };
- int clrCount=sizeof(colors)/sizeof(Color);
- pthGrBrush.SetSurroundColors(colors, &clrCount);
- //缩小画刷
- pthGrBrush.ScaleTransform(0.2f,0.2f);
- int i=0;
- //对渐变画刷使用平铺排列方式(默认方式)
- pthGrBrush.SetWrapMode(WrapModeTile);
- graphics.FillRectangle(&pthGrBrush, Rect(i*120,0,120,120));
- graphics.DrawRectangle(&pen, Rect(i*120,0,120,120));
- MyDrawString(graphics, "Tile",myFont,brush, PointF(20,125));
- i+=1;
- pthGrBrush.SetWrapMode(WrapModeTileFlipX);
- graphics.FillRectangle(&pthGrBrush, Rect(i*120,0,120,120));
- graphics.DrawRectangle(&pen, Rect(i*120,0,120,120));
- MyDrawString(graphics, "TileFlipX",myFont,brush, PointF(170,125));
- //对渐变画刷使用垂直翻转排列方式
- pthGrBrush.SetWrapMode(WrapModeTileFlipY);
- graphics.FillRectangle(&pthGrBrush, Rect(0,200,120,120));
- graphics.DrawRectangle(&pen, Rect(0,200,120,120));
- MyDrawString(graphics, "TileFlipY",myFont,brush, PointF(0,325));
- //对渐变画刷使用水平、垂直同时翻转排列方式
- pthGrBrush.SetWrapMode(WrapModeTileFlipXY);
- graphics.FillRectangle(&pthGrBrush, Rect(120,200,120,120));
- graphics.DrawRectangle(&pen, Rect(120,200,120,120));
- MyDrawString(graphics, "TileFlipXY",myFont,brush, PointF(170,325));
- //输出路径的约束矩形、中心点信息
- RectF rect;
- //获取画刷的约束矩形对象
- pthGrBrush.GetRectangle(&rect);
- PointF CenterPoint;
- //获取画刷的中心点信息
- pthGrBrush.GetCenterPoint(&CenterPoint);
- CString tmp;
- //格式化字符串
- tmp.Format("当前约束矩形的左上坐标为(%f,%f),宽度=%f 高度=%f\n当前渐变路径的中心点坐标为(%.2f,%.2f)",
- rect.X,rect.Y,rect.Height,rect.Width,
- CenterPoint.X,CenterPoint.Y);
- //输出中心点及约束矩形对象的信息
- MyDrawString(graphics, tmp,myFont,brush, PointF(0,395));
- }
- //更改路径渐变画刷的中心点
- void Brush_PathGradientBrush_CenterPoint_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //构造一个圆形区域
- GraphicsPath path;
- path.AddEllipse(0, 0, 200, 200);
- SolidBrush brush(_MakeA_RGB(155,Color::Red));
- //构造一个圆形路径渐变画刷
- PathGradientBrush pthGrBrush(&path);
- //设置中心点色彩
- pthGrBrush.SetCenterColor(_MakeA_RGB(155,Color::White));
- Color colors[] ={_MakeA_RGB(55, Color::Blue)};
- //设置边缘色
- int clrCount=1;
- pthGrBrush.SetSurroundColors(colors, &clrCount);
- //填充区域,使用单色画刷
- graphics.FillEllipse(&pthGrBrush, 0, 0, 200, 200);
- //获取中心点色彩
- PointF center(0,0);
- pthGrBrush.GetCenterPoint(¢er);
- //更改画刷的中心点沿圆周的上半部分平移
- for(int i=0;i<200;i++)
- {
- center.X=(REAL)i;
- center.Y=10.f;
- pthGrBrush.SetCenterPoint(center);
- graphics.FillEllipse(&pthGrBrush, 0, 0, 200, 200);
- //标记当前中心点
- graphics.FillEllipse(&brush, center.X, center.Y, 2.f, 2.f);
- }
- //更改画刷的中心点沿圆周的下半部分平移
- for(i=200;i>0;i--)
- {
- center.X=(REAL)i;
- center.Y=190;
- pthGrBrush.SetCenterPoint(center);
- graphics.FillEllipse(&pthGrBrush, 0, 0, 200, 200);
- //标记当前中心点
- graphics.FillEllipse(&brush, center.X, center.Y, 2.f, 2.f);
- }
- }
- //对路径渐变画刷使用多色渐变
- void Brush_PathGradientBrush_InterpolationColors_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush brush(Color::Red);
- //设置三角形的三个点
- Point points[]=
- {
- Point(100, 0),
- Point(200, 200),
- Point(0, 200)
- };
- //创建一个三角形渐变画刷
- PathGradientBrush pthGrBrush(points, 3);
- //-定义参与渐变的色彩
- Color colors[]=
- {
- Color::Red, //红
- Color::Green, //绿
- Color::Blue // 蓝
- };
- //设置合成点的位置
- float pos[] =
- {
- 0.0f, // 红色在区域边界为红色
- 0.4f, //在距离中心40%的位置处使用绿色
- 1.0f //中心点使用蓝色
- };
- //设置渐变的过渡色
- pthGrBrush.SetInterpolationColors(colors, pos, 3);
- //填充区域
- graphics.FillRectangle(&pthGrBrush, 0, 0, 300, 300);
- //标记中心点
- PointF centerpoint(0,0);;
- pthGrBrush.GetCenterPoint(¢erpoint);
- graphics.FillEllipse(&brush, centerpoint.X-5.f, centerpoint.Y-5.f, 10.f, 10.f);
- }
- //更改路径渐变画刷的焦点缩放比例
- void Brsuh_PathGradietBrush_Focus_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen pen(Color::Black,3);
- SolidBrush brush(Color::Black);
- Font myFont(L"Arial", 12);
- //指定三角形三点坐标,创建三角形渐变画
- Point points[]=
- {
- Point(100, 0),
- Point(200, 200),
- Point(0, 200)
- };
- PathGradientBrush pthGrBrush(points, 3);
- //指明渐变色
- Color colors[] = {Color::Red, Color::Blue};
- //指明色彩合成位置
- float relativePositions[]=
- {
- 0.0f, // 红色做边界
- 1.0f // 蓝色为中心
- };
- //设置渐变色
- pthGrBrush.SetInterpolationColors(colors, relativePositions, 2);
- //使用默认缩放因子进行填充
- graphics.FillRectangle(&pthGrBrush, 0, 0, 200, 200);
- //获取默认缩放因子
- PointF FocusScales(0,0);
- pthGrBrush.GetFocusScales(&FocusScales.X, &FocusScales.Y);
- //输出缩放信息
- CString tmp;
- tmp.Format("水平:x=%.2f\n垂直:y=%.2f",FocusScales.X,FocusScales.Y);
- MyDrawString(graphics, tmp,myFont,brush, PointF(0,210));
- //平移绘图平面
- graphics.TranslateTransform(200,0);
- //更改缩放因子
- FocusScales.X=0.6f;
- FocusScales.Y=0.6f;
- pthGrBrush.SetFocusScales(FocusScales.X,FocusScales.Y);
- graphics.FillRectangle(&pthGrBrush, 0, 0, 200, 200);
- MyDrawString(graphics, "水平:x=0.6\n垂直:y=0.6", myFont,brush, PointF(0,210));
- //水平缩放不等于垂直缩放时特例
- graphics.TranslateTransform(200,0);
- //更改缩放因子
- FocusScales.X=0.1f;
- FocusScales.Y=0.8f;
- pthGrBrush.SetFocusScales(FocusScales.X,FocusScales.Y);
- graphics.FillRectangle(&pthGrBrush,0, 0, 200, 200);
- MyDrawString(graphics, "水平:x=0.1\n垂直:y=0.8", myFont,brush, PointF(0,210));
- //水平缩放=垂直缩放=1.0
- graphics.TranslateTransform(200,0);
- FocusScales.X=1.0f;
- FocusScales.Y=1.0f;
- pthGrBrush.SetFocusScales(FocusScales.X,FocusScales.Y);
- graphics.FillRectangle(&pthGrBrush, 0, 0, 200, 200);
- MyDrawString(graphics, "水平:x=1.0\n垂直:y=1.0", myFont,brush, PointF(0,210));
- //水平缩放不等于垂直缩放时特例:放大原有区域
- graphics.TranslateTransform(200,0);
- FocusScales.X=2.0f;
- FocusScales.Y=1.5f;
- pthGrBrush.SetFocusScales(FocusScales.X,FocusScales.Y);
- graphics.FillRectangle(&pthGrBrush, 0, 0, 200, 200);
- MyDrawString(graphics, "水平:x=2.0\n垂直:y=1.5", myFont,brush, PointF(0,210));
- }
- //路径渐变画刷的变换
- void Brush_PathGradientBrush_Transform_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- // 定义一个三角形路径渐变画刷
- Point pts[]={Point(50, 0), Point(100, 100), Point(0, 100)};
- PathGradientBrush pthGrBrush(pts, 3);
- //对画刷使用垂直翻转方式
- pthGrBrush.SetWrapMode(WrapModeTileFlipY);
- //缩小画刷
- pthGrBrush.ScaleTransform(0.5f, 0.5f);
- //填充当前窗口
- graphics.FillRectangle(&pthGrBrush, 0, 0, MyClient.Width,MyClient.Height);
- //将画刷旋转90度
- pthGrBrush.RotateTransform(90.0f, MatrixOrderAppend);
- //再次填充当前窗口
- graphics.FillRectangle(&pthGrBrush, 0, 0, MyClient.Width,MyClient.Height);
- }
- void Brsuh_LinearGradientBrush_UsingGamma_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- LinearGradientBrush linGrBrush(
- Point(0, 10),
- Point(200, 10),
- Color::Red,
- Color::Blue);
- graphics.FillRectangle(&linGrBrush, 0, 0, 200, 50);
- linGrBrush.SetGammaCorrection(true);
- graphics.FillRectangle(&linGrBrush, 0, 60, 200, 50);
- }
- //简单的使用字体示意
- void Font_UsingFontInGDIPlus_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //新建一个字体对话框
- CFontDialog dlg;
- //允许对字体进行色彩设置
- //dlg.ShowColor=true;
- //调用字体选择对话框
- if(dlg.DoModal()!=IDOK)
- return;
- //获取在字体对话框中的字体信息
- LOGFONT lf; dlg.GetCurrentFont(&lf);
- Font myFont(CWideCharString(dlg.GetFaceName()), lf.lfHeight);
- //根据字体色彩创建画刷
- SolidBrush brush(dlg.GetColor());
- //设置文本输出格式:在当前窗口中居中显示
- StringFormat fmt;
- fmt.SetAlignment(StringAlignmentCenter);
- fmt.SetLineAlignment(StringAlignmentCenter);
- //输出文本
- graphics.DrawString(L"输出文本", 4, &myFont, RectF(0,0,MyClient.Width,MyClient.Height),&fmt, &brush);
- }
- //枚举所有的字体系列
- void Font_EnumAllFonts_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush solidBrush(Color::Black);
- FontFamily fontFamily(L"Arial");
- Font font(&fontFamily, 8, FontStyleRegular, UnitPoint);
- //设置文本输出格式
- StringFormat fmt;
- fmt.SetAlignment(StringAlignmentNear);
- fmt.SetLineAlignment(StringAlignmentNear);
- CString tmp="";
- //获取所有已经安装的字体系列
- InstalledFontCollection installedFontCollection;
- int ffcount=installedFontCollection.GetFamilyCount();
- FontFamily *fontfamily = ::new FontFamily[ffcount];
- installedFontCollection.GetFamilies(ffcount, fontfamily, &ffcount);
- int index=0;
- //访问fontfamily数组的每一个成员
- for( int i=0; i<ffcount; i++)
- {
- //获取当前字体系列名称
- WCHAR name[LF_FACESIZE];
- fontfamily[i].GetFamilyName(name);
- CString t=name;
- CString s; s.Format(_T("%s%s"), (i? _T(", "):_T("")), t);
- tmp+=s;
- index++;
- }
- ::delete []fontfamily;
- //在窗口中输出所有的字体系列名
- RectF r(0,10,MyClient.Width,MyClient.Height);
- MyDrawString(graphics, tmp, font, solidBrush, r, fmt);
- //输出字体系列信息
- tmp.Format("在你的系统中已经安装的字体有%d种,其名称分别为:\n",index);
- MyDrawString(graphics, tmp,font,solidBrush, Point(0,0));
- }
- //启用增强型选择对话框表单
- void Font_EnhanceFontDialog_Click()
- {
- MyDrawString(GetGraphics(), "无增强型选择对话框表单",
- Font(L"Arial",12), SolidBrush(Color::Red), Point(0,0));
- /*
- Form3 f= Form3();
- f.ShowDialog();
- */
- }
- //使用不同的字体边缘处理方式
- void Font_UsingTextRenderHint_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush brs(Color::Black);
- FontFamily fontFamily(L"Arial");
- Font font(&fontFamily, 60, FontStyleRegular, UnitPixel);
- //使用不同的边缘处理方式输出六行文本
- for (int i=TextRenderingHintSystemDefault; i<=TextRenderingHintClearTypeGridFit; i++)
- {
- //设置边缘处理方式
- graphics.SetTextRenderingHint((TextRenderingHint)i);
- //输出文本
- MyDrawString(graphics, "Render",font,brs, PointF(0, 0));
- //绘图平面下移地行
- graphics.TranslateTransform(0, font.GetHeight(&graphics));
- }
- }
- //使用私有字体集合
- void Font_Privatefontcollection_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- PointF pointF(0.0f, 10.0f);
- SolidBrush solidBrush(Color::Red);
- //创建私有字体集
- PrivateFontCollection privateFontCollection;
- //在私有字体集中追迷你繁启体字库文件"繁启体.TTF"
- WCHAR fontfile[]= L"c:\\繁启体.TTF";
- if (Ok != privateFontCollection.AddFontFile(fontfile))
- {
- AfxMessageBox("字体文件加载失败\n出错啦!");
- return;
- }
- //从私有字体集合中构造繁启体,大小为35像素
- FontFamily pFontFamily(L"迷你繁启体", &privateFontCollection);
- Font tmpFont(&pFontFamily, 35);
- //输出繁启体文字
- MyDrawString(graphics, "沉舟侧畔千帆过", tmpFont,solidBrush, pointF);
- //垂直坐标一移一行,行高为 字体的高度
- pointF.Y +=tmpFont.GetHeight(&graphics);
- MyDrawString(graphics, "病树前头万木春", tmpFont,solidBrush, pointF);
- }
- //在私有字体集合中使用多种字体
- void Font_Privatefontcollection2_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- PointF pointF(0.0f, 10.0f);
- SolidBrush solidBrush(Color::Red);
- //创建私有字体集
- PrivateFontCollection privateFontCollection;
- //在私有字体集中追加三个不同的字体文件
- privateFontCollection.AddFontFile(L"C:\\WINDOWS\\Fonts\\STCAIYUN.TTF");
- privateFontCollection.AddFontFile(L"C:\\WINDOWS\\Fonts\\SIMLI.TTF");
- privateFontCollection.AddFontFile(L"C:\\WINDOWS\\Fonts\\Arial.ttf");
- FontFamily fontfamily[200];
- int ffcount=privateFontCollection.GetFamilyCount();
- if (ffcount>200) ffcount=200;
- privateFontCollection.GetFamilies(ffcount, fontfamily, &ffcount);
- int index=0;
- //访问私有字体集合中的所有字体系列
- for(int i=0; i<ffcount; i++)
- {
- Font tmpFont(&fontfamily[i], 35);
- //获取当前字体系列名称
- WCHAR name[LF_FACESIZE];
- fontfamily[i].GetFamilyName(name);
- CString s; s.Format(_T("字体名: %s"), name);
- //输出繁启体文字
- MyDrawString(graphics, s, tmpFont, solidBrush, pointF);
- graphics.TranslateTransform(0, tmpFont.GetHeight(&graphics));
- }
- /*
- //通过访问PrivateFontCollection类的Families数组成员来字体
- for(int index=0;index< privateFontCollection..Families.Length;index++)
- {
- Font tmpFont(privateFontCollection.Families[index].Name,35);
- //输出繁启体文字
- MyDrawString(graphics, "字体名: "+privateFontCollection.Families[index].Name,
- tmpFont,solidBrush, pointF);
- graphics.TranslateTransform(0,tmpFont.Height);
- }
- */
- }
- //检查字体风格是否可用
- void Font_IsStyleAvailable_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen pen(Color::Gray);
- PointF pointF(10.0f, 0.0f);
- SolidBrush solidBrush(Color::Black);
- //信息输出所使用的字体
- Font msgfont(L"Arial",12);
- //创建私有字体集
- PrivateFontCollection privateFontCollection;
- //在私有字体集中追加三个字体文件
- //黑体
- privateFontCollection.AddFontFile(L"C:\\windows\\Fonts\\simhei.ttf");
- //Courier New字体
- privateFontCollection.AddFontFile(L"C:\\windows\\Fonts\\CourBI.ttf");
- //华文隶书
- privateFontCollection.AddFontFile(L"C:\\windows\\Fonts\\STLITI.ttf");
- CString familyName;
- CString tmpmsg;
- //查询私有字体集中字体系列数量
- int count=privateFontCollection.GetFamilyCount();
- //获取字体系列数组
- FontFamily *pFontFamily = ::new FontFamily[count];
- //将字体系列中的字体名中加入pFontFamily中
- privateFontCollection.GetFamilies(count, pFontFamily, &count);
- /*对私有字体集中每一个字体系列进行五种字体风格
- 的查询,如果字体风格可用,使用该风格进行文本绘制
- */
- for(int j = 0; j< count; ++j)
- {
- //从字体系列中获取字体名
- WCHAR name[LF_FACESIZE];
- pFontFamily[j].GetFamilyName(name);
- familyName=name;
- // 常规风格是否可用
- if(pFontFamily[j].IsStyleAvailable(FontStyleRegular))
- {
- tmpmsg=" 字体的常规风格可用";
- //构造字体
- FontFamily fm(CWideCharString(familyName), &privateFontCollection);
- Font tmpFont(&fm, 12, FontStyleRegular, UnitPixel);
- //输出使用常规风格的字体
- MyDrawString(graphics, familyName+tmpmsg,tmpFont,solidBrush,pointF );
- //垂直坐标一移一行,行高为 字体的高度
- pointF.Y += tmpFont.GetHeight(&graphics);
- }
- else
- {
- MyDrawString(graphics, familyName+tmpmsg+
- CString(L" 字体的常规风格不可用"),msgfont,solidBrush,pointF);
- pointF.Y += msgfont.GetHeight(&graphics);
- }
- tmpmsg.Empty();
- // 粗体风格是否可用
- if(pFontFamily[j].IsStyleAvailable(FontStyleBold))
- {
- tmpmsg=" 字体的粗体风格可用";
- //构造字体
- FontFamily fm(CWideCharString(familyName), &privateFontCollection);
- Font tmpFont(&fm, 12, FontStyleBold, UnitPixel);
- //输出使用常规风格的字体
- MyDrawString(graphics, familyName+tmpmsg,tmpFont,solidBrush,pointF );
- //垂直坐标一移一行,行高为 字体的高度
- pointF.Y += tmpFont.GetHeight(&graphics);
- }
- else
- {
- MyDrawString(graphics, familyName+tmpmsg+
- CString(" 字体的粗体风格不可用"),msgfont,solidBrush,pointF);
- pointF.Y += msgfont.GetHeight(&graphics);
- }
- tmpmsg.Empty();
- // 斜体风格是否可用
- if(pFontFamily[j].IsStyleAvailable(FontStyleItalic))
- {
- tmpmsg=" 字体的斜体风格可用";
- //构造字体
- FontFamily fm(CWideCharString(familyName),&privateFontCollection);
- Font tmpFont(&fm, 12, FontStyleItalic, UnitPixel);
- //输出使用常规风格的字体
- MyDrawString(graphics, familyName+tmpmsg,tmpFont,solidBrush,pointF );
- //垂直坐标一移一行,行高为 字体的高度
- pointF.Y += tmpFont.GetHeight(&graphics);
- }
- else
- {
- MyDrawString(graphics, familyName+tmpmsg+
- " 字体的斜体风格不可用",msgfont,solidBrush,pointF);
- pointF.Y += msgfont.GetHeight(&graphics);
- }
- tmpmsg.Empty();
- // 查询下划线风格是否可用
- if(pFontFamily[j].IsStyleAvailable(FontStyleUnderline))
- {
- tmpmsg=" 字体的下划线风格可用";
- //构造字体
- FontFamily fm(CWideCharString(familyName),&privateFontCollection);
- Font tmpFont(&fm, 12, FontStyleUnderline, UnitPixel);
- //输出使用常规风格的字体
- MyDrawString(graphics, familyName+tmpmsg,tmpFont,solidBrush,pointF );
- //垂直坐标一移一行,行高为 字体的高度
- pointF.Y += tmpFont.GetHeight(&graphics);
- }
- else
- {
- MyDrawString(graphics, familyName+tmpmsg+
- " 字体的下划线风格不可用",msgfont,solidBrush,pointF);
- pointF.Y += msgfont.GetHeight(&graphics);
- }
- tmpmsg.Empty();
- // 查询强调线风格是否可用
- if(pFontFamily[j].IsStyleAvailable(FontStyleStrikeout))
- {
- tmpmsg=" 字体的强调线风格可用";
- //构造字体
- FontFamily fm(CWideCharString(familyName),&privateFontCollection);
- Font tmpFont(&fm, 12, FontStyleStrikeout, UnitPixel);
- //输出使用常规风格的字体
- MyDrawString(graphics, familyName+tmpmsg,tmpFont,solidBrush,pointF );
- //垂直坐标一移一行,行高为 字体的高度
- pointF.Y += tmpFont.GetHeight(&graphics);
- }
- else
- {
- MyDrawString(graphics, familyName+tmpmsg+
- " 字体的强调线风格不可用",msgfont,solidBrush,pointF);
- pointF.Y += msgfont.GetHeight(&graphics);
- }
- tmpmsg.Empty();
- // 在不同的字体系列之间加上间隔线
- graphics.DrawLine(&pen,0,(int )pointF.Y,400,(int)pointF.Y);
- pointF.Y += 10.0f;
- }
- ::delete []pFontFamily;
- }
- //获取字体的大小
- void Font_Size_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- PointF pointF(0.0f, 0.0f);
- SolidBrush solidBrush(Color::Black);
- CString infoString;
- int ascent;
- float ascentPixel;
- int descent;
- float descentPixel;
- int lineSpacing;
- float lineSpacingPixel;
- FontFamily fontFamily(L"Arial");
- //创建一个大小为16像素的Aria字体
- Font font(&fontFamily, 16, FontStyleRegular, UnitPixel);
- Font font2(&fontFamily, 14, FontStyleRegular, UnitPixel);
- // 显示字体大小
- infoString.Format("font的Size属性值为%.2f",font.GetSize());
- MyDrawString(graphics, infoString,font2,solidBrush,pointF);
- // 下移一行
- pointF.Y += font2.GetHeight(&graphics);
- //显示字体高度:像素
- infoString.Format("font的Height属性值为%.2f", font.GetHeight(&graphics));
- MyDrawString(graphics, infoString,font2,solidBrush,pointF);
- // 下移一行
- pointF.Y += font2.GetHeight(&graphics);
- // 显示字体系列的高度(设计单位)
- infoString.Format("使用fontFamily.GetEmHeight函数返回的字体高度为 %d 个设计单位。"
- ,fontFamily.GetEmHeight(FontStyleRegular));
- MyDrawString(graphics, infoString,font2,solidBrush,pointF);
- // 下移两行(Height属性的值是字体的高度,单位为像素)
- pointF.Y += 2.0f*font2.GetHeight(&graphics);
- // 获取字体的Ascent(上部距离)
- ascent = fontFamily.GetCellAscent(FontStyleRegular);
- //将上部距离的设计单位转换成像素单位
- ascentPixel=
- font.GetSize() * ascent / fontFamily.GetEmHeight(FontStyleRegular);
- infoString.Format("上部距离为 %d个设计单位, %.2f个像素。",
- ascent, ascentPixel);
- MyDrawString(graphics, infoString,font2,solidBrush,pointF);
- // 下移一行
- pointF.Y += font2.GetHeight(&graphics);
- // 获取字体的Descent(下部距离),设计单位
- descent = fontFamily.GetCellDescent(FontStyleRegular);
- //将下部距离的设计单位转换成像素单位
- descentPixel =
- font.GetSize() * descent / fontFamily.GetEmHeight(FontStyleRegular);
- infoString.Format("下部距离为%d 个设计单位, %.2f个像素。",
- descent, descentPixel);
- MyDrawString(graphics, infoString,font2,solidBrush,pointF);
- // 下移一行
- pointF.Y += font2.GetHeight(&graphics);
- //获取行距(设计单位)
- lineSpacing = fontFamily.GetLineSpacing(FontStyleRegular);
- //将行距的设计单位转换成像素单位
- lineSpacingPixel =
- font.GetSize() * lineSpacing / fontFamily.GetEmHeight(FontStyleRegular);
- infoString.Format("行距为%d 个设计单位, %.2f 像素。",
- lineSpacing, lineSpacingPixel);
- MyDrawString(graphics, infoString,font2,solidBrush,pointF);
- }
- //设置文本输出基线
- void Font_BaseLine_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- FontFamily fontFamily(L"Arial");
- //创建一个大小为60像素的Aria字体
- Font font(&fontFamily, 60, FontStyleRegular, UnitPixel);
- //获取当前窗口的矩形宽、高
- int cy=MyClient.Height;
- int cx=MyClient.Width;
- Pen pen(Color::Blue, 1);
- SolidBrush brush(Color::Blue);
- //将窗口垂直方向的1/2部分作为基线
- float yBaseline = cy / 2.f;
- //绘制基线
- graphics.DrawLine(&pen, PointF(0, yBaseline), PointF((REAL)cx,yBaseline));
- //得到字体高度
- float cyLineSpace = font.GetHeight(&graphics);
- //获取行距
- int iCellSpace = fontFamily.GetLineSpacing(FontStyleRegular);
- //得到上半部分距离
- int iCellAscent = fontFamily.GetCellAscent(FontStyleRegular);
- //计算文本输出的起始位置相对于基线的偏移
- float cyAscent = cyLineSpace * iCellAscent / iCellSpace;
- //从中央向上绘制两条直线,间隔为cyAscent
- graphics.DrawLine(&pen,
- PointF(0, yBaseline-cyAscent), PointF(cx,yBaseline-cyAscent));
- graphics.DrawLine(&pen,
- PointF(0, yBaseline-2.0f*cyAscent), PointF(cx,yBaseline-2.0f*cyAscent));
- //分别在两条基线上输出文本
- MyDrawString(graphics, "AaFfgSs}", font,brush, PointF(0,yBaseline - cyAscent));
- MyDrawString(graphics, "AaFfgSs}",font,brush, PointF(0,yBaseline - 2.0f*cyAscent));
- }
- void Font_DrawString_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //输出文本
- CString msg="示例文本";
- Font myFont(L"宋体", 16);
- RectF layoutRect(50, 50, 200, 50);
- //设置对齐方式
- StringFormat format;
- format.SetAlignment(StringAlignmentCenter);
- SolidBrush blackBrush(Color::Black);
- // 绘制文本
- MyDrawString(graphics, msg, myFont, blackBrush, layoutRect, format);
- // 绘制文本输出的矩形区域
- Pen pen(Color::Black, 3);
- graphics.DrawRectangle(&pen, layoutRect);
- }
- void Font_MeasureString_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //设置一个包含三行文本的字符串
- CString txtOut="123456789\n";
- txtOut+="ABCDEFGHIJKLM\n";
- txtOut+="一二三四五六七八九";
- FontFamily fontFamily(L"Arial");
- //创建两个个大小不同的Aria字体
- Font font(&fontFamily, 30, FontStyleRegular, UnitPixel);
- Font font2(&fontFamily, 14, FontStyleRegular, UnitPixel);
- SolidBrush brush(Color::Black);
- StringFormat fmt;
- fmt.SetAlignment(StringAlignmentCenter);
- fmt.SetLineAlignment(StringAlignmentCenter);
- SizeF stringSize, strSize(1000,1000);
- //测量输出字符串所需要的矩形空间
- CWideCharString txtOutW(txtOut);
- graphics.MeasureString(txtOutW, txtOutW.GetLength(), &font, strSize, &fmt, &stringSize);
- CString tmp;
- tmp.Format("输出字符串所需要的宽度为:%2f 高度为%2f", stringSize.Width,stringSize.Height);
- // 绘制输出文本的限制矩形
- Pen pen(Color::Red);
- graphics.DrawRectangle(&pen,
- 10.0f,10.0f,stringSize.Width,stringSize.Height);
- //输出字符串的测量信息
- MyDrawString(graphics, txtOut, font, brush,
- RectF(10.0f,10.0f,stringSize.Width,stringSize.Height),fmt);
- graphics.TranslateTransform(0,10+stringSize.Height);
- MyDrawString(graphics, tmp,font2,brush, PointF(0,0));
- }
- //计算在指定的区域中显示的字符总数及行数
- void Font_MeasureString2_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- CString txtOut="123456789";
- txtOut+="ABCDEFGHIJKLM";
- txtOut+="一二三四五六七八九";
- FontFamily fontFamily(L"Arial");
- //创建两个个大小不同的Aria字体
- Font font(&fontFamily, 30, FontStyleRegular, UnitPixel);
- Font font2(&fontFamily, 14, FontStyleRegular, UnitPixel);
- //设置文本输出矩形
- RectF layoutRect(10, 10,100,100);
- SolidBrush brush(Color::Black);
- //设置文本显示格式
- StringFormat fmt;
- fmt.SetAlignment(StringAlignmentCenter);
- fmt.SetLineAlignment(StringAlignmentCenter);
- int codepointsFitted=0;
- int linesFilled=0;
- SizeF stringSize;
- //计算指定的区域能够显示的字符总数及行数
- CWideCharString txtOutW(txtOut);
- graphics.MeasureString(txtOutW, txtOutW.GetLength(),
- &font, SizeF(layoutRect.Width,layoutRect.Height),
- &fmt, &stringSize, &codepointsFitted, &linesFilled);
- CString tmp;
- tmp.Format("欲输出的字串共%d个字符\n其中,在指定的输出矩形中\n只输出了%d行共%d个字符",
- txtOutW.GetLength(), linesFilled, codepointsFitted);
- // 绘制指定的文本输出区域
- Pen pen(Color::Blue,2);
- graphics.DrawRectangle(&pen, layoutRect);
- //在指定的区域矩形中显示文本
- MyDrawString(graphics, txtOut, font, brush, layoutRect, fmt);
- //输出字符串的测量信息
- graphics.TranslateTransform(0,10+stringSize.Height);
- MyDrawString(graphics, tmp, font2, brush, PointF(0,0));
- }
- //文本的分栏输出
- void Font_ColumnTextOut_Click()
- {
- CString s_Filename;
- //假设读取 <exePath> ../ReadMe.txt
- TCHAR ModuleDirectory[_MAX_PATH];
- ::GetModuleFileName(NULL, ModuleDirectory, _MAX_PATH);
- TCHAR *p=::_tcsrchr(ModuleDirectory, '\\'); *p=0;
- p=::_tcsrchr(ModuleDirectory, '\\'); *p=0;
- ::_tcscat(ModuleDirectory, "\\ReadMe.txt");
- s_Filename=ModuleDirectory;
- FILE *stream = ::fopen(s_Filename, "rt");
- if (!stream)
- return;
- CString str;
- //读取所有的文件内容
- char buffer[1024]="";
- while (!feof(stream))
- {
- CString s;
- ::fgets(buffer, 1024, stream);
- s=buffer; s.TrimLeft(); s.TrimRight(); s+="\r\n";
- str+=s;
- }
- //关闭文件
- fclose(stream);
- //定义栏宽及栏与栏之间的间隔
- int COLUMWIDTH=256;
- int SPACE=10;
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush brush(Color::Black);
- Font myFont(L"宋体", 10);
- int codepointsFitted=0;
- int linesFilled=0;
- //设置输出格式
- StringFormat format;
- //禁用自动剪载
- format.SetFormatFlags(StringFormatFlagsNoClip);
- //输出文本以单词结束
- format.SetTrimming(StringTrimmingWord);
- CWideCharString strW(str);
- //从左至右按列(栏)输出文本
- for (int x = 0; strW.GetLength()> 0&&x<MyClient.Width; x +=(COLUMWIDTH+SPACE))
- {
- //设置文本对应的矩形区域(一栏)
- RectF layoutRect(x, 0, COLUMWIDTH,MyClient.Height-myFont.GetHeight(&graphics));
- SizeF stringSize;
- //计算指定的区域能够显示的字符总数及行数
- graphics.MeasureString(strW, strW.GetLength(),
- &myFont, SizeF(layoutRect.Width,layoutRect.Height),
- &format, &stringSize, &codepointsFitted, &linesFilled);
- //显示一栏文本
- graphics.DrawString(strW, strW.GetLength(), &myFont, layoutRect, &format, &brush);
- //去掉已经输出的字符,以便下一栏能够正常显示
- strW=strW.Mid(codepointsFitted);
- }
- }
- //设置文本的去尾方式
- void Font_StirngTrimming_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush solidBrush(Color::Black);
- FontFamily fontFamily(L"Times New Roman");
- //用于文本输出的字体
- Font font(&fontFamily, 25, FontStyleRegular, UnitPixel);
- Pen pen(Color::Red);
- StringFormat stringFormat;
- //用于提示信息的字体及格式
- Font font2(&fontFamily, 14, FontStyleRegular, UnitPixel);
- //设置文本输出的格式
- StringFormat msgFormat;
- msgFormat.SetAlignment(StringAlignmentCenter);
- msgFormat.SetLineAlignment(StringAlignmentCenter);
- //提示信息输出区域
- RectF outrect(30, 100, 160, font2.GetHeight(&graphics)*2);
- //去尾方式:Character
- stringFormat.SetTrimming(StringTrimmingCharacter);
- //输出文本
- CString text="One two three four five seven eight nine ten";
- MyDrawString(graphics, text, font, solidBrush,
- RectF(30, 30, 160, 60), stringFormat);
- //绘制文本输出区域
- graphics.DrawRectangle(&pen, 30, 30, 160, 60);
- //输出提示信息
- MyDrawString(graphics, "Character",font2,solidBrush,outrect,
- msgFormat);
- //绘图平面沿水平方向平移160个像素
- graphics.TranslateTransform(160,0);
- //更改去尾方式:Word
- stringFormat.SetTrimming(StringTrimmingWord);
- MyDrawString(graphics, text,font,solidBrush,Rect(30, 30, 160, 60), stringFormat);
- graphics.DrawRectangle(&pen, 30, 30, 160, 60);
- MyDrawString(graphics, "Word",font2,solidBrush,outrect, msgFormat);
- //更改去尾方式:EllipsisCharacter
- graphics.TranslateTransform(160,0);
- stringFormat.SetTrimming(StringTrimmingEllipsisCharacter);
- MyDrawString(graphics, text,font,solidBrush, Rect(30, 30, 160, 60), stringFormat);
- graphics.DrawRectangle(&pen, 30, 30, 160, 60);
- MyDrawString(graphics, "EllipsisCharacter",font2,solidBrush,outrect, msgFormat);
- //更改去尾方式:EllipsisWord
- graphics.TranslateTransform(160,0);
- stringFormat.SetTrimming(StringTrimmingEllipsisWord);
- MyDrawString(graphics, text,font,solidBrush, Rect(30, 30, 160, 60), stringFormat);
- graphics.DrawRectangle(&pen, 30, 30, 160, 60);
- MyDrawString(graphics, "EllipsisWord",font2,solidBrush,outrect, msgFormat);
- //更改去尾方式:EllipsisPath
- graphics.TranslateTransform(160,0);
- stringFormat.SetTrimming(StringTrimmingEllipsisPath);
- MyDrawString(graphics, text,font,solidBrush, Rect(30, 30, 160, 60), stringFormat);
- graphics.DrawRectangle(&pen, 30, 30, 160, 60);
- MyDrawString(graphics, "EllipsisPath",font2,solidBrush,outrect, msgFormat);
- }
- //文本输出的剪裁处理
- void Font_TextOutClip_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush solidBrush(Color::Black);
- FontFamily fontFamily(L"宋体");
- Font font(&fontFamily, 25, FontStyleRegular, UnitPixel);
- Pen pen(Color::Red);
- StringFormat stringFormat;
- RectF outtext(10, 10, 120, 45);
- //使用默认值输出文本
- CString text="文本的剪裁示意";
- MyDrawString(graphics, text,font,solidBrush,outtext,
- stringFormat);
- //绘制文本输出区域
- graphics.DrawRectangle(&pen, outtext.X,
- outtext.Y,outtext.Width,outtext.Height);
- //在水平方向平移170个像素更
- graphics.TranslateTransform(120,0);
- RectF out2=outtext;
- //将输出区域的高度调整为字体高度的整数倍
- out2.Height=font.GetHeight(&graphics)*2;
- MyDrawString(graphics, text,font,solidBrush,out2, stringFormat);
- //绘制文本输出区域
- graphics.DrawRectangle(&pen, out2.X,
- out2.Y,out2.Width,out2.Height);
- graphics.TranslateTransform(120,0);
- //在输出文本时不使用剪截
- stringFormat.SetFormatFlags(StringFormatFlagsNoClip);
- MyDrawString(graphics, text,font,solidBrush,outtext, stringFormat);
- //绘制文本输出区域
- graphics.DrawRectangle(&pen, outtext.X,outtext.Y, outtext.Width,outtext.Height);
- }
- //测量文本局部输出区域
- void Font_MeasureCharacterRanges_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- // 设置绘制文本、填充区域的画刷
- SolidBrush TextBrush(Color::Red);
- SolidBrush RegionBrush(_MakeA_RGB(100, Color::Black));
- Pen blackPen(Color::Black);
- // 使用三个宽度不同的矩形区域绘制文本,示意不同局部区域测量结果
- RectF layoutRect_A(20.0f, 20.0f, 120.0f, 180.0f);
- RectF layoutRect_B(140.0f, 20.0f, 140.0f, 180.0f);
- RectF layoutRect_C(280.0f, 20.0f, 180.0f, 180.0f);
- //添加需要输出的字串
- CString ts="秦时明月汉时关,";
- ts+="万里长征人未还。";
- ts+="但使龙城飞将在,";
- ts+="不教胡马度阴山。";
- // 在字串中需要局部测量的文本定义区域:每行诗的最后三个字
- CharacterRange charRanges[] =
- {
- CharacterRange(4, 3),
- CharacterRange(12, 3),
- CharacterRange(20, 3),
- CharacterRange(28, 3)
- };
- // 绘制文本时使用的字体及大小
- FontFamily fontFamily(L"华文新魏");
- Font myFont(&fontFamily, 25, FontStyleItalic, UnitPixel);
- StringFormat strFormat;
- //指向需要局部测量区域的数组
- Region CharRangeRegions[4];
- //在字体格式中设置需要局部测量的区域
- strFormat.SetMeasurableCharacterRanges(4, charRanges);
- //在矩形layoutRect_A中对文本进行局部测量,测量的结果保存在CharRangeRegions中
- CWideCharString tsW(ts);
- graphics.MeasureCharacterRanges(tsW,tsW.GetLength(),&myFont,layoutRect_A, &strFormat,4,CharRangeRegions);
- //输出文本
- MyDrawString(graphics, ts, myFont,TextBrush,layoutRect_A, strFormat);
- //绘制文本输出边框
- graphics.DrawRectangle(&blackPen, layoutRect_A.X,layoutRect_A.Y,layoutRect_A.Width,layoutRect_A.Height);
- short i;
- //填充由CharRangeRegions保存的区域
- for ( i = 0; i < 4; i++)
- {
- graphics.FillRegion(&RegionBrush, CharRangeRegions+i);
- }
- //在矩形layoutRect_B中对文本进行局部测量,测量的结果保存在CharRangeRegions中
- graphics.MeasureCharacterRanges(tsW, tsW.GetLength(), &myFont, layoutRect_B, &strFormat,4,CharRangeRegions);
- //输出文本
- MyDrawString(graphics, ts, myFont,TextBrush,layoutRect_B, strFormat );
- graphics.DrawRectangle(&blackPen,
- layoutRect_B.X,layoutRect_B.Y,layoutRect_B.Width,layoutRect_B.Height);
- //填充区域
- for ( i = 0; i <4; i++)
- {
- graphics.FillRegion(&RegionBrush, CharRangeRegions+i);
- }
- //在矩形layoutRect_c中对文本进行局部测量,测量的结果保存在CharRangeRegions中
- graphics.MeasureCharacterRanges(tsW,tsW.GetLength(),&myFont,layoutRect_C, &strFormat,4,CharRangeRegions);
- //输出文本
- MyDrawString(graphics, ts, myFont,TextBrush,layoutRect_C, strFormat);
- graphics.DrawRectangle(&blackPen,
- layoutRect_C.X,layoutRect_C.Y,layoutRect_C.Width,layoutRect_C.Height);
- //填充区域
- for ( i = 0; i <4; i++)
- {
- graphics.FillRegion(&RegionBrush, CharRangeRegions+i);
- }
- }
- //控件文本输出方向
- void Font_TextoutDirection_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //添加需要输出的字串
- CString ts="秦时明月汉时关,";
- ts+="万里长征人未还。";
- ts+="但使龙城飞将在,";
- ts+="不教胡马度阴山。";
- //设置输出字体
- FontFamily fontFamily(L"幼圆");
- Font myFont(&fontFamily, 20, FontStyleBold, UnitPixel);
- SolidBrush brush(Color::Red);
- Pen pen(Color::Black);
- //设置输出区间
- Rect f(10,10,120,myFont.GetHeight(&graphics)*8);
- //构造StringFormat对象
- StringFormat strFormat;
- //从至左输出文本
- int flag=StringFormatFlagsDirectionRightToLeft;
- //文本在竖直方向上输出
- flag|=StringFormatFlagsDirectionVertical;
- //设置输出格式标志
- strFormat.SetFormatFlags(flag);
- //设置文本对齐方式
- strFormat.SetAlignment(StringAlignmentCenter);
- strFormat.SetLineAlignment(StringAlignmentCenter);
- //绘制输出区间及文本
- graphics.DrawRectangle(&pen,f);
- MyDrawString(graphics, ts,myFont,brush,f,strFormat);
- }
- //设置文本对齐方式
- void Font_TextAlignment_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //将当前窗口大小信息保存
- Rect rect(0, 0,
- MyClient.Width, MyClient.Height);
- SolidBrush brush(Color::Black);
- Pen pen(Color::Gray);
- //以当前窗口中心点为原点绘制坐标轴
- graphics.DrawLine(&pen,
- PointF(rect.Width/2,0), PointF(rect.Width/2,rect.Height));
- graphics.DrawLine(&pen,
- PointF(0,rect.Height/2), PointF(rect.Width,rect.Height/2));
- //将StringAlignment枚举的三种不同的对齐方式设置成数组成员,以便直接访问
- StringAlignment Align[3]=
- {
- StringAlignmentNear,
- StringAlignmentCenter,
- StringAlignmentFar
- };
- CString strAlign[] ={ "近", "中", "远" };
- //设置输出字体
- FontFamily fontFamily(L"幼圆");
- Font font(&fontFamily, 12, FontStyleRegular);
- StringFormat strfmt;
- //分别在垂直和水平方向上使用三种不同的对齐方式
- for (int iVert = 0; iVert < 3; iVert++)
- for (int iHorz = 0; iHorz < 3; iHorz++)
- {
- //设置垂直对齐方式
- strfmt.SetLineAlignment((StringAlignment)Align[iVert]);
- //设置水平对齐方式
- strfmt.SetAlignment((StringAlignment)Align[iHorz]);
- //将对齐信息输出
- CString s;
- s.Format("水平对齐:%d\n垂直对齐:%d", strAlign[iVert],strAlign[iHorz]);
- //在当前区域中按照设置的对齐方式绘制文本
- MyDrawString(graphics, s,font,brush,rect, strfmt);
- }
- }
- void Font_TextAlignment2_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //将当前窗口大小信息保存
- Rect rect(0, 0, MyClient.Width, MyClient.Height);
- SolidBrush brush(Color::Black);
- Pen pen(Color::Gray);
- //以当前窗口中心点为原点绘制坐标轴
- graphics.DrawLine(&pen,
- PointF(rect.Width/2,0), PointF(rect.Width/2,rect.Height));
- graphics.DrawLine(&pen,
- PointF(0,rect.Height/2), PointF(rect.Width,rect.Height/2));
- //将StringAlignment枚举的三种不同的对齐方式设置成数组成员,以便直接访问
- StringAlignment Align[3]=
- {
- StringAlignmentNear,
- StringAlignmentCenter,
- StringAlignmentFar
- };
- CString strAlign[]={ "近", "中", "远" };
- //设置输出字体
- FontFamily fontFamily(L"幼圆");
- Font font(&fontFamily, 12, FontStyleRegular);
- StringFormat strfmt;
- //分别在垂直和水平方向上使用三种不同的对齐方式
- for (int iVert = 0; iVert < 3; iVert+=2)
- for (int iHorz = 0; iHorz < 3; iHorz+=2)
- {
- //设置垂直对齐方式
- strfmt.SetLineAlignment((StringAlignment)Align[iVert]);
- //设置水平对齐方式
- strfmt.SetAlignment((StringAlignment)Align[iHorz]);
- //将对齐信息输出
- CString s;
- s.Format("水平对齐:%d\n垂直对齐:%d", strAlign[iVert],strAlign[iHorz]);
- //在当前区域中按照设置的对齐方式绘制文本
- MyDrawString(graphics, s,font,brush, PointF(MyClient.Width/2,MyClient.Height/2),strfmt);
- }
- }
- //使用制表位
- void Font_TextoutUsingTabs_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- StringFormat stringFormat;
- //输出文本使用的字体
- FontFamily fontFamily(L"Arial");
- Font font(&fontFamily, 30, FontStyleBold, UnitPixel);
- SolidBrush solidBrush(Color::Black);
- //不使用制表位信息的输出
- CString text = "A\tB\tC\t\t\tD";
- MyDrawString(graphics, text, font, solidBrush, PointF(0,10), stringFormat);
- //设置制表位信息1
- float tabs[]={40.0f, 30.0f,40};
- stringFormat.SetTabStops(0.0f, sizeof(tabs)/sizeof(float), tabs);
- //使用制表位信息的进行文本输出
- graphics.TranslateTransform(0,30);
- text="A\tB\tC\tD";
- MyDrawString(graphics, text, font, solidBrush, PointF(0,10), stringFormat);
- //设置制表位信息2
- float tabs2[] = {60.0f, 60.0f, 60};
- stringFormat.SetTabStops(0.0f, 3, tabs2);
- graphics.TranslateTransform(0,30);
- MyDrawString(graphics, text, font, solidBrush, PointF(0,10), stringFormat);
- //获取制表信息
- float firstTabOffset = 0;
- float tabStops[3];
- stringFormat.GetTabStops(3, &firstTabOffset, tabStops);
- //清空制表位
- for(int j = 0; j <3; ++j)
- tabStops[j]=0;
- stringFormat.SetTabStops(0.0f, 3, tabStops);
- //在清空制表位信息后输出文本
- graphics.TranslateTransform(0,30);
- MyDrawString(graphics, text, font, solidBrush, PointF(0,10), stringFormat);
- }
- //定义商品信息结构
- struct SListItem
- {
- public: CString name;//商品名
- public: float price;//单价
- public: float num;//数量
- //初如化
- public: SListItem(CString p1, float p2,float p3)
- {
- name = p1;
- price=p2;
- num = p3;
- }
- };
- void Font_UsingTabs_Click()
- {
- //初始化商品信息
- SListItem items[5]=
- {
- SListItem(L"MP3播放机",1228,10),
- SListItem(L"笔记本电脑",17200,1),
- SListItem(L"激光打印机",3200,5),
- SListItem(L"喷墨打印机",620,1),
- SListItem(L"数码相机",4800,5)
- };
- //定义表头
- CString title;
- title.Format("\n%s\t%s\t%s\t%s\n","商品名","单价","数量","总价");
- //格式化每种商品对应的字符串
- CString iteminfo[5];
- for (int i=0;i<5;i++)
- {
- //计算总价
- float total=items[i].price*items[i].num;
- //定制输出格式
- CString s;
- s.Format("%d\t%.2f\t%.2f\t%.2f\n",
- items[i].name,items[i].price,items[i].num,total);
- iteminfo[i]=s;
- }
- //在表格每一行的内容合并
- CString xxx;;
- for (i=0;i<5;i++)
- xxx+=iteminfo[i];
- //表头+表体
- title+=xxx;
- //在当前窗口中输出表格
- RectF rect(0.0f,0.0f,
- MyClient.Width,MyClient.Height);
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush solidBrush(Color::Black);
- //定制输出字体
- FontFamily fontFamily(L"宋体");
- Font font(&fontFamily,
- 16, FontStyleRegular, UnitPixel);
- StringFormat stringFormat;
- //设置制表位信息
- float tabs[] = {120.0f, 120.0f,60.0f};
- stringFormat.SetTabStops(0.0f, sizeof(tabs)/sizeof(float), tabs);
- //输出表格
- MyDrawString(graphics, title,font, solidBrush,rect, stringFormat);
- }
- void Font_TextoutHotkeyPrefix_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush solidBrush(Color::Black);
- FontFamily fontFamily(L"宋体");
- Font font(&fontFamily, 20, FontStyleRegular, UnitPixel);
- Pen pen(Color::Red);
- StringFormat stringFormat;
- //显示快捷键字符,不解释"&"号
- stringFormat.SetHotkeyPrefix(HotkeyPrefixNone);
- MyDrawString(graphics, "工具栏(&T )\n状态栏(&S )", font, solidBrush,
- RectF(30, 30, 160, font.GetHeight(&graphics)*2),stringFormat);
- //绘制边框
- graphics.DrawRectangle(&pen, Rect(30, 30, 160,font.GetHeight(&graphics)*2));
- //解释""号为下划线
- stringFormat.SetHotkeyPrefix(HotkeyPrefixShow);
- graphics.TranslateTransform(0,font.GetHeight(&graphics)*3);
- MyDrawString(graphics, "工具栏(&T )\n状态栏(&S )", font, solidBrush,
- RectF(30, 30, 160, font.GetHeight(&graphics)*2),stringFormat);
- //绘制边框
- graphics.DrawRectangle(&pen, Rect(30, 30, 160,font.GetHeight(&graphics)*2));
- //隐藏""号
- graphics.TranslateTransform(0,font.GetHeight(&graphics)*3);
- stringFormat.SetHotkeyPrefix(HotkeyPrefixHide);
- MyDrawString(graphics, "工具栏(&T )\n状态栏(&S )", font, solidBrush,
- RectF(30, 30, 160, font.GetHeight(&graphics)*2),stringFormat);
- graphics.DrawRectangle(&pen, Rect(30, 30, 160,font.GetHeight(&graphics)*2));
- }
- void Font_TextoutShadow_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //对文本使用去锯齿的边缘处理
- graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
- FontFamily fontFamily(L"华文楷体");
- Font font(&fontFamily, 100, FontStyleBold, UnitPixel);
- //文本输出框
- RectF textout(font.GetHeight(&graphics),
- MyClient.Height/2,MyClient.Width,MyClient.Height);
- //在两个不同的位置绘制文本,形成阴影
- //solidBrush的色彩透明度为100,暗黑
- SolidBrush solidBrush(_MakeA_RGB(100,Color::Black));
- SolidBrush redBrush(Color::Red);
- MyDrawString(graphics, "阴影字",font, solidBrush,
- PointF(27.0f, 27.0f));
- MyDrawString(graphics, "阴影字",font, redBrush,
- PointF(12.0f, 20.0f));
- //另一种阴影字,阴影为线条
- //构造影线画刷
- HatchBrush brush_tmp(
- HatchStyleDarkDownwardDiagonal,Color::Black, Color::White);
- int reptime=40;
- //先画背景
- for(int i=0;i<reptime;i++)
- MyDrawString(graphics, "阴影字",font,brush_tmp,
- PointF(textout.X+i+2,textout.Y+i+2));
- //再画前景
- MyDrawString(graphics, "阴影字",font, redBrush, PointF(textout.X,textout.Y));
- }
- void Font_TextoutHashline_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //对文本使用去锯齿的边缘处理
- graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
- FontFamily fontFamily(L"华文楷体");
- Font font(&fontFamily, 100, FontStyleBold, UnitPixel);
- //文本输出框
- RectF textout(font.GetHeight(&graphics),0,MyClient.Width,MyClient.Height);
- //画刷样本显示框
- RectF brushdemo(0,0,font.GetHeight(&graphics),font.GetHeight(&graphics));
- //创建一个影线画刷
- HatchBrush brush_tmp(HatchStyle75Percent,Color::Black, Color::White);
- //设置文本输出格式:水平靠左、垂直居中
- StringFormat format;
- format.SetAlignment(StringAlignmentNear);
- format.SetLineAlignment(StringAlignmentCenter);
- //显示画刷样本
- graphics.FillRectangle(&brush_tmp,brushdemo);
- //使用画刷绘制文本
- MyDrawString(graphics, "影线字",font,brush_tmp,textout,format);
- }
- void Font_TextoutTexture_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //对文本使用去锯齿的边缘处理
- graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
- FontFamily fontFamily(L"华文楷体");
- Font font(&fontFamily, 70, FontStyleBold, UnitPixel);
- //文本输出框
- RectF textout(0,0,MyClient.Width,MyClient.Height);
- //画刷样本显示框
- RectF brushdemo(0,0,font.GetHeight(&graphics),font.GetHeight(&graphics));
- //装入纹理图片
- Bitmap image(L"TEXTURE3.bmp");
- //构造纹理画刷
- TextureBrush brush_tmp(&image);
- //设置文本输出格式:水平、垂直居中
- StringFormat format;
- format.SetAlignment(StringAlignmentCenter);
- format.SetLineAlignment(StringAlignmentCenter);
- //显示画刷样本
- graphics.FillRectangle(&brush_tmp,brushdemo);
- //使用画刷绘制文本
- MyDrawString(graphics, "纹理字",font,brush_tmp,textout,format);
- }
- void Font_TextoutGradient_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //对文本使用去锯齿的边缘处理
- graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
- FontFamily fontFamily(L"华文楷体");
- Font font(&fontFamily, 70, FontStyleBold, UnitPixel);
- RectF textout(0,0,MyClient.Width,MyClient.Height);
- //画刷样本显示框
- RectF brushdemo(0,0,font.GetHeight(&graphics),font.GetHeight(&graphics));
- //定义一个多色渐变画刷
- Color colors[] =
- {
- Color::Red, // 红色
- Color::Green,//过度色为绿色
- Color::Blue // 蓝色
- };
- float positions[] =
- {
- 0.0f, // 由红色起
- 0.3f, // 绿色始于画刷长度的三分之一
- 1.0f // 由蓝色止
- };
- //构造一条从黑色到白色的渐变画刷
- LinearGradientBrush brush_tmp(
- Point(0, 0),
- Point(100, font.GetHeight(&graphics)),
- Color::White, // 黑
- Color::White); // 白
- //设置渐变画刷的多色渐变信息
- //ColorBlend clrBlend(3);
- //clrBlend.Colors=colors;
- //clrBlend.Positions=positions;
- brush_tmp.SetInterpolationColors(colors, positions, 3);
- //设置文本输出格式:水平靠左、垂直居中
- StringFormat format;
- format.SetAlignment(StringAlignmentNear);
- format.SetLineAlignment(StringAlignmentCenter);
- //显示画刷样本
- graphics.FillRectangle(&brush_tmp,brushdemo);
- //使用画刷绘制文本
- MyDrawString(graphics, "渐变字",font,brush_tmp,textout,format);
- }
- //构造路径
- void Path_Construct_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //定义构成路径的点坐标
- PointF data[]=
- {
- PointF(40,140),
- PointF(275,200),
- PointF(105,225),
- PointF(190,300),
- PointF(50,350),
- PointF(20,180)
- };
- //设置定义点的类型将每点都处理成直线的端点
- BYTE typeline[]=
- {
- (BYTE)PathPointTypeLine,
- (BYTE)PathPointTypeLine,
- (BYTE)PathPointTypeLine,
- (BYTE)PathPointTypeLine,
- (BYTE)PathPointTypeLine,
- (BYTE)PathPointTypeLine
- };
- //构造路径
- GraphicsPath tmp1(data,typeline,6);
- //填充路径
- graphics.FillPath(&SolidBrush(Color::Red),&tmp1);
- //绘制定义路径的端点
- for(int i=0;i<6;i++)
- graphics.FillEllipse(&SolidBrush(Color::Black),
- RectF(data[i].X-5,data[i].Y-5,10,10));
- //绘图平面右移
- graphics.TranslateTransform(360,0);
- //更改路径的端点类型
- BYTE type[]=
- {
- (BYTE)PathPointTypeStart,
- (BYTE)PathPointTypeBezier,
- (BYTE)PathPointTypeBezier,
- (BYTE)PathPointTypeBezier,
- (BYTE)PathPointTypeLine,
- (BYTE)PathPointTypeLine
- };
- //构造并填充路径
- GraphicsPath tmp(data,type,6);
- graphics.FillPath(&SolidBrush(Color::Red),&tmp);
- //绘制定义路径的端点
- for(i=0;i<6;i++)
- graphics.FillEllipse(&SolidBrush(Color::Black),
- RectF(data[i].X-5,data[i].Y-5,10,10));
- }
- void Path_AddLines_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen pen(Color::Blue,2);
- //创建路径对象
- GraphicsPath path;
- Rect rect1(20, 20, 100, 200);
- Rect rect2(40, 40, 100, 200);
- //添加两段弧线
- path.AddArc(rect1, 0.0f, 180.0f);
- path.AddArc(rect2, 0.0f, 180.0f);
- graphics.DrawPath(&pen, &path);
- }
- //封闭图形
- void Path_CloseFigure_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen pen(Color::Blue,2);
- //创建路径对象
- GraphicsPath path;
- Rect rect1(20, 20, 100, 200);
- Rect rect2(40, 40, 100, 200);
- GraphicsPath path2;
- path2.AddArc(rect1, 0.0f, 180.0f);
- //封闭弧线1
- path2.CloseFigure();
- path2.AddArc(rect2, 0.0f, 180.0f);
- //封闭弧线2
- path2.CloseFigure();
- graphics.DrawPath(&pen, &path2);
- }
- //路径的填充
- void Path_FillPath_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- GraphicsPath path1;
- GraphicsPath path;
- Pen pen(_MakeA_RGB(128, Color::Blue), 5);
- SolidBrush brush(Color::Red);
- //添加三条直线:开放的图形
- path1.AddLine(10,10,10,50);
- path1.AddLine(10,50,50,50);
- path1.AddLine(50,50,50,10);
- path1.StartFigure();
- //添加一个闭合的图形
- RectF rect(110,10,40,40);
- //开始第二个子路径的图形追加
- path1.AddRectangle(rect);
- //绘制、填充路径
- graphics.DrawPath(&pen, &path1);
- graphics.FillPath(&brush, &path1);
- //添加开放的弧线
- path.AddArc(0, 0, 150, 120, 30, 120);
- //添加封闭的椭圆
- path.AddEllipse(50, 50, 50, 100);
- //使用默认的填充方式填充路径
- graphics.FillPath(&brush, &path);
- graphics.DrawPath(&pen, &path);
- }
- void Path_AddSubPath_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen pen(Color::Blue,2);
- Rect rect(0, 0, 100, 50);
- GraphicsPath path;
- //在路径中按垂直方向添加五段弧线
- path.AddArc(0, 0, 100, 50, 0.0f, 180.0f);
- path.AddArc(0, 30, 100, 50, 0.0f, 180.0f);
- path.AddArc(0, 60, 100, 50, 0.0f, 180.0f);
- path.AddArc(0, 90, 100, 50, 0.0f, 180.0f);
- path.AddArc(0, 120, 100, 50, 0.0f, 180.0f);
- graphics.DrawPath(&pen, &path);
- //获取路径所占区域
- RectF rect2; path.GetBounds(&rect2);
- //绘图平面右移
- graphics.TranslateTransform(rect2.Width+10,0);
- GraphicsPath path2;
- //构造第一个子路径
- path2.AddArc(0, 0, 100, 50, 0.0f, 180.0f);
- path2.AddArc(0, 30, 100, 50, 0.0f, 180.0f);
- path2.AddArc(0, 60, 100, 50, 0.0f, 180.0f);
- //在不封闭当前子路径的情况下开始第二个子路径的绘制
- path2.StartFigure();
- //添加弧线
- path2.AddArc(0, 90, 100, 50, 0.0f, 180.0f);
- //在封闭当前子路径的情况下开始第三个子路径的绘制
- path2.CloseFigure();
- path2.AddArc(0, 120, 100, 50, 0.0f, 180.0f);
- //绘制子路径
- graphics.DrawPath(&pen, &path2);
- }
- void Path_GetSubPath_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen bluePen(Color::Blue,3);
- GraphicsPath path;
- // 字路径1
- path.AddRectangle(Rect(20, 20, 60, 30));
- // 字路径2
- path.AddLine(100, 20, 160, 50);
- path.AddArc(180, 20, 60, 30, 0.0f, 180.0f);
- // 字路径3
- path.AddRectangle(Rect(260, 20, 60, 30));
- // 字路径4
- path.AddLine(340, 20, 400, 50);
- path.AddArc(340, 20, 60, 30, 0.0f, 180.0f);
- path.CloseFigure();
- // 字路径5
- path.AddRectangle(Rect(420, 20, 60, 30));
- //绘制路径
- graphics.DrawPath(&bluePen, &path);
- // 建立路径描述对象iterator
- GraphicsPathIterator iterator(&path);
- GraphicsPath subpath;
- int isClosed=false;
- int count=0;
- //连续调用两次NextSubpath,获取子路径
- //将路径的当前位置移动到1
- count =iterator.NextSubpath(&subpath, &isClosed);
- //将路径的当前位置移动到2
- count = iterator.NextSubpath(&subpath, &isClosed);
- //在第二行绘制提取的子路径
- graphics.TranslateTransform(0,50);
- graphics.DrawPath(&bluePen, &subpath);
- //复位所有的位置移动信息
- iterator.Rewind();
- count = iterator.NextSubpath(&subpath, &isClosed);
- //在第三行绘制新抽取的子路径
- graphics.TranslateTransform(0,50);
- Pen redPen(Color::Red,3);
- graphics.DrawPath(&redPen, &subpath);
- //连续调用两次NextSubpath,以获取子路径的定义点信息
- int start=0;
- int end=0;
- CString msg;
- //复位所有的位置移动信息
- iterator.Rewind();
- count = iterator.NextSubpath(&start, &end, &isClosed);
- //保存位置索引信息
- msg.Format(_T("第一次移动子路径的定义点起止位置为%d\t和\t%d\n"),start,end);
- count = iterator.NextSubpath(&start, &end, &isClosed);
- CString s;
- s.Format(_T("第二次移动子路径的定义点起止位置为%d\t和\t%d\n"),start,end);
- msg+=s;
- //绘制子路径的定义点位置索引信息
- FontFamily fontFamily(L"宋体");
- Font font(&fontFamily,12);
- graphics.TranslateTransform(0,60);
- MyDrawString(graphics, msg,font,SolidBrush(Color::Black), PointF(0,0));
- }
- void Path_GetPoints_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //创建路径对象
- GraphicsPath path;
- //添加直线
- path.AddLine(20, 100, 150, 120);
- //添加矩形
- path.AddRectangle(Rect(40, 30, 20, 60));
- //添加椭圆
- path.AddEllipse(Rect(70, 30, 100, 40));
- //添加曲线
- Point points[] =
- {
- Point(300, 40),
- Point(350, 60),
- Point(300, 80),
- Point(300, 100),
- Point(350, 150)
- };
- path.AddCurve(points,5);
- //绘制路径
- Pen pen(Color::Blue);
- graphics.DrawPath(&pen, &path);
- // 获取定义路径的点总数
- int count =path.GetPointCount();
- //绘制点定义路径的点
- SolidBrush brush(Color::Red);
- PointF *points0 = new PointF[count];
- path.GetPathPoints(points, count);
- for(int j = 0; j < count; ++j)
- {
- graphics.FillEllipse(
- &brush,
- points0[j].X - 3.0f,
- points0[j].Y - 3.0f,
- 6.0f,
- 6.0f);
- }
- delete points0;
- /*另外一一种遍历数组的方式
- foreach( PointF x in path.PathPoints)
- graphics.FillEllipse(
- brush,
- x.X - 3.0f,
- x.Y - 3.0f,
- 6.0f,
- 6.0f);
- */
- }
- void Path_PathData_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //曲线的定义点
- Point points[] =
- {
- Point(40, 60),
- Point(50, 70),
- Point(30, 90)
- };
- Pen pen(Color::Red, 2);
- GraphicsPath path;
- //弧线
- path.AddArc(175, 50, 50, 50, 0.0f, -180.0f);
- //直线
- path.AddLine(100, 0, 250, 20);
- path.StartFigure();
- //直线
- path.AddLine(50, 20, 5, 90);
- //曲线
- path.AddCurve(points, 3);
- path.AddLine(50, 150, 150, 180);
- path.CloseAllFigures();
- //绘制路径
- graphics.DrawPath(&pen, &path);
- //获取PathData属性值
- PathData pathData;
- path.GetPathData(&pathData);
- SolidBrush brush(Color::Red);
- //绘制定义点
- for (int i=0; i<pathData.Count; i++)
- graphics.FillEllipse(&SolidBrush(Color::Black),
- pathData.Points[i].X - 5.0f,pathData.Points[i].Y - 5.0f,10.0f,10.0f);
- //输出每一个路径定义点的类型
- FontFamily fontFamily(L"宋体");
- Font font(&fontFamily,12);
- RectF rect;
- path.GetBounds(&rect);
- graphics.TranslateTransform(rect.Width+10,0);
- for (i=0; i<pathData.Count; i++)
- {
- CString msg;
- msg.Format("第%d点的类型为:\t%d", i+1, pathData.Types[i]);
- MyDrawString(graphics, msg,
- font,SolidBrush(Color::Black), PointF(0,(i+1)*font.GetHeight(&graphics)));
- }
- }
- void Path_ViewPointFLAG_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- graphics.ScaleTransform(0.8f,0.8f);
- SolidBrush brush(Color::Red);
- //创建一个具有四个子路径的路径
- GraphicsPath path;
- path.AddRectangle(Rect(20, 20, 60, 30));
- path.AddLine(100, 20, 160, 50);
- path.AddArc(180, 20, 60, 30, 0, 180);
- path.AddRectangle(Rect(260, 20, 60, 30));
- path.AddLine(340, 20, 400, 50);
- path.AddArc(340, 20, 60, 30, 0, 180);
- path.CloseAllFigures();
- //填充路径
- graphics.FillPath(&brush, &path);
- //将路径的标记信息传入GraphicsPathIterator对象
- GraphicsPathIterator iterator(&path);
- //获取标记点信息
- int start=0;
- int end=0;
- int count=0;
- //获取第一个标记区间
- count = iterator.NextMarker(&start, &end);
- //为点坐标、类型分配空间
- PointF *points = new PointF[count];
- BYTE *types = new BYTE[count];
- //从iterator复制信息
- iterator.CopyData(points, types, start, end);
- //根据复制的信息新创建一个路径
- GraphicsPath pathnew(points, types, count);
- //在新位置绘制路径的定义点
- graphics.TranslateTransform(0,60);
- for(int j = 0; j < count; ++j)
- graphics.FillEllipse(&SolidBrush(Color::Black),points[j].X - 2.0f,
- points[j].Y - 2.0f, 4.0f,4.0f);
- //填充新创建的路径
- graphics.TranslateTransform(0,60);
- graphics.FillPath(&brush, &pathnew);
- graphics.TranslateTransform(0,60);
- //输出所有的标记点信息
- CString msg;
- for(j = 0; j < count; ++j)
- {
- CString s;
- s.Format("第%s的信息:X=%.2f\tY=%.2f\t类型=%d\n",
- j,points[j].X,points[j].Y,types[j]);
- msg+=s;
- }
- //绘制字符串
- FontFamily fontFamily(L"宋体");
- Font font(&fontFamily, 12, FontStyleRegular, UnitPixel);
- MyDrawString(graphics, msg, font,SolidBrush(Color::Black), PointF(0,0));
- delete types;
- delete points;
- }
- void Path_SubPathRange_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush brush(Color::Red);
- //创建一个具有五个子路径的路径
- GraphicsPath path;
- // *************path.SetMarkers();**************
- //添加第一个子路径
- path.AddRectangle(Rect(20, 20, 60, 30));
- //添加第二个子路径
- path.AddLine(100, 20, 160, 50);
- //第二个标记将第二个子路径从中央截断
- path.SetMarker();
- path.AddArc(180, 20, 60, 30, 0, 180);
- //添加第三个子路径
- path.AddRectangle(Rect(260, 20, 60, 30));
- //第三个标记
- path.SetMarker();
- //添加第四个子路径
- path.AddLine(340, 20, 400, 50);
- path.AddArc(340, 20, 60, 30, 0, 180);
- //填充路径
- graphics.FillPath(&brush,&path);
- //将路径的标记信息传入GraphicsPathIterator对象
- GraphicsPathIterator iterator(&path);
- int d=iterator.GetSubpathCount();
- //获取标记点信息
- int start;
- int end;
- int count;
- //移到第二个标记
- count = iterator.NextMarker(&start, &end);
- //移到第三个标记
- count = iterator.NextMarker(&start, &end);
- //为点坐标、类型分配空间
- PointF *points = new PointF[count];
- BYTE *types = new BYTE[count];
- //从iterator复制信息
- iterator.CopyData(points, types, start, end);
- //根据复制的信息新创建一个路径
- GraphicsPath pathnew(points, types, count);
- //在新位置绘制标记点
- graphics.TranslateTransform(0,60);
- for(int j = 0; j < count; ++j)
- graphics.FillEllipse(&SolidBrush(Color::Black),
- points[j].X - 2.0f,points[j].Y - 2.0f,
- 4.0f,
- 4.0f);
- //填充新创建的路径
- graphics.TranslateTransform(0,60);
- graphics.FillPath(&brush,&pathnew);
- //输出所有的标记点信息
- CString msg;
- for( j = 0; j < count; ++j)
- {
- CString s;
- s.Format(_T("第%d点的信息:X=%.2f\tY=%.2f \t类型=%d\n"),
- j,points[j].X,points[j].Y,types[j]);
- msg+=s;
- }
- FontFamily fontFamily(L"宋体");
- Font font(&fontFamily, 12, FontStyleRegular, UnitPixel);
- //绘制字符串
- graphics.TranslateTransform(0,60);
- MyDrawString(graphics, msg, font,SolidBrush(Color::Black), PointF(0,0));
- delete points;
- delete types;
- }
- void Path_Flatten_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //曲线定义点
- Point pts[] =
- {
- Point(20,50),
- Point(60,70),
- Point(80,10),
- Point(120,50)
- };
- GraphicsPath path;
- path.AddCurve(pts, 4);
- path.AddEllipse(160, 10, 150, 80);
- path.AddBezier(20, 100, 20, 150, 50, 80, 100, 60);
- //按照默认的方式绘制路径
- Pen pen(Color::Black,3);
- graphics.DrawPath(&pen, &path);
- //获取路径点信息
- PathData pathData;
- path.GetPathData(&pathData);
- // 绘制路径定义点
- SolidBrush brush(Color::Red);
- for(int j = 0; j <pathData.Count; ++j)
- {
- graphics.FillEllipse(&brush, pathData.Points[j].X - 3.0f,
- pathData.Points[j].Y - 3.0f,6.0f, 6.0f);
- }
- //绘图平面下移100个单位
- graphics.TranslateTransform(0,120);
- //进行曲线转
- path.Flatten(&Matrix(), 14.0f);
- pen.SetColor(Color::Green);
- //绘制转换后的路径
- graphics.DrawPath(&pen, &path);
- }
- void Path_Warp_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- // 创建一个全部由线条组成的路径
- PointF points[] =
- {
- PointF(20.0f, 60.0f),
- PointF(30.0f, 90.0f),
- PointF(15.0f, 110.0f),
- PointF(15.0f, 145.0f),
- PointF(55.0f, 145.0f),
- PointF(55.0f, 110.0f),
- PointF(40.0f, 90.0f),
- PointF(50.0f, 60.0f)
- };
- GraphicsPath path;
- path.AddLines(points, sizeof(points)/sizeof(PointF));
- path.CloseFigure();
- // 绘制扭曲前的路径
- Pen bluePen(Color::Blue,3);
- graphics.DrawPath(&bluePen, &path);
- // 定义放置路径的源矩形
- RectF srcRect(10.0f, 50.0f, 50.0f, 100.0f);
- //定义矩形映射的目标点
- PointF destPts[] =
- {
- PointF(220.0f, 10.0f),
- PointF(280.0f, 10.0f),
- PointF(100.0f, 150.0f),
- PointF(300.0f, 150.0f)
- };
- //扭曲矩形
- path.Warp(destPts, 4, srcRect);
- // 绘制扭曲后的路径
- graphics.DrawPath(&bluePen, &path);
- // 绘制源矩形和目标的多边形
- Pen blackPen(Color::Black,1);
- graphics.DrawRectangle(&blackPen,
- srcRect.X,srcRect.Y,srcRect.Width,srcRect.Height);
- graphics.DrawLine(&blackPen, destPts[0], destPts[1]);
- graphics.DrawLine(&blackPen, destPts[0], destPts[2]);
- graphics.DrawLine(&blackPen, destPts[1], destPts[3]);
- graphics.DrawLine(&blackPen, destPts[2], destPts[3]);
- }
- void Path_Transform_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- GraphicsPath path;
- //添加一个矩形子路径
- path.AddRectangle(Rect(40, 10, 200, 50));
- //绘制坐标变换前的路径
- graphics.DrawPath(&Pen(Color::Blue,4), &path);
- //旋转路径
- Matrix matrix;
- matrix.Rotate(15.0f);
- path.Transform(&matrix);
- //绘制进行坐标变换后的路径
- graphics.DrawPath(&Pen(Color::Red,4), &path);
- }
- void Path_Widen_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen bluePen(Color::Blue,1);
- Pen greenPen(Color::Green, 10);
- //定义曲线
- PointF points[] =
- {
- PointF(10.0f, 10.0f),
- PointF(130.0f, 90.0f),
- PointF(140.0f, 60.0f),
- PointF(60.0f, 90.0f)
- };
- GraphicsPath path;
- //添加四条曲线
- path.AddClosedCurve(points, 4);
- //复制三个相同的路径进行轮廓拓宽处理
- GraphicsPath *path2=path.Clone();
- GraphicsPath *path3=path.Clone();
- GraphicsPath *path4=path.Clone();
- //使用宽度为10的画笔绘制路径
- graphics.DrawPath(&Pen(Color::Blue,10), &path);
- RectF rect; path.GetBounds(&rect);
- //使用绿色画笔拓宽路径轮廓
- path2->Widen(&greenPen);
- //绘图平面右移,绘制拓宽的路径
- graphics.TranslateTransform(rect.Width+10,0);
- graphics.DrawPath(&bluePen, path2);
- //下移
- graphics.TranslateTransform(-rect.Width,rect.Height);
- //使用间断线风格的绿色画笔拓宽路径
- greenPen.SetDashStyle(DashStyleDash);
- path3->Widen(&greenPen);
- //填充路径
- graphics.FillPath(&SolidBrush(Color::Red), path3);
- //对画笔进行缩放变换
- Pen pentmp(Color::Blue);
- pentmp.ScaleTransform(3.0f, 20.0f);
- path4->Widen(&pentmp);
- //绘图平面右移
- graphics.TranslateTransform(rect.Width,0);
- graphics.DrawPath(&bluePen, path4);
- delete path2;
- delete path3;
- delete path4;
- }
- void Path_WidenDemo_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen bluePen(Color::Blue,1);
- Pen greenPen(Color::Green, 20);
- GraphicsPath path;
- //添加一条直线
- path.AddLine(10,20,100,20);
- GraphicsPath *path2=path.Clone();
- PathData pathData;
- PathData pathData2;
- path.GetPathData(&pathData);
- int num1=path.GetPointCount();
- //保存路径的定义点信息
- CString msg;
- for(int i=0;i<num1;i++)
- {
- CString s;
- s.Format("点%d的坐标分别为:X=%.2f\tY=%.2f\n",
- i,pathData.Points[i].X,pathData.Points[i].Y);
- msg+=s;
- }
- //绘制路径
- graphics.DrawPath(&Pen(Color::Blue), &path);
- //使用绿色画笔拓宽路径轮廓
- path2->Widen(&greenPen);
- path2->GetPathData(&pathData2);
- int num2=path2->GetPointCount();
- //绘图平面右移,绘制拓宽的路径
- RectF rect; path.GetBounds(&rect);
- graphics.TranslateTransform(rect.Width*2,0);
- graphics.DrawPath(&bluePen, path2);
- graphics.ResetTransform();
- //保存拓宽后路径的定义点信息
- CString msg2;
- for(i=0;i<num2;i++)
- {
- CString s;
- s.Format("拓宽后的路径点%d的坐标分别为:X=%.2f\tY=%.2f\n",
- i,pathData2.Points[i].X,pathData2.Points[i].Y);
- msg2+=s;
- }
- //输出路径拓宽前后的定义点坐标位置信息
- FontFamily fontFamily(L"宋体");
- Font font(&fontFamily, 12, FontStyleRegular, UnitPixel);
- graphics.TranslateTransform(0,120);
- MyDrawString(graphics, msg,font,SolidBrush(Color::Black), PointF(0,0));
- graphics.TranslateTransform(0,font.GetHeight(&graphics)*(num1+1));
- MyDrawString(graphics, msg2,font,SolidBrush(Color::Black), PointF(0,0));
- }
- void Region_FromPath_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //设定文本输出质量
- graphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit);
- graphics.SetSmoothingMode(SmoothingModeAntiAlias);
- FontFamily fontFamily(L"隶书");
- //创建路径区域
- GraphicsPath path;
- //向区域中追加文本
- path.AddString(L"文字区域",4,&fontFamily,
- (int)FontStyleRegular,100.0f, Point(0, 0), &StringFormat() );
- Pen pen(Color::Red);
- //绘制路径
- graphics.DrawPath(&pen, &path);
- graphics.TranslateTransform(0,80);
- //从路径中构造区域
- Region region(&path);
- //填充区域
- graphics.FillRegion(&SolidBrush(Color::Red),®ion);
- }
- void Region_Calculate_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- FontFamily fontFamily(L"宋体");
- Font font(&fontFamily, 16, FontStyleRegular, UnitPixel);
- SolidBrush solidBrush2(Color::Red);
- SolidBrush solidBrush(Color::Green);
- Pen pen(Color::Cyan);
- //定义多边形的端点坐标
- Point points[]=
- {
- Point(75, 0),
- Point(100, 50),
- Point(150, 50),
- Point(112, 75),
- Point(150, 150),
- Point(75, 120),
- Point(10, 150),
- Point(37, 75),
- Point(0, 50),
- Point(50, 50),
- Point(75, 0)
- };
- //创建路径并添加线条
- GraphicsPath path;
- path.AddLines(points, sizeof(points)/sizeof(Point));
- //缩小路径
- Matrix matrix;
- matrix.Scale(0.5f,0.5f);
- path.Transform(&matrix);
- //获取路径所占的矩形区域
- RectF tmp; path.GetBounds(&tmp);
- //定义区域2
- RectF rect(0,10,tmp.Width,50);
- //计算文本信息输出区域
- RectF textout(0,tmp.Height,tmp.Width,font.GetHeight(&graphics)*2);
- //设置文本输出格式:水平居中、垂直靠下
- StringFormat format;
- format.SetAlignment(StringAlignmentCenter);
- format.SetLineAlignment(StringAlignmentFar);
- //填充路径
- graphics.FillPath(&solidBrush2,&path);
- graphics.TranslateTransform(tmp.Width+10,0);
- //填充矩形
- graphics.FillRectangle(&solidBrush,rect);
- //从路径中创建区域
- Region region(&path);
- //求交集
- region.Intersect(rect);
- graphics.TranslateTransform(tmp.Width+10,0);
- //填充交集
- graphics.FillRegion(&solidBrush2, ®ion);
- MyDrawString(graphics, "交集",font,SolidBrush(Color::Black),textout,format);
- //绘制路径所占的矩形区域
- graphics.DrawRectangle(&pen,tmp.X,tmp.Y,tmp.Width,tmp.Height);
- //求并集
- Region region2(&path);
- region2.Union(rect);
- graphics.TranslateTransform(tmp.Width+10,0);
- graphics.FillRegion(&solidBrush2, ®ion2);
- MyDrawString(graphics, "并集",font,solidBrush,textout,format);
- //绘制路径所占的矩形区域
- graphics.DrawRectangle(&pen,tmp.X,tmp.Y,tmp.Width,tmp.Height);
- //换行输出区域运算结果
- graphics.TranslateTransform(-(tmp.Width+10)*3,tmp.Height+textout.Height);
- //求异并集
- Region region3(&path);
- region3.Xor(rect);
- //填充异并集
- graphics.FillRegion(&solidBrush2, ®ion3);
- MyDrawString(graphics, "异并集",font,SolidBrush(Color::Black),textout,format);
- //绘制路径所占的矩形区域
- graphics.DrawRectangle(&pen,tmp.X,tmp.Y,tmp.Width,tmp.Height);
- //求补集
- Region region4(&path);
- region4.Complement(rect);
- graphics.TranslateTransform(tmp.Width+10,0);
- //填充补集
- graphics.FillRegion(&solidBrush2, ®ion4);
- MyDrawString(graphics, "补集",font,SolidBrush(Color::Black),textout,format);
- //绘制路径所占的矩形区域
- graphics.DrawRectangle(&pen,tmp.X,tmp.Y,tmp.Width,tmp.Height);
- //求排斥集
- Region region5(&path);
- region5.Exclude(rect);
- graphics.TranslateTransform(tmp.Width+10,0);
- //填排斥集
- graphics.FillRegion(&solidBrush2, ®ion5);
- MyDrawString(graphics, "排斥集",font,SolidBrush(Color::Black),textout,format);
- graphics.DrawRectangle(&pen,tmp.X,tmp.Y,tmp.Width,tmp.Height);
- }
- void Region_GetRects_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush solidBrush(Color::Red);
- Pen pen(Color::Black);
- GraphicsPath path;
- Matrix matrix;
- //从路径中创建区域
- path.AddEllipse(10, 0, 80, 120);
- Region pathRegion(&path);
- //填充区域
- graphics.FillRegion(&solidBrush, &pathRegion);
- //获取路径的矩形组成集信息(不使用变换)
- int count = pathRegion.GetRegionScansCount(&matrix);
- RectF *rects = new RectF[count];
- pathRegion.GetRegionScans(&matrix, rects, &count);
- //绘制平面右移,以绘制矩形集中所有的矩形
- graphics.TranslateTransform(90,0);
- for(int i=0; i<count; i++)
- graphics.DrawRectangle(&pen, rects[i]);
- //实施变换
- matrix.Scale(1.0f,0.75f);
- matrix.Shear(0.2f,0.3f);
- //获取路径的矩形组成集信息(使用变换)
- count = pathRegion.GetRegionScansCount(&matrix);
- delete rects;
- rects = new RectF[count];
- pathRegion.GetRegionScans(&matrix, rects, &count);
- //绘制平面右移,以绘制变换后的矩形集
- graphics.TranslateTransform(90,0);
- for(i=0; i<count; i++)
- graphics.DrawRectangle(&pen, rects[i]);
- delete rects;
- }
- void Transform_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush mySolidBrush1(Color::Blue);
- SolidBrush mySolidBrush2(_MakeA_RGB(155,Color::Chocolate));
- GraphicsPath myGraphicsPath;
- Rect myRect(60, 60, 60, 60);
- //在路径中添加矩形
- myGraphicsPath.AddRectangle(myRect);
- //填充路径
- graphics.FillPath(&mySolidBrush1, &myGraphicsPath);
- // 进行坐标变换
- Matrix myPathMatrix;
- //水平方向扩大两倍,垂直方向保持不变
- myPathMatrix.Scale(2, 1);
- myPathMatrix.Rotate(30.0f);
- //对路径实话坐标变换
- myGraphicsPath.Transform(&myPathMatrix);
- //填充变换后的路径
- graphics.FillPath(&mySolidBrush2, &myGraphicsPath);
- }
- void TranslateTransform_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //装入图片
- Bitmap image(L"nemo.bmp");
- //定义图片的显示区域
- Rect rect(0,0,110,70);
- for(int i=0;i<10;i++)
- {
- //显示图片
- graphics.DrawImage(&image,rect);
- //绘图平面右移,每次沿水平方面右移110个像素
- graphics.TranslateTransform(110,0);
- }
- }
- void RotateTransform_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //装入图片
- Bitmap image(L"nemo.bmp");
- //获取当前窗口的中心点
- Rect rect(0,0,MyClient.Width,MyClient.Height);
- PointF center(rect.Width/2,rect.Height/2);
- float offsetX=0;
- float offsetY=0;
- offsetX=center.X-image.GetWidth()/2;
- offsetY=center.Y-image.GetHeight()/2;
- //构造图片显示区域:让图片的中心点与窗口的中心点一致
- RectF picRect(offsetX,offsetY,image.GetWidth(),image.GetHeight());
- PointF Pcenter(picRect.X+picRect.Width/2,
- picRect.Y+picRect.Height/2);
- //让图片绕中心旋转一周
- for(int i=0;i<361;i+=10)
- {
- // 绘图平面以图片的中心点旋转
- graphics.TranslateTransform(Pcenter.X, Pcenter.Y);
- graphics.RotateTransform(i);
- //恢复绘图平面在水平和垂直方向的平移
- graphics.TranslateTransform(-Pcenter.X, -Pcenter.Y);
- //绘制图片并延时
- graphics.DrawImage(&image,picRect);
- ::Sleep(100);
- //重置绘图平面的所有变换
- graphics.ResetTransform();
- }
- }
- void DrawWatch_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Rect clientrect(0,0,
- MyClient.Width,MyClient.Height);
- //设置路码表的高和宽
- float WIDTH=clientrect.Width/2;
- float HEIGHT =clientrect.Height/2;
- //设置文本输出外观
- graphics.SetSmoothingMode(SmoothingModeAntiAlias);
- FontFamily fontFamily(L"Times New Roman");
- Font font(&fontFamily, 14, FontStyleBold, UnitPixel);
- //将绘图平面的坐标原点移到窗口中心
- graphics.TranslateTransform(WIDTH /2,HEIGHT/2);
- //绘制仪表盘
- graphics.FillEllipse(&SolidBrush(Color::Black),
- HEIGHT/-2,HEIGHT/-2,HEIGHT, HEIGHT);
- //输出文本
- MyDrawString(graphics, "公里/小时",font,SolidBrush(Color::Green),
- PointF(-26,HEIGHT/-4-font.GetHeight(&graphics)));
- //绘制刻度标记
- graphics.RotateTransform(225);
- for(int x = 0; x<55;x++)
- {
- graphics.FillRectangle(&SolidBrush(Color::Green),
- Rect(-2, -(HEIGHT/2-2), 3, 15));
- graphics.RotateTransform(5);
- }
- //重置绘图平面的坐标变换
- graphics.ResetTransform();
- graphics.TranslateTransform(WIDTH /2,HEIGHT/2);
- graphics.RotateTransform(225);
- int sp = 0;
- CString tmp;
- //绘制刻度值(整数类)
- for(x = 0; x<7;x++)
- {
- tmp.Format("%d",sp);
- //绘制红色刻度
- graphics.FillRectangle(&SolidBrush(Color::Red),Rect(-3,(HEIGHT/2 -2)* -1,6,25));
- //绘制数值
- MyDrawString(graphics, tmp,font,SolidBrush(Color::Green), PointF(tmp.GetLength()*(-6),
- (HEIGHT/-2) + 25));
- //旋转45度
- graphics.RotateTransform(45);
- sp += 20;
- }
- //重置绘图平面的坐标变换
- graphics.ResetTransform();
- graphics.TranslateTransform(WIDTH /2,HEIGHT/2);
- //绘制指针在30公里/秒的情形
- float angle= 30*2.25f+ 225;
- graphics.RotateTransform((float)angle);
- Pen pen(Color::Blue,14);
- //设置线帽
- pen.SetEndCap(LineCapArrowAnchor);
- pen.SetStartCap(LineCapRoundAnchor);
- //画指针
- graphics.DrawLine(&pen, PointF(0,0), PointF(0,(-1)*(HEIGHT/2.75F)));
- graphics.ResetTransform();
- graphics.TranslateTransform(WIDTH /2,HEIGHT/2);
- //绘制中心点
- graphics.FillEllipse(&SolidBrush(Color::Black),-6,-9,14,14);
- graphics.FillEllipse(&SolidBrush(Color::Red),-7,-7,14,14);
- //绘制速度极限标记
- pen.SetWidth(4);
- pen.SetColor(Color::Black);
- pen.SetEndCap(LineCapRound);
- pen.SetStartCap(LineCapFlat);
- graphics.DrawLine(&pen, PointF(HEIGHT/15.75F,HEIGHT/3.95F),
- PointF(HEIGHT/10.75F,HEIGHT/5.2F));
- pen.SetColor(Color::Red);
- graphics.DrawLine(&pen, PointF(HEIGHT/15.75F,HEIGHT/3.95F),
- PointF(HEIGHT/15.75F,HEIGHT/4.6F));
- }
- void ScaleTransform_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Rect clientrect(0,0,
- MyClient.Width,MyClient.Height);
- //获取当前窗口的中心点
- float width=clientrect.Width/2;
- float height=clientrect.Height/2;
- PointF center(width,height);
- //设置矩形初始大小为30*30
- float offsetX=center.X-15;
- float offsetY=center.Y-15;
- //构造矩形的缩放区域:让矩形的中心点与窗口的中心点一致
- RectF rotateRect(offsetX,offsetY,30,30);
- PointF Pcenter(rotateRect.X+rotateRect.Width/2,
- rotateRect.Y+rotateRect.Height/2);
- //设置初始缩放倍数
- int scale=1;
- //对矩形rotateRect旋转360度并不断放大矩形
- for(int i=0;i<360;i+=60)
- {
- // 绘图平面以图片的中心点旋转
- graphics.TranslateTransform(Pcenter.X, Pcenter.Y);
- //在水平和垂直方向上同时扩大矩形
- graphics.ScaleTransform(scale,scale);
- graphics.RotateTransform(i);
- //恢复绘图平面在水平和垂直方向的平移
- graphics.TranslateTransform(-Pcenter.X, -Pcenter.Y);
- //绘制矩形并延时
- graphics.DrawRectangle(&Pen(Color::Black),
- rotateRect.X,rotateRect.Y,rotateRect.Width,rotateRect.Height);
- ::Sleep(100);
- //重置绘图平面的所有变换
- graphics.ResetTransform();
- scale++;
- }
- }
- void RectScale_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Rect clientrect(0,0,
- MyClient.Width,MyClient.Height);
- //获取当前窗口的中心点
- float width=clientrect.Width/2;
- float height=clientrect.Height/2;
- PointF center(width,height);
- //设置矩形初始大小为30*30
- float offsetX=center.X-15;
- float offsetY=center.Y-15;
- //构造矩形的缩放区域:让矩形的中心点与窗口的中心点一致
- RectF rotateRect(offsetX,offsetY,30,30);
- PointF Pcenter(rotateRect.X+rotateRect.Width/2,
- rotateRect.Y+rotateRect.Height/2);
- //设置初始缩放倍数
- int scale=0;
- //对矩形rotateRect旋转360度并不断放大矩形
- for(int i=0;i<361;i+=60)
- {
- // 绘图平面以图片的中心点旋转
- graphics.TranslateTransform(Pcenter.X, Pcenter.Y);
- //在水平和垂直方向上同时增加矩形的宽度和高度
- rotateRect.Inflate(scale*15,scale*15);
- graphics.RotateTransform(i);
- //恢复绘图平面在水平和垂直方向的平移
- graphics.TranslateTransform(-Pcenter.X, -Pcenter.Y);
- //绘制图片并延时
- graphics.DrawRectangle(&Pen(Color::Black),
- rotateRect.X,rotateRect.Y,rotateRect.Width,rotateRect.Height);
- ::Sleep(100);
- //重置对矩形大小的所有变换
- graphics.ResetTransform();
- rotateRect.Inflate(-scale*15,-scale*15);
- scale++;
- }
- }
- void FontRotate_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- FontFamily fontFamily(L"宋体");
- Font myFont(&fontFamily, 20, FontStyleBold, UnitPixel);
- SolidBrush redBrush(Color::Red);
- //设置文本输出区域
- RectF layoutRect(myFont.GetHeight(&graphics),myFont.GetHeight(&graphics),
- MyClient.Width/2,MyClient.Height/2);
- StringFormat format;
- //设置文本输出格式
- format.SetAlignment(StringAlignmentNear);
- format.SetLineAlignment(StringAlignmentCenter);
- //将文本的输出起点设置成窗口的中心
- graphics.TranslateTransform(layoutRect.Width,layoutRect.Height);
- //在旋转时每隔30度输出文本
- for(int i=0;i<360;i+=30)
- {
- //旋转绘图平面
- graphics.RotateTransform(i);
- MyDrawString(graphics, " 旋转字体",myFont, redBrush,
- layoutRect, format);
- //恢复所做的旋转
- graphics.RotateTransform(-i);
- }
- }
- void MirrorText_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush redBrush(Color::Red);
- //构造字体,用于镜像文本输出
- FontFamily fontFamily(L"华文楷体");
- Font myFont(&fontFamily,
- 48, FontStyleRegular, UnitPixel);
- //构造英文字体,用于演示文本的输出方向
- FontFamily fontFamily2(L"Courier New");
- Font myFont2(&fontFamily2, 16, FontStyleRegular, UnitPixel);
- //设置文本输出区域
- RectF layoutRect(myFont.GetHeight(&graphics),myFont.GetHeight(&graphics),
- MyClient.Width/2,MyClient.Height/2);
- StringFormat format;
- //设置文本输出时精确位置
- //行距-下行部分 (设计单位)
- float ascent =fontFamily.GetLineSpacing(FontStyleRegular)-
- fontFamily.GetCellDescent(FontStyleRegular);
- //行距-上行部分 (设计单位)
- float ascent2 =fontFamily.GetLineSpacing(FontStyleRegular)-
- fontFamily.GetCellAscent(FontStyleRegular);
- //将上、下部距离的设计单位转换成像素单位
- float ascentPixel = myFont.GetSize()*
- ascent /(fontFamily.GetEmHeight(FontStyleRegular));
- float ascentPixel2 = myFont.GetSize()*
- ascent2 /(fontFamily.GetEmHeight(FontStyleRegular));
- //绘图平面下移
- graphics.TranslateTransform(0,MyClient.Height/4);
- //绘制正常方向的文本
- MyDrawString(graphics, "镜像输出",myFont,redBrush,
- PointF(0,-myFont.GetHeight(&graphics)+ascentPixel2), format);
- //绘图平面右移,演示当前文本输出文向
- graphics.TranslateTransform(200,0);
- CString msg;
- for(int i=0;i<3;i++)
- {
- msg.Format("Line %d...........",i);
- MyDrawString(graphics, msg,myFont2,redBrush,
- PointF(0,myFont2.GetHeight(&graphics)*i),format);
- }
- //恢复对绘图平面的右移
- graphics.TranslateTransform(-200,0);
- //绘图平面在垂直方向倒置
- graphics.ScaleTransform(1,-1);
- //输出镜像文本
- MyDrawString(graphics, "镜像输出",myFont,SolidBrush(Color::Gray),
- PointF(0,-ascentPixel), format);
- //绘图平面再次右移,演示当前文本输出文向
- graphics.TranslateTransform(200,0);
- for(i=0;i<3;i++)
- {
- msg.Format("Line %d...........",i);
- MyDrawString(graphics, msg,myFont2, SolidBrush(Color::Gray),
- PointF(0,myFont2.GetHeight(&graphics)*i), format);
- }
- //重置在绘图平面上进行的所有变换:演示水平镜像文本输出
- graphics.ResetTransform ( ) ;
- //绘图平面下移
- graphics.TranslateTransform(MyClient.Width/2,
- MyClient.Height/2+myFont.GetHeight(&graphics)*2);
- //绘制正常方向的文本
- MyDrawString(graphics, "镜像输出",myFont, redBrush,
- PointF(0,0), format);
- //绘图平面在水平方向倒置
- graphics.ScaleTransform(-1,1);
- //输出水平镜像文本
- MyDrawString(graphics, "镜像输出",myFont, SolidBrush(Color::Gray),
- PointF(0,0), format);
- }
- void Matrix_ListElements_Click_1()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush redBrush(Color::Red);
- FontFamily fontFamily(L"华文楷体");
- Font myFont(&fontFamily,20,
- FontStyleRegular, UnitPixel);
- //进行平移变换
- graphics.TranslateTransform(10,10);
- graphics.TranslateTransform(20,10);
- Matrix m;;
- graphics.GetTransform(&m);
- //获取已经实施在绘图平面的坐标变换
- CString msg;
- float x[6]; m.GetElements(x);
- //输出矩阵的每一个元素
- for(int i=0; i<6; i++)
- {
- msg.Format("第%d个矩阵元素为%.2f",i,x[i]);
- MyDrawString(graphics, msg,myFont, redBrush,
- PointF(0, myFont.GetHeight(&graphics)*(i+1)));
- }
- }
- void MatrixPos_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen pen(Color::Blue,4);
- Pen pen2(Color::Gray);
- FontFamily fontFamily(L"宋体");
- Font myFont(&fontFamily,
- 20, FontStyleRegular, UnitPixel);
- //获取当前窗口的大小
- Rect rect(0,0,
- MyClient.Width,MyClient.Height);
- //设置定义椭圆的矩形
- Rect r(-40, -65,80, 130);
- Matrix matrix;
- graphics.TranslateTransform(rect.Width/2, rect.Height/2);
- //使用默认的旋转顺序(MatrixOrderPrepend)
- graphics.RotateTransform(20);
- //获取对绘图平面已经进行的坐标变换
- graphics.GetTransform(&matrix);
- //绘制椭圆
- graphics.DrawEllipse(&pen, r);
- //使用MatrixOrderAppend旋转顺序
- graphics.ResetTransform();
- graphics.TranslateTransform(rect.Width/2, rect.Height/2);
- graphics.RotateTransform(20,MatrixOrderAppend);
- //使用红色画笔绘制另一个椭圆
- graphics.DrawEllipse(&Pen(Color::Red,1), r);
- Matrix matrix2;
- graphics.GetTransform(&matrix2);
- //重置坐标变换、以窗口中心点为原点绘制坐标轴
- graphics.ResetTransform();
- graphics.DrawLine(&pen2, 0, rect.Height/2,
- rect.Width, rect.Height/2);
- graphics.DrawLine(&pen2, rect.Width/2,
- 0, rect.Width/2, rect.Height);
- CString msg;
- //获取矩阵元素值
- float x[6]; matrix.GetElements(x);
- //输出矩阵元素值
- int i=0;
- for (i=0; i<6; i++)
- {
- msg.Format("%8.2f",x[i]);
- //输出行向量
- if(i==1||i==3||i==5)
- {
- //垂直方向上下移一行
- graphics.TranslateTransform(0,myFont.GetHeight(&graphics));
- MyDrawString (graphics, msg, myFont, SolidBrush(Color::Black), PointF(0,0));
- }
- }
- }
- void Martrix_Invert_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen myPen(Color::Blue,4);
- Matrix matrix(1.0f, 0.0f, 0.0f, 1.0f, 30.0f, 20.0f);
- //检查矩阵是否可逆?
- if(!matrix.IsInvertible())
- {
- AfxMessageBox("该矩形为不可逆矩形,无法演示");
- return;
- }
- graphics.SetTransform(&matrix);
- for(int i=0;i<200;i++)
- {
- //计算逆矩形
- matrix.Invert();
- //对绘图平面进行坐标变换
- graphics.SetTransform(&matrix);
- graphics.DrawRectangle(&myPen, 35, 25, 200, 100);
- }
- }
- void Matrix_Multiply_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush brush(Color::Blue);
- FontFamily fontFamily(L"Arial");
- Font myFont(&fontFamily,
- 16, FontStyleRegular, UnitPixel);
- //加载图片
- Bitmap image(L"jieba.bmp");
- //缩放
- Matrix matrix1(0.80f, 0.0f, 0.0f, 0.8f, 0.0f, 0.0f);
- //位移
- Matrix matrix2(1.0f, 0.0f, 0.0f, 1.0f, 20.0f, 10.0f);
- //从右上往左下翻转图片:X轴与Y轴对调
- Matrix matrix3(0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
- //第一次追加变换
- matrix1.Multiply(&matrix2, MatrixOrderAppend);
- //第二次追加变换
- matrix1.Multiply(&matrix3,MatrixOrderAppend);
- //进行复合变换
- graphics.SetTransform(&matrix1);
- //绘制图片
- graphics.DrawImage(&image, 0, 0);
- //获取已经作用在绘图平面上的坐标变换
- Matrix matrix;
- graphics.GetTransform(&matrix);
- graphics.ResetTransform();
- //在新位置上查看变换矩阵信息
- graphics.TranslateTransform(image.GetHeight(),0);
- //获取矩阵元素值
- float x[6]; matrix.GetElements(x);
- //输出每一个矩阵元素的值
- CString msg;
- int i=0;
- for(i=0; i<6; i++)
- {
- msg.Format("%6.2f",x[i]);
- //输出行向量
- if(i==1||i==3||i==5)
- {
- //垂直方向上下移一行
- graphics.TranslateTransform(0,myFont.GetHeight(&graphics));
- MyDrawString(graphics, msg,myFont,SolidBrush(Color::Black),
- PointF(0,0));
- }
- }
- }
- void Matrix_TransformPoints_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush redBrush(Color::Red);
- SolidBrush blueBrush(Color::Blue);
- Pen pen1(Color::Blue,2);
- Pen pen2(Color::Red,1);
- //定义构成曲线的点坐标
- Point points[] =
- {
- Point(50, 100),
- Point(100, 50),
- Point(150, 125),
- Point(200, 100),
- Point(250, 125),
- };
- //绘制曲线(未使用变换时)
- graphics.DrawCurve(&pen1, points, 5);
- //绘制曲线定义点信息
- for(int i=0;i<5;i++)
- {
- graphics.FillEllipse(&blueBrush,
- points[i].X-5,points[i].Y-5,10,10);
- }
- //定义一个在竖直方向上缩放因子为0.5的矩阵
- Matrix matrix(1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f);
- //对points数据中的每一个成员进行矩形运算
- matrix.TransformPoints(points);
- //绘制曲线(使用变换后)
- graphics.DrawCurve(&pen2, points,5);
- //再次绘制曲线定义点信息
- for(i=0;i<5;i++)
- {
- graphics.FillEllipse(&redBrush,
- points[i].X-5,points[i].Y-5,10,10);
- }
- }
- void Matrix_TransformPoints2_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //构造字体系列
- FontFamily fontFamily(L"隶书");
- SolidBrush redBrush(Color::Red);
- //设置文本输出质量
- graphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit);
- graphics.SetSmoothingMode(SmoothingModeAntiAlias);
- //创建路径区域
- GraphicsPath path;
- //向区域中追加文本,字体大小为60
- path.AddString(L"文字特效",4 ,&fontFamily,(int)FontStyleRegular,
- 60, Point(0, 0), &StringFormat());
- //获取路径的点信息
- //获取路径的点类型信息
- PathData pathdata;
- path.GetPathData(&pathdata);
- //将文本在水平方向上缩小,在垂直方向上拉伸
- Matrix matrix(0.50f, 0.0f, 0.0f, 3.5f, 0.0f,0.0f);
- //对points数据中的每一个成员进行矩形运算
- matrix.TransformPoints(pathdata.Points, pathdata.Count);
- //根据计算后的点重新构造路径
- GraphicsPath newpath(pathdata.Points,pathdata.Types,pathdata.Count);
- //填充路径
- graphics.FillPath(&redBrush,&newpath);
- //第二种特效
- Matrix matrix2(0.6f, 0.5f, 0.20f, 1.5f, 160.0f, -40.0f);
- PathData pathdata2;
- path.GetPathData(&pathdata2);
- //对所有的点进行计算
- matrix2.TransformPoints(pathdata2.Points, pathdata2.Count);
- //根据计算后的点重新构造路径
- GraphicsPath newpath2(pathdata2.Points,pathdata2.Types,pathdata2.Count);
- graphics.FillPath(&redBrush,&newpath2);
- }
- void Matrix_TransformVectors_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen pen(Color::Blue, 7);
- pen.SetEndCap(LineCapArrowAnchor);
- SolidBrush brush(Color::Blue);
- // 定义一个点和一个向量
- PointF point[]={PointF(100, 50)};
- PointF vector[]={PointF(100, 50)};
- // 绘制点point
- graphics.FillEllipse(&brush, RectF(point[0].X-5, point[0].Y-5, 10, 10));
- //将点(10,10)与vector表示的位置连接起来
- graphics.DrawLine(&pen, PointF(10, 10), vector[0]);
- // 定义变换矩阵
- Matrix matrix(0.8f, 0.6f, -0.6f, 0.8f, 100.0f, 0.0f);
- //对点point的位置信息进行变换
- matrix.TransformPoints(point);
- //对点vector的位置信息进行二维向量变换
- matrix.TransformVectors(vector);
- // 绘制变换后的点
- pen.SetColor(Color::Red);
- brush.SetColor(Color::Red);
- graphics.FillEllipse(&brush, RectF(point[0].X - 5, point[0].Y - 5, 10, 10));
- //将点(10,10)与变换后的vector表示的位置连接起来
- graphics.DrawLine(&pen, PointF(10, 10), vector[0]);
- }
- void Matrix_RotateAt_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Pen pen(Color::Blue,4);
- Pen pen2(Color::Gray);
- //获取当前窗口区域
- RectF rect(0,0,MyClient.Width,MyClient.Height);;
- //构造并平移矩阵到窗口中心点
- Matrix matrix;
- matrix.Translate(rect.Width/2, rect.Height/2);
- //将绘图平面依次旋转一周
- for(int i=0;i<360;i+=30)
- {
- //旋转矩阵,相对于窗口中心点
- matrix.RotateAt(i, PointF(rect.Width/2,
- rect.Height/2), MatrixOrderAppend);
- //将旋转后的矩阵作用于绘图平面
- graphics.SetTransform(&matrix);
- //绘制椭圆
- graphics.DrawEllipse(&pen, -80, -30, 160, 60);
- //重置作用在绘图平面是所有的变换
- graphics.ResetTransform();
- }
- //以窗口中心点为原点绘制坐标轴
- graphics.DrawLine(&pen2, PointF(0, rect.Height/2),
- PointF(rect.Width, rect.Height/2));
- graphics.DrawLine(&pen2, PointF(rect.Width/2, 0),
- PointF(rect.Width/2, rect.Height));
- }
- void Matrix_Shear_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //装入图片
- Bitmap image(L"jieba.bmp");
- //定义图片的显示区域
- Rect rect(0,0,100,150);
- //绘制源图
- graphics.DrawImage(&image,rect);
- Matrix matrix;
- //设置水平投射因子
- matrix.Shear(0.8f,0.0f);
- //对绘图平面使用投射变换
- graphics.SetTransform(&matrix);
- graphics.TranslateTransform(100,0);
- //绘制投射变换后的图片
- graphics.DrawImage(&image,rect);
- graphics.ResetTransform();
- Matrix matrix2;
- //设置垂直投射因子
- matrix2.Shear(0.0f,0.8f);
- //对绘图平面使用投射变换
- graphics.SetTransform(&matrix2);
- graphics.TranslateTransform(200,0);
- //绘制投射变换后的图片
- graphics.DrawImage(&image,rect);
- }
- void Matrix_TextoutShear_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush brush(Color::Blue);
- FontFamily fontFamily(L"华文楷体");
- Font myFont(&fontFamily, 20, FontStyleRegular, UnitPixel);
- Font myfont(L"Times New Roman", 100);
- Matrix mymat;
- //投射
- mymat.Shear( -1.4f, 0.0f ) ;
- //缩放
- mymat.Scale( 1, 0.5f ) ;
- //平移
- mymat.Translate ( 236, 170 ) ;
- //对绘图平面实施坐标变换、、
- graphics.SetTransform(&mymat);
- SolidBrush mybrush(Color::Gray);
- SolidBrush redbrush(Color::Red);
- //绘制阴影
- MyDrawString(graphics, "Hello",myfont,mybrush, PointF(0, 50));
- graphics.ResetTransform ( ) ;
- //绘制前景
- MyDrawString(graphics, "Hello",myfont,redbrush, PointF(0, 50));
- CString msg;
- //获取矩阵元素值
- float x[6]; mymat.GetElements(x);
- //输出每一个矩阵元素的值
- int i=0;
- for(; i<6; i++)
- {
- msg.Format("{0,%6.2f}",x[i]);
- //输出行向量
- if(i==1||i==3||i==5)
- {
- MyDrawString(graphics, msg,myFont,SolidBrush(Color::Black),
- PointF(0,0));
- //垂直方向上下移一行
- graphics.TranslateTransform(0,myFont.GetHeight(&graphics));
- }
- }
- }
- void Matrix_ChangeFontHeight_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //构造字体系列
- FontFamily fontFamily(L"隶书");
- //创建路径区域
- GraphicsPath path;
- SolidBrush redBrush(Color::Red);
- //设置文本输出质量
- graphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit);
- graphics.SetSmoothingMode(SmoothingModeAntiAlias);
- //向区域中追加文本,字体大小为80
- path.AddString(L"大小渐变",4,&fontFamily,(int)FontStyleRegular,
- 80, Point(0, 0), &StringFormat());
- //获取路径所占的矩形区域
- RectF bound; path.GetBounds(&bound);
- //获取路径区域的中心点
- float halfH=bound.Height/2;
- float halfW=bound.Width/2;
- //对路径实施变换,更改路径区域的中心点
- Matrix pathMartrix(1,0,0,1,-halfW,-halfH);
- path.Transform(&pathMartrix);
- //获取路径的点信息
- //获取路径的点类型信息
- PathData pathdata;
- path.GetPathData(&pathdata);
- //依次对路径的定义点的Y值进行缩放
- for(int i=0;i<pathdata.Count;i++)
- {
- //根据该点距路径起点的距离占整个路径长度的比例更改Y值
- pathdata.Points[i].Y*=2*(bound.Width-abs(pathdata.Points[i].X))/bound.Width;
- }
- //根据更改后的路径定义点重新构造路径
- GraphicsPath newpath(pathdata.Points,pathdata.Types,pathdata.Count);
- //将绘图平面的原点移到窗口中心
- graphics.TranslateTransform(MyClient.Width/2,MyClient.Height/2-40);
- //填充路径
- graphics.FillPath(&redBrush,&newpath);
- }
- void ColorMatrix_Start_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //加载图片
- Bitmap image(L"ColorInput.bmp");
- int width = image.GetWidth();
- int height = image.GetHeight();
- ImageAttributes imageAttributes;
- //定义色彩变换矩阵
- ColorMatrix colorMatrix=
- {
- 2.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 1.0f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- //启用色彩变换矩阵
- imageAttributes.SetColorMatrix(
- &colorMatrix,
- ColorMatrixFlagsDefault,
- ColorAdjustTypeBitmap);
- //绘制源图
- graphics.DrawImage(&image, 0, 0);
- //使用色彩变换矩阵输出图片
- graphics.TranslateTransform(width+10,0);
- graphics.DrawImage(
- &image,
- Rect(0, 0, width, height),
- 0, 0,
- width, height,UnitPixel,
- &imageAttributes);
- }
- void TranslateColor_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //加载图片
- Bitmap image(L"ColorBar.bmp");
- ImageAttributes imageAttributes;
- int width = image.GetWidth();
- int height = image.GetHeight();
- //定义色彩变换矩阵
- ColorMatrix colorMatrix=
- {
- 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.75f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- //启用色彩变换矩阵
- imageAttributes.SetColorMatrix(&colorMatrix,
- ColorMatrixFlagsDefault,ColorAdjustTypeBitmap);
- //绘制源图
- graphics.DrawImage(&image, 0, 0);
- //使用色彩变换矩阵输出图片
- graphics.TranslateTransform(width+10,0);
- graphics.DrawImage(&image, Rect(0, 0, width, height),
- 0, 0,width, height,UnitPixel,&imageAttributes);
- }
- void ScaleColor_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //加载图片
- Bitmap image(L"ColorBar.bmp");
- ImageAttributes imageAttributes;
- int width = image.GetWidth();
- int height = image.GetHeight();
- //定义色彩变换矩阵1
- ColorMatrix colorMatrix=
- {
- 0.5f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.5f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.5f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- //定义色彩变换矩阵2
- ColorMatrix colorMatrix2=
- {
- 0.5f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.5f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 500.5f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- //启用色彩变换矩阵1
- imageAttributes.SetColorMatrix(
- &colorMatrix,
- ColorMatrixFlagsDefault,
- ColorAdjustTypeBitmap);
- //绘制源图
- graphics.DrawImage(&image, 0, 0);
- //使用色彩变换矩阵输出图片
- graphics.TranslateTransform(width+10,0);
- graphics.DrawImage(
- &image,
- Rect(0, 0, width, height),
- 0, 0,
- width, height,UnitPixel,
- &imageAttributes);
- //清除已经采取的色彩变换
- imageAttributes.ClearColorMatrix(ColorAdjustTypeBitmap);
- //重新加载另一变换矩阵Matrix2
- imageAttributes.SetColorMatrix(
- &colorMatrix2,
- ColorMatrixFlagsDefault,
- ColorAdjustTypeBitmap);
- //演示使用Matrix2的色彩调整情况
- graphics.TranslateTransform(width+10,0);
- graphics.DrawImage(
- &image,
- Rect(0, 0, width, height),
- 0, 0,
- width, height,UnitPixel,
- &imageAttributes);
- }
- void RotateColor_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //加载图片
- Bitmap image(L"Colorinput.bmp");
- int width = image.GetWidth();
- int height = image.GetHeight();
- float degrees = 90;
- //从角度到弧度
- double r = degrees * PI/ 180.0f;
- ImageAttributes imageAttributes;
- //绘制源图
- graphics.DrawImage(&image, 0, 0);
- //红色绕着蓝色旋转
- ColorMatrix colorMatrix=
- {
- (float)cos(r), (float)sin(r), 0.0f, 0.0f, 0.0f,
- -(float)sin(r), (float)cos(r), 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- //使用色彩变换矩阵输出图片(R->B)
- graphics.TranslateTransform(width+10,0);
- //设置R->B色彩变换矩阵
- imageAttributes.SetColorMatrix(
- &colorMatrix,
- ColorMatrixFlagsDefault,
- ColorAdjustTypeBitmap);
- graphics.DrawImage(
- &image,
- Rect(0, 10, width, height),
- 0, 0,width,height,
- UnitPixel,
- &imageAttributes);
- //清除已经采取的色彩变换
- imageAttributes.ClearColorMatrix(ColorAdjustTypeBitmap);
- //重新加载另一变换矩阵Matrix2
- //绿色绕着红色旋转
- ColorMatrix colorMatrix2=
- {
- 1, 0, 0.0f, 0.0f, 0.0f,
- 0, (float)cos(r), (float)sin(r), 0.0f, 0.0f,
- 0.0f, -(float)sin(r), (float)cos(r), 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- imageAttributes.SetColorMatrix(
- &colorMatrix2,
- ColorMatrixFlagsDefault,
- ColorAdjustTypeBitmap);
- //在第二行输出
- graphics.ResetTransform();
- graphics.TranslateTransform(0,height+10);
- graphics.DrawImage(&image,
- Rect(0, 0, width, height),
- 0, 0,width, height,UnitPixel,
- &imageAttributes);
- //清除已经采取的色彩变换
- imageAttributes.ClearColorMatrix(ColorAdjustTypeBitmap);
- //蓝色绕着红色旋
- ColorMatrix colorMatrix3=
- {
- (float)cos(r), 0, -(float)sin(r), 0.0f, 0.0f,
- 0, 1, 0.0f, 0.0f, 0.0f,
- (float)sin(r), 0, (float)cos(r), 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- //重新加载另一变换矩阵Matrix3
- imageAttributes.SetColorMatrix(
- &colorMatrix3,
- ColorMatrixFlagsDefault,
- ColorAdjustTypeBitmap);
- graphics.TranslateTransform(width+10,0);
- graphics.DrawImage(
- &image,
- Rect(0, 0, width, height),
- 0, 0,
- width, height,UnitPixel,
- &imageAttributes);
- }
- void ColorShear_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //加载图片
- Bitmap image(L"Colorinput.bmp");
- ImageAttributes imageAttributes;
- int width = image.GetWidth();
- int height = image.GetHeight();
- //定义色彩变换矩阵
- ColorMatrix colorMatrix=
- {
- 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
- 0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- //启用色彩变换矩阵
- imageAttributes.SetColorMatrix(
- &colorMatrix,
- ColorMatrixFlagsDefault,
- ColorAdjustTypeBitmap);
- //绘制源图
- graphics.DrawImage(&image, 0, 0);
- //使用色彩变换矩阵输出图片
- graphics.TranslateTransform(width+10,0);
- graphics.DrawImage(&image, Rect(0, 0, width, height),
- 0, 0,width, height,UnitPixel,&imageAttributes);
- }
- void ColorRemap_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //加载蓝色背景的图片
- Bitmap image(L"Nemo_Blue.bmp");
- ImageAttributes imageAttributes;
- int width = image.GetWidth();
- int height = image.GetHeight();
- //将蓝色替换成白色,以达到抠除的效果
- ColorMap colorMap;
- colorMap.oldColor = Color(255, 0, 0, 255);
- colorMap.newColor = Color(255, 255,255,255);
- //设置色彩转换表
- ColorMap remapTable[1]; remapTable[0]=colorMap;
- //设置图片的色彩信息
- imageAttributes.SetRemapTable(1, remapTable, ColorAdjustTypeBitmap);
- //绘制原始图像
- graphics.DrawImage(&image, 0, 0, width, height);
- //绘制已经抠除背景色的新图像
- graphics.DrawImage(&image,
- Rect(width+10, 0, width, height), //目标区域
- 0, 0, // 源图左上角坐标
- width, // 源图宽
- height, // 源图宽
- UnitPixel,
- //图片的色彩信息
- &imageAttributes);
- }
- void SetRGBOutputChannel_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //加载图片
- Bitmap image(L"jieba.bmp");
- //绘制源图
- graphics.DrawImage(&image, 0, 0);
- int width = image.GetWidth();
- int height = image.GetHeight();
- ImageAttributes imageAttributes;
- //设置红色通道
- ColorMatrix colorMatrix=
- {
- 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- //启用色彩变换矩阵
- imageAttributes.SetColorMatrix(
- &colorMatrix,
- ColorMatrixFlagsDefault,
- ColorAdjustTypeBitmap);
- //使用色彩变换矩阵输出图片
- graphics.TranslateTransform(width+10,0);
- graphics.DrawImage(&image, Rect(0, 0, width, height),
- 0, 0,width, height,UnitPixel,&imageAttributes);
- //清除已经采取的色彩变换
- imageAttributes.ClearColorMatrix(ColorAdjustTypeBitmap);
- //设置绿色通道
- ColorMatrix colorMatrix2=
- {
- 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- //启用色彩变换矩阵
- imageAttributes.SetColorMatrix(
- &colorMatrix2,
- ColorMatrixFlagsDefault,
- ColorAdjustTypeBitmap);
- //使用色彩变换矩阵输出图片
- graphics.ResetTransform();
- graphics.TranslateTransform(0,height+10);
- graphics.DrawImage(&image, Rect(0, 0, width, height),
- 0, 0,width, height,UnitPixel,&imageAttributes);
- //清除已经采取的色彩变换
- imageAttributes.ClearColorMatrix(ColorAdjustTypeBitmap);
- //设置蓝色通道
- ColorMatrix colorMatrix3=
- {
- 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- //启用色彩变换矩阵
- imageAttributes.SetColorMatrix(
- &colorMatrix3,
- ColorMatrixFlagsDefault,
- ColorAdjustTypeBitmap);
- //使用色彩变换矩阵输出图片
- graphics.TranslateTransform(width+10,0);
- graphics.DrawImage(&image, Rect(0, 0, width, height),
- 0, 0,width, height,UnitPixel,&imageAttributes);
- }
- void Metafile_Click()
- {
- // Note: To record a metafile, you must construct a Graphics
- //object based on a Metafile object. The recording of the metafile
- //ends when that Graphics object is deleted or goes out of scope.
- //新建一个图元文件
- HDC hdc =GetGraphics().GetHDC();
- Metafile metaFile1(L"图元文件示例.emf", hdc);
- {
- //使用Metafile对象的地址做为绘图平面
- Graphics graphics(&metaFile1);
- //定义一个由红到蓝的渐变色画刷
- LinearGradientBrush RtoBBrush(
- Point(0, 10),
- Point(200, 10),
- Color::Red,
- Color::Blue);
- //定义一个由蓝到黄的渐变色画刷
- LinearGradientBrush BtoYBrush(
- Point(0, 10),
- Point(200, 10),
- Color::Blue,
- Color::Yellow);
- Pen bluePen(Color::Blue);
- // 以下的操作是往屏幕上绘制一八卦图形
- Rect ellipseRect(0, 0, 200, 200);
- Rect left(0, 50, 100, 100);
- graphics.DrawArc(&bluePen,left,180.0f,180.0f);
- Rect right(100, 50, 100, 100);
- graphics.FillPie(&RtoBBrush, ellipseRect,0.0f,180.0f);
- graphics.FillPie(&BtoYBrush, ellipseRect,180.0f,180.0f);
- graphics.FillPie(&RtoBBrush, left,180.0f,180.0f);
- graphics.FillPie(&BtoYBrush, right,0.0f,180.0f);
- //文本输出
- SolidBrush solidBrush(Color::Black);
- FontFamily fontFamily(L"隶书");
- Font font(&fontFamily, 27,
- FontStyleRegular, UnitPixel);
- MyDrawString(graphics, "图元文件示例", font, solidBrush,
- PointF(20.0f, 80.0f));
- //到此,GDI+进行的只是往硬盘中存放图片信息的操作
- }
- //将上面的绘图信息进行回放
- Graphics playbackGraphics(hdc);
- playbackGraphics.Clear(Color::White);
- //打开并显示图元文件
- Metafile metaFile2(L"图元文件示例.emf");
- playbackGraphics.DrawImage(&metaFile2, Point(0,0));
- //释放HDC
- playbackGraphics.ReleaseHDC(hdc);
- }
- void CroppingAndScaling_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //加载图片
- Bitmap image(L"nemo.bmp");
- int width = image.GetWidth();
- int height = image.GetHeight();
- // 目标显示区域在源图大小的基础上放大1.4倍
- RectF destinationRect(
- width+10, 0.0f, 1.4f* width, 1.4f* height);
- //绘制源图
- graphics.DrawImage(&image, 0, 0);
- //在目标区域内输出位图
- graphics.DrawImage(
- &image,
- destinationRect,
- 0.f, 0.f, // 原图左上角
- 0.65f*width, // 仅显示原图宽度的65%部分
- 0.65f*height, // 仅显示原图高度的65%部分
- UnitPixel);
- }
- void UsingInterpolationMode_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //装入图片
- Bitmap image(L"eagle.bmp");
- int width = image.GetWidth();
- int height = image.GetHeight();
- //绘制源图
- graphics.DrawImage(
- &image,
- Rect(0, 0, width, height), //目标区域
- 0, 0, //源图左上角坐标
- width, //源图宽度
- height, //源图高
- UnitPixel);
- //绘图平面右移
- graphics.TranslateTransform( width+10,0);
- //最临近插值法(低质量)
- graphics.SetInterpolationMode(InterpolationModeNearestNeighbor);
- graphics.DrawImage(
- &image,
- RectF(0.0f, 0.0f, 0.6f*width, 0.6f*height), //目标区域
- 0, 0, //源图左上角坐标
- width, //源图宽度
- height, //源图高
- UnitPixel);
- //绘图平面右移
- graphics.TranslateTransform( 0.6f*width+10,0);
- // 高质量双线性插值法
- graphics.SetInterpolationMode(InterpolationModeHighQualityBilinear);
- graphics.DrawImage(
- &image,
- RectF(0, 0, 0.6f * width, 0.6f * height), //目标区域
- 0, 0, //源图左上角坐标
- width, //源图宽度
- height, //源图高
- UnitPixel);
- //绘图平面右移
- graphics.TranslateTransform(width*0.6f+10, 0.f);
- // 高质量双三次插值法
- graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
- graphics.DrawImage(
- &image,
- RectF(0, 0, 0.6f * width, 0.6f * height), //目标区域
- 0, 0, //源图左上角坐标
- width, //源图宽度
- height, //源图高
- UnitPixel);
- }
- void RotateFlip_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //加载图片
- Bitmap photo(L"nemo2.bmp");
- //得到图片尺寸
- int iWidth = photo.GetWidth();
- int iHeight = photo.GetHeight();
- //绘制原始图片
- graphics.DrawImage(&photo, 10+photo.GetWidth()+2,
- 10, photo.GetWidth(), photo.GetHeight());
- //水平翻转图片
- photo.RotateFlip(RotateNoneFlipX);
- //旋转后的图片
- graphics.DrawImage(&photo, 10, 10, photo.GetWidth(), photo.GetHeight());
- }
- void ImageSkewing_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //定义图形的目标显示区域
- Point destination[]=
- {
- Point(200, 20), //原始图像左上角映射后的坐标
- Point(110, 100), //原始图像右上角映射后的坐标
- Point(250, 30) //原始图像左下角映射后的坐标
- };
- Bitmap image(L"Stripes.bmp");
- // 绘制原始图像
- graphics.DrawImage(&image, 0, 0);
- // 绘制基于平行四边形映射后的图像
- graphics.TranslateTransform(image.GetWidth(),0);
- graphics.DrawImage(&image, destination, 3);
- }
- void Cubeimage_Click()
- {
- int WIDTH=200;
- int LEFT=200;
- int TOP=200;
- Graphics &graphics=GetGraphics();
- //使用蓝色做背景色清屏
- graphics.Clear(Color::Blue);
- //设置插值模式:高质量双三次插值法
- graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
- //分别装入张贴在立方体三面的图片
- Bitmap face(L"rose.bmp");
- Bitmap top(L"flower.bmp");
- Bitmap right(L"yujinxiang.bmp");
- //重新定义用于张贴在正面的图片坐标
- Point destinationFace[] =
- {
- Point(LEFT,TOP),
- Point(LEFT+WIDTH, TOP),
- Point(LEFT, TOP+WIDTH)
- };
- //张贴正面图像
- graphics.DrawImage(&face,destinationFace,3);
- //重新定义用于张贴在顶部的图片坐标
- PointF destinationTop[]=
- {
- PointF(LEFT+WIDTH/2, TOP-WIDTH/2),
- PointF(LEFT+WIDTH/2+WIDTH, TOP-WIDTH/2),
- PointF(LEFT, TOP)
- };
- //张贴顶部面图像
- graphics.DrawImage(&top, destinationTop, 3);
- //重新定义用于张贴在右侧的图片坐标
- Point destinationRight[]=
- {
- Point(LEFT+WIDTH, TOP),
- Point(LEFT+WIDTH/2+WIDTH, TOP-WIDTH/2),
- Point(LEFT+WIDTH,TOP+WIDTH)
- };
- //张贴右侧面图像
- graphics.DrawImage(&right, destinationRight, 3);
- }
- void ThumbnailImage_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //设置插值模式:高质量双三次插值法
- graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
- //加载欲查看缩略图的图片
- Bitmap image(L"flower.bmp");
- //获取当前窗口大小
- Rect client(0,0,
- MyClient.Width,MyClient.Height);
- float width=image.GetWidth();
- float height=image.GetHeight();
- //获取指定大小的缩略图
- Image *pThumbnail = image.GetThumbnailImage(40,40, NULL, NULL);
- //将缩略图作为画刷
- TextureBrush picBrush(pThumbnail);
- //填充窗口
- graphics.FillEllipse(&picBrush,client);
- }
- void Clone_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Bitmap image(L"head.bmp");
- int Height=image.GetHeight();
- int Width=image.GetWidth();
- //定义将图片切分成四个部分的区域
- RectF block[4]=
- {
- RectF(0,0,Width/2,Height/2),
- RectF(Width/2,0,Width/2,Height/2),
- RectF(0,Height/2,Width/2,Height/2),
- RectF(Width/2,Height/2,Width/2,Height/2)
- };
- //分别克隆图片的四个部分
- Bitmap *s[4];
- s[0]=image.Clone(block[0],PixelFormatDontCare);
- s[1]=image.Clone(block[1],PixelFormatDontCare);
- s[2]=image.Clone(block[2],PixelFormatDontCare);
- s[3]=image.Clone(block[3],PixelFormatDontCare);
- //绘制图片的四个部分,各部分绘制时间间隔为1秒
- graphics.DrawImage(s[0],0,0);
- //延时,以达到分块显示的效果
- ::Sleep(1000);
- graphics.DrawImage(s[1],Width/2,0);
- ::Sleep(1000);
- graphics.DrawImage(s[3],Width/2,Height/2);
- ::Sleep(1000);
- graphics.DrawImage(s[2],0,Height/2);
- delete s[0];
- delete s[1];
- delete s[2];
- delete s[3];
- }
- void Picturescale_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //装入图片
- Bitmap image(L"photo.bmp");
- //定义图片的显示区域
- Rect rect(0,0,image.GetWidth(),image.GetHeight());
- graphics.DrawImage(&image,rect);
- //局部缩小的区域大小为80*80
- graphics.TranslateTransform(image.GetWidth()+10,0);
- Rect smallrect(0,0,80,80);
- //局部缩小
- graphics.DrawImage(&image,smallrect,80,10,106,112,UnitPixel);
- graphics.TranslateTransform(0,100);
- //局部放大的区域大小为80*80
- Rect largerect(0,0,80,80);
- //绘制放大后的局部图像
- graphics.DrawImage(&image,largerect,56,101,35,40,UnitPixel);
- }
- void ImageAttributesSetNoOp_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Bitmap image(L"ColorTable.bmp");
- int width=image.GetWidth();
- //绘制标准图片
- graphics.DrawImage(&image,0,0);
- graphics.TranslateTransform(image.GetWidth()+10,0);
- ImageAttributes imAtt;
- //构造一个红色转换到绿色的变换矩阵
- ColorMatrix brushMatrix=
- {
- 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- //设置色彩校正
- imAtt.SetColorMatrix(
- &brushMatrix,
- ColorMatrixFlagsDefault,
- ColorAdjustTypeBitmap);
- //使用色彩校正绘制图片
- graphics.DrawImage(
- &image,
- Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标位置
- 0, 0, image.GetWidth(), image.GetHeight(), //源位置
- UnitPixel,
- &imAtt);
- //临时关闭色彩校正
- imAtt.SetNoOp(ColorAdjustTypeBitmap);
- //不使用色彩校正绘制图片红色->红色
- graphics.TranslateTransform(width+10,0);
- graphics.DrawImage(
- &image,
- Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标位置
- 0, 0, image.GetWidth(), image.GetHeight(), //源位置
- UnitPixel,
- &imAtt);
- //撤消对色彩校正的关闭
- imAtt.ClearNoOp(ColorAdjustTypeBitmap);
- //使用色彩校正绘制图片:红色->绿色
- graphics.TranslateTransform(width+10,0);
- graphics.DrawImage(
- &image,
- Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标位置
- 0, 0, image.GetWidth(), image.GetHeight(), //源位置
- UnitPixel,
- &imAtt);
- }
- void CreateMetaFile()
- {
- Graphics &metagraph=GetGraphics();
- //新建一个图元文件
- HDC hdc =metagraph.GetHDC();
- Metafile metaFile1(CWideCharString("ddd.emf"), hdc);
- //使用Metafile对象的地址做为绘图平面
- Graphics *graphics=Graphics::FromImage(&metaFile1);
- graphics->ScaleTransform(0.8f,0.8f);
- //在沿水平方向输出三个椭圆并填充
- graphics->SetSmoothingMode(SmoothingModeHighQuality);
- //红色椭圆
- graphics->DrawEllipse(&Pen(Color.Red,10), Rect(0, 0, 75, 95));
- graphics->FillEllipse(&SolidBrush(Color::Red), Rect(0, 0, 75, 95));
- //绿色椭圆
- graphics->DrawEllipse(&Pen(Color::Green,10), Rect(40, 0, 75, 95));
- graphics->FillEllipse(&SolidBrush(Color::Green), Rect(40, 0, 75, 95));
- //蓝色椭圆
- graphics->DrawEllipse(&Pen(Color.Blue,10), Rect(80, 0, 75, 95));
- graphics->FillEllipse(&SolidBrush(Color::Blue), Rect(80, 0, 75, 95));
- // 追加三种色彩的文本
- FontFamily fontFamily(CWideCharString("Arial"));
- Font font(&fontFamily, 24, FontStyleRegular, UnitPixel);
- graphics->SetTextRenderingHint(TextRenderingHintAntiAlias);
- //红色文字
- MyDrawString(*graphics, "GDI+",font, SolidBrush(Color::Red), PointF(-80.0f, 0.0f));
- //绿色文字
- MyDrawString(*graphics, "GDI+",font, SolidBrush(Color::Green), PointF(-80.0f, font.GetHeight(graphics)));
- //蓝色文字
- MyDrawString(*graphics, "GDI+",font, SolidBrush(Color::Blue), PointF(-80.0f, font.GetHeight(graphics)*2));
- //释放所有资源。
- //graphics->Dispose();
- //metaFile1.Dispose();
- metagraph.ReleaseHDC(hdc);
- //metagraph.Dispose();
- delete graphics;
- }
- void SetColorMatrices_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //加载图元文件
- Metafile image(L"ddd.emf");
- Unit unit=UnitPixel;
- //获取图片区间
- RectF rect; image.GetBounds(&rect, &unit);
- //不使用任何色彩校正输出图片
- graphics.DrawImage(&image, 0.0f, 0.0f, rect.Width, rect.Height);
- ImageAttributes imAtt;
- //定义一个使红色分量递增1.5的矩阵
- ColorMatrix defaultColorMatrix=
- {
- 1.5f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- //定义一个使绿色分量递增1.5的矩阵
- ColorMatrix defaultGrayMatrix=
- {
- 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 1.5f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- //画笔的彩色色彩信息较校正矩阵:蓝色分量递增1.5的矩阵
- ColorMatrix penColorMatrix=
- {
- 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 1.5f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- //画笔的灰度色矩阵:所有分量递增1.5的矩阵
- ColorMatrix penGrayMatrix=
- {
- 1.5f, 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 1.5f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 1.5f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
- };
- // 设置默认的彩色及灰度校正矩阵.
- //ColorAdjustTypeDefault:修改所有的色彩信息
- imAtt.SetColorMatrices(
- &defaultColorMatrix,
- &defaultGrayMatrix,
- ColorMatrixFlagsAltGray,
- ColorAdjustTypeDefault);
- //使用校正矩阵绘制图元文件:校正所有的色彩
- graphics.TranslateTransform(image.GetWidth()+10,0);
- graphics.DrawImage(
- &image,
- Rect(0, 0, (int)rect.Width,(int)rect.Height),
- rect.X, rect.Y,
- rect.Width,
- rect.Height,
- UnitPixel,
- &imAtt);
- //设置画笔的彩色及灰度色彩校正矩阵
- //ColorAdjustTypePen:修正画笔色彩
- imAtt.SetColorMatrices(
- &penColorMatrix,
- &penGrayMatrix,
- ColorMatrixFlagsAltGray,
- ColorAdjustTypePen);
- //在第二行绘制
- graphics.ResetTransform();
- graphics.TranslateTransform(0,image.GetHeight());
- //使用修正后的画笔绘制图片
- graphics.DrawImage(
- &image,
- Rect(0, 0, (int)rect.Width,(int)rect.Height),
- rect.X, rect.Y,
- rect.Width,
- rect.Height,
- UnitPixel,
- &imAtt);
- graphics.TranslateTransform(image.GetWidth()+10,0);
- //清除在画笔上的所有变换
- imAtt.ClearColorMatrix(ColorAdjustTypePen);
- graphics.DrawImage(
- &image,
- Rect(0, 0, (int)rect.Width,(int)rect.Height),
- rect.X, rect.Y,
- rect.Width,
- rect.Height,
- UnitPixel,
- &imAtt);
- }
- void SetOutputChannelColorProfile_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Bitmap image(L"car.bmp");
- ImageAttributes imAtt;
- int width = image.GetWidth();
- int height = image.GetHeight();
- Rect rect(0, 0, width, height);
- //绘制原始图片
- graphics.DrawImage(&image, rect);
- graphics.TranslateTransform(width,0);
- //设置色彩配置文件
- imAtt.SetOutputChannelColorProfile(
- CWideCharString("kodak_dc.ICM"), ColorAdjustTypeBitmap);
- //使用色彩配置文件输出图片
- graphics.DrawImage(
- &image,
- rect,
- 0, 0, width, height,
- UnitPixel,
- &imAtt);
- }
- void Gammaadjust_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //装入原始图片
- Bitmap image(L"warrior.bmp");
- int width = image.GetWidth();
- int height = image.GetHeight();
- // 设置字体信息
- Font myFont(L"宋体", 12);
- //设置提示信息的显示区域
- PointF origin(width+10, height+20);
- SolidBrush blackBrush(Color::Black);
- ImageAttributes imAtt;
- CString msg;
- //从0-3依次调整输出图片时所使用的Gamma值
- for(float i=0.0f;i<3.0f;i+=0.1f)
- {
- //绘制原始图片
- graphics.DrawImage(&image,0,0);
- //设置Gamma值
- imAtt.SetGamma(i,ColorAdjustTypeBitmap);
- //使用修改后Gamma值进行图片输出
- graphics.DrawImage(
- &image,
- Rect(width+10, 0, width, height), //目标区域
- 0, 0, width, height, //源区域
- UnitPixel,
- &imAtt);
- msg.Format("正在修改Gamma值,Gamma=%.2f",i);
- //显示当前的Gamma值信息
- MyDrawString(graphics, msg,myFont,blackBrush,origin);
- //延时一秒以便观看效果
- ::Sleep(100);
- graphics.Clear(Color::White);
- imAtt.ClearGamma();
- }
- }
- void SetOutputChannel_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //装入图片
- Bitmap image(L"jieba.bmp");
- //图片的高度
- int width = image.GetWidth();
- int height = image.GetHeight();
- //绘制原始图片
- graphics.DrawImage(&image, RectF(0, 0, width, height));
- ImageAttributes imAtt;
- //设置色彩输出通道cyan
- imAtt.SetOutputChannel(ColorChannelFlagsC,
- ColorAdjustTypeBitmap);
- //右移,绘制图片
- graphics.TranslateTransform(width, 0);
- graphics.DrawImage(
- &image,
- Rect(0, 0, width, height),
- 0, 0, width, height,
- UnitPixel,
- &imAtt);
- //设置色彩输出通道:magenta
- imAtt.SetOutputChannel(ColorChannelFlagsM,
- ColorAdjustTypeBitmap);
- //右移,绘制图片
- graphics.TranslateTransform(width, 0);
- graphics.DrawImage(
- &image,
- Rect(0, 0, width, height),
- 0, 0, width, height,
- UnitPixel,
- &imAtt);
- //设置色彩输出通道:yellow
- imAtt.SetOutputChannel(ColorChannelFlagsY,
- ColorAdjustTypeBitmap);
- //右移,绘制图片
- graphics.TranslateTransform(width, 0);
- graphics.DrawImage(
- &image,
- Rect(0, 0, width, height),
- 0, 0, width, height,
- UnitPixel,
- &imAtt);
- //设置色彩输出通道:black
- imAtt.SetOutputChannel(ColorChannelFlagsK,
- ColorAdjustTypeBitmap);
- //右移,绘制图片
- graphics.TranslateTransform(width, 0);
- graphics.DrawImage(
- &image,
- Rect(0, 0, width, height),
- 0, 0, width, height,
- UnitPixel,
- &imAtt);
- }
- void Colorkey_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //装入前后背景图片
- Bitmap forground(L"grid.bmp");
- Bitmap background(L"snike.bmp");
- int width=background.GetWidth();
- int height=background.GetHeight();
- Rect rect(0,0,width,height);
- //将红色设置成关键色
- ImageAttributes imAtt;
- imAtt.SetColorKey(
- Color::Red,
- Color::Red,
- ColorAdjustTypeBitmap);
- //绘制背景
- graphics.DrawImage(&background,0,0);
- //绘制前景
- graphics.DrawImage(
- &forground,
- rect,
- 0, 0, forground.GetWidth(), forground.GetHeight(),
- UnitPixel,
- &imAtt);
- graphics.TranslateTransform(width+20,0);
- graphics.DrawImage(&background,0,0);
- //清除已经应用的关键色信息
- imAtt.ClearColorKey(ColorAdjustTypeBitmap);
- ///将蓝色设置成关键色
- imAtt.SetColorKey(
- Color::Blue,
- Color::Blue,
- ColorAdjustTypeBitmap);
- graphics.DrawImage(&forground,
- rect,
- 0, 0, forground.GetWidth(), forground.GetHeight(),
- UnitPixel,
- &imAtt);
- graphics.TranslateTransform(width+20,0);
- //绘制源图
- graphics.DrawImage(&background,0,0);
- }
- void Setthreshold_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Bitmap image(L"box-2.bmp");
- int Width=image.GetWidth();
- int Height=image.GetHeight();
- //绘制原始图片
- graphics.DrawImage(&image, 10, 10, Width, Height);
- //将阈值从0到1依次运用
- ImageAttributes imAtt;
- for(float i=0.0f;i<1.0f;i+=0.1f)
- {
- //设置输出图片时使用的阈值
- imAtt.SetThreshold(i, ColorAdjustTypeBitmap);
- //绘制已经使用了阈值的图片
- graphics.DrawImage(&image,
- Rect(10+Width, 10, Width, Height),
- 0, 0, Width, Height,
- UnitPixel,
- &imAtt);
- //延时
- ::Sleep(1000);
- }
- }
- void AdjustedPalette_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- graphics.ScaleTransform(0.7f,0.7f);
- //加载图片
- Bitmap image(L"lord-256.bmp");
- //复制图片
- Bitmap image2(L"lord-256.bmp");
- //获取图片使用的调色板信息
- int size=image.GetPaletteSize();
- ColorPalette *palette= (ColorPalette *)new BYTE[size];
- image.GetPalette(palette, size);
- //获取调色板所包含的色彩总数
- int count=palette->Count;
- if(count<1)
- {
- AfxMessageBox("图片无调色板信息可用");
- return;
- }
- //更改调色板中的每一种色彩信息
- for(int i=0;i<count;i++)
- {
- Color u(palette->Entries[i]);
- int r=u.GetR()/2;
- int g=u.GetG()/2;
- int b=u.GetB()/2;
- if(r<1)
- r=0;
- if(g<1)
- g=0;
- if(b<1)
- b=0;
- palette->Entries[i]=u.MakeARGB(255, r,g,b);
- }
- //设置图像的新调色板
- image.SetPalette(palette);
- //绘制原图
- graphics.DrawImage(&image2,0,0);
- //绘制修改后的图片
- graphics.DrawImage(&image,image.GetWidth()+10,0);
- delete palette;
- }
- void SetWrapMode_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //加载图片
- Bitmap image(L"yueru.bmp");
- ImageAttributes imAtt;
- //设置图片排列方式为WrapModeClamp:图片不进行平铺
- imAtt.SetWrapMode(WrapModeClamp,Color::Red);
- //缩小显示源图
- graphics.DrawImage(&image,
- Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标区域
- 0, 0, 2*image.GetWidth(), 2*image.GetHeight(), //源图片区域
- UnitPixel,
- &imAtt);
- graphics.TranslateTransform(image.GetWidth()+10,0);
- //设置图片排列方式为WrapModeTileFlipXY:图片在水平和垂直方向上同时翻转
- imAtt.SetWrapMode(WrapModeTileFlipXY);
- graphics.DrawImage(&image,
- Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标区域
- 0, 0, 2*image.GetWidth(), 2*image.GetHeight(), //源图片区域
- UnitPixel,
- &imAtt);
- graphics.TranslateTransform(image.GetWidth()+10,0);
- //设置图片排列方式为WrapModeTileFlipX:图片在水平上翻转
- imAtt.SetWrapMode(WrapModeTileFlipX);
- graphics.DrawImage(&image,
- Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标区域
- 0, 0, 2*image.GetWidth(), 2*image.GetHeight(), //源图片区域
- UnitPixel,
- &imAtt);
- graphics.TranslateTransform(image.GetWidth()+10,0);
- //设置图片排列方式为WrapModeTileFlipY:图片在垂直上翻转
- imAtt.SetWrapMode(WrapModeTileFlipY);
- graphics.DrawImage(&image,
- Rect(0, 0, image.GetWidth(), image.GetHeight()), //目标区域
- 0, 0, 2*image.GetWidth(), 2*image.GetHeight(), //源图片区域
- UnitPixel,
- &imAtt);
- }
- void ListAllImageEncoders_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush brush(Color::Blue);
- FontFamily fontFamily(L"华文楷体");
- Font myFont(&fontFamily, 20, FontStyleRegular, UnitPixel);
- //获取编码器信息
- UINT num, size;
- GetImageEncodersSize(&num, &size);
- ImageCodecInfo *pImageCodecInfo = (ImageCodecInfo*)new BYTE[size];
- GetImageEncoders(num, size, pImageCodecInfo);
- //输出编码信息
- CString msg;
- for(UINT j = 0; j <num; ++j)
- {
- CString s;
- s.Format("编码器名称:%s\t文件格式扩展名:%s\t\n",
- CString(pImageCodecInfo[j].CodecName),
- CString(pImageCodecInfo[j].FilenameExtension));
- msg+=s;
- }
- MyDrawString(graphics, msg,myFont,brush, PointF(0,0));
- delete pImageCodecInfo;
- }
- //帮助函数
- CString MyStringFromGUID2(const GUID &rguid)
- {
- WCHAR strGuid[39];
- StringFromGUID2(rguid, strGuid, 39);
- return CString(strGuid);
- }
- void ListImageEncoder_Detail_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //获取编码器信息
- UINT num, size;
- GetImageEncodersSize(&num, &size);
- ImageCodecInfo *pImageCodecInfo = (ImageCodecInfo*) new BYTE[size];
- GetImageEncoders(num, size, pImageCodecInfo);
- //查找指定格式文件的编码器信息
- CString msg;
- //查询所有的编码器信息
- for(UINT j = 0; j <num; j++)
- {
- CString s;
- s.Format("开始描述第%d种图形编码信息\n\n",j); msg+=s;
- s.Format("编码标识: %s\n",MyStringFromGUID2(pImageCodecInfo[j].Clsid)); msg+=s;
- s.Format("文件格式标识: %s\n", MyStringFromGUID2(pImageCodecInfo[j].FormatID)); msg+=s;
- s.Format("编码器名称: %s\n", CString(pImageCodecInfo[j].CodecName)); msg+=s;
- s.Format("编码器依存的动态连接库名: %s\n",CString(pImageCodecInfo[j].DllName)); msg+=s;
- s.Format("编码描述: %s\n", CString(pImageCodecInfo[j].FormatDescription)); msg+=s;
- s.Format("编码器对应的文件扩展名: %s\n",CString(pImageCodecInfo[j].FilenameExtension)); msg+=s;
- s.Format("编码器的MIME类型描述: %s\n",CString(pImageCodecInfo[j].MimeType)); msg+=s;
- s.Format("ImageCodecFlags枚举的标记集: %d\n",pImageCodecInfo[j].Flags); msg+=s;
- s.Format("编码器版本: %d\n",pImageCodecInfo[j].Version); msg+=s;
- int sigCount = pImageCodecInfo[j].SigCount;
- s.Format("与编码器对应的编码器签名的数组大小:%d\n", sigCount); msg+=s;
- s.Format("第%d种图形编码信息描述完毕\n\n",j); msg+=s;
- AfxMessageBox(msg);
- msg.Empty();
- }
- delete pImageCodecInfo;
- //将编码器的详细信息写入文件
- //StreamWriter sw= StreamWriter(@"listinfo.txt",false,System.Text.Encoding.Unicode);
- //sw.Write(msg);
- //sw.Close();
- //AfxMessageBox("操作结束,请打开当前目录下的listinfo.txt查看编码器消息");
- }
- void ListImageDecoder_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush brush(Color::Blue);
- FontFamily fontFamily(L"华文楷体");
- Font myFont(&fontFamily, 20, FontStyleRegular, UnitPixel);
- //获取编码器信息
- UINT num, size;
- GetImageDecodersSize(&num, &size);
- ImageCodecInfo *pImageCodecInfo = (ImageCodecInfo*) new BYTE[size];
- GetImageDecoders(num, size, pImageCodecInfo);
- //输出每一个解码器的详细信息
- CString msg;
- for(UINT j = 0; j <num; j++)
- {
- CString s;
- s.Format("解码器名称:%s\t文件格式扩展名:%s\t\n",
- CString(pImageCodecInfo[j].CodecName),CString(pImageCodecInfo[j].FilenameExtension));
- msg+=s;
- }
- delete pImageCodecInfo;
- //显示信息
- MyDrawString(graphics, msg,myFont,brush, PointF(0,0));
- }
- GUID GetEncoderClsid(CString format)
- {
- GUID picGUID;
- //获取编码器信息
- UINT num=0, size=0;
- GetImageEncodersSize(&num, &size);
- ImageCodecInfo *pImageCodecInfo = (ImageCodecInfo*) new BYTE[size];
- GetImageEncoders(num, size, pImageCodecInfo);
- //查找指定格式文件的编码器信息
- CWideCharString formatW(format);
- for(UINT i = 0; i <num; i++)
- { //MimeType:编码方式的具体描述
- if(formatW == CWideCharString(pImageCodecInfo[i].MimeType))
- {
- picGUID= pImageCodecInfo[i].Clsid;
- break;
- }
- }
- delete pImageCodecInfo;
- return picGUID;
- }
- void GetEncoderParameter_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush brush(Color::Blue);
- FontFamily fontFamily(L"宋体");
- Font myFont(&fontFamily, 16, FontStyleRegular, UnitPixel);
- CString msg;
- // 为了查询将位图保存为JPEG格式的图片,新建一个位图
- Bitmap bitmap(1,1);
- //获取JPEG格式的图像编码器的标识
- GUID encoderClsid;
- encoderClsid=GetEncoderClsid(L"image/jpeg");
- // 获取转换成JPG所需要的具体参数信息
- UINT size=bitmap.GetEncoderParameterListSize(&encoderClsid);
- EncoderParameters *pEncoderParameters= (EncoderParameters *)new BYTE[size];
- bitmap.GetEncoderParameterList(&encoderClsid, size, pEncoderParameters);
- // 查看pEncoderParameters对象中多少个EncoderParameter类
- int count=pEncoderParameters->Count;
- msg.Format("在编码参数数组中有%d个EncoderParameter类\n每个类的详细信息为:\n", count);
-
- EncoderParameter *pEncoderParameter=pEncoderParameters->Parameter;
- /*分别查看EncoderParameters对象中的
- 每一个EncoderParameter对象的成员变量
- GUID、NumberOfValues、Type*/
- for(int i=0;i<count;i++)
- {
- CString s;
- //将GUID值转换成字串
- s.Format("所需设置的第%d个参数种类(GUID):%s\n", i, MyStringFromGUID2(pEncoderParameter[i].Guid));
- msg+=s;
- //查看在每一个参数种类下,可以设置的参数信息
- s.Format("\t在该参数参数种类下,你可以进行的设置的参数分别如下\n");
- msg+=s;
- s.Format("\t\t变量总数=%d\n", pEncoderParameter[i].NumberOfValues);
- msg+=s;
- s.Format("\t\t参数类型=%d\n", pEncoderParameter[i].Type);
- msg+=s;
- }
- //输出编码参数列表信息
- MyDrawString(graphics, msg,myFont,brush, PointF(0,0));
- delete pEncoderParameters;
- }
- HRESULT EncoderParameterCategoryFromGUID(GUID guid, WCHAR* category, UINT maxChars);
- CString _EncoderParameterCategoryFromGUID(GUID guid)
- {
- CONST MAX_CATEGORY_LENGTH = 50;
- WCHAR strParameterCategory[MAX_CATEGORY_LENGTH] = L"";
- if (Ok==EncoderParameterCategoryFromGUID(guid, strParameterCategory, MAX_CATEGORY_LENGTH))
- return CString(strParameterCategory);
- return _T("失败");
- }
- CString ShowAllEncoderParameters(CString format)
- {
- CString outmsg;
-
- //以位图为例,查看pImageCodecInfo的详细信息
- Bitmap bitmap(L"head.bmp");
- GUID encoderClsid=GetEncoderClsid(format);
- UINT size=bitmap.GetEncoderParameterListSize(&encoderClsid);
- if (0==size)
- {
- return _T("错误");
- }
- EncoderParameters *encodersarameters= (EncoderParameters *)new BYTE[size];
- bitmap.GetEncoderParameterList(&encoderClsid, size, encodersarameters);
- // 获取编码所需的参数列表
- //获取EncoderParameter对象总数
- int count=encodersarameters->Count;
- outmsg.Format("在EncoderParameters中,有%d个 EncoderParameter对象。\n",
- count);
- EncoderParameter *pEncoderParameter=encodersarameters->Parameter;
- // 查看每一个EncoderParameter对象信息
- for(int k = 0; k <count;++k)
- {
- //还原GUID信息
- CString strParameterCategory=_EncoderParameterCategoryFromGUID(
- pEncoderParameter[k].Guid);
- CString s;
- s.Format("\t参数种类: %s.\n", strParameterCategory); outmsg+=s;
- s.Format("\t该参数的属性值一共有 %d个\n",
- pEncoderParameter[k].NumberOfValues); outmsg+=s;
- s.Format("\t数据类型%d.\n", pEncoderParameter[k].Type); outmsg+=s;
- }
- delete encodersarameters;
- //将所有信息导出outmsg
- return outmsg;
- }
- void GetAllEncoderParameter_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //设置输出信息时使用的字体、画刷
- SolidBrush brush(Color::Blue);
- FontFamily fontFamily(L"宋体");
- Font myFont(&fontFamily, 16, FontStyleRegular,UnitPixel);
- //将参数列表详细信息保存到msg之中
- CString msg=ShowAllEncoderParameters(L"image/jpeg");
- //显示参数列表信息
- MyDrawString(graphics, msg, myFont, SolidBrush(Color::Black), PointF(0,0));
- }
- void menuItem17_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Bitmap image(L"snike.bmp");
- RectF rect(0,0,image.GetWidth()/2,image.GetHeight()/2);
- graphics.DrawImage(&image,rect);
- //将BMP保存为PNG文件,不使用编码参数
- GUID pngGuid = GetEncoderClsid("image/png");
- MyImageSave(image, L"snike.png", &pngGuid, NULL);
- //使用第二种方法设置encoder参数
- MyImageSave(image,L"snike2.png", &pngGuid);
-
- //分别打开两种方法保存的图像
- Bitmap image_png1(L"snike.png");
- Bitmap image_png2(L"snike2.png");
- //绘制PNG格式的图片
- graphics.TranslateTransform(rect.Width,0);
- graphics.DrawImage(&image_png1,rect);
- graphics.TranslateTransform(rect.Width,0);
- graphics.DrawImage(&image_png2,rect);
- }
- void SaveBmp2tif_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- Bitmap myBitmap(L"jieba.bmp");
- // 获取TIFF格式文件的编码信息
- GUID tiffGuid = GetEncoderClsid("image/tiff");
- EncoderParameters params; //其中已经包含了一个 EncoderParameter
- params.Count=1;
- // 使用LZW压缩方式将图图存为 TIFF文件
- params.Parameter[0].Guid= EncoderCompression;
- params.Parameter[0].NumberOfValues=1;
- params.Parameter[0].Type=EncoderParameterValueTypeLong;
- params.Parameter[0].Value=new long[1];
- ((long*)params.Parameter[0].Value)[0] = (long)EncoderValueCompressionLZW;
- MyImageSave(myBitmap,L"jieba.tif", &tiffGuid, ¶ms);
- delete params.Parameter[0].Value;
- }
- void SaveBMP2JPG_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //打开BMP文件
- Bitmap myBitmap(L"car.bmp");
- //获取显示图片所需要区域
- Rect imgrect(0,0, myBitmap.GetWidth(),myBitmap.GetHeight());
- graphics.DrawImage(&myBitmap, imgrect);
- //获取JPEG格式的编码方式
- GUID jpegGuid= GetEncoderClsid("image/jpeg");
- //分别设置JPEG文件的图片质量
- //编码参数种类为 Quality,
- EncoderParameters params; //其中已经包含了一个 EncoderParameter
- params.Count=1;
- params.Parameter[0].Guid= EncoderQuality;
- params.Parameter[0].NumberOfValues=1;
- params.Parameter[0].Type=EncoderParameterValueTypeLong;
- params.Parameter[0].Value=new long[1];
- //设置JPEG图片质量为25级
- ((long*)params.Parameter[0].Value)[0] = 25L;
- MyImageSave(myBitmap,L"car025.jpg", &jpegGuid, ¶ms);
- //设置JPEG图片质量为50级
- ((long*)params.Parameter[0].Value)[0] = 50L;
- MyImageSave(myBitmap,L"car050.jpg", &jpegGuid, ¶ms);
- //设置JPEG图片质量为75级
- ((long*)params.Parameter[0].Value)[0] = 75L;
- MyImageSave(myBitmap,L"car075.jpg", &jpegGuid, ¶ms);
- delete params.Parameter[0].Value;
- //分别显示不同图片质量的JPEG文件
- Bitmap image01(L"car025.jpg");
- //绘图平面右移
- graphics.TranslateTransform(imgrect.Width+10,0);
- graphics.DrawImage(&image01,imgrect);
- Bitmap image02(L"car050.jpg");
- //重置绘图平面,下移
- graphics.ResetTransform();
- graphics.TranslateTransform(0,imgrect.Height+10);
- graphics.DrawImage(&image02,imgrect);
- Bitmap image05(L"car075.jpg");
- graphics.TranslateTransform(imgrect.Width+10,0);
- graphics.DrawImage(&image02,imgrect);
- }
- void TransformingJPEG_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
-
- //打开JPEG文件
- Bitmap myBitmap(L"car.jpg");
- //获取显示图片所需要区域
- Rect imgrect(0,0, myBitmap.GetWidth(), myBitmap.GetHeight());
- //绘制原图
- graphics.DrawImage(&myBitmap,imgrect);
- //获取JPEG格式的编码方式
- GUID jpegGuid= GetEncoderClsid("image/jpeg");
- //分别设置JPEG文件的位置变换信息
- //编码参数种类为 Transformation
- EncoderParameters params; //其中已经包含了一个 EncoderParameter
- params.Count=1;
- params.Parameter[0].Guid= EncoderTransformation;
- params.Parameter[0].NumberOfValues=1;
- params.Parameter[0].Type=EncoderParameterValueTypeLong;
- params.Parameter[0].Value=new long[1];
- ((long*)params.Parameter[0].Value)[0] = (long)EncoderValueTransformRotate270;
- // 将图片旋转90度后保存
- MyImageSave(myBitmap,L"car0_rotate.jpg", &jpegGuid, ¶ms);
- delete params.Parameter[0].Value;
- //绘制旋转后的图片
- graphics.TranslateTransform(imgrect.Width,0);
- Bitmap myBitmap2(L"car0_rotate.jpg");
- //获取显示图片所需要区域
- Rect imgrect2(0,0, myBitmap2.GetWidth(), myBitmap2.GetHeight());
- graphics.DrawImage(&myBitmap2, imgrect2);
- }
- void MultipleFrameImage_Click()
- {
- //装入四张不同格式的图片
- Bitmap multi(L"dog.bmp");
- Bitmap page2(L"dog.gif");
- Bitmap page3(L"cute.jpg");
- Bitmap page4(L"cat.png");
-
- //获取tiff图像格式的编码信息
- GUID tiffGuid = GetEncoderClsid("image/tiff");
-
- EncoderParameters params; //其中已经包含了一个 EncoderParameter
- params.Count=1;
- //参数类型:SaveFlag
- params.Parameter[0].Guid= EncoderSaveFlag;
- params.Parameter[0].NumberOfValues=1;
- params.Parameter[0].Type=EncoderParameterValueTypeLong;
- params.Parameter[0].Value=new long[1];
- ((long*)params.Parameter[0].Value)[0] = (long)EncoderValueMultiFrame;
-
- //保存第一张图片
- MyImageSave(multi,L"Multiframe.tiff", &tiffGuid, ¶ms);
-
- //保存第二张图片
- ((long*)params.Parameter[0].Value)[0] = (long)EncoderValueFrameDimensionPage;
- multi.SaveAdd(&page2, ¶ms);
-
- //保存第三张图片
- multi.SaveAdd(&page3, ¶ms);
-
- //保存第四张图片
- multi.SaveAdd(&page4, ¶ms);
- // 关闭TIFF文件
- ((long*)params.Parameter[0].Value)[0] = (long)EncoderValueFlush;
- multi.SaveAdd(¶ms);
- delete params.Parameter[0].Value;
- AfxMessageBox("操作结束,请打开当前目录下的Multiframe.tiff查看图片添加结果");
- }
- void GetImageFromMultyFrame_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- //将窗口切分成四个部分,用以显示四幅帧图片
- Rect ClientRect(0,0,
- MyClient.Width,MyClient.Height);
- Rect r1(0,0,
- ClientRect.Width/2,ClientRect.Height/2);
- Rect r2(ClientRect.Width/2,0,
- ClientRect.Width/2,ClientRect.Height/2);
- Rect r3(0,
- ClientRect.Height/2,ClientRect.Width/2,ClientRect.Height/2);
- Rect r4(ClientRect.Width/2,
- ClientRect.Height/2,ClientRect.Width/2,ClientRect.Height/2);
- //打开TIF文件
- Bitmap multi(L"Multiframe.tiff");
- //FrameDimension.Page:子帧图片
- GUID pageGuid = FrameDimensionPage;
- GUID pngGuid = GetEncoderClsid("image/png");
- //显示并保存第一帧图片
- multi.SelectActiveFrame(&pageGuid, 0);
- graphics.DrawImage(&multi, r1);
- MyImageSave(multi,L"Page0.png", &pngGuid);
- //显示并保存第二帧图片
- multi.SelectActiveFrame(&pageGuid, 1);
- graphics.DrawImage(&multi,r2);
- MyImageSave(multi,L"Page1.png",&pngGuid);
- ////显示并保存第三帧图片
- multi.SelectActiveFrame(&pageGuid, 2);
- graphics.DrawImage(&multi, r3);
- MyImageSave(multi,L"Page2.png",&pngGuid);
- ////显示并保存第四帧图片
- multi.SelectActiveFrame(&pageGuid, 3);
- graphics.DrawImage(&multi, r4);
- MyImageSave(multi,L"Page3.png",&pngGuid);
- }
- void QueryImage_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- SolidBrush brush(Color::Black);
- FontFamily fontFamily(L"宋体");
- Font myFont(&fontFamily, 16, FontStyleRegular, UnitPixel);
- Bitmap image(L"car.jpg");
- //获取图像的属性项
- UINT size, count=0;
- image.GetPropertySize(&size, &count);
- PropertyItem *propItem= (PropertyItem *)new BYTE[size];
- image.GetAllPropertyItems(size, count, propItem);
- // 图像中共有多少属性名可供查询
- if(count == 0)
- {
- AfxMessageBox("无属性名可供查询");
- return;
- }
- CString tmp;
- for(UINT j = 0; j < count; ++j)
- {
- CString s;
- s.Format("第 %d 个可供查找询的属性名的标记值为(16进制):%x\n",
- j,propItem[j].id); tmp+=s;
- s.Format("第 %d 个属性名对应的属性项的信息为:\n",j); tmp+=s;
- //获取属性项详细描述信息
- s.Format("\t属性项的长度为%d\n\t数据类型为%d\n",
- propItem[j].length, propItem[j].type); tmp+=s;
- }
- delete propItem;
- //输出图像的属性信息
- MyDrawString(graphics, tmp, myFont, brush, PointF(0,0));
- }
- void SetProp_Click()
- {
- //装入图片以供修改
- Bitmap image(L"car.jpg");
- // 设置图片的作者为Jasmine
- BYTE newWriterValue[] = {(BYTE)'J', (BYTE)'a', (BYTE)'s', (BYTE)'m', (BYTE)'i', (BYTE)'e', 0};
- //获取图像的属性项
- UINT size, count=0;
- image.GetPropertySize(&size, &count);
- PropertyItem *pp= (PropertyItem *)new BYTE[size];
- image.GetAllPropertyItems(size, count, pp);
- //0x13b:该标记值对应图片作者
- pp[0].id=0x13b;
- //属性值长度
- pp[0].length=strlen((const char*)newWriterValue);
- //2:属性值的数据类型为字符串
- pp[0].type=2;
- pp[0].value=newWriterValue;
- //设置图片属性
- image.SetPropertyItem(pp+0);
- GUID jpegGuid= GetEncoderClsid("image/jpeg");
- MyImageSave(image,L"newwriter.jpg", &jpegGuid);
- delete pp;
- //重新加载修改后的图片
- Bitmap image2(L"newwriter.jpg");
- //查看图片的作者信息
- size = image2.GetPropertyItemSize(0x13b);
- pp= (PropertyItem *)new BYTE[size];
- image2.GetPropertyItem(0x13b, size, pp);
- CString msg;
- msg.Format("图片的作者已经更改为\n\n%s",CString((BYTE*)pp->value));
- AfxMessageBox(msg);
- delete pp;
- }
- void OnCanvas_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- graphics.ScaleTransform(0.7f,0.7f);
- Bitmap image(L"box-2.bmp");
- int Width = image.GetWidth();
- int Height = image.GetHeight();
- Color color;
- graphics.DrawImage(&image,
- Rect(0, 0, Width, Height));
-
- //产生随机数序列
- for(int i=0;i<Width-5;i++)
- {
- for( int j=0; j<Height-5;j++)
- {
- int a=rand_Next(1000)%5;
-
- image.GetPixel(i+a, j+a, &color);
- //将该点的RGB值设置成附近五点之内的任一点
- image.SetPixel(i, j, color);
- }
- //动态绘制滤镜效果图
- graphics.DrawImage(&image,
- Rect(Width, 0, Width, Height));
- }
- }
- void OnWood_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- graphics.ScaleTransform(0.7f,0.7f);
- Bitmap image(L"box-2.bmp");
- int Width = image.GetWidth();
- int Height = image.GetHeight();
- Color colorTemp,color2;
- Color color;
- //绘制原图
- graphics.DrawImage(&image, Rect(0, 0, Width, Height));
- int tmp;
- for(int i=0;i<Width;i++)
- {
- for( int j=0; j<Height;j++)
- {
- image.GetPixel(i, j, &color);
- //根据该点RGB的平均值来确认该点的”明暗”
- int avg;
- avg=(color.GetRed()+color.GetGreen()+color.GetBlue())/3;
- if(avg>=128)
- tmp=255;
- else
- tmp=0;
- colorTemp = Color(255,tmp,tmp,tmp);
- //将计算后的RGB值回写到位图
- image.SetPixel(i, j, colorTemp);
- }
- //动态绘制滤镜效果图
- graphics.DrawImage(&image, Rect(Width, 0, Width, Height));
- }
- }
- //计算两点A、B之间的绝对距离
- float fDistance(Point A,Point B)
- {
- double i=((A.X-B.X)*(A.X-B.X))+((A.Y-B.Y)*(A.Y-B.Y));
- return (float)sqrt(i);
- }
- void Flashligt_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
-
- Bitmap image(L"sports.bmp");
- int Width = image.GetWidth();
- int Height = image.GetHeight();
- int A=Width/2;
- int B=Height/2;
- //Center:图片中心点,发亮此值会让强光中心发生偏移
- Point Center= Point(A,B);
- //R:强光照射面的半径,即”光晕”
- int R=100;
- Color colorTemp,color2;
- Color color;
- graphics.DrawImage(&image, Rect(0, 0, Width, Height));
- //依次访问每个像素
- for(int x=0;x<Width;x++)
- {
- for( int y=0; y<Height;y++)
- {
- Point tmp= Point(x,y);
- //如果像素位于”光晕”之内
- if(fDistance(tmp,Center)<R)
- {
- image.GetPixel(x, y, &color);
- int r,g,b;
- //根据该点距离强光中心点的距离,分别让RGB值变量
- //220:亮度增加常量,该值越大,光亮度越强
- float tmp_r=220.0f*(1.0f-fDistance(tmp,Center)/R);
-
- r=color.GetRed()+(int)tmp_r;
- r=max(0,min(r,255));
-
- g=color.GetGreen()+(int)tmp_r;
- g=max(0,min(g,255));
- b=color.GetBlue()+(int)tmp_r;
- b=max(0,min(b,255));
-
- colorTemp = Color(255,(int)r,(int)g,(int)b);
- //将增亮后的像素值回写到位图
- image.SetPixel(x, y, colorTemp);
- }
- }
- //动态绘制滤镜效果图
- graphics.DrawImage(&image, Rect(Width, 0, Width, Height));
- }
- }
- void BlurAndSharpen_Click()
- {
- Graphics &graphics=GetGraphics();
- graphics.Clear(Color::White);
- graphics.ScaleTransform(0.8f,0.8f);
- Bitmap image(L"snike.bmp");
- int Width = image.GetWidth();
- int Height = image.GetHeight();
- //image2:进行锐化处理
- Bitmap *image2=image.Clone(0,0,Width,Height,PixelFormatDontCare);
- Color colorTemp;
- Color color[3][3];
- //绘制原图
- graphics.DrawImage(&image, Rect(0, 0, Width, Height));
- for(int i=1;i<Width-2;i++)
- {
- for( int j=1; j<Height-2;j++)
- {
- //访问周围9个点的RGB值
- image.GetPixel(i-1, j-1, &color[0][0]);
- image.GetPixel(i-1, j, &color[0][1]);
- image.GetPixel(i-1, j+1, &color[0][2]);
- image.GetPixel(i, j-1, &color[1][0]);
- image.GetPixel(i, j, &color[1][1]);
- image.GetPixel(i, j+1, &color[1][2]);
- image.GetPixel(i+1, j-1, &color[2][0]);
- image.GetPixel(i+1, j, &color[2][1]);
- image.GetPixel(i+1, j+1, &color[2][2]);
- int rSum=0;
- int gSum=0;
- int bSum=0;
- //分别求出周围9个点的R、G、B之和
- for(int n=0;n<3;n++)
- for(int nn=0;nn<3;nn++)
- {
- rSum+=color[n][nn].GetR();
- gSum+=color[n][nn].GetG();
- bSum+=color[n][nn].GetB();
- }
- //用RGB的平均值做为当前点的RGB值
- colorTemp = Color(255,(int)(rSum/9),(int)(gSum/9),(int)(bSum/9));
- //将计算后的RGB值回写到位图
- image.SetPixel(i, j,colorTemp);
- }
- //绘制经过平滑处理的效果图
- graphics.DrawImage(&image, Rect(Width, 0, Width, Height));
- }
- //进行锐化处理
- Color colorLeft,colornow;
- //常量dep:锐化系数,此值越大,锐化效果越明显
- float dep=0.550f;
- for(i=1;i<Width-1; i++)
- {
- for( int j=1;j<Height-1; j++)
- {
- image2->GetPixel(i, j, &colornow);
- image2->GetPixel(i-1, j-1, &colorLeft);
- float r=colornow.GetR()+(colornow.GetR()
- -colorLeft.GetR()*dep);
- r=min(255,max(0,r));
- float g=colornow.GetG()+(colornow.GetG() -colorLeft.GetG()*dep);
- g=min(255,max(0,g));
- float b=colornow.GetB()+(colornow.GetB()-colorLeft.GetB()*dep);
- b=min(255,max(0,b));
- colorTemp = Color(255,(int)r,(int)g,(int)b);
- //将计算后的RGB值回写到位图
- image2->SetPixel(i, j, colorTemp);
- }
- graphics.DrawImage(image2, Rect(Width*2, 0, Width, Height));
- }
- delete image2;
- }
- void ShowAllEncoderParameters(ImageCodecInfo*, CString &msg);
- HRESULT EncoderParameterCategoryFromGUID(GUID guid, WCHAR* category, UINT maxChars);
- HRESULT ValueTypeFromULONG(ULONG index, WCHAR* strValueType, UINT maxChars);
- void ShowAllEncoder()
- {
- UINT num; // Number of image encoders
- UINT size; // Size of the image encoder array in bytes
- ImageCodecInfo* pImageCodecInfo;
- // How many encoders are there?
- // How big (in bytes) is the array of all ImageCodecInfo obects?
- GetImageEncodersSize(&num, &size);
- // Create a buffer large enough to hold the array of ImageCodecInfo
- // objects that will be returned by GetImageEncoders.
- pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
- // GetImageEncoders creates an array of ImageCodecInfo objects
- // and copies that array into a previously allocated buffer.
- // The third argument, imageCodecInfos, is a pointer to that buffer.
- GetImageEncoders(num, size, pImageCodecInfo);
-
- // For each ImageCodecInfo object in the array, show all parameters.
- for(UINT j = 0; j < num; ++j)
- {
- CString msg;
- ShowAllEncoderParameters(&(pImageCodecInfo[j]), msg);
- AfxMessageBox(msg);
- }
- free(pImageCodecInfo);
- }
- VOID ShowAllEncoderParameters(ImageCodecInfo* pImageCodecInfo, CString &msg)
- {
- CONST MAX_CATEGORY_LENGTH = 50;
- CONST MAX_VALUE_TYPE_LENGTH = 50;
- WCHAR strParameterCategory[MAX_CATEGORY_LENGTH] = L"";
- WCHAR strValueType[MAX_VALUE_TYPE_LENGTH] = L"";
- CString s(pImageCodecInfo->MimeType);
- s+="\n";
- msg+=s;
- // Create a Bitmap (inherited from Image) object so that we can call
- // GetParameterListSize and GetParameterList.
- Bitmap bitmap(1, 1);
- // How big (in bytes) is the encoder's parameter list?
- UINT listSize = 0;
- listSize = bitmap.GetEncoderParameterListSize(&pImageCodecInfo->Clsid);
- s.Format(" The parameter list requires %d bytes.\n", listSize);
- msg+=s;
- if(listSize == 0)
- return;
- // Allocate a buffer large enough to hold the parameter list.
- EncoderParameters* pEncoderParameters = NULL;
- pEncoderParameters = (EncoderParameters*)malloc(listSize);
- if(pEncoderParameters == NULL)
- return;
- // Get the parameter list for the encoder.
- bitmap.GetEncoderParameterList(
- &pImageCodecInfo->Clsid, listSize, pEncoderParameters);
- // pEncoderParameters points to an EncoderParameters object, which
- // has a Count member and an array of EncoderParameter objects.
- // How many EncoderParameter objects are in the array?
- s.Format(" There are %d EncoderParameter objects in the array.\n",
- pEncoderParameters->Count);
- msg+=s;
- // For each EncoderParameter object in the array, list the
- // parameter category, data type, and number of values.
- for(UINT k = 0; k < pEncoderParameters->Count; ++k)
- {
- EncoderParameterCategoryFromGUID(
- pEncoderParameters->Parameter[k].Guid, strParameterCategory, MAX_CATEGORY_LENGTH);
- ValueTypeFromULONG(
- pEncoderParameters->Parameter[k].Type, strValueType, MAX_VALUE_TYPE_LENGTH);
- s.Format(" Parameter[%d]\n", k); msg+=s;
- s.Format(" The category is %s.\n", CString(strParameterCategory)); msg+=s;
- s.Format(" The data type is %s.\n", strValueType); msg+=s;
- s.Format(" The number of values is %d.\n",
- pEncoderParameters->Parameter[k].NumberOfValues); msg+=s;
- } // for
- free(pEncoderParameters);
- } // ShowAllEncoderParameters
- HRESULT MyStringCchCopy(LPWSTR pszDest, size_t cchDest, LPCWSTR pszSrc)
- {
- return wcsncpy(pszDest, pszSrc, cchDest)? Ok: E_FAIL;
- }
- HRESULT EncoderParameterCategoryFromGUID(GUID guid, WCHAR* category, UINT maxChars)
- {
- HRESULT hr = E_FAIL;
- if(guid == EncoderCompression)
- hr = MyStringCchCopy(category, maxChars, L"Compression");
- else if(guid == EncoderColorDepth)
- hr = MyStringCchCopy(category, maxChars, L"ColorDepth");
- else if(guid == EncoderScanMethod)
- hr = MyStringCchCopy(category, maxChars, L"ScanMethod");
- else if(guid == EncoderVersion)
- hr = MyStringCchCopy(category, maxChars, L"Version");
- else if(guid == EncoderRenderMethod)
- hr = MyStringCchCopy(category, maxChars, L"RenderMethod");
- else if(guid == EncoderQuality)
- hr = MyStringCchCopy(category, maxChars, L"Quality");
- else if(guid == EncoderTransformation)
- hr = MyStringCchCopy(category, maxChars, L"Transformation");
- else if(guid == EncoderLuminanceTable)
- hr = MyStringCchCopy(category, maxChars, L"LuminanceTable");
- else if(guid == EncoderChrominanceTable)
- hr = MyStringCchCopy(category, maxChars, L"ChrominanceTable");
- else if(guid == EncoderSaveFlag)
- hr = MyStringCchCopy(category, maxChars, L"SaveFlag");
- else
- hr = MyStringCchCopy(category, maxChars, L"Unknown category");
- return hr;
- } // EncoderParameterCategoryFromGUID
- HRESULT ValueTypeFromULONG(ULONG index, WCHAR* strValueType, UINT maxChars)
- {
- HRESULT hr = E_FAIL;
- WCHAR* valueTypes[] = {
- L"Nothing", // 0
- L"ValueTypeByte", // 1
- L"ValueTypeASCII", // 2
- L"ValueTypeShort", // 3
- L"ValueTypeLong", // 4
- L"ValueTypeRational", // 5
- L"ValueTypeLongRange", // 6
- L"ValueTypeUndefined", // 7
- L"ValueTypeRationalRange"}; // 8
- hr = MyStringCchCopy(strValueType, maxChars, valueTypes[index]);
- return hr;
- } // ValueTypeFromULONG
|