DRAWTEXTPARAMS
The DRAWTEXTPARAMS structure contains extended formatting options for the DrawTextEx function.typedef struct { 
    UINT cbSize; 
    int  iTabLength; 
    int  iLeftMargin; 
    int  iRightMargin; 
    UINT uiLengthDrawn; 
} DRAWTEXTPARAMS, FAR *LPDRAWTEXTPARAMS; 
 
Members
cbSize 
Specifies the structure size, in bytes. 
iTabLength 
Specifies the size of each tab stop, in units equal to the average character width. 
iLeftMargin 
Specifies the left margin, in units equal to the average character width. 
iRightMargin 
Specifies the right margin, in units equal to the average character width. 
uiLengthDrawn 
Receives the number of characters processed by DrawTextEx, including white-space characters. The number can be the length of the string or the index of the first line that falls below the drawing area. Note that DrawTextEx always processes the entire string if the DT_NOCLIP formatting flag is specified. 
QuickInfo
  Windows NT: Requires version 4.0 or later.
  Windows: Requires Windows 95 or later.
  Windows CE: Unsupported.
  Header: Declared in winuser.h.

解决方案 »

  1.   

    DrawTextEx
    The DrawTextEx function draws formatted text in the specified rectangle.int DrawTextEx(
      HDC hdc,                     // handle to device context
      LPTSTR lpchText,             // pointer to string to draw
      int cchText,                 // length of string to draw
      LPRECT lprc,                 // pointer to rectangle coordinates
      UINT dwDTFormat,             // formatting options
      LPDRAWTEXTPARAMS lpDTParams  // pointer to struct with options
    );
     
    Parameters
    hdc 
    Handle to the device context to draw in. 
    lpchText 
    Pointer to the string to draw. The string must be null-terminated if the cchText parameter is –1. 
    If dwDTFormat includes DT_MODIFYSTRING, the function could add up to four additional characters to this string. The buffer containing the string should be large enough to accommodate these extra characters. cchText 
    Specifies the length, in characters, of the string specified by the lpchText parameter. If the string is null-terminated, this parameter can be –1 to calculate the length. 
    lprc 
    Pointer to a RECT structure that contains the rectangle, in logical coordinates, in which the text is to be formatted. 
    dwDTFormat 
    Specifies formatting options. This parameter can be one or more of these values: Value Meaning 
    DT_BOTTOM Justifies the text to the bottom of the rectangle. This value must be combined with DT_SINGLELINE. 
    DT_CALCRECT Determines the width and height of the rectangle. If there are multiple lines of text, DrawTextEx uses the width of the rectangle pointed to by the lprc parameter and extends the base of the rectangle to bound the last line of text. If there is only one line of text, DrawTextEx modifies the right side of the rectangle so that it bounds the last character in the line. In either case, DrawTextEx returns the height of the formatted text, but does not draw the text. 
    DT_CENTER Centers text horizontally in the rectangle. 
    DT_EDITCONTROL Duplicates the text-displaying characteristics of a multiline edit control. Specifically, the average character width is calculated in the same manner as for an edit control, and the function does not display a partially visible last line. 
    DT_END_ELLIPSIS or DT_PATH_ELLIPSIS Replaces part of the given string with ellipses, if necessary, so that the result fits in the specified rectangle. The given string is not modified unless the DT_MODIFYSTRING flag is specified.
    You can specify DT_END_ELLIPSIS to replace characters at the end of the string, or DT_PATH_ELLIPSIS to replace characters in the middle of the string. If the string contains backslash (\) characters, DT_PATH_ELLIPSIS preserves as much as possible of the text after the last backslash.
     
    DT_EXPANDTABS Expands tab characters. The default number of characters per tab is eight. 
    DT_EXTERNALLEADING Includes the font external leading in line height. Normally, external leading is not included in the height of a line of text. 
    DT_INTERNAL Uses the system font to calculate text metrics.  
    DT_LEFT Aligns text to the left. 
    DT_MODIFYSTRING Modifies the given string to match the displayed text. This flag has no effect unless the DT_END_ELLIPSIS or DT_PATH_ELLIPSIS flag is specified. 
    DT_NOCLIP Draws without clipping. DrawTextEx is somewhat faster when DT_NOCLIP is used. 
    DT_NOPREFIX Turns off processing of prefix characters. Normally, DrawTextEx interprets the ampersand (&) mnemonic-prefix character as a directive to underscore the character that follows, and the double ampersand (&&) mnemonic-prefix characters as a directive to print a single ampersand. By specifying DT_NOPREFIX, this processing is turned off. 
    DT_RIGHT Aligns text to the right. 
    DT_RTLREADING Layout in right to left reading order for bi-directional text when the font selected into the hdc is a Hebrew or Arabic font. The default reading order for all text is left to right. 
    DT_SINGLELINE Displays text on a single line only. Carriage returns and linefeeds do not break the line. 
    DT_TABSTOP Sets tab stops. The DRAWTEXTPARAMS structure pointed to by the lpDTParams parameter specifies the number of average character widths per tab stop. 
    DT_TOP Top justifies text (single line only). 
    DT_VCENTER Centers text vertically (single line only). 
    DT_WORDBREAK Breaks words. Lines are automatically broken between words if a word extends past the edge of the rectangle specified by the lprc parameter. A carriage return-linefeed sequence also breaks the line. 
    DT_WORD_ELLIPSIS Truncates text that does not fit in the rectangle and adds ellipses.  
    dwDTParams 
    Pointer to a DRAWTEXTPARAMS structure that specifies additional formatting options. This parameter can be NULL. 
    Return Values
    If the function succeeds, the return value is the text height.If the function fails, the return value is zero. Windows NT: To get extended error information, callGetLastError.QuickInfo
      Windows NT: Requires version 4.0 or later.
      Windows: Requires Windows 95 or later.
      Windows CE: Unsupported.
      Header: Declared in winuser.h.
      Import Library: Use user32.lib.
      Unicode: Implemented as Unicode and ANSI versions on Windows NT.See Also
    Fonts and Text Overview, Font and Text Functions, DrawText, DRAWTEXTPARAMS 
      

  2.   

    'In a module
    Public Const DT_CENTER = &H1
    Public Const DT_WORDBREAK = &H10
    Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    Declare Function DrawTextEx Lib "user32" Alias "DrawTextExA" (ByVal hDC As Long, ByVal lpsz As String, ByVal n As Long, lpRect As RECT, ByVal un As Long, ByVal lpDrawTextParams As Any) As Long
    Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    Global Cnt As Long, sSave As String, sOld As String, Ret As String
    Dim Tel As Long
    Function GetPressedKey() As String
        For Cnt = 32 To 128
            'Get the keystate of a specified key
            If GetAsyncKeyState(Cnt) <> 0 Then
                GetPressedKey = Chr$(Cnt)
                Exit For
            End If
        Next Cnt
    End Function
    Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
        Ret = GetPressedKey
        If Ret <> sOld Then
            sOld = Ret
            sSave = sSave + sOld
        End If
    End Sub'In a form
    Private Sub Form_Load()
        'KPD-Team 1999
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        Me.Caption = "Key Spy"
        'Create an API-timer
        SetTimer Me.hwnd, 0, 1, AddressOf TimerProc
    End Sub
    Private Sub Form_Paint()
        Dim R As RECT
        Const mStr = "Start this project, go to another application, type something, switch back to this application and unload the form. If you unload the form, a messagebox with all the typed keys will be shown."
        'Clear the form
        Me.Cls
        'API uses pixels
        Me.ScaleMode = vbPixels
        'Set the rectangle's values
        SetRect R, 0, 0, Me.ScaleWidth, Me.ScaleHeight
        'Draw the text on the form
        DrawTextEx Me.hDC, mStr, Len(mStr), R, DT_WORDBREAK Or DT_CENTER, ByVal 0&
    End Sub
    Private Sub Form_Resize()
        Form_Paint
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        'Kill our API-timer
        KillTimer Me.hwnd, 0
        'Show all the typed keys
        MsgBox sSave
    End Sub