How to Print Text to MDI Child Custom Control with Windows API

ID Number: Q80909

1.00

WINDOWS

Summary:

Because there is no Print method for a Visual Basic Professional

Toolkit MDI Child custom control, you must use Windows API calls to

send the text to the MDI child window. This article describes how to

send text and specify certain colors of text to MDI Child controls by

using the API calls SetTextColor and TextOut.

This information applies to Microsoft Professional Toolkit for

Microsoft Visual Basic programming system version 1.0 for Windows.

More Information:

The following API calls are used in this example:

API Description

--- -----------

GetDC This function retrieves a handle to a display context for

the client area of the given window. The display context

can be used in subsequent GDI functions to draw in the

client window.

ReleaseDC This function releases device context so it can be used

by other applications.

GetFocus This function retrieves the handle of the window that

currently has the focus.

TextOut This function writes a character to the current display

using the currently selected font.

SetTextColor This function sets the text color to the color specified.

Code Example

------------

To create a form with an MDI Child control, do the following:

1. Start Visual Basic. Form1 will be created by default.

2. From the File menu, choose Add File, and select the MDICHILD.VBX

custom control file. The MDI Child tool will appear in the Toolbox.

3. Click the MDI Child tool in the Toolbox, and draw an MDI Child

control on your form (Form1).

4. Add the following declarations in the global module or in the

general Declarations section of the form you want to call them

from:

Note: The following Declare statements must each be on one line.

Declare Function GetDC Lib "user" (ByVal hWnd%) As Integer

Declare Function ReleaseDC Lib "User"

(ByVal hWnd As Integer, ByVal hdc As Integer)

As Integer

Declare Function TextOut Lib "gdi"

(ByVal hdc As Integer, ByVal x As Integer,

ByVal y As Integer, ByVal text$, ByVal ncount

As Integer) As Integer

Declare Function SetTextColor Lib "gdi"

(ByVal hdc As Integer, ByVal crcolor As Long)

As Long

5. Double-click Form1 to bring up the Code window for Form1.click, and

add the following code:

Sub Form1_Click()

hDec% = GetDC(MDIChild1.hwnd)

' Set the text color to blue

' Note the value returned by the function is the previous

' color and should be returned

oldcolor! = SetTextColor(hDec%, RGB(0, 0, 255))

' Sends the text out to the MDIChild control

bool% = TextOut(hDec%, 5, 10, "Do right, fear not", 13)

' Release device context

bool% = ReleaseDC(hWind%, hDec%)

' Reset color to previous

oldcolor! = SetTextColor(hDec%, oldcolor!)

End Sub

6. From the Run menu, choose Start to run the program.

7. Click the form. Your text will appear on the MDI Child control.

Note: You can move where the text is printed by changing x and y

coordinates in the TextOut call. Also, different RGB values will

cause different color text to be printed.

Additional reference words: 1.00