Introduction to Windows Programming. First Windows Program This program simply displays a blank window. The following code is the minimum necessary to.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Prof. Muhammad Saeed. Procedure-Driven Programming Event-Driven Programming Events Messages Event Handlers GUI Windows and Multitasking Queues ( System.
Introduction to Macromedia Director 8.5 – Lingo
Lecture 2Slide 1 Event Driven Computing Basic Interaction Handling –Interactive programs - must pay attention to the user interface.
6/13/20151 CS 160: Lecture 13 Professor John Canny Fall 2004.
IN-LAB # 1 - GETTING STARTED - BUT FIRST: 1.Project ideas - watch the scope!!! 2.Check accounts 3. Either myself or the TA need to check you off BEFORE.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
How to Debug VB .NET Code.
Cosc 4755 Phone programming: GUI Concepts & Threads.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Object Oriented Software Development
Welcome to CIS 083 ! Events CIS 068.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Prepared by Fareeha Lecturer DCS IIUI 1 Windows API.
Getting Started The structure of a simple wxWidgets program, Look at where and how a wxWidgets application starts and ends, how to show the main window,
Overview of Previous Lesson(s) Over View  Visual C++ provides us with 3 basic ways of creating an interactive Windows application  Using the Windows.
Lecture 5: Interaction 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 711,  ex 2271 
Visual Basic .NET BASICS
Chapter 1: Hello, MFC Windows Programming Model Department of Digital Contents Sang Il Park.
Visual Basic for Applications Macro Programming For Microsoft Office.
CSE3AGT Paul Taylor Stupid Conventions! l = Long p = Pointer h = handle g = global wnd = Windows WM = Windows Message d3d = Direct3D hr = HRESULT.
Winsock Programming Blocking and Asynchronous Sockets for Windows.
BZUPAGES.COM Visual Programming Lecture – 2 Miss. SADAF MAJEED SIAL Computer Science Department Bahauddin Zakariya University Multan.
Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
GAM666 – Introduction To Game Programming You must use special libraries (aka APIs – application programming interfaces) to make something other than a.
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 15: GUIs 1.
CNS 1410 Graphical User Interfaces. Obectives Students should understand the difference between a procedural program and an Event Driven Program. Students.
Concurrent Programming and Threads Threads Blocking a User Interface.
Applications Development
Lecture 7 Menu, controls, scroll bars. Menu Menu item sends WM_COMMAND message to the application window Menu is attached to window when the window is.
“The perfect project plan is possible if one first documents a list of all the unknowns.” Bill Langley.
Game Maker – Getting Started What is Game Maker?.
Distributed Virtual Environment and Simulation Package Stephen Lawrence
Registering a window class Windows allows many styles of window to be created. To tell Windows to create a window as you want it you need to define a class.
More on GLUT Programming Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, September 15, 2003.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
GUI-Based Programming ECE 417/617: Elements of Software Engineering Stan Birchfield Clemson University.
Dialog boxes Modal and modeless dialog boxes Displaying about dialog box: case WM_COMMAND : switch (LOWORD (wParam)) { case IDM_APP_ABOUT : DialogBox (hInstance,
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Pop-Up Menus Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Friday, September 26, 2003.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
SCRIPT PROGRAMMING WITH FLASH Introductory Level 1.
Creating a GUI Class An example of class design using inheritance and interfaces.
CHAPTER 2 The Game Loop - Variables, Types, Classes and Objects in XNA XNA Game Studio 4.0.
The WM_NCHITTEST Message  This is an internal message used by Windows for generating all the other mouse messages.  Though it almost never needs to be.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Overview of Previous Lesson(s) Over View  Windows Programming  WinMain()  Where execution of the program begins and basic program initialization is.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Andy Wigley Device Application Development MVP APPA Mundi Ltd SESSION CODE: WEM309.
Our good old first Win32 programme of lecture 8 #include int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
Windows Programming Lecture 14. WM_PAINT message Whenever an application receives the WM_PAINT message, whether the entire window needs repainting? WM_PAINT.
Message Handling in MFC
Topics Graphical User Interfaces Using the tkinter Module
Event Loops and GUI Intro2CS – weeks
PYGAME.
Windows Programming Lecture 10.
Windows Programming Lecture 09.
Window.
Windows Programming Model
Chapter 2: GUI API Chapter 2.
The program in traditional OS
Windows Programming Lecture 13
Event Driven Programming Anatomy – Handle
Tonga Institute of Higher Education
Windows Development Dynadata Copyright, 2014 © DynaData S.A. 1/10.
Graphics Laboratory Korea University
CMSC 202 Exceptions.
GUI Socket Application
Presentation transcript:

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. 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.

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

//////////////////////////////////// //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

//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.

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.

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.

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

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: 1. Setting up the application’s window. 2. Running the message pump.

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).

//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;

//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;

//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);

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.

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…

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(); }

if(PeekMessage(&msg,NULL,0,0,PM_REM OVE)) 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.

//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.

//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