如何自定义一个过程处理自己进程上的键盘消息?

xushanfeng 2009-11-09 05:48:31
在非主窗口上如何定义个过程来处理键盘消息?
本来用TApplicationEvents控件的消息事件处理是能实现的,但是由于我的主窗口有了这个控件处理主窗口的消息,所以要是在这个非主窗口上再用一个TApplicationEvents控件,那么主窗口的TApplicationEvents控件的消息处理过程会给覆盖掉的。
现在还有什么方法能接受到我这个非主窗口上的键盘消息呢?
...全文
151 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
taste品味 2009-11-14
  • 打赏
  • 举报
回复
动态创建也有个类名吧,你可以先建一个基类,再截获基类的消息就OK
贝隆 2009-11-11
  • 打赏
  • 举报
回复
学习
wintergoes 2009-11-11
  • 打赏
  • 举报
回复
TApplicationEvents是可以控制整个程序的消息的
放在主窗体上也会响应子窗体的消息的


procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
if Msg.message = wm_LBUTTONDOWN then
if (form2 <> nil) and (IsChild(Form2.Handle, msg.hwnd)
or (form2.Handle = msg.hwnd)) then
begin
Caption := 'a';
end;
end;

tjj5203 2009-11-11
  • 打赏
  • 举报
回复
重载那个窗体的WndProc试试
xushanfeng 2009-11-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tjj5203 的回复:]
重载那个窗体的WndProc试试
[/Quote]
您好,谢谢你,但是无不知道TMessage中的那个才是键盘按下或者抬起的消息?
请问能否提示下?

procedure TCustomForm.WndProc(var Message: TMessage);
var
FocusHandle: HWND;
SaveIndex: Integer;
MenuItem: TMenuItem;
Canvas: TCanvas;
DC: HDC;
begin
with Message do
case Msg of
WM_ACTIVATE, WM_SETFOCUS, WM_KILLFOCUS:
begin
if not FocusMessages then Exit;
if (Msg = WM_SETFOCUS) and not (csDesigning in ComponentState) then
begin
FocusHandle := 0;
if FormStyle = fsMDIForm then
begin
if ActiveMDIChild <> nil then FocusHandle := ActiveMDIChild.Handle;
end
else if (FActiveControl <> nil) and (FActiveControl <> Self) then
FocusHandle := FActiveControl.Handle;
if FocusHandle <> 0 then
begin
Windows.SetFocus(FocusHandle);
Exit;
end;
end;
end;
CM_EXIT:
if HostDockSite <> nil then DeActivate;
CM_ENTER:
if HostDockSite <> nil then Activate;
WM_WINDOWPOSCHANGING:
if ([csLoading, csDesigning] * ComponentState = [csLoading]) then
begin
if (Position in [poDefault, poDefaultPosOnly]) and
(WindowState <> wsMaximized) then
with PWindowPos(Message.lParam)^ do flags := flags or SWP_NOMOVE;
if (Position in [poDefault, poDefaultSizeOnly]) and
(BorderStyle in [bsSizeable, bsSizeToolWin]) then
with PWindowPos(Message.lParam)^ do flags := flags or SWP_NOSIZE;
end;
WM_DRAWITEM:
with PDrawItemStruct(Message.LParam)^ do
if (CtlType = ODT_MENU) and Assigned(Menu) then
begin
MenuItem := Menu.FindItem(itemID, fkCommand);
if MenuItem <> nil then
begin
Canvas := TControlCanvas.Create;
with Canvas do
try
SaveIndex := SaveDC(hDC);
try
Handle := hDC;
Font := Screen.MenuFont;
Menus.DrawMenuItem(MenuItem, Canvas, rcItem,
TOwnerDrawState(LongRec(itemState).Lo));
finally
Handle := 0;
RestoreDC(hDC, SaveIndex)
end;
finally
Free;
end;
Exit;
end;
end;
WM_MEASUREITEM:
with PMeasureItemStruct(Message.LParam)^ do
if (CtlType = ODT_MENU) and Assigned(Menu) then
begin
MenuItem := Menu.FindItem(itemID, fkCommand);
if MenuItem <> nil then
begin
DC := GetWindowDC(Handle);
try
Canvas := TControlCanvas.Create;
with Canvas do
try
SaveIndex := SaveDC(DC);
try
Handle := DC;
Font := Screen.MenuFont;
TMenuItemAccess(MenuItem).MeasureItem(Canvas,
Integer(itemWidth), Integer(itemHeight));
finally
Handle := 0;
RestoreDC(DC, SaveIndex);
end;
finally
Canvas.Free;
end;
finally
ReleaseDC(Handle, DC);
end;
Exit;
end;
end;
else if Message.Msg = RM_TaskbarCreated then
begin
Perform(CM_WININICHANGE, 0, 0);
Perform(CM_SYSCOLORCHANGE, 0, 0);
Perform(CM_SYSFONTCHANGED, 0, 0);
Perform(CM_PARENTCOLORCHANGED, 0, 0);
Perform(CM_PARENTFONTCHANGED, 0, 0);
Perform(CM_PARENTBIDIMODECHANGED, 0, 0);
end;
end;
inherited WndProc(Message);
end;
xushanfeng 2009-11-11
  • 打赏
  • 举报
回复
但是我那个非主窗体是动态创建的,可能创建N个,消息过程也不同的
xushanfeng 2009-11-10
  • 打赏
  • 举报
回复
晕?怎么办?
zhong198801 2009-11-10
  • 打赏
  • 举报
回复
无语中!
[Quote=引用 1 楼 hjkto 的回复:]
mark
[/Quote]
hjkto 2009-11-09
  • 打赏
  • 举报
回复
mark

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧