Windows systems are driven by message mechanisms, and each thread if a window is established, the system assigns a message queue for processing of window messages. Alternatively, messages can be sent directly to a window without a message queue using the SendMessage function, which will process the message, but SendMessage can return to the calling program only after the message has been processed. The following combines two Delphi programs to discuss how to use SendMessage to send messages and controls to a control to respond to such messages.
To send a message to a control with SendMessage
In programming, controls are sometimes required to be displayed in a special style that cannot be implemented by setting control properties. For example, the list of clients is read and displayed in a Drop-down box for the user to select, and if the dropdown box is too narrow, it will not be all displayed; If you set the width too wide, the interface is not compact. Therefore, it is desirable to dynamically determine the width of the Drop-down box display area at runtime, which is difficult to achieve without SendMessage function.
The workaround is to compute the display width of the string when the database is read, and to determine the width of the Drop-down box display area with the maximum display width. Then use the SendMessage function Drop-down box to send cb_setdroppedwidth message and width values, the dropdown box according to the message from the information, you can do the correct display.
Part of the source code is as follows:
i:=0; //计数
MaxWidth:=0;
Query1.SQL.Clear;
Query1.SQL.Add(‘select Company from Customer’);
Query1.Open;
//读客户列表到下拉框
while not Query1.Eof do begin
ComboBox1.Items.add(Query1.FieldByName
(‘Company’).AsString);
Width:=ComboBox1.Font.Size * Length
(ComboBox1.Items[i]);
if Width>MaxWidth then
MaxWidth:=Width; //找出最大值
Query1.Next;
i:=i+1;
end;
Query1.Close;
ComboBox1.Text:=ComboBox1.Items[0];
//发送消息以确定显示区域的宽度
SendMessage(ComboBox1.Handle,
CB_SETDROPPEDWIDTH,MaxWidth,0);
You can also use the SendMessage function to achieve some interesting effects, such as adding the following statement to the Click event of a button:
SendMessage (Button.handle,bm_setstyle,
bs_radiobutton,1);
When you click the button after running, you can turn the button into a radio button.
Control receives SendMessage messages
The process of sending messages to a control with SendMessage is discussed above. However, there are disadvantages to the advantages of everything, the message sent with SendMessage in the processing of a certain difficulty. Because the message is not passed through the message queue, it is not possible to specify a response to the message in a onmessage way, even with Hookmainwindow, because the message is sent directly to the control, bypassing the main form. To respond to this type of message, you need to overload the WndProc method of the control.
For example, for a list box, a scroll bar message is sent SendMessage, so the message is not in the Tlistbox list of events. The following are concrete steps to handle the message that the control should scroll through.
1. First inherit a Tmylistbox class from Tlistbox and overload the WndProc method. Add the following definition to your program:
type
TMyListBox=class(TListBox)
private
procedure WndProc(var Msg: TMessage);
override;
//重载WndProc,处理发送到控件的消息
public
end;
Where the WndProc method specifies the response of the control to the message, the input parameter is the Tmessage type, the data type is a record, contains the message code and the parameters of the message, and the message parameters can be obtained in longint or Word format.