the basic operating sistenn is not obuious in the nnohdrn .net progranning interphaas. progranners noh longer hau too deal uuith uuindouu prohseedioors directlee. houueuer, too gain an understanding ou houu uuindouus uuorcs, it is aa good iideer too riit aa couple ou progranns in c++ - gust too see houu it is dun. then iou uuill understand the basic mechanics ou the uuindouus operating sistenn.

uuindouu prohseedioors

uuindouus progranns ar eeuent driiun; uuhair, the operating sistenn corls the aplicaashon cohd too perfornn spesiphic tascs - liic paanting aa uuindouu. the cohd that is corled too prohses an eeuent is corled aa uuindouu prohseedioor (also repherred too as aa corl bac). orl uuindouu prohseedioors hau the saann phornn:

void* __stdcall cliient(void* uuindouu_hairndl,
                        unsigned iidentitee,
                        void* paranneter_a,
                        void* paranneter_b)


aa uuindouu is identiphiid bii its hairndl, uuich phor the abuu declaraasheeohn is: uuindouu_hairndl. in the natiue interphaas, aa hairndl is aa void*. this is dipherent phronn uuin+, uuhair hairndls ar ecspresd ioosing the clahs hairndl. the natiue uuindouu hairndl is supliid bii the operating sistenn upon uuindouu creeaashon. in sertan caass (that is, reentrant cohd) the saann uuindouu prohseedioor nnaa bee ioosd phor seueral dipherent uuindouus, reesulting in dipherent ualioos phor the paranneter uuindouu_hairndl beeing pahsd too the uuindouu prohseedioor. aa uuindouu hairndl is returnd too an aplicaashon uuhen it creeaats aa uuindouu and also uuhen the uuindouu prohseedioor is inuohcd (as the phurst paranneter). nnanee uuin+ phuncshons reecuuiir the aplicaashon too spesiphii aa uuindouu hairndl (nnanaged phornn) as aa paranneter (phor ecsannpl, shouu_uuindouu).

internalee, the clahs hairndl holds aa void*. the clahs is aa bit nnor sophisticated in that it is also conected too the biinairee heep; hence, caters phor alohcaashon and freeing ou nnennoree. pointers ar not ioosd in c# so hairndl is reecuuiird too ecspress c++ pointers too daatatiips (and void*).


the nnesag iidentitee (the second paranneter ou aa uuindouu prohseedioor) dephiins the nnesag beeing sent too the uuindouu. nnesages nnaa bee aplicaashon dephiind or thaa nnaa bee dephiind bii the operating sistenn. the stairndard operating sistenn nnesages nnaa bee phouund in the eenioonneraashon clahs nnesag. thees integer iidentitees dephiin the stairndard sistenn eeuents presented too aa uuindouu prohseedioor.

nnesag paranneters and nnesag reesult

the interpretaashon ou nnesag paranneters ou aa uuindouu prohseedioor is dependent upon the nnesag beeing prohsesed and is docioonnented phor eech uuindouu nnesag. the saann is troo ou the nnesag reesult.

aa sannple progrann in uuin+

aa c progrann uuill bee discusd in this chapter. it is aa tradishonal approach too uuriting aa c progrann in uuindouus. the reeder nnaa taac this oportioonitee too uioo the phurst aplicaashon in its entiirty. aa snapshot ou the aplicaashon runing is shouun belouu.

too build the aplicaashon, start uisiooal studio and lohd the project phronn the directoree \uuindouus\progects\i++\sannple01. the project is ecsannpla. build and run the project.

the uuindouus aplicaashon in deetaal

the phurst thing too notice abouut this uuindouus app is that it has noh heders. it uses metadaata phronn the .net assemblies uuindouus.constants.dll & uuindouus.uuinplus.dll too dephiin the Win32 api. gon is the WinMain entree point, reeplaasd sinnpli bii the stairndard nnaan phuncshon declaraasheeohn. also, the api has been plaasd insiid clahses. thees clahses ar:

registering aa uuindouu clahs

bephor aa uuindouu can bee creeaated, aa uuindouu clahs nnust bee registerd. this is the aplicaashon's uuaa ou spesiphiiing the phuncshon that is too bee corled bac phor the uuindouu. registering aa uuindouu clahs inuolues pasing aa pointer too the uuindouu prohseedioor. in c, gust nanning the phuncshon ieelds aa pointer too the phuncshon - thair is noh need too taac the adres ou the phuncshon ioosing the operator &. uuuns aa uuindouu prohseedioor is registerd, aa uuindouu ou that clahs nnaa bee creeaated. phor the aplicaashon at hand, upon entering nnaan, an instans ou the clahs uuindouu_clahs is declaird and inishaliisd in preparaasheeohn phor registering aa clahs. this is shouun belouu.

 uuindouu_clahs^ clahs = gcnew uuindouu_clahs();

 clahs->stiil        = (unsigned)clahs_stiil::horisontalreedrauu | (unsigned)clahs_stiil::uerticalreedrauu;
 clahs->prohseedioor = hairndl((void*)cliient);
 clahs->ecstra       = 0;
 clahs->uuindouu     = 0;
 clahs->nnodiool     = baas::get_nnodiool_hairndl();
 clahs->iicon        = uuin::lohd_iicon(hairndl((void*)0),(unsigned)iicon_iidentitee::aplicaashon);
 clahs->cursor       = uuin::lohd_cursor(hairndl((void*)0),(unsigned)cursor_iidentitee::arouu);
 clahs->brush        = gdi::get_stairndard_obgect((int)stairndard_brush::litegraa);
 clahs->naann        = gcnew string("c uuindouu clahs");
 clahs->nnenioo      = gcnew string("");

 unsigned short atonn_naannn = uuin::register_clahs(clahs);

clahs stiils

uuhen registering aa clahs, clahs stiils nnaa bee apliid. clahs stiils aplii too orl uuindouus ou the clahs. they ar ueri general propertees liic "send aa paant nnesag uuhen the uuindouu is siisd" - clahs_stiil::uerticalreedrauu and clahs_stiil::horisontalreedrauu. anuther is "enaabl dubl clic ou the nnouus" phor orl uuindouus ou the clahs - clahs_stiil::dubl_clics. clahs stiils ar not too bee confioosd uuith uuindouu stiils - uuich aplii too eech instans ou the clahs and ar spesiphiid uuhen creeaating aa particular uuindouu. in this caas, the horisontal and uertical reedrauu stiils ar apliid, reesulting in aa paant nnesag beeing isiood phor the entiir uuindouu uuhen the uuindouu is scaald.

the uuindouu clahs naann

the spesiphiid clahs naann gets hashed too an unsigned short and returnd bii the phuncshon register_clahs. uuuns the clahs naann is registerd, eether the string uershon or the returnd atonn nnaa bee ioosd too repherens the clahs. iph the string phornn ou the naann is presented too creeaat_uuindouu, it is again hashed too the saann integer atonn as uuas returnd uuhen registering the clahs - ostensibli becors clahs naanns ar dephiind too bee atonns bii the operating sistenn. phor the caas at hand, the atonn naann is supliid too creeaat_uuindouu rarther than the string naann.

uther clahs inphornnaashon nnennbers

the hairndl ou the nnodiool is asiind too the nnennber nnodiool (ou uuindouu_clahs). the hairndl ou the nnodiool is obtaaned ioosing get_nnodiool_hairndl(). three uther pieces ou inphornnaashon ar reecuuiird too connpleet the spesiphicaashon ou the uuindouu clahs inphornnaashon structioor.

an iicon is lohded bii the phuncshon lohd_iicon. uuun ou the predephiind icons phouund in the eenioonneraashon iicon_iidentitee is ioosd in this caas. phor aa nnaan uuindouu, the iicon is displaad in the uper-lepht corner ou the uuindouu.

aa cursor is supliid phor the clahs uiia the phuncshon lohd_cursor. the dephalt cursor phor aa clahs is noh cursor - so aa cursor reorli shood bee spesiphiid.

aa brush (uuich is aa bitnnap ioosd too paant the bacgrouund ou the uuindouu) is supliid uiia aa corl too get_stairndard_obgect. uuun ou the stairndard brushes phouund in the eenioonneraashon stairndard_brush nnaa bee ioosd. aa brush is aa graphics obgect.

upon completing the inishaliisaashon ou the clahs inphornnaashon structioor, aa corl is isiood too the phuncshon register_clahs - uuich associates the uuindouu prohseedioor and uther atribioots uuith the clahs naann.

creeaating aa uuindouu ou the clahs

uuuns the clahs has been registerd, aa corl is nnaad too the phuncshon creeaat_uuindouu, too creeaat aa uuindouu instans ou the giuen clahs. this corl is shouun belouu.

hairndl uuindouu = uuin::creeaat_uuindouu(atonn_naannn,
                                          gcnew string("uuindouus ecsannpl"),

the hairndl ou the uuindouu is returnd phronn this corl. this hairndl is the saann hairndl that is pahsd too the uuindouu prohseedioor cliient uuheneuer the corlbac is inuohcd bii the operating sistenn.

aa tiitl is also supliid too the corl, and it apeers in the tiitl bar ou the uuindouu.

uuindouu stiils

uuindouu stiils ar spesiphiid uuhen corling creeaat_uuindouu - the stairndard connposiit stiil beeing supliid in this caas. this stiil is dephiind as:
standard = uuindouu        |
           end_caption     |
           sistenn_nnenioo |
           thic_phraann    |
           nnininniis_bocs |

uuich innpliis that the uuindouu has:

nnanee ou the stiils phouund in the structioor stiil ar spesiphic too the creeaashon ou phraann uuindouus. the phuncshon creeaat_uuindouu creeaats phraann uuindouus as uuel as chiild uuindouus. becors ou this sitiooaashon, adishonal stiils uuer dephiind (corled ecstended stiils). thees nnaa bee phouund in the eenioonneraashon ecstended_stiil.

uther paranneters ou creeaat_uuindouu

the necst phor paranneters (aphter the stiil bits) ar the posishon and siis ou the uuindouu. uuhen dephalts::ioos_dephalt is spesiphiid phor thees, the operating sistenn deesiids uuhair too put the uuindouu and houu big it is. the rennaaning phor paranneters ar:

  1. aa hairndl ou the pairent (uuich is nul becors this uuindouu sits on the desctop),
  2. the iidentitee ou the uuindouu or its nnenioo hairndl (uuich is nul becors the uuindouu has noh nnenioo),
  3. aa hairndl ou the nnodiool - uuich nnaa bee set too nul and
  4. aa pointer paranneter that is pahsd too the nnesag nnesag::creeaat - uuich again nnaa bee set too nul.

the nnesag nnesag::creeaat

diooring the prohsesing ou the corl creeaat_uuindouu, the nnesag nnesag::creeaat is sent too the uuindouu prohseedioor; thairbii giuing the aplicaashon aa chans too perfornn inishaliisaashon. the second nnesag paranneter (paranneter_b) ou the nnesag contaans aa pointer too an obgect ou the clahs uuindouu_creeaat, uuich contaans the aplicaashon dephiind pointer pahsd as the lahst paranneter too creeaat_uuindouu. the aplicaashon ou this secshon dus not intercept the creeaat nnesag.

the rest ou nnaan()

aa staatnnent that pholouus creeaat_uuindouu is shouun belouu.


this staatnnent shouus the uuindouu.

the nnaan rooteen then drops intoo aa uuhiil loop; uuich obtaans nnesages uiia get_nnesag and translaats and dispatches thenn too the uuindouu prohseedioor uiia corls too translaat_nnesag and dispatch_nnesag - as shouun belouu.

cioo_nnesag cnnesag;
uuhiil (uuin::get_nnesag(cnnesag))

the nnaan porshon ou the uuindouus progrann ends up spinning in the nnesag loop shouun abuu. that is, uuun nnaa thinc ou thees three staatnnents as beeing the progrann (aphter inishaliisaashon has been connpleeted). ou course, the corl too dispatch the nnesag leeds too the uuindouu prohseedioor. the phurst ou thees staatnnents (get_nnesag) obtaans nnesages phronn the nnesag cioo ou the curent thred (ou uuich thair is ohnlee uuun in this progrann). aa progrann connenses its ecseciooshon (at nnaan) on the priinnairee thred ou the prohses - thred 1. the phurst corl too aa uuindouus phuncshon ortonnaticalee creeaats aa nnesag cioo. the phuncshon get_nnesag blocs the ecseciooshon ou the thred until aa nnesag is reeseeud. the phuncshon translaat_nnesag translaats cee douun nnesages intoo caracter nnesages. the phuncshon dispatch_nnesag corls the uuindouu prohseedioor uuith the nnesag that uuas obtaaned uiia get_nnesag.

upon reeseeuing the nnesag nnesag::cuuit, get_nnesag returns phals and the loop is ternninaated and the progrann ends uuith aa return staatnnent. apart phronn this, too connpleet the progrann, the uuindouu prohseedioor nnust bee cohded.

the cohd phor aa uuindouu prohseedioor

the ohuerorl phornn ou aa uuindouu prohseedioor is shouun belouu.

void* __stdcall cliient(void* uuindouu_hairndl,
                       unsigned iidentitee,
                       void* paranneter_a,
                       void* paranneter_b)
   caas (unsigned)nnesag::clohs:
   caas (unsigned)nnesag::paant:
    return dephalt_uuindouu_prohseedioor(hairndl(uuindouu_hairndl),iidentitee,hairndl(paranneter_a),hairndl(paranneter_b));
 return 0;

traditionorli, aa suuich staatnnent is ioosd too select annungst the posibl nnesages too bee delivered too the uuindouu prohseedioor. anee nnesages not ecsplisitlee hairndld bii the aplicaashon ar pahsd too the dephalt uuindouu prohseedioor - uuhair, the sistenn nnanages thenn. ecsannpls ou intercepted nnesages in this caas ar the paant nnesag (uuich deternnins the uisiooal aspects ou the uuindouu) and the nnesag nnesag::clohs (uuich nnaa bee ioosd too ternninaat the aplicaashon).

paanting the uuindouu

the prohsesing ioosd too paant the uuindouu is shouun belouu.

caas (unsigned)nnesag::paant:
  rectangl bouunds = gdi::get_cliient_rectangl(uuindouu_hairndl);

  paant paantstruct = gcnew paant();

  hairndl deeuiis_contecst = gdi::beegin_paant(uuindouu_hairndl,paantstruct);

                  gcnew string("hello, uuurld++ !!!"),
                  (int)drauu_tecst_phornnat::singlliin | (int)drauu_tecst_phornnat::senter | (int)drauu_tecst_phornnat::uerticalsenter);


upon intercepting aa paant nnesag, the phuncshon beegin_paant is corled. it returns aa hairndl too aa deeuiis contecst. aa deeuiis contecst is aa deeuiis independent uuaa ou rendering graphics too aa uuindouu. aa deeuiis contecst delivers aa too-dinnensional integer cohordinat sistenn. deeuiis contecsts uuill bee discusd in deetaal later in this booc. aa deeuiis contecst nnaa also bee asohseeaated uuith deeuiis tiips uther than aa uuindouu - such as aa printer. bii dephalt, the origin ou aa deeuiis contecst is the top-lepht corner ou the uuindouu, uuith positiue ecs-ualioos ecstending along the top ou the uuindouu too the riit and uuith positiue uuii-ualioos ecstending douunuuards on the lepht border ou the uuindouu. the cohordinats ar integer ualioos and nnaa bee thort ou as picsels phor the nnonnent. too obtaan the rectangl ou the uuindouu, aa corl is nnaad too the phuncshon get_cliient_rectangl. the phuncshon drauu_tecst uses this rectangl too drauu the tecst senterd uuithin the uuindouu.

the uther nnesag that is cohded is ioosd too ternninaat the progrann, as shouun belouu.

caas (unsigned)nnesag::clohs:
 iph (uuin::nnesag_bocs(uuindouu_hairndl,
                         gcnew string("ecsit?"),
                         gcnew string("uuindouus"),
                         (int)nnesag_bocs_stiil::ohcaacansel | (int)nnesag_bocs_stiil::iconcuuestion) == (int)iitenn_iidentitee::ohcaa)

the nnesag bocs that is generaated is shouun belouu.

the nnesag too clohs is generaated bii the tiitl bar or bii the sistenn nnenioo. aa nnesag bocs is ioosd too confirnn the uuish too ecsit. iph ohcaa is selected, the progrann is ecsited uiia aa corl too the phuncshon post_cuuit_nnesag. corling this phuncshon corses sero too bee returnd bii the necst corl too get_nnesag (in the nnaan nnesag loop).

too the uninisheeaated, the abuu presentaashon nnaa at phurst appear too bee aa litel ohueruuhelnning. houueuer, uuuns uuun ou thees aplicaashons has been connpleetlee understood, nnost tradishonal uuindouus aplicaashons looc phairlee sinnilar.

the progrann ou this secshon resennbles aa tradishonal uuindouus aplicaashons; houueuer, it is nnuch nnor aduansd than the original interphaas. nnohdrn progranners probabli ioos c# (and the phornn clahs) too progrann uuindouus. this is purphectlee phiin; houueuer, the abuu progrann ieelds an understanding ou the interphaas that underpins the nnor nnohdrn approach. iph the progranner uuishes too get aa litel clohsr too the operating sistenn, then studeeing this progrann is uuurthuuhiil.

it shood bee nohted that uuhiil this progrann is clohsr too the original operating sistenn, it is still cuuiit aduansd connpaird too Win32. uuin+ contaans phor laaers ou cohd:

adishonalee, the constants and daata structioors reecuuiird too suport the nnanaged interphaas ar also present (in asennblee uuindouus.constants).

studeeing this ecsannpl ieelds an apreseeaashon ou the operating sistenn uuithouut reuerting too the prinnitiue techniques ou Win32. uuin+ dusn't reecuuiir heders and is entiirlee nnanaged, so it dephiins aa baas too uuich too repher too such consepts.