More Win32 API by bzs12927


									        More Win32 API                                             Mouse Messages
                                                       l    Client Area Mouse Messages—
l More Mouse Stuff
                                                            – Mouse msgs generated when mouse moves
l GetSystemMetrics()                                          over the window’s client area
l WM_PAINT Messages                                         – or when pressed/released within window’s
l GetClientRect()                                             client area
l TextOut()
                                                            – 21 messages in all
                                                            – WM_MOUSEMOVE: Sent to window under
l wsprintf()
                                                              cursor when mouse moved
l Using Fonts                                                  • lParam = mouse cursor X,Y position
l GetTextMetrics()                                             • wParam : Mouse notification code
                                                                  – MK_LBUTTON, MK_RBUTTON, MK_SHIFT,
l Example Program                                                   MK_CONTROL

                                                                          Input Focus
                                                       l Window whose caption line is highlighted has
l   * = L, M, R                                          "input focus"
l   # = DOWN, UP, DBLCLK                               l Only this window will receive keyboard input
l   DBLCLK message sent only if                        l Run 2 instances of Winapp2 contains CS_DBLCLKS
                                                            – Note: keyboard accelerators only work with
                                                              instance that has input focus
                                                       l    Input focus not significant for mouse input
                                                       l    Good since mouse is used to activate a

l   When a window gains (loses) input focus:           Nonclient Area Mouse Messages
    – WM_SETFOCUS (WM_KILLFOCUS)                       l                                         è
                                                           Mouse actions in other parts of windowè
      message sent to window
                                                           – WM_NC* messages sent
l   Common responses:                                        • (* = MOUSEMOVE, etc.)
    – highlight an edit area, change a caption, etc.                                  à
                                                           – wParam: HT* hit test codeà
l   SetFocus(hWnd)                                           • non-client area where action occurred
    – Give a window (or a control) the input focus         – lParam: mouse cursor position
l   Response to receiving input focus                      – Usually not processed by applications
    depends on window style                                – Could use to generate other messages
                                                             • e.g., WM_NCLBUTTONDOWN + coordinates à

         Capturing the Mouse                         Getting information on
l To limit mouse to interacting with just             user interface items
  one program                                    l Use:
l e.g., screen capture program                       GetSystemMetrics(nIndex)
l Application that does this has "captured"
                                                     –nIndex specifes which item
  the mouse
                                                     –See online help
l Only it will receive mouse messages.

l Use: SetCapture(hWnd);

l Release with: ReleaseCapture(void);

      WM_PAINT Messages                                      PAINTSTRUCT
                                                typedef struct tagPAINTSTRUCT
l   Sent any time client area is invalidated
    (exposed)                                     {
l   Should redraw everything in exposed area       HDC     hdc;     // device context handle
l   Use BeginPaint(hWnd,&ps) to get a DC           BOOL fErase; // should bkgnd be redrawn? T/F
l   ps is a pointer to a PAINTSTRUCT               RECT rcPaint; // rectangular area to update
     – contains info about area to be redrawn      BOOL fRestore; // reserved for use by Windows
l   Use EndPaint(hWnd,&ps) to release the          BOOL fIncUpdate;           // reserved
    DC                                             BYTE rgbReserved[16]; // reserved
                                                  } PAINTSTRUCT;

       WM_PAINT Message                                 Forcing a WM_PAINT
l   If you want to keep stuff already drawn      l   InvalidateRect (hWnd,&rect,bErase);
    in your window after it’s exposed:               – parameters:
                                                       • window to be invalidated
    – You need to keep track of everything
                                                       • rectangular area (NULL ==> entire client area)
                                                       • background erased (TRUE/FALSE)
    – Then redraw in response to WM_PAINT
                                                 l Causes a WM_PAINT message to be
                                                   placed on the queue
                                                 l This could be done in response to
                                                   mouse & other messages

     Determining Client Area                                      Displaying Text
                                                     l   TextOut(hDC,x,y,lpTxt,cbTxt);
l   GetClientRect(hWnd,&rect)                            – x,y: position on client area of window
    – rect pointer will contain (0,0,width,height)       – lpTxt: string to be displayed
    – You may need to know this                          – cbTxt: length of the string
      • for animations                                   – current DC text color & bkgnd color used
                                                         – current DC font is used
                                                         – can use lstrlen() to get cbTxt
                                                            • for example:
                                                            char cBuf [] = “Hello, World”;
                                                            TextOut (hDC, 0, 0, cBuf , lstrlen(cBuf )) ;

 Displaying Numeric Values                               Using and Changing Fonts
                                                     l   FONT: Typeface, style, size of characters in a
l Must format values into a string                       character set
                                                     l   Three basic kinds of fonts--
l Can use wsprintf()
                                                          – Stock fonts--built into Windows, always
l See online help                                           available
l Example:                                                – Logical or GDI fonts--defined in separate
    char cBuf[50];                                          .fon (stroke or raster) or .fot/.ttf (TrueType)
    int num = 19;                                           font resource files in \windows\system and
                                                            stored on disk
    wsprintf(cBuf, “The number is: %d ", num);
                                                          – Device fonts--native to the output device
    TextOut(hDC, 10, 10, cBuf, lstrlen(cBuf) );
                                                            (e.g., built-in printer fonts).

           Some Stock Fonts                                     Some Stroke Fonts

             Some Raster Fonts                                          Some True Type Fonts

           Using Stock Fonts                                             Using Logical Fonts
                                                                l   Obtain a handle to the font data resource
 l   GetStockObject ()                                              and select it into the DC
     – returns handle to the desired font                           – Just like a stock font, except it's loaded from
     – can be selected into a DC                                      separate file (.fon, .fot/.ttf).
     HDC     hDC;                                                   – Use CreateFont() instead of GetStockObject()
     HFONT hFont;                                                     to load and get a font handle.
     hDC = GetDC(hWnd);                                             – CreateFont() makes new fonts by
     hFont = GetStockObject (ANSI_VAR_FONT);                          interpolating data in a font file
                                                                       • ==> New sizes, bold/underlined, rotated/distorted
     SelectObject (hDC,hFont);
                                                                       • Called logical since they come from program logic
                                                                         not just from a file

                  CreateFont()                                          Escapement & Orientation
hFont = CreateFont (Ht, Width, Escapement,
  Orientation, Weight, Italic, Underline, StrikeOut,
  CharSet, OutputPrecision, ClipPrecision, Quality,
  PitchAndFamily, Facename);
  – 14 parameters, many are often set to 0 ==> defaults
  – See the on-line help on CreateFont ():
Example call to CreateFont()--
  hFont = CreateFont (36, 0, 3000, 0, 0, 0, 0, 0, 0, 0, 0, 0,

    Determining Character Sizes                               FONT1 Example Program
l   With CreateFont(), may not get what you want
                                                      l   User types ==> blue text in client area
l   Use GetTextMetrics(hDC,lpTextmetric)              l   Can change font from menu
    – See online help                                 l   Backspace editing feature
                                                      l   cBuf[] builds text string as it's input
                                                      l   WM_CHAR message received ==> character
                                                          tested & appended to cBuf if:
                                                           – Character is alphanumeric
                                                             • IsCharAlphaNumeric()
                                                          – Or character is punctuation
                                                             • if helper function IsAnsiPunc() returns TRUE
                                                          – And cBuf[] hasn't been filled

    l   To display, force a WM_PAINT message          l WM_CHAR for printable characters
        – InvalidateRect()                            l WM_KEYDDOWN for Backspace
    l Response: draw cBuf[] string                    l IsAnsiPunc()--a helper function that
    l Also string will be redrawn automatically         tests ranges of ANSI codes for
      after exposure (resizing, uncovering)             punctuation characters
                                                      l   WM_CREATE when program starts -->
                                                          – Use CreateFont() to create new Roman font &
                                                            save handle in hFont
                                                      l   WM_COMMAND to choose font from a
                                                          popup menu (set nFontChoice variable)

    WM_PAINT message:
    1. Get a DC with BeginPaint()
    2. Change color to blue
    3. Check value of nFontChoice
    4. SelectObject() to select chosen font into DC
    5. TextOut() to output the cBuf[] string
    6. Release DC with EndPaint()

    Note use of static variables to “remember”
      variable values from one WndProc() callback
      to another


To top