Windows Basics - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Windows Basics

Description:

NULL, //parent window handle. NULL, // window menu handle. hInstance, // program instance handle ... Have to trap WM_CLOSE message as discussed on Web. ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 16
Provided by: phi140
Category:
Tags: basics | parent | the | trap | windows

less

Transcript and Presenter's Notes

Title: Windows Basics


1
Windows Basics
  • Windows is a 32 bit, preemptive, multitasking,
    multithreaded, graphical operating system.
  • Preemptive multitasking multiple programs
    executing, OS time slices all programs.
  • Multithreaded Program decomposed into multiple
    threads, threads are time sliced.

2
Windows
  • Implemented as a set of dynamic link libraries
    (dlls).
  • dlls are not loaded into executable.
  • Provide the Application Program Interface (API).
  • API dictates event-driven (asynchronous) program
    architecture.
  • Reacts to messages sent by OS or application.

3
Program Architecture
  • Logically message driven
  • OS creates one message queue for application.
  • Logically, OS places messages in queue.
  • Application provides message loop to retrieve
    messages from queue.
  • Polling for messages.
  • Causes messages to be processed.
  • Lots of messages sent to program, most of which
    the program does not process.

4
Program Architecture
  • If interested in event specified by a message,
    provide a handler.
  • Example Application receives WM_PAINT message
    every time the window needs to be updated.
  • Provide handler.
  • Another example Application receives a WM_NCHIT
    message every time the mouse moves onto
    non-client piece of the window.
  • May receive one per pixel.
  • Must provide default handler for every message
    not processed.

5
Default Message Processing
  • switch(msg_type)
  • case WM_PAINT // do something. Then
    return
  • case WM_SIZE //do something. Then return.
  • ..
  • return DefWindowProc(hwnd, message, wParam,
    lParam)
  • This catches all messages not caught by
    handlers.

6
Default Message Processing
  • Default processing normally generates other
    messages that are also sent to application.
  • Such messages also processed through default
    processing function. Generates other messages,
    and so forth.
  • Point There is a whole lot going on under the
    covers that the application programmer will never
    deal with.

7
Unicode
  • Characters are 8 bits 256 combinations.
  • Unicode extends to 16 bits 65,536 comb.
  • Use TEXT(this is a string) macro.
  • TCHAR Pointer to a string.
  • Static TCHAR grumpy I feel grumpy

8
No Printf or Cout
  • Windows does not understand sdtin, stdout.
  • wsprintf(buf, format string, variables)
  • char buf100
  • int a,b,c
  • a 10 b 2 c a b
  • wsprintf(buf, The sum of a d and b d is
    d, a, b, c)
  • s, c, f
  • Use with TextOut function. Significantly more
    powerful than printf, cout, ..

9
Windows Programs
  • All windows based on class, each class has
    exactly one message processing function.
  • Provide handlers for messages of interest.
  • All others handled by default processing
    function.
  • Message passing is really the OS calling the
    message processing function for the window.
  • Messages can be placed in message queue, or the
    OS can call window procedure directly.

10
Function Prototypes
  • include ltwindows.hgt
  • LRESULT CALLBACK WndProc (HWND, UINT, WPARAM,

    LPARAM)
  • Int WinAPI WinMain(HINSTANCE hInstance,
  • HINSTANCE hPrevInstance,
  • PSTR szCmdLine,
  • int iCmdShow)

11
Variable Declarations
  • static TCHAR szAppName TEXT(HelloWin)
  • HWND hwnd
  • MSG msg
  • WNDCLASS wndclass

12
Fill in Class Characteristics
  • wndclass.style CS_HREDRAW CS_VREDRAW
  • wndclass.lpfnWndProc WndProc
  • .
  • wndclass.hInstance hInstance
  • wndclass.hIcon LoadIcon(NULL,
    IDI_APPLICATION)
  • wndclass.hCursor LoadCursor(NULL, ID_ARROW)
  • wndclass.hbrBackground (HBRUSH)
  • GetStockObject(WHITE_BRUSH)
  • wndclass.lpszClassName szAppName

13
Register Class
  • if (!RegisterClass (wndclass))
  • MessageBox (..)
  • return 0

14
Create Window
  • hwnd CreateWindow(szAppName, //set above
  • TEXT(The Hello Program), //caption
  • WS_OVERLAPPEDWINDOW, //style
  • CW_USEDEFAULT, // initial x
  • CW_USEDEFAULT, // initial y
  • CW_USEDEFAULT, // initial x size
  • CW_USEDEFAULT, // initial y size
  • NULL, //parent window handle
  • NULL, // window menu handle
  • hInstance, // program instance
    handle
  • NULL // creation parameters)

15
Lab 2
  • Declare an array of size WM_USER to track
    messages sent but not processes.
  • Element 0 of array tracks number of messages of
    type 0 (if message 0 is not processed by
    application), element 1 tracks number of messages
    of type 1and so forth.
  • Print out the top 5 such messages.
  • Scan the array (at least) 5 times to pick out
    max.
  • Have to trap WM_CLOSE message as discussed on Web.
Write a Comment
User Comments (0)
About PowerShow.com