procedure TControl.WndProc(var Message: TMessage); var Form: TCustomForm; begin //由擁有control的窗體來處理設計期間的消息 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 //假如需要,鍵盤消息交由擁有control的窗體來處理 else 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); end; end // 下面一行有點非凡。假如您仔細的話會看到這個消息是CM_VISIBLECHANGED. // 而不是我們熟悉的WM_開頭的標準Windows消息. // 盡管Borland沒有在它的幫助中提到有這一類的CM消息存在。但很顯然這是BCB的 // 自定義消息。呵呵,假如您對此有愛好可以在VCL源碼中查找相關的內容。一定會有不小的收獲。 else if Message.Msg = CM_VISIBLECHANGED then with Message do SendDockNotification(Msg, WParam, LParam); // 最后調用dispatch方法。 Dispatch(Message); end; 看完這段代碼,你會發現TControl類實際上只處理了鼠標消息,沒有處理的消息最后都轉入Dispatch()來處理。
procedure TWinControl.WndProc(var Message: TMessage); var Form: TCustomForm; KeyState: TKeyboardState; WheelMsg: TCMMouseWheel; 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_NCH99vTEST: 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: //下面這一句話指出,鼠標消息實際上轉入IsControlMouseMsg方法來處理了。 if IsControlMouseMsg(TWMMouse(Message)) then begin if Message.Result = 0 then DefWindowProc(Handle, Message.Msg, Message.wParam, Message.lParam); Exit; en