Você está na página 1de 4

[ contents | #winprog ]

Handling Messages
Example: window_click Alright, we've got a window, but it doesn't do anything except what D e f W i n d o w P r o c ( )allows it to, like be sized, maximised, etc... Not really all that exciting. In the next section I am going to show you how to modify what you already have to do somehting new. This way I can just tell you "Handle this message, and do this in it..." and you will know what I mean and be able to do so without seeing an entire example. That's the hope anyway, so pay attention :P Okay for starters take the example code for the last window we worked on and make sure it compiles and runs as expected. Then you can either keep working on it for the next little bit or copy it to a new project to modify. We're going to add the capability to show the user what the name of our program is when they click on our window. Not very exciting, it's basically to get the hang of handling messages. Lets look at what we have in our W n d P r o c ( ) : L R E S U L TC A L L B A C KW n d P r o c ( H W N Dh w n d ,U I N Tm s g ,W P A R A Mw P a r a m ,L P A R A Ml P a r a m ) { s w i t c h ( m s g ) { c a s eW M _ C L O S E : D e s t r o y W i n d o w ( h w n d ) ; b r e a k ; c a s eW M _ D E S T R O Y : P o s t Q u i t M e s s a g e ( 0 ) ; b r e a k ; d e f a u l t : r e t u r nD e f W i n d o w P r o c ( h w n d ,m s g ,w P a r a m ,l P a r a m ) ; } r e t u r n0 ; } If we want to handle mouse clicks, we need to add a W M _ L B U T T O N D O W Nhandler (or W M _ R B U T T O N D O W N ,W M _ M B U T T O N D O W N , for right and middle clicks respectively). If I or someone else refers to handling a message they mean to add it into the W n d P r o c ( )of your window class as follows: L R E S U L TC A L L B A C KW n d P r o c ( H W N Dh w n d ,U I N Tm s g ,W P A R A Mw P a r a m ,L P A R A Ml P a r a m ) { s w i t c h ( m s g ) { c a s eW M _ L B U T T O N D O W N : / /< / /< w ej u s ta d d e dt h i ss t u f f b r e a k ; / /< c a s eW M _ C L O S E : D e s t r o y W i n d o w ( h w n d ) ; b r e a k ; c a s eW M _ D E S T R O Y : P o s t Q u i t M e s s a g e ( 0 ) ; b r e a k ; d e f a u l t :

r e t u r nD e f W i n d o w P r o c ( h w n d ,m s g ,w P a r a m ,l P a r a m ) ; } r e t u r n0 ; } The order in which you handle your messages rarely matters. Just make sure you've got your b r e a k ;after each one. As you can see we added another c a s einto our s w i t c h ( ) . Now we want something to happen when we get to this part of our program. First I will present the code we want to add (that will show the user the filename of our program) and then I will integrate it into our program. Later on I will probably just show you the code and let you integrate it into your program. This is of course better for me as I don't have to type as much and it's better for you because you will be able to add the code into ANY program and not just the ones I present. If you aren't sure how to do it, look at the example zip file included with the section. G e t M o d u l e F i l e N a m e ( h I n s t a n c e ,s z F i l e N a m e ,M A X _ P A T H ) ; M e s s a g e B o x ( h w n d ,s z F i l e N a m e ," T h i sp r o g r a mi s : " ,M B _ O K|M B _ I C O N I N F O R M A T I O N ) ; Now this code does not stand on it's own, it can't just be slapped into our code any old place. We specifically want it to run when the user clicks the mouse button so this is how I would merge this small bit of code into our skeleton program: L R E S U L TC A L L B A C KW n d P r o c ( H W N Dh w n d ,U I N Tm s g ,W P A R A Mw P a r a m ,L P A R A Ml P a r a m ) { s w i t c h ( m s g ) { c a s eW M _ L B U T T O N D O W N : / /B E G I NN E WC O D E { c h a rs z F i l e N a m e [ M A X _ P A T H ] ; H I N S T A N C Eh I n s t a n c e=G e t M o d u l e H a n d l e ( N U L L ) ; G e t M o d u l e F i l e N a m e ( h I n s t a n c e ,s z F i l e N a m e ,M A X _ P A T H ) ; M e s s a g e B o x ( h w n d ,s z F i l e N a m e ," T h i sp r o g r a mi s : " ,M B _ O K|M B _ I C O N I N F O R M A T I O N ) ; } / /E N DN E WC O D E b r e a k ; c a s eW M _ C L O S E : D e s t r o y W i n d o w ( h w n d ) ; b r e a k ; c a s eW M _ D E S T R O Y : P o s t Q u i t M e s s a g e ( 0 ) ; b r e a k ; d e f a u l t : r e t u r nD e f W i n d o w P r o c ( h w n d ,m s g ,w P a r a m ,l P a r a m ) ; } r e t u r n0 ; } Note the new set of curly braces {} . These are required when declaring variables inside a s w i t c h ( )statement. This should be basic C knowledge but I thought I should point it out anyway for those of you doing things the hard way. So if you've added in that code, compile it now. If it works, click on the window and you should see a box with the name of the .exe pop up. You'll notice we've added two variables, h I n s t a n c eand s z F i l e N a m e . Look up G e t M o d u l e F i l e N a m e ( )and you will see that the first parameter is a H I N S T A N C Erefering to the executable module (our program, the .exe file). Where do we get such a thing? G e t M o d u l e H a n d l e ( )is the answer. The references for G e t M o d u l e H a n d l e ( )indicate that passing in NULL will return us " a handle to the file used to create the calling process", which is exactly what we need, the H I N S T A N C Ejust mentioned. Putting all this information together we end up with the following declaration: H I N S T A N C Eh I n s t a n c e=G e t M o d u l e H a n d l e ( N U L L ) ;

Now on to the second parameter, again turning to our trusty reference manual, we see that it is " a pointer to a buffer that receives the path and file name of the specified module" and the data type is L P T S T R(or L P S T Rif your references are old). Since L P S T Ris equivalent to c h a r *we can declare an array of c h a r 's like this: c h a rs z F i l e N a m e [ M A X _ P A T H ] ; M A X _ P A T His a handy macro included via < w i n d o w s . h >that is defined to the maximum length of a buffer needed to store a filename under Win32. We also pass M A X _ P A T Hto G e t M o d u l e F i l e N a m e ( )so it knows the size of the buffer. After G e t M o d u l e F i l e N a m e ( )is called, the buffer s z F i l e N a m ewill be filled with a null terminated string containing the name of our .exe file. We pass this value to M e s s a g e B o x ( )as an easy way of displaying it to the user. So if you've added in that code, compile it now. If it works, click on the window and you should see a box with the name of the .exe pop up. If it doesn't work, here's the full code to the program. Compare it to what you have and see what, if any, mistakes you made. # i n c l u d e< w i n d o w s . h > c o n s tc h a rg _ s z C l a s s N a m e [ ]=" m y W i n d o w C l a s s " ; L R E S U L TC A L L B A C KW n d P r o c ( H W N Dh w n d ,U I N Tm s g ,W P A R A Mw P a r a m ,L P A R A Ml P a r a m ) { s w i t c h ( m s g ) { c a s eW M _ L B U T T O N D O W N : { c h a rs z F i l e N a m e [ M A X _ P A T H ] ; H I N S T A N C Eh I n s t a n c e=G e t M o d u l e H a n d l e ( N U L L ) ; G e t M o d u l e F i l e N a m e ( h I n s t a n c e ,s z F i l e N a m e ,M A X _ P A T H ) ; M e s s a g e B o x ( h w n d ,s z F i l e N a m e ," T h i sp r o g r a mi s : " ,M B _ O K|M B _ I C O N I N F O R M A T I O N ) ; } b r e a k ; c a s eW M _ C L O S E : D e s t r o y W i n d o w ( h w n d ) ; b r e a k ; c a s eW M _ D E S T R O Y : P o s t Q u i t M e s s a g e ( 0 ) ; b r e a k ; d e f a u l t : r e t u r nD e f W i n d o w P r o c ( h w n d ,m s g ,w P a r a m ,l P a r a m ) ; } r e t u r n0 ; } i n tW I N A P IW i n M a i n ( H I N S T A N C Eh I n s t a n c e ,H I N S T A N C Eh P r e v I n s t a n c e , L P S T Rl p C m d L i n e ,i n tn C m d S h o w ) { W N D C L A S S E Xw c ; H W N Dh w n d ; M S GM s g ; w c . c b S i z e w c . s t y l e w c . l p f n W n d P r o c w c . c b C l s E x t r a w c . c b W n d E x t r a w c . h I n s t a n c e w c . h I c o n =s i z e o f ( W N D C L A S S E X ) ; =0 ; =W n d P r o c ; =0 ; =0 ; =h I n s t a n c e ; =L o a d I c o n ( N U L L ,I D I _ A P P L I C A T I O N ) ;

w c . h C u r s o r =L o a d C u r s o r ( N U L L ,I D C _ A R R O W ) ; w c . h b r B a c k g r o u n d=( H B R U S H ) ( C O L O R _ W I N D O W + 1 ) ; w c . l p s z M e n u N a m e =N U L L ; w c . l p s z C l a s s N a m e=g _ s z C l a s s N a m e ; w c . h I c o n S m =L o a d I c o n ( N U L L ,I D I _ A P P L I C A T I O N ) ; i f ( ! R e g i s t e r C l a s s E x ( & w c ) ) { M e s s a g e B o x ( N U L L ," W i n d o wR e g i s t r a t i o nF a i l e d ! " ," E r r o r ! " , M B _ I C O N E X C L A M A T I O N|M B _ O K ) ; r e t u r n0 ; } h w n d=C r e a t e W i n d o w E x ( W S _ E X _ C L I E N T E D G E , g _ s z C l a s s N a m e , " T h et i t l eo fm yw i n d o w " , W S _ O V E R L A P P E D W I N D O W , C W _ U S E D E F A U L T ,C W _ U S E D E F A U L T ,2 4 0 ,1 2 0 , N U L L ,N U L L ,h I n s t a n c e ,N U L L ) ; i f ( h w n d= =N U L L ) { M e s s a g e B o x ( N U L L ," W i n d o wC r e a t i o nF a i l e d ! " ," E r r o r ! " , M B _ I C O N E X C L A M A T I O N|M B _ O K ) ; r e t u r n0 ; } S h o w W i n d o w ( h w n d ,n C m d S h o w ) ; U p d a t e W i n d o w ( h w n d ) ; w h i l e ( G e t M e s s a g e ( & M s g ,N U L L ,0 ,0 )>0 ) { T r a n s l a t e M e s s a g e ( & M s g ) ; D i s p a t c h M e s s a g e ( & M s g ) ; } r e t u r nM s g . w P a r a m ; }

Copyright 1998-2003, Brook Miles (theForger). All rights reserved.

Você também pode gostar