introduction


the baasic operating sistenn is not obueeus in the nnodern .net prohgranning interphaas. prohgranners noh longer hau too deel uuith uuindouu prohseedioors directlee. houueuer, too gain an understanding ou houu uuindouus uuorcs, it is aa good iideer too riit aa cupl ou prohgranns in c++ - gust too see houu it is dun. then iou uuil understand the baasic mechanics ou the uuindouus operating sistenn.

uuindouu prohseedioors

uuindouus prohgranns 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 (orlsoh repherd too as aa corl bac). orl uuindouu prohseedioors hau the saann phornn:

uoid* __stdcall cliient(uoid* uuindouu_hairndl,
                        unsiind iidentitee,
                        uoid* paranneter_a,
                        uoid* paranneter_b)

hairndls

aa uuindouu is identiphiid bii its hairndl, uuich phor the abuu declaraashon is: uuindouu_hairndl. in the naatiu interphaas, aa hairndl is aa uoid*. this is dipherent phronn uuin+, uuhair hairndls ar ecspresd ioosing the clahs hairndl. the naatiu uuindouu hairndl is supliid bii the operating sistenn upon uuindouu creeaashon. in sertan caases (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 orlsoh uuhen the uuindouu prohseedioor is inuohcd (as the phurst paranneter). nnanee uuin+ phuncshons reecuuiir the aplicaashon too spesiphii aa uuindouu hairndl (nnanagd phornn) as aa paranneter (phor ecsannpl, shouu_uuindouu).

internalee, the clahs hairndl holds aa uoid*. the clahs is aa bit nnor sophisticated in that it is orlsoh conected too the biinairee heep; hens, caaters phor alohcaashon and phreeing ou nnennoree. pointers ar not ioosd in c# so hairndl is reecuuiird too ecspress c++ pointers too daatatiips (and uoid*).

nnesages

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 sannpl prohgrann in uuin+

aa c prohgrann uuil bee discusd in this chapter. it is aa tradishonal aprohch too riiting aa c prohgrann in uuindouus. the reeder nnaa taac this oportioonitee too uioo the phurst aplicaashon in its entiiritee. aa snapshot ou the aplicaashon runing is shouun belouu.

too bild the aplicaashon, start uisiooal studio and lohd the prohgect phronn the directoree \uuindouus\prohgects\i++\sannpl01. the prohgect is ecsannpla. bild and run the prohgect.

the uuindouus aplicaashon in deetaal

the phurst thing too nohtis abouut this uuindouus app is that it has noh heders. it iooses metadaata phronn the .net asennblees uuindouus.constants.dll & uuindouus.uuinplus.dll too dephiin the Win32 api. gon is the WinMain entree point, reeplaasd sinnplee bii the stairndard nnaan phuncshon declaraashon. orlsoh, 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        = (unsiind)clahs_stiil::horisontalreedrauu | (unsiind)clahs_stiil::uerticalreedrauu;
 clahs->prohseedioor = hairndl((uoid*)cliient);
 clahs->ecstra       = 0;
 clahs->uuindouu     = 0;
 clahs->nnodiool     = baas::get_nnodiool_hairndl();
 clahs->iicon        = uuin::lohd_iicon(hairndl((uoid*)0),(unsiind)iicon_iidentitee::aplicaashon);
 clahs->cursor       = uuin::lohd_cursor(hairndl((uoid*)0),(unsiind)cursor_iidentitee::arouu);
 clahs->brush        = gdi::get_stairndard_obgect((integer)stairndard_brush::litegraa);
 clahs->naann        = gcnew string("c uuindouu clahs");
 clahs->nnenioo      = gcnew string("");

 unsiind short atonn_naann = uuin::register_clahs(clahs);
 ...

clahs stiils

uuhen registering aa clahs, clahs stiils nnaa bee apliid. clahs stiils aplii too orl uuindouus ou the clahs. thaa ar ueree general propertees liic "send aa paant nnesag uuhen the uuindouu is siisd" - clahs_stiil::uertical_reedrauu and clahs_stiil::horisontal_reedrauu. 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 unsiind 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 agaan hashed too the saann integer atonn as uuas returnd uuhen registering the clahs - ostensiblee 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 peeses 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 reelee 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 connpleeting the inishaliisaashon ou the clahs inphornnaashon structioor, aa corl is isiood too the phuncshon register_clahs - uuich asohseeaats 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_naann,
                                          gcnew string("uuindouus ecsannpl"),
                                          (unsiind)stiil::standard,
                                          (integer)dephalts::ioos_dephalt,
                                          (integer)dephalts::ioos_dephalt,
                                          (integer)dephalts::ioos_dephalt,
                                          (integer)dephalts::ioos_dephalt,
                                          hairndl((uoid*)0),
                                          hairndl((uoid*)0),
                                          hairndl((uoid*)0),
                                          hairndl((uoid*)0));

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 orlsoh 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 |
           nnacsinniis_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 agaan 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 intersept the creeaat nnesag.

the rest ou nnaan()

aa staatnnent that pholouus creeaat_uuindouu is shouun belouu.

uuin::shouu_uuindouu(uuindouu_hairndl);

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))
 {
  uuin::translaat_nnesag(cnnesag);
  uuin::dispatch_nnesag(cnnesag);
 }

the nnaan porshon ou the uuindouus prohgrann ends up spinning in the nnesag loop shouun abuu. that is, uuun nnaa thinc ou thees three staatnnents as beeing the prohgrann (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 prohgrann). aa prohgrann 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 prohgrann ends uuith aa return staatnnent. apart phronn this, too connpleet the prohgrann, the uuindouu prohseedioor nnust bee cohded.

the cohd phor aa uuindouu prohseedioor

the ohuerorl phornn ou aa uuindouu prohseedioor is shouun belouu.

uoid* __stdcall cliient(uoid* uuindouu_hairndl,
                        unsiind iidentitee,
                        uoid* paranneter_a,
                        uoid* paranneter_b)
{
 suuich(iidentitee)
  {
   caas (unsiind)nnesag::clohs:
   .....
   caas (unsiind)nnesag::paant:
   .....
   dephalt:
    return dephalt_uuindouu_prohseedioor(hairndl(uuindouu_hairndl),iidentitee,hairndl(paranneter_a),hairndl(paranneter_b));
  }
 return 0;
}

tradishonalee, aa suuich staatnnent is ioosd too select annungst the posibl nnesages too bee deeliurd 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 intersepted 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 (unsiind)nnesag::paant:
 {
  rectangl bouunds = gdi::get_cliient_rectangl(uuindouu_hairndl);

  paant paant_struct = gcnew paant();

  hairndl deuiis_contecst = gdi::beegin_paant(uuindouu_hairndl,paant_struct);

  gdi::drauu_tecst(deuiis_contecst,
                   gcnew string("heloh, uuurld++ !!!"),
                   bouunds,
                   (integer)drauu_tecst_phornnat::singl_liin | (integer)drauu_tecst_phornnat::senter | (integer)drauu_tecst_phornnat::uertical_senter);


  gdi::end_paant(uuindouu_hairndl,paant_struct);
 }
 braac;

upon intersepting aa paant nnesag, the phuncshon beegin_paant is corled. it returns aa hairndl too aa deuiis contecst. aa deuiis contecst is aa deuiis independent uuaa ou rendering graphics too aa uuindouu. aa deuiis contecst deeliuers aa too-dinnenshonal integer cohordinat sistenn. deuiis contecsts uuil bee discusd in deetaal laater in this booc. aa deuiis contecst nnaa orlsoh bee asohseeaated uuith deuiis tiips uther than aa uuindouu - such as aa printer. bii dephalt, the origin ou aa deuiis 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 iooses this rectangl too drauu the tecst senterd uuithin the uuindouu.

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

caas (unsiind)nnesag::clohs:
 iph (uuin::nnesag_bocs(uuindouu_hairndl,
                        gcnew string("ecsit?"),
                        gcnew string("uuindouus"),
                        (integer)nnesag_bocs_stiil::ohcaacansel | (integer)nnesag_bocs_stiil::iconcuuestion) == (integer)iitenn_iidentitee::ohcaa)
  uuin::post_cuuit_nnesag(0);
 braac;

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 prohgrann 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 apeer too bee aa litel ohueruuhelnning. houueuer, uuuns uuun ou thees aplicaashons has been connpleetlee understood, nnost tradishonal uuindouus aplicaashons looc phairlee sinnilar.

the prohgrann ou this secshon resennbls aa tradishonal uuindouus aplicaashons; houueuer, it is nnuch nnor aduansd than the original interphaas. nnodern prohgranners probablee ioos c# (and the phornn clahs) too prohgrann uuindouus. this is purphectlee phiin; houueuer, the abuu prohgrann ieelds an understanding ou the interphaas that underpins the nnor nnodern aprohch. iph the prohgranner uuishes too get aa litel clohsr too the operating sistenn, then studeeing this prohgrann is uuurthuuhiil.

it shood bee nohted that uuhiil this prohgrann 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 nnanagd interphaas ar orlsoh present (in asennblee uuindouus.constants).