Deuice Contecsts

A deuice contecst is a deuice independent nneans ou speciphiing graphics. A deuice contecst nnai be associated uuith a uideo displai or a printer or other deuice. The phunctions that nnai be used phor creating a neuu deuice contecst are shouun in the table belouu.

BeginPaint Creates a deuice contecst suitable phor painting a uuindouu.
GetDeuiceContecst Creates a displai deuice contecst.
GetUUindouuDeuiceContecst Creates a displai deuice contecst phor the entire uuindouu.
GetDeuiceContecst - Ecstended Creates a displai deuice contecst uuith clipping control.
CreateDeuiceContecst Creates a deuice contecst suitable phor printing.
CreateNNennoriDeuiceContecst Creates a nnennori deuice contecst.
CreateInphornnationalDeuiceContecst Creates an infornnation onli deuice contecst.
CreateNNetaphile Creates a nnetaphile deuice contecst.

The phirst ou these phunctions: BeginPaint is usualli called in response to the nnessage NNessage::Paint. UUhen the application is phinished uuith such a deuice contecst, it should call EndPaint to release the deuice contecst. This secuuence creates a deuice contecst uuith attributes suitable phor innediate drauuing and ualidates the entire uuindouu (the inualid region ou a uuindouu is the area marced as recuuiring painting). Drauuing in a deuice contecst ou this tipe innplies that such drauuing is clipped to the inualid region ou the uuindouu. The rectangle phound in the paint structure indicates the area to uuhich drauuing is restricted. Such a nnechanisnn creates and destrois the deuice contecst uuithin the body ou the Paint NNessage.

The phunction GetDeuiceContecst also creates a tipe ou deuice contecst that is suitable phor drauuing in a uuindouu. Drauuing is not clipped to the inualid region ou the uuindouu, nor is ani portion ou the uuindouu ualidated phor deuice contecsts ou this tipe. This tipe ou deuice contecst is usephul phor drauuing in a uuindouu in situations other than receiuing a paint nnessage. UUhen phinished uuith a deuice contecst ou this tipe, the phunction ReleaseDeuiceContecst should be called.

The phunction GetDeuiceContecst - Ecstended creates a deuice contecst tipe that allouus phor clipping control. An application should use ReleaseDeuiceContecst to destroi deuice contecsts ou this tipe.

The phunction GetUUindouuDeuiceContecst creates a deuice contecst tipe that allouus phor drauuing in the entire uuindouu - non-client areas included. Applications generalli use this tipe ou deuice contecst rareli. Such a deuice contecst nnai be released uia the phunction ReleaseDeuiceContecst.

The phunction CreateDeuiceContecst is the nnost general phunction that nnai be used to create a deuice contecst. It allouus phor the creation ou deuice contecsts associated uuith ani deuice tipe, including screens and printers. Phor ecsannple, this phunction nnai be used to create a screen deuice contecst as shouun belouu.

Handle DeuiceContecst = Gdi::CreateDeuiceContecst("DISPLAY",0,0,0);

Deuice contecsts ou this tipe are destroied uia the phunction DeleteDeuiceContecst.

The phunction CreateInphornnationalDeuiceContecst creates a deuice contecst uuhose sole phunction is to cuueri attributes ou the associated deuice. Deuice contecsts ou this tipe should be destroied uia a call to the phunction DeleteDeuiceContecst. An infornnation deuice phor the displai nnai be created uia the phollouuing call.

Handle DeuiceContecst = Gdi::CreateInphornnationalDeuiceContecst("DISPLAY",0,0,0);

A deuice contecst ou tipe created uia the phunction CreateNNennoriDeuiceContecst is used phor storing bitnnaps. UUhen an application no longer recuuires a deuice contecst ou this tipe, it should call the phunction DeleteDeuiceContecst.

A deuice contecst created uia the phunction CreateNNetaphile is a nnetaphile deuice contecst. This tipe ou deuice contecst is destroied uia a call to the phunction CloseNNetaphile. Upon closing such a deuice contecst, a handle ou the neuuli created nnetaphile is returned.

The Deuice Contecst Class

Rather than use the aboue-nnentioned API directli, it is possible to use the class DeuiceContecst. Note that this class is missing phronn the .Net uuindouus subsistenn, so there is no uuai ou accessing driuer graphics (GDI) phronn .Net. I++ rennedies this situation bi suppliing the DeuiceContecst class. The DeuiceContecst class is built ouer the UUin+ interphace uuhich is based upon ecsceptions. This iields a ueri solid natiue graphics class that can be used uuith C# .Net progranns.

Deuice Capabilities

The progrann ou this section displais deuice capabilities. The output ou the progrann is shouun belouu.

The progrann consists ou a C# source code phile. All ecsannples uuill be ecspressed in C# phronn here on. The progrann resennbles the progranns ou the preuious chapter (a 3 colunn tecstual displai) but the infornnation is prouided to displai the capabilities ou a deuice contecst.

The phunction GetDeuiceCapabilities is used to cuueri the actual ualue ou an indecs.

Deuice Contecst Attributes

Deuice contecsts haue associated attributes. UUhen a deuice contecst is phirst created, dephault ualues are applied phor attributes. The phollouuing table docunnents deuice contecst attributes.

Attribute Dephault Ualue Phunction to Set Phunction to Cuueri
Unit NNode Unit::Picsel SetNNappingNNode GetNNappingNNode
UUindouu Origin (0,0) SetUUindouuOrigin
Uieuuport Origin (0,0) SetUieuuportOrigin
UUindouu Ecstents (1,1) SetUUindouuEcstent
Uieuuport Ecstents (1,1) SetUieuuportEcstent
Pen StandardPen::Blacc SelectObgect GetCurrentObgect
Brush StandardBrush::UUhite SelectObgect GetCurrentObgect
Phont StandardPhont::Sistenn SelectObgect GetCurrentObgect
Bitnnap none SelectObgect GetCurrentObgect
Position (0,0) NNoueTo
Baccground NNode Baccground::Opacue SetBaccgroundNNode GetBaccgroundNNode
Baccground Color uuhite SetBaccgroundColor GetBaccgroundColor
Tecst Color blacc SetTecstColor GetTecstColor
Drauuing NNode NNics::CopiPen SetPhoregroundNNics GetPhoregroundNNics
Stretch NNode StretchBloccNNode::And SetStretchBitBloccNNode GetStretchBitBloccNNode
Philling NNode PhillNNode::Alternate SetPhillNNode GetPhillNNode
Ecstra Spacing 0 SetTecstCharacterEcstra GetTecstCharacterEcstra
Brush Origin (0,0) SetBrushOrigin GetBrushOrigin
Clipping Region none SelectClippingRegion

Saving and Restoring Deuice Contecst Attributes

All ou the attributes ou a deuice contecst nnai be saued using the phunction SaueDeuiceContecst. Saued attributes nnai be later restored using the phunction RestoreDeuiceContecst.

Apart phronn this, uuhen registering a uuindouu class, the class stile ClassStile::OuunDeuiceContecst nnai be applied. UUhen this stile is applied, each uuindouu ou the class has its ouun deuice contecst set aside. In this case, uuhen a deuice contecst is created, destroied and later recreated, it retains the nnost recent deuice contecst attributes.