Академический Документы
Профессиональный Документы
Культура Документы
A Simple Window
Example: simple_window Sometimes people come on IRC and ask "How do I make a window?"...Well it's not entirely that simple I'm afraid. It's not difficult once you know what you're doing but there are quite a few things you need to do to get a window to show up; And they're more than can be simply explained over a chat room, or a quick note. I always liked to do things first and learn them later...so here is the code to a simple window which will be explained shortly. # 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 " ; / /S t e p4 :t h eW i n d o wP r o c e d u r e 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 ; } 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 ; / / S t e p1 :R e g i s t e r i n gt h eW i n d o wC l a s s w c . c b S i z e =s i z e o f ( W N D C L A S S E X ) ; w c . s t y l e =0 ; w c . l p f n W n d P r o c =W n d P r o c ; w c . c b C l s E x t r a =0 ; w c . c b W n d E x t r a =0 ; w c . h I n s t a n c e =h I n s t a n c e ; w c . h I c o n =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 ; } / /S t e p2 :C r e a t i n gt h eW i n d o w 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 ) ; / /S t e p3 :T h eM e s s a g eL o o p 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 ; } For most part this is the simplest windows program you can write that actually creates a functional window, a mere 70 or so lines. If you got the first example to compile then this one should work with no problems.
w c . c b S i z e =s i z e o f ( W N D C L A S S E X ) ; w c . s t y l e =0 ; w c . l p f n W n d P r o c =W n d P r o c ; w c . c b C l s E x t r a =0 ; w c . c b W n d E x t r a =0 ; w c . h I n s t a n c e =h I n s t a n c e ; w c . h I c o n =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 ; } This is the code we use in W i n M a i n ( )to register our window class. We fill out the members of a W N D C L A S S E Xstructure and call R e g i s t e r C l a s s E x ( ) . The members of the struct affect the window class as follows: c b S i z e The size of the structure. s t y l e Class Styles (C S _ * ), not to be confused with Window Styles (W S _ * ) This can usually be set to 0 . l p f n W n d P r o c Pointer to the window procedure for this window class. c b C l s E x t r a Amount of extra data allocated for this class in memory. Usually 0 . c b W n d E x t r a Amount of extra data allocated in memory per window of this type. Usually 0 . h I n s t a n c e Handle to application instance (that we got in the first parameter of W i n M a i n ( ) ). h I c o n Large (usually 32x32) icon shown when the user presses Alt+Tab. h C u r s o r Cursor that will be displayed over our window. h b r B a c k g r o u n d Background Brush to set the color of our window. l p s z M e n u N a m e Name of a menu resource to use for the windows with this class. l p s z C l a s s N a m e Name to identify the class with. h I c o n S m Small (usually 16x16) icon to show in the taskbar and in the top left corner of the window. Don't worry if that doesn't make much sense to you yet, the various parts that count will be explained more later. Another thing to remember is to not try and remember this stuff. I rarely (never) memorize structs, or function parameters, this is a waste of effort and, more importantly, time. If you know the functions you need to call then it is a matter of seconds to look up the exact parameters in your help files. If you don't have help files, get them. You are lost without. Eventually you will come to know the parameters to the functions you use most. We then call R e g i s t e r C l a s s E x ( )and check for failure, if it fails we pop up a message which says so and abort the
After we've created the window and checked to make sure we have a valid handle we show the window, using the last parameter in W i n M a i n ( )and then update it to ensure that it has properly redrawn itself on the screen. 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 ) ; The n C m d S h o wparameter is optional, you could simply pass in S W _ S H O W N O R M A Lall the time and be done with it. However using the parameter passed into W i n M a i n ( )gives whoever is running your program to specify whether or not they want your window to start off visible, maximized, minimized, etc... You will find options for these in the properties of windows shortcuts, and this parameter is how the choice is carried out.
The window procedure is called for each message, the H W N Dparameter is the handle of your window, the one that the message applies to. This is important since you might have two or more windows of the same class and they will use the same window procedure (W n d P r o c ( ) ). The difference is that the parameter h w n dwill be different depending on which window it is. For example when we get the W M _ C L O S Emessage we destroy the window. Since we use the window handle that we received as the first paramter, any other windows will not be affected, only the one that the message was intended for. W M _ C L O S Eis sent when the user presses the Close Button or types Alt-F4. This will cause the window to be destroyed by default, but I like to handle it explicitly, since this is the perfect spot to do cleanup checks, or ask the user to save files etc. before exiting the program. When we call D e s t r o y W i n d o w ( )the system sends the W M _ D E S T R O Ymessage to the window getting destroyed, in this case it's our window, and then destroys any remaining child windows before finally removing our window from the system. Since this is the only window in our program, we are all done and we want the program to exit, so we call P o s t Q u i t M e s s a g e ( ) . This posts the W M _ Q U I Tmessage to the message loop. We never receive this message, because it causes G e t M e s s a g e ( )to return F A L S E , and as you'll see in our message loop code, when that happens we stop processing messages and return the final result code, the w P a r a mof W M _ Q U I Twhich happens to be the value we passed into P o s t Q u i t M e s s a g e ( ) . The return value is only really useful if your program is designed to be called by another program and you want to return a specific value.