Search the web
Sign In
New User? Sign Up
fdb · FDB's "Advanced Programming" list
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Sending Messages   Message List  
Reply | Forward Message #92 of 103 |
Sending Messages
Found at http://www.vbweb.co.uk/show.asp?id=46

Introduction
Visual C++ revolves around messages. Messages are sent to controls from
windows giving them information and instructing them to do things. Messages
are also sent from controls to windows, and to other controls. Being able to
send messages to controls allows you to do much more than using Visual Basic
procedures. Messages can be sent using the SendMessage function in the
user32.dll. To use it, simply enter the following declaration into a form.
If you want to enter it into a module, and use it throughout the project,
change the Private to Public.

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
Any) As Long

All you need to know to send a message to a control is its hWnd property and
the command you want to send to it. To find the command you want to use,
load up the API Text Viewer, and select Constants from the API Type list
box. Constants that begin with WM_ tell the control to do something, EM_ to
return a value.

The advantage of being able to send these commands is that you are using the
controls code, which you presume is bug free, rather than your own. This
will save space as well as time. Of course if you want to do something the
control can't do you have to write your own code still.


Simple Commands
All the commands that tell a control to do something begin with WM_ so enter
WM_ into the text box, and it will bring you to the point in the list with
all those constants in. One of the easiest things to try out sending
messages on is a text box. There are constants that will allow you to
perform basic functions by sending a command to a text box, or Rich Text Box
to Cut, Copy, Paste, Clear and Undo. These are WM_CUT, WM_COPY, WM_PASTE,
WM_CLEAR and WM_UNDO. Their values are listed below:

Const WM_CUT = &H300 ' Cut the selected text to the clipboard (Ctrl+X)
Const WM_COPY = &H301 ' Copy the selected text to the clipboard (Ctrl+C)
Const WM_PASTE = &H302 ' Pastes the text from the clipboard (Ctrl+V)
Const WM_CLEAR = &H303 ' Clears the selected text (Del)
Const WM_UNDO = &H304 ' Undos the last action (Ctrl+Z)

To send a command to the text box to do any of these things, use the
following syntax:

SendMessage(TextBoxName.hWnd, Constant, 0, 0& )

Where TextBoxName is the name of your text box, and Constant is one of the
constants listed above. The other two properties are set to 0 and 0& (Null)
as in this case we are telling the TextBox to do something, not to set a
property and so they are not needed. The following example cuts the text
from Text1 onto the Clipboard, when Command1 is pressed:

Private Const WM_COPY = &H301
Private Const WM_PASTE = &H302
Private Const WM_CUT = &H300

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
Any) As Long

Private Sub Command1_Click()
SendMessage Text1.hwnd, WM_CUT, True, 0&
End Sub


More Commands
Below is a list of all the commands you can send to Edit controls. You can
find the declarations required using the WinAPI viewer. This table has been
extracted from MSDN. To view the page, and get information on each of these
messages, display MSDN contents, and go:

Platform SDK | User Interface Services | Controls | Edit Controls | About
Edit Controls | Edit Control Default Message Processing

A full list of messages is available at

Platform SDK | User Interface Services | Controls | Edit Controls | Edit
Control Reference | Edit Control Messages

Message Default action
EM_CANUNDO Returns TRUE if the edit control operation can be undone.
EM_CHARFROMPOS Returns the character index and line index of the character
nearest the specified point.
EM_EMPTYUNDOBUFFER Empties the undo buffer and sets the undo flag retrieved
by the EM_CANUNDO message to FALSE. The system automatically clears the undo
flag whenever the edit control receives a WM_SETTEXT or EM_SETHANDLE
message.
EM_FMTLINES Adds or removes soft line break characters (two carriage returns
and a linefeed) to the ends of wrapped lines in a multiline edit control. It
is not processed by single-line edit controls.
EM_GETFIRSTVISIBLELINE Returns the zero-based index of the first visible
character in a single-line edit control or the zero-based index of the
uppermost visible line in a multiline edit control.
EM_GETHANDLE Returns a handle identifying the buffer containing the
multiline edit control's text. It is not processed by single-line edit
controls.
EM_GETLIMITTEXT Returns the current text limit, in characters.
EM_GETLINE Copies characters in a single-line edit control to a buffer and
returns the number of characters copied. In a multiline edit control,
retrieves a line of text from the control and returns the number of
characters copied.
EM_GETLINECOUNT Returns the number of lines in the edit control.
EM_GETMARGINS Returns the widths of the left and right margins.
EM_GETMODIFY Returns a flag indicating whether the content of an edit
control has been modified.
EM_GETPASSWORDCHAR Returns the character that edit controls use in
conjunction with the ES_PASSWORD style.
EM_GETRECT Returns the coordinates of the formatting rectangle in an edit
control.
EM_GETSEL Returns the starting and ending character positions of the current
selection in the edit control.
EM_GETTHUMB Returns the position of the scroll box.
EM_GETWORDBREAKPROC Returns the address of the current word wrap function in
an edit control.
EM_LINEFROMCHAR Returns the zero-based number of the line in a multiline
edit control that contains a specified character index This message is the
reverse of the EM_LINEINDEX message. It is not processed by single-line edit
controls.
EM_LINEINDEX Returns the character of a line in a multiline edit control.
This message is the reverse of the EM_LINEFROMCHAR message. It is not
processed by single-line edit controls.
EM_LINELENGTH Returns the length, in characters, of a single-line edit
control. In a multiline edit control, returns the length, in characters, of
a specified line.
EM_LINESCROLL Scrolls the text vertically in a single-line edit control or
horizontally in a multiline edit control (when the control has the ES_LEFT
style). The lParam parameter specifies the number of lines to scroll
vertically, starting from the current line. The wParam parameter specifies
the number of characters to scroll horizontally, starting from the current
character.
EM_POSFROMCHAR Returns the client coordinates of the specified character.
EM_REPLACESEL Replaces the current selection with the text in an
application-supplied buffer, sends the parent window EN_UPDATE and EN_CHANGE
messages, and updates the undo buffer.
EM_SCROLL Scrolls the text vertically in a multiline edit control. This
message is equivalent to sending a WM_VSCROLL message to the edit control.
It is not processed by single-line edit controls.
EM_SCROLLCARET Scrolls the caret into view in an edit control.
EM_SETFONT Unsupported.
EM_SETHANDLE Sets a handle to the memory used as a text buffer, empties the
undo buffer, resets the scroll positions to zero, and redraws the window.
EM_SETLIMITTEXT Sets the maximum number of characters the user may enter in
the edit control.
Windows NT: For single-line edit controls, this value is either 0x7FFFFFFE
or the value of the wParam parameter, whichever is smaller. For multiline
edit controls, this value is either 0xFFFFFFFF or the value of the wParam
parameter, whichever is smaller.

Windows 95 and Windows 98: For single-line edit controls, this value is
either 0x7FFE or the value of the wParam parameter, whichever is smaller.
For multiline edit controls, this value is either 0xFFFF or the value of the
wParam parameter, whichever is smaller.

EM_SETMARGINS Sets the widths of the left and right margins, and redraws the
edit control to reflect the new margins.
EM_SETMODIFY Sets or clears the modification flag to indicate whether the
edit control has been modified.
EM_SETPASSWORDCHAR Defines the character that edit controls use in
conjunction with the ES_PASSWORD style.
EM_SETREADONLY Sets or removes the read-only style (ES_READONLY) in an edit
control.
EM_SETRECT Sets the formatting rectangle for the multiline edit control and
redraws the window. It is not processed by single-line edit controls.
EM_SETRECTNP Sets the formatting rectangle for the multiline edit control
but does not redraw the window. It is not processed by single-line edit
controls.
EM_SETSEL Selects a range of characters in the edit control by setting the
starting and ending positions to be selected.
EM_SETTABSTOPS Sets tab-stop positions in the multiline edit control. It is
not processed by single-line edit controls.
EM_SETWORDBREAKPROC Replaces the default word wrap function with an
application-defined word wrap function.
EM_UNDO Removes any text that was just inserted or inserts any deleted
characters and sets the selection to the inserted text. If necessary, sends
the EN_UPDATE and EN_CHANGE notification messages to the parent window.
WM_CHAR Writes a character to the single-line edit control and sends the
EN_UPDATE and EN_CHANGE notification messages to the parent window. Writes a
character to the multiline edit control. Handles the accelerator keys for
standard functions, such as ctrl+c for copying and ctrl+v for pasting. In
multiline edit controls, also processes tab, and ctrl+tab keystrokes to move
among the controls in a dialog box and to insert tabs into multiline edit
controls. Uses the MessageBeep function for illegal characters.
WM_CLEAR Clears the current selection, if any, in an edit control. If there
is no current selection, deletes the character to the right of the caret. If
the user presses the shift key, this cuts the selection to the clipboard, or
deletes the character to the left of the caret when there is no selection.
If the user presses the ctrl key, this deletes the selection, or deletes to
the end of the line when there is no selection.
WM_COPY Copies text to the clipboard unless the style is ES_PASSWORD, in
which case the message returns zero.
WM_CREATE Creates the edit control and notifies the parent window with TRUE
for success or -1 for failure.
WM_CUT Cuts the selection to the clipboard, or deletes the character to the
left of the cursor if there is no selection.
WM_ENABLE Causes the rectangle to be redrawn in gray for single-line edit
controls. Returns the enabled state for single-line and multiline edit
controls.
WM_ERASEBKGND Fills the multiline edit control window with the current color
of the edit control.
WM_GETDLGCODE Returns the following values: DLGC_WANTCHARS, DLGC_HASSETSEL,
and DLGC_WANTARROWS. In multiline edit controls, it also returns
DLGC_WANTALLKEYS. If the user presses alt+backspace, it also returns
DLGC_WANTMESSAGE.
WM_GETFONT Returns the handle of the font being used by the control, or NULL
if the control uses the system font.
WM_GETTEXT Copies the specified number of characters to a buffer and returns
the number of characters copied.
WM_GETTEXTLENGTH Returns the length, in characters, of the text in an edit
control. The length does not include the null- terminating character.
WM_HSCROLL Scrolls the text in a multiline edit control horizontally and
handles scroll box movement.
WM_KEYDOWN Performs standard processing of the virtual-key codes.
WM_KILLFOCUS Removes the keyboard focus of an edit control window, destroys
the caret, hides the current selection, and notifies the parent window that
the edit control has lost the focus.
WM_LBUTTONDBLCLK Clears the current selection and selects the word under the
cursor. If the shift key is down, extends the selection to the word under
the cursor.
WM_LBUTTONDOWN Changes the current insertion point. If the shift key is
down, it extends the selection to the position of the cursor. In multiline
edit controls, also sets the timer to automatically scroll when the user
holds down the mouse button outside the multiline edit control window.
WM_LBUTTONUP Releases the mouse capture and sets the text insertion point in
the single-line edit control. In a multiline edit control, it also kills the
timer set in the WM_LBUTTONDOWN message.
WM_MOUSEMOVE Changes the current selection in the single-line edit control,
if the mouse button is down. In a multiline edit controls, also sets the
timer to automatically scroll if the user holds down the mouse button
outside the multiline edit control window.
WM_NCCREATE Pointer to the CREATESTRUCT structure for the window. This
message is sent to the WM_CREATE message when a window is first created.
WM_NCDESTROY Frees all memory associated with the edit control window,
including the text buffer, undo buffer, tab-stop buffer, and highlight
brush.
WM_PAINT Erases the background, fills the window with the current color of
the edit control window, draws the border (if any), sets the font and draws
any text, and shows the text-insertion caret.
WM_PASTE Pastes text from the clipboard into the edit control window at the
caret position.
WM_SETFOCUS Sets the keyboard focus of an edit control window (shows the
current selection, if it was hidden, and creates the caret).
WM_SETFONT Sets the font and optionally redraws the edit control.
WM_SETTEXT Copies text to the single-line edit control, notifies the parent
window when there is insufficient memory, empties the undo buffer, and sends
the EN_UPDATE and EN_CHANGE notification messages to the parent window. In
multiline edit controls, also rewraps the lines (if necessary) and sets the
scroll positions.
WM_SIZE Changes the size of the edit control window and ensures that the
minimum size accommodates the height and width of a character.
WM_SYSCHAR Returns TRUE if the user presses alt+backspace; otherwise, it
takes no action.
WM_SYSKEYDOWN Undoes the last action if the user presses alt+backspace;
otherwise, it takes no action.
WM_TIMER Scrolls the text in the edit control window if the user holds down
the mouse button outside the multiline edit control window.
WM_UNDO Removes any text that was just inserted or inserts any deleted
characters and sets the selection to the inserted text. If necessary, sends
the EN_UPDATE and EN_CHANGE notification messages to the parent window.
WM_VSCROLL Scrolls a multiline edit control vertically and handles scroll
box movement. It is not processed by single-line edit controls.



Getting Values
There are a number of constants that return values from the SendMessage
function. You can also use the SendMessage function to get information from
a control because, as it is a function it can return a value. These expose a
number of useful properties which are not available with the standard VB
control properties. These all begin with EM_ . A few are listed below

Const EM_CANUNDO = &HC6 ' Can we undo the last action?
Const EM_GETFIRSTVISIBLELINE = &HCE ' Get the first visible line in the rich
text box
Const EM_GETLINE = &HC4 ' Get the current line number
Const EM_GETLINECOUNT = &HBA ' Get the total number of lines

To send a command to the text box to do any of these things and get a return
value, use the following syntax:

Variable = SendMessage(TextBoxName.hWnd, Constant, 0, 0& )

Where Variable is a variable with the correct data type (in the case of
using the above constants - Boolean), TextBoxName is the name of your text
box, and Constant is one of the constants listed above. The other two
properties are set to 0 and 0& (Null) as they are not needed.

The following example also uses a text box, but finds out if you can Undo in
Text1, and if so enables the Undo menu item.

mnuUndo.Enabled = SendMessage(Text1.hWnd, EM_CANUNDO, 0, 0&) ' This will
return either True or False

The following example uses a text box, finds out the number of lines, and
displays it in a message box.

Dim nLines As Long
nLines = SendMessage(Text1.hWnd, EM_GETLINECOUNT, 0, 0&) ' This will return
either True or False
Msgbox "This document has " & nLines & " lines"


Setting Properties
Using Messaging, you can also set properties of controls. One of these is
the redraw properties. For example, when you add lots of items to a listbox,
or move the position of a cursor around a text box before restoring it to
its previous position you will see lots of flickering. This is because the
control is redrawing every time you add an item, or change the selection.
Using messaging, you can change a property to stop this. Two are listed
below

Const WM_SETFOCUS = &H7
Const WM_SETREDRAW = &HB

Using the constants above, you normally use the following syntax:

SendMessage(ControlName.hWnd, Constant, TRUE_OR_FALSE, 0& )

Where ControlName is the name of the control, Constant is one of the
constants above, and TRUE_OR_FALSE is either True or False. The following
example will stop Text1 from redrawing (ie the control being updated).

SendMessage(Text1.hWnd, WM_SETREDRAW, False, 0& )

To allow it to redraw, simply change False to True. Sometimes, if the
control things it does not need to redraw, you need to force it to redraw:

Text1.Refresh

The following example will stop Text1 from ever getting the focus. You will
receive the GotFocus event once, and then never again, until you call the
SendMessage procedure and change False to True

SendMessage Text1.hwnd, WM_SETFOCUS, False, 0&



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






Sat Jun 30, 2001 6:54 am

ferialb@...
Send Email Send Email

Forward
Message #92 of 103 |
Expand Messages Author Sort by Date

Sending Messages Found at http://www.vbweb.co.uk/show.asp?id=46 Introduction Visual C++ revolves around messages. Messages are sent to controls from windows...
FDB & HRB
ferialb@...
Send Email
Jun 30, 2001
10:08 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help