1 / 20

Introduction to Windows Programming

Introduction to Windows Programming. First Windows Program. This program simply displays a blank window. The following code is the minimum necessary to run a windows program. Windows Messages. Almost everything that happens in windows programs is coordinated by messages.

kfederico
Download Presentation

Introduction to Windows Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Windows Programming

  2. First Windows Program • This program simply displays a blank window. • The following code is the minimum necessary to run a windows program

  3. Windows Messages • Almost everything that happens in windows programs is coordinated by messages. • Messages are created by the Windows Operating System, response to events, e.g. key being pressed, mouse moved, a window becoming visible. • Windows sends messages to the appropriate application. The application programmer must code how to respond to the messages. • Usually messages are placed on the Message queue.

  4. Do Nothing Win32 Program #define WIN32_LEAN_AND_MEAN Tells the complier to use the simple windows framework. #include <windows.h> Header contains all the prototypes for the windows functions, as well as macros and constants

  5. //////////////////////////////////// //DEFINES ////////////////////////////////////// //name for our window class #define WINDOWCLASS "win32ex1“ //title of the application #define WINDOWTITLE "Do nothing Win32 Program" /////////////////////////////////////////////// //PROTOTYPES /////////////////////////////////////////////// bool Prog_Init();//game data initalizer void Prog_Loop();//main game loop void Prog_Done();//game clean up

  6. //GLOBALS HINSTANCE hInstMain=NULL;//main application handle HWND hWndMain=NULL;//handle to our main window These two global variables are used to store handles for the application and our applications window. Initialise them with NULL, we will give them actual values when we create the app and the window. Handles are simply unique integers used to keep track of items. Every running application, window, button, icon, control, menu has a handle.

  7. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { ...Stuff missing } Every windows program must have this function. This is used to handle the messages from used interaction and other messages. Inside this function will be a switch statement, which will run different code for each different type of message. The programmer does not need to code for every possible message, only the ones s/he need to respond to.

  8. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { switch(uMsg) { case WM_DESTROY: {...Stuff } case WM_PAINT: {...Stuff } } //pass along any other message to default message handler return(DefWindowProc(hwnd,uMsg,wParam,lParam)); } Every windows program must have this function. This is used to handle the messages from used interaction and other messages. Inside this function will be a switch statement, which will run different code for each different type of message. The programmer does not need to code for every possible message, only the ones s/he need to respond to. The last line sends any unresponded messages to the default windows procedure.

  9. The WM_DESTROY message is passed if the user is closing the window, in most cases if the user wants the window closed, we should also kill the application. • The WM_PAINT message is sent when ever windows thinks that part of the window needs redrawing

  10. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) { ...stuff } In a traditional, non-windows C++ program, the entry point was a specially named function called main(). For windows programs, the equivalent function is called WinMain(). Every windows program must have a WinMain function. This function is usually responsible for the following two tasks: • Setting up the application’s window. • Running the message pump.

  11. Making a Window in Windows • Create a description of your window by filling in a windows class. Not to be confused with a C++ class. • Tell Windows about your new window by registering the class. • Ask Windows to create an instance of the window (based on your previously registered class).

  12. //create window class WNDCLASSEX wcx; //set the size of the structure wcx.cbSize=sizeof(WNDCLASSEX); //class style wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; //window procedure wcx.lpfnWndProc=TheWindowProc; //extra stuff, don’t worry wcx.cbClsExtra=0; wcx.cbWndExtra=0; //application handle wcx.hInstance=hInstMain;

  13. //icon wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION); //cursor wcx.hCursor=LoadCursor(NULL,IDC_ARROW); //background color wcx.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); //menu wcx.lpszMenuName=NULL; //class name wcx.lpszClassName=WINDOWCLASS; //small icon wcx.hIconSm=NULL;

  14. //register the window class, //return 0 if not successful if(!RegisterClassEx(&wcx)) return(0); //create main window hWndMain=CreateWindowEx(0,WINDOWCLASS, WINDOWTITLE, WS_BORDER | WS_SYSMENU | WS_VISIBLE, 0,0,320,240,NULL,NULL, hInstMain,NULL); //error check if(!hWndMain) return(0); //if program initialization failed, //then return with 0 if(!Prog_Init()) return(0);

  15. The Message Loop • Windows communicates with your program by sending it messages: Window needs updating, mouse has moved into your window, a key has been presses, etc. • Imprtant messages are sent by Windows calling the WinProc function directly. • Other messages are placed on a message queue, ready for your application to handle, when it gets a chance.

  16. Message Pump • Most windows applications are event driven, i.e. user does something, program responds, wait until user does something else. • Most games are similar except that they will also need to move the game along even if the user is not responding, e.g move the bad guys, move the speeding car, redraw the next frame… • This behaviour is programmed using the message pump. • The message pump is an infinite loop which checks for a message, deals with it, update the game, check for another message…

  17. Message Pump code MSG msg; // create a message structure //message pump (infinite loop) for(;;) { //look for a message if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { //there is a message //check that we arent quitting if(msg.message==WM_QUIT) break; //translate message TranslateMessage(&msg); //dispatch message DispatchMessage(&msg); } //run main game loop Prog_Loop(); }

  18. if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) • This function simply checks if a message exists on the queue, returns zero if not. • If there is a message on the queue it is removed and put into the MSG structure.

  19. //check that we arent quitting if(msg.message==WM_QUIT) break; • If the message is WM_QUIT, the application is being asked to close down. The simplest way to do this is to break out of the message pump.

  20. //translate message TranslateMessage(&msg); //dispatch message DispatchMessage(&msg); • Translate message is used to convert key strokes to keyboard acceleration messages. • Dispatch message, sends the message to WinProc for processing

More Related