We display some text in the middle of a window, and see how the text remains there whenever the window is re-sized or moved.

Similar documents
Window programming. Programming

Game Programming I. Introduction to Windows Programming. Sample Program hello.cpp. 5 th Week,

hinstance = ((LPCREATESTRUCT)lParam)->hInstance obtains the program's instance handle and stores it in the static variable, hinstance.

Windows and Messages. Creating the Window

Windows Programming. 1 st Week, 2011

Chapter 15 Programming Paradigm

/*********************************************************************

We take a look at implementing some file menu options: New, Open, Save, Save As and Exit.

Course 3D_OpenGL: 3D-Graphics with C++ and OpenGL Chapter 1: Moving Triangles

Alisson Sol Knowledge Engineer Engineering Excellence June 08, Public version

Designing Interactive Systems II

Advantech Windows CE.net Application Hand on Lab

Introduction to Computer Graphics (CS602) Lecture No 04 Point

Tutorial 7: Mouse Input

Programming in graphical environment. Introduction

Getting Started. 1 st Week, Sun-Jeong Kim. Computer Graphics Applications

Programmer s Manual MM/104 Multimedia Board

Aspect-Oriented Programming with C++ and AspectC++ AOSD 2007 Tutorial. Part V Examples. Examples V/1

LSN 4 GUI Programming Using The WIN32 API

2. On the main menu, click File -> New... or File -> New -> Other... Windows Fundamentals

Tutorial 9:Child Window Controls

Task Toolkit Manual for InduSoft Web Studio v6.1+sp3

Windows Printer Driver User Guide for NCR Retail Printers. B Issue G

4 th (Programmatic Branch ) Advance Windows Programming class

Tutorial 3: A Simple Window

Using DAQ Event Messaging under Windows NT/95/3.1

Computer Programming Lecture 11 이윤진서울대학교

CS410 Visual Programming Solved Online Quiz No. 01, 02, 03 and 04. For Final Term Exam Preparation by Virtualians Social Network

An Introduction to Windows Programming Using VC++ Computer Graphics. Binghamton University. EngiNet. Thomas J. Watson

Modern GUI applications may be composed from a number of different software components.

IFE: Course in Low Level Programing. Lecture 5

Hands-On Lab. Multitouch Gestures - Native. Lab version: Last updated: 12/3/2010

Guide to Good Practice in using Open Source Compilers with the AGCC Lexical Analyzer

Review. Designing Interactive Systems II. The Apple Macintosh

MOBILE COMPUTING Practical 1: Graphic representation

Win32 Multilingual IME Overview for IME Development

: Lập trình Win32. Lập trình Win32 API. vncoding.net Page 1. Date : 07/09/2014

Qno.2 Write a complete syantax of "Getparents" functions? What kind of value it return and when this function is use? Answer:-

systemove programovanie win32 programovanie

CS410. Note: VuGujranwala.com is not responsible for any solved solution, but honestly we are trying our best to Guide correctly.

Question No: 1 (Marks: 1) - Please choose one For showing Dialog we can use "ShowWindow(...)" function.

Input and Interaction

Creating a DirectX Project

Different Ways of Writing Windows Programs

HANNAH HOWARD #ABOUTME

Abstract. MSc. Electronics: Computer Graphics. Sept Abstract.

Developing a Prototype Window s Mobile Application

Windows Programming in C++

CS410 Visual Programming Solved Objective Midterm Papers For Preparation of Midterm Exam

. DRAFT version 0.681

Front Panel: Free Kit for Prototyping Embedded Systems on Windows

CIS 488 Programming Assignment 14 Using Borland s OWL

Multithreading Multi-Threaded Programming. Prof. Lin Weiguo Copyleft 2009~2017, School of Computing, CUC

Introducing MFC. Programming Windows with MFC, Second Edition. Jeff Prosise

Hands-On Lab. Multi-Touch WMTouch - Native. Lab version: Last updated: 12/3/2010

Mobile Application Development (Part 1)-Program a Sound-Monitoring Prototype

Programming In Windows. By Minh Nguyen

Windows Programming. Krzysztof Mossakowski, MSc

Lecture 4. Drawing - coordinate systems Advanced drawing - bitmaps, regions, clipping, metafiles

On Typesafe Aspect Implementations in C++

Application Note QWinTM GUI Kit for Prototyping Embedded Systems on Windows

Graphics Software. Interaction schemes. Input handling. Graphics application interfaces. Output handling. General scheme of CG applications

Learning to Draw Basic Graphics

Game Programming Genesis Part II : Using Resources in Win32 Programs by Joseph "Ironblayde" Farrell

int fnvgetconfig(handle h, UINT32 id, const void *cfg, size_t sz);... 4

In this lesson you are going to create a drawing program similar to Windows Paint. 1. Start with a new project and remove the default cat sprite.

Using Windows XP Visual Styles

Graphics Software. Interaction schemes. General scheme of CG applications. Balázs Csébfalvi

Labels and Envelopes in Word 2013

Game Programming Genesis Part III : Tracking Your Window and Using GDI by Joseph "Ironblayde" Farrell

arxiv: v1 [physics.med-ph] 22 Aug 2016

Animated 3D Worlds Using Java and SketchUp Chonmaphat Roonnapak

Digital Storage Oscilloscope. Appendix

Detecting USB Device Insertion and Removal Using Windows API

SHORT QUESTIONS & ANSWERS

SFU CMPT Topic: More MFC Programming

Based on the following Circuit Cellar Articles. Developing a Mobile Phone Application. The story Spark podcast 49. The articles

CMSC434. Introduction to Human-Computer Interaction. Week 10 Lecture 17 Mar 31, 2016 Engineering Interfaces III. Jon

CS1253-VISUAL PROGRAMMING S4 CSE & S6 IT 2 MARKS & 16 MARKS PREPARED BY S.VINILA JINNY, L/CSE Y. JEYA SHEELA, L/IT

Events. Dispatch, event-to-code binding. Review: Events Defined 1/17/2014. occurrence.

LightningChart Quick Reference

WAIPA DISTRICT COUNCIL. Maps Online 9. Updated January This document contains an overview of IntraMaps/Maps Online version 9.

CISC 1600, Lab 3.1: Processing

ITEC185. Introduction to Digital Media

Clip Art and Graphics. Inserting Clip Art. Inserting Other Graphics. Creating Your Own Shapes. Formatting the Shape

How to...create a Video VBOX Gauge in Inkscape. So you want to create your own gauge? How about a transparent background for those text elements?

Word 2007: Flowcharts Learning guide

CISC 1600, Lab 2.1: Processing

Undocumented CreateProcess

PowerPoint Introduction. Video: Slide Basics. Understanding slides and slide layouts. Slide Basics

CS 2110 Fall Instructions. 1 Installing the code. Homework 4 Paint Program. 0.1 Grading, Partners, Academic Integrity, Help

BUSINESS PROCESS DOCUMENTATION. Presented By Information Technology

Message-Passing Interface for Microsoft Windows 3.1

uvi ... Universal Validator Interface Software Developers Kit Revision /29/04 Happ Controls

The Fundamentals. Document Basics

Charles River Media. A part of Course Technology, Cengage Learning

Windows Sockets: A Quick And Dirty Primer

INFORMATION TECHNOLOGY

mdolphin Plug-in Programming Guide Version 2.0 For mdolphin Version 2.0

Word 3 Microsoft Word 2013

Transcription:

1 Programming Windows Terry Marris January 2013 2 Hello Windows We display some text in the middle of a window, and see how the text remains there whenever the window is re-sized or moved. 2.1 Hello Windows The output from the hellowin program is A window class structure is initialised. A window is created based on this structure and a message loop is set up. A windows procedure manages the message queue and processes selected messages. Here is the entire source code: /* hellowin.c - Displays "Hello, Windows" in the client area */ #include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprevinst, PSTR cmdline, int cmdshow) static char appname[] = "HelloWin"; HWND hwnd; MSG msg; WNDCLASS wc; wc.style = CS_HREDRAW CS_VREDRAW; wc.lpfnwndproc = WndProc; wc.cbclsextra = 0; wc.cbwndextra = 0; wc.hinstance = hinst; wc.hicon = NULL; wc.hcursor = LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED); wc.hbrbackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

2 wc.lpszmenuname = NULL; wc.lpszclassname = appname; RegisterClass(&wc); hwnd = CreateWindow(appName, "The Hello Windows Program", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hinst, NULL); ShowWindow(hwnd, cmdshow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) TranslateMessage(&msg); DispatchMessage(&msg); return msg.wparam ; LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) HDC hdc; PAINTSTRUCT ps; RECT rect; switch (msg) case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect); DrawText(hdc, "Hello, Windows", -1, &rect, DT_SINGLELINE DT_CENTER DT_VCENTER); EndPaint(hwnd, &ps); case WM_DESTROY: PostQuitMessage(0); return DefWindowProc(hwnd, msg, wparam, lparam); 2.2 The Window Class A window is always based on a window class. A window class identifies the procedure that processes messages to the window. That procedure is named WndProc in our example.

3 And the messages include WM_PAINT and WM_DESTROY. WndProc and the messages are discussed below. static char appname[] = "HelloWin"; WNDCLASS wc; wc.style = CS_HREDRAW CS_VREDRAW; wc.lpfnwndproc = WndProc; wc.cbclsextra = 0; wc.cbwndextra = 0; wc.hinstance = hinst; wc.hicon = NULL; wc.hcursor = LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED); wc.hbrbackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszmenuname = NULL; wc.lpszclassname = appname; A WNDCLASS structure has ten members. wc.style = CS_HREDRAW CS_VREDRAW; says that the entire window is to be redrawn if it changes in size, either horizontally or vertically. CS stands for Class Style. is the C bitwise or operator. wc.lpfnwndproc = WndProc; WindProc, shown above and discussed below, is the function that draws the text in the middle of the window. lpfn stands for long pointer to a function. And the type of lpnfnwndproc is WNDPROC. wc.cbclsextra = 0; wc.cbwndextra = 0; cbclsextra and cbwndextra are both set to 0. These reserve extra space in both the class and windows structures if required by the program. Both are ints. They are not used here. wc.hinstance = hinst; The field hinstance is set to the parameter hinst. hinstance is a handle that uniquely identifies the program at run time. wc.hicon = NULL; hicon represents the little icon shown on the left side of the title bar. If its value is NULL then Windows provides a default icon. Its type is HICON, a handle to an icon. WNDCLASSEX, an extended version of WNDCLASS, would be used if you want to set an icon. The cursor is set with wc.hcursor = LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED); NULL indicates that the image is to be loaded from a standalone resource, in this case, a standard system cursor. IDC_ARROW is the standard arrow-shaped cursor.

4 IMAGE_CURSOR says that the image is a cursor. The 0, 0, means that the actual width and height of the cursor is used. And LR_SHARED must be specified to load a system cursor. LoadImage() returns a handle to a cursor, or NULL if it fails. The background colour is set to white with wc.hbrbackground = (HBRUSH)GetStockObject(WHITE_BRUSH); GetStockObject() returns a handle to the requested object, or NULL if it fails for any reason. wc.lpszmenuname = NULL; says there is no menu set. wc.lpszclassname = appname; sets the class name to HelloWin. appname is also used in the call to CreateWindow - see below. The class is registered with the Windows operating system by RegisterClass(&wc); and made ready for a subsequent call to CreateWindow. You would use RegisterClassEx if you wanted to set the small window icon. RegisterClass returns zero on error. 2.3 The Window A window is created by a call to CreateWindow, and displayed by a call to ShowWindow. Windows are identified by a handle, an integer that uniquely identifies the window. HWND is the handle-to-a-window type. CreateWindow returns a handle to a window, or NULL if it fails. HWND hwnd; hwnd = CreateWindow( appname, /* the class name */ "The Hello Program", /* title bar text */ WS_OVERLAPPEDWINDOW, /* window style */ CW_USEDEFAULT, CW_USEDEFAULT, /* position of top left corner */ CW_USEDEFAULT, CW_USEDEFAULT, /* window width and height */ NULL, /* parent window */ NULL, /* menu */ hinstance, /* program instance handle */ NULL); /* additional creation parameters */ The class name defines the window type. It could be a programmer-defined type, the class name as used here, or a pre-defined type such as BUTTON, COMBOBOX or EDIT depending on the purpose. The window style WS_OVERLAPPED gives the window a title bar, a system menu, a border, and minimise, maximise and close buttons.

5 The Create Window identifier CW_USEDEFAULT means a value chosen by Windows itself is used to position and size the window. CreateWindow returns NULL on error. ShowWindow(hwnd, cmdshow); sets the given window's display state and displays the window. hwnd is the handle to the window previously created. cmdshow determines how the window is to be initially shown, for example, normally, or minimised. If the value of cmdshow is SW_SHOWNORMAL the client area of the window is erased with the background brush specified in the window class. The client area is then repainted with UpdateWindow(hwnd); by sending a WM_PAINT message to the Windows procedure WndProc. The client area is the part of a window below the title bar. UpdateWindow returns zero on error. 2.4 The Message Loop Events such as mouse clicks and keystrokes generate messages. Windows holds these messages in the message queue. GetMessage retrieves the next message from the queue. TranslateMessage translates keyboard keystrokes and DispatchMessage passes messages to the registered window procedure, WndProc. The message structure, MSG, looks like this: typedef struct tagmsg HWND hwnd; UINT message; WPARAM wparam; LPARAM lparam; DWORD time; POINT pt; MSG; where POINT is typedef strict tagppoint LONG x; LONG y; POINT; DWORD stands for double size WORD, where a WORD is 16 bits, an unsigned integer. And LONG is a long integer. Messages in the form of an integer or a handle are usually passed in a WPARAM object, and messages in the form of a pointer are usually passed in a LPARAM object. MSG msg; while (GetMessage(&msg, NULL, 0, 0)) TranslateMessage(&msg); DispatchMessage(&msg); return msg.wparam ;

6 In GetMessage, msg is the message structure, NULL specifies any current window, and the 0, 0 specify that no message filters are used. A WM_QUIT Windows Message, generated, for example by clicking on a window's close icon, terminates the loop and the wparam member of the message structure is returned. 2.5 The Window Procedure The windows procedure, named WinProc in our example, determines how the window responds to user input. It is referred to when the window class is created, and it is called automatically by DispatchMessage. You could have several windows based on the window class. The function header is LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) LRESULT is an integer returned to Windows and contains the response to a message. CALLBACK is the calling convention for the function. The four parameters are identical to the first four fields of the MSG structure. hwnd is the handle to the window receiving the message. This is the same handle returned by a call to CreateWindow. UINT is an unsigned int. msg identifies the message code. For example, the WM_SIZE message indicates the window was resized. wparam and lparam both contain more information about the message. wparam usually contains a handle or an integer. lparam usually contains a pointer. 2.7 Window Messages Window Messages, such as WM_PAINT and WM_DESTROY, are identified by a number contained within the msg parameter to WndProc. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) HDC hdc; PAINTSTRUCT ps; RECT rect; switch (msg) case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect); DrawText(hdc, "Hello, Windows", -1, &rect, DT_SINGLELINE DT_CENTER DT_VCENTER);

7 EndPaint(hwnd, &ps); case WM_DESTROY: PostQuitMessage(0); return DefWindowProc(hwnd, msg, wparam, lparam); HDC is a handle to a device context. A device context refers to an actual output device, such as a video display, and its driver. PAINTSTRUCT contains information that is used to paint a client area. RECT is a structure that defines the top left and bottom right corners of a rectangle. Messages are usually processed by a switch-with-case statement. switch (msg) case WM_PAINT: case WM_DESTROY: A WM_PAINT is a message to repaint the window. The process begins with a call to BeginPaint and ends with a call to EndPaint. hdc = BeginPaint(hwnd, &ps); EndPaint(hwnd, &ps); BeginPaint fills the paint structure ps and prepares the window for painting. BeginPaint returns NULL on failure, indicating that no device context was available. GetClientRect retrieves the coordinates of the client area and returns zero on failure. Now we come to DrawText(hdc, "Hello, Windows", -1, &rect, DT_SINGLELINE DT_CENTER DT_VCENTER); DrawText writes formatted text in the given rectangle. Here, hdc is the handle to a device context, Hello, Windows is the text to be displayed, -1 means the text is terminated with the null character, &rect is a pointer to the rectangle, DT_SINGLELINE DT_CENTER DT_VCENTER is how the text is to be formatted, on a single line in the middle of the client area. DT stands for Draw Text. DrawText returns zero on error. The WM_DESTROY message indicates that the user has clicked on the window close button or has selected close from the program's system menu. PostQuit(0) inserts a WM_QUIT message into the message queue. When GetMessage retrieves this WM_QUIT message, GetMessage returns zero and the message loop terminates. The final statement in WndProc

8 return DefWindowProc(hwnd, msg, wparam, lparam); deals with all the messages not processed by WndProc. Its arguments are the same as WndProc's parameters. 2.8 Size and Complexity To manage size and complexity, we usually put the logic for handling each message into a separate function, something like this: void CALLBACK printhello(hwnd hwnd) HDC hdc; PAINTSTRUCT ps; RECT rect; hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect); DrawText(hdc, "Hello, Windows", -1, &rect, DT_SINGLELINE DT_CENTER DT_VCENTER); EndPaint(hwnd, &ps); LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) switch (msg) case WM_PAINT: printhello(hwnd); case WM_DESTROY: PostQuitMessage(0); return DefWindowProc(hwnd, msg, wparam, lparam); CALLBACK is the calling convention that deals with arguments in a Win32-specific way. You could (should?) use it whenever your function makes Win32 function calls, or when your function uses a Win32 parameter type.