关于SendMessage的问题?(50分)

  • 主题发起人 主题发起人 handsome1234
  • 开始时间 开始时间
H

handsome1234

Unregistered / Unconfirmed
GUEST, unregistred user!
关于sendmessage的问题?
用到一个控件,里面的demo这么一段代码
SendMessage(sncDBCurrencyInplaceEdit.Handle, WM_Char, WORD(Key), 0);
不知他的意识和作用,请问大家碰到类似的问题怎么解决?
vc中有个spy++好象是看消息的,delphi中如何作呢?
 
winsight呀,可以看到所有的消息
不过sendmessage是向窗口发一个消息,等到消息处理完毕返回
 
是的. sendmessage()把消息发送到窗口过程(WNDPROC),等到消息处理完毕返回.
想看消息标识在那里定义:按CTRL+鼠标点在消息标识上.
窗口过程(WNDPROC)的具体动作,你可以到CONTROLS.PAS中看看:
TControl的处理部分
procedure TControl.WndProc(var Message: TMessage);
var
Form: TCustomForm;
KeyState: TKeyboardState;
WheelMsg: TCMMouseWheel;
begin
if (csDesigning in ComponentState) then
begin
Form := GetParentForm(Self);
if (Form <> nil) and (Form.Designer <> nil) and
Form.Designer.IsDesignMsg(Self, Message) then Exit
end;
if (Message.Msg >= WM_KEYFIRST) and (Message.Msg <= WM_KEYLAST) then
begin
Form := GetParentForm(Self);
if (Form <> nil) and Form.WantChildKey(Self, Message) then Exit;
end
else if (Message.Msg >= WM_MOUSEFIRST) and (Message.Msg <= WM_MOUSELAST) then
begin
if not (csDoubleClicks in ControlStyle) then
case Message.Msg of
WM_LBUTTONDBLCLK, WM_RBUTTONDBLCLK, WM_MBUTTONDBLCLK:
Dec(Message.Msg, WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
end;
case Message.Msg of
WM_MOUSEMOVE: Application.HintMouseMessage(Self, Message);
WM_LBUTTONDOWN, WM_LBUTTONDBLCLK:
begin
if FDragMode = dmAutomatic then
begin
BeginAutoDrag;//拖放
Exit;
end;
Include(FControlState, csLButtonDown);
end;
WM_LBUTTONUP:
Exclude(FControlState, csLButtonDown);
else
with Mouse do
if WheelPresent and (RegWheelMessage <> 0) and
(Message.Msg = RegWheelMessage) then
begin
GetKeyboardState(KeyState);
with WheelMsg do
begin
Msg := Message.Msg;
ShiftState := KeyboardStateToShiftState(KeyState);
WheelDelta := Message.WParam;
Pos := TSmallPoint(Message.LParam);
end;
MouseWheelHandler(TMessage(WheelMsg));
Exit;
end;
end;
end
else if Message.Msg = CM_VISIBLECHANGED then
with Message do
SendDockNotification(Msg, WParam, LParam);
Dispatch(Message);//发送消息
end;
TWinControl重载后新加的处理部分
procedure TWinControl.WndProc(var Message: TMessage);
var
Form: TCustomForm;
begin
case Message.Msg of
WM_SETFOCUS:
begin
Form := GetParentForm(Self);
if (Form <> nil) and not Form.SetFocusedControl(Self) then Exit;
end;
WM_KILLFOCUS:
if csFocusing in ControlState then Exit;
WM_NCHITTEST:
begin
inherited WndProc(Message);//先重载父类的方法
if (Message.Result = HTTRANSPARENT) and (ControlAtPos(ScreenToClient(
SmallPointToPoint(TWMNCHitTest(Message).Pos)), False) <> nil) then
Message.Result := HTCLIENT;
Exit;
end;
WM_MOUSEFIRST..WM_MOUSELAST:
if IsControlMouseMsg(TWMMouse(Message)) then
begin
{ Check HandleAllocated because IsControlMouseMsg might have freed the
window if user code executed something like Parent := nil. }
if (Message.Result = 0) and HandleAllocated then
DefWindowProc(Handle, Message.Msg, Message.wParam, Message.lParam);
Exit;
end;
WM_KEYFIRST..WM_KEYLAST:
if Dragging then Exit;
WM_CANCELMODE:
if (GetCapture = Handle) and (CaptureControl <> nil) and
(CaptureControl.Parent = Self) then
CaptureControl.Perform(WM_CANCELMODE, 0, 0);
end;
inherited WndProc(Message);
end;

由此可知:消息进入WNDPROC后,VCL进行处理,再用DISPATCH()发送消息,
在SYSTEM.PAS中处理TObject.Dispatch(var Message).

WINDOWS中的得到消息,消息队列,消息循环在:
Application.Run中.

 
SendMessage(sncDBCurrencyInplaceEdit.Handle, WM_Char, WORD(Key), 0);
的意思是向sncDBCurrencyInplaceEdit控件发送WM_CHAR消息,第一个参数为Key,
第二个参数为0,我也经常这么写。
简单地说:作者意思是模拟在sncDBCurrencyInplaceEdit控件上按下"Key"键,比如,
SendMessage(sncDBCurrencyInplaceEdit.Handle, WM_Char, #13, 0);与在
sncDBCurrencyInplaceEdit按回车键相同
SendMessage(sncDBCurrencyInplaceEdit.Handle, WM_Char, #27, 0);与在
sncDBCurrencyInplaceEdit按ESC键相同
SendMessage(sncDBCurrencyInplaceEdit.Handle, WM_KEYDOWN, 13, 0);与在
sncDBCurrencyInplaceEdit按回车键相同
SendMessage(sncDBCurrencyInplaceEdit.Handle, WM_KEYDOWN, 27, 0);与在
sncDBCurrencyInplaceEdit按ESC键相同

 
后退
顶部