如何在窗体中捕获控件的消息
如题 问题点数:20、回复次数:13Top
1 楼mbzdh(一天一个进步)回复于 2003-09-02 19:56:35 得分 0
这么没人缘Top
2 楼lw549(那个孩子他爹)回复于 2003-09-02 19:59:05 得分 0
顶一下。Top
3 楼mbzdh(一天一个进步)回复于 2003-09-02 19:59:39 得分 0
谢谢!Top
4 楼blackbeetle(票房毒药)回复于 2003-09-03 09:33:09 得分 0
说的不清初啊,太抽象了。还是问具体点。Top
5 楼lihao_ningxia(耗子)回复于 2003-09-03 09:41:55 得分 0
这个呀,我给你一段我写的C的代码,你看吧,其实DELPHI只是封装了一些消息处理或其它的东西罢了:
LRESULT CTopEisKeyboardCtrl::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
switch(message)
{
case WM_ACTIVATE:
MSG msg;
wParam=WA_INACTIVE;
msg.wParam=wParam;
msg.lParam=wParam;
msg.message=message;
msg.hwnd=hwnd;
DispatchMessage(&msg);
break;
case WM_MOUSEACTIVATE:
return MA_NOACTIVATE;
break;
case WM_SETFOCUS:
{
::PostMessage(hwnd,WM_KILLFOCUS,0,0);
CWnd *wnd=::AfxGetMainWnd();
HWND hwndwnd=wnd->m_hWnd;
lihao=hwndwnd;
::SetFocus(hwndwnd);
//&&gFocus!=NULL
/*if (hwndwnd!=gFocus)
{
::SetFocus(gFocus);
char buffer[9];
long l=(long)gFocus;
_ltoa(l,buffer,16);
//MessageBox(buffer);
}*/
}
break;
case WM_KILLFOCUS:
{
//::SetFocus(lihao);
/*if (hwndwnd!=hwnd)
b=::PostMessage(hwndwnd,WM_MOUSEACTIVATE,0,0);
if (b)
{
::SetFocus(hwndwnd);
lihao=hwndwnd;
}9*/
}
case WM_DESTROY:
if (gShift==true)
{
keybd_event(0x20,0,KEYEVENTF_KEYUP,0);
}
break;
case WM_CREATE:
CWnd *wnd;
//MSG msg;
hwnd=CWnd::m_hWnd;
wnd=AfxGetMainWnd();
hInst=AfxGetResourceHandle();
//HANDLE hmutex;
//LPCTSTR szAppName;
//CreateMutex
/*while (GetMessage(&msg,NULL,0,0))
{
HWND hwndx=::GetForegroundWindow();
//BringWindowToTop(hwndMain);
if (IsWindow(hwndx))
{
if (hwndx!=hwnd)
{
if (gFocus!=hwndx)
{
if (IsWindow(gFocus))
{
AttachThreadInput(GetWindowThreadProcessId(hwnd,NULL),GetWindowThreadProcessId(gFocus,NULL),false);
}
gFocus=hwndx;
AttachThreadInput(GetWindowThreadProcessId(hwnd,NULL),GetWindowThreadProcessId(gFocus,NULL),true);
}
}
}
if (!hwnd||!::IsDialogMessage(hwnd,&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//ReleaseMutex(hMutex);
return(msg.wParam );*/
//AfxGetInstanceHandle();
/*char buffer[20];
long l=(long)hInst;
_ltoa(l,buffer,16);
//MessageBox(buffer);
/*MessageBox("next");
HINSTANCE hin=AfxGetInstanceHandle();
l=(long)hin;
_ltoa(l,buffer,16);
MessageBox(buffer);*/
/*HDC hdc;
HDC hMemdc;
HBITMAP hbitmap;
BITMAP bm;
hbitmap=LoadBitmap(hInst,"IDB_KEYBOARD");
GetObject(hbitmap,sizeof(BITMAP),&bm);
hdc=::GetDC(hwnd);
hMemdc=CreateCompatibleDC(hdc);
GetClientRect(&rect);
StretchBlt(hdc,0,0,rect.right,rect.bottom,hMemdc,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
DeleteDC(hMemdc);
::ReleaseDC(hwnd,hdc);
DeleteObject(hbitmap);
/*char buff[20];
_itoa(rect.top,buff,10);
MessageBox(buff);
_itoa(rect.left,buff,10);
MessageBox(buff);
_itoa(rect.bottom,buff,10);
MessageBox(buff);
_itoa(rect.right,buff,10);
MessageBox(buff);*/
break;
case WM_LBUTTONDOWN:
int x,y;
RECT rect;
x=LOWORD(lParam);
y=HIWORD(lParam);
POINT p;
p.x=x;
p.y=y;
//the '~'
rect=CRect(0,0,25,27);
if (PtInRect(&rect,p))
{
keybd_event(0xC0,0,0,0);
keybd_event(0xC0,0,KEYEVENTF_KEYUP,0);
if (gShift==true)
{
keybd_event(0x20,0,KEYEVENTF_KEYUP,0);
}
}
//'1'
rect=CRect(25,0,50,27);
if (PtInRect(&rect,p))
{
keybd_event(0x31,0,0,0);
keybd_event(0x31,0,KEYEVENTF_KEYUP,0);
if (gShift==true)
{
keybd_event(0x20,0,KEYEVENTF_KEYUP,0);
}
}
//'2'
rect=CRect(50,0,75,27);
if (PtInRect(&rect,p))
{
keybd_event(0x32,0,0,0);
keybd_event(0x32,0,KEYEVENTF_KEYUP,0);
if (gShift==true)
{
keybd_event(0x20,0,KEYEVENTF_KEYUP,0);
}
}
//'3'
rect=CRect(75,0,100,27);
if (PtInRect(&rect,p))
{
keybd_event(0x33,0,0,0);
keybd_event(0x33,0,KEYEVENTF_KEYUP,0);
if (gShift==true)
{
keybd_event(0x20,0,KEYEVENTF_KEYUP,0);
}
}
//'4'
rect=CRect(100,0,125,27);
if (PtInRect(&rect,p))
{
keybd_event(0x34,0,0,0);
keybd_event(0x34,0,KEYEVENTF_KEYUP,0);
if (gShift==true)
{
keybd_event(0x20,0,KEYEVENTF_KEYUP,0);
}
}
-------------
//'5'
}}
就是处理它的窗口的消息处理函数,我给你写一个DELPHI的Top
6 楼lihao_ningxia(耗子)回复于 2003-09-03 09:57:53 得分 0
对了,在DELPHI的DEMOS中有个APPEVENTS这个例子,你看看就会明白了!Top
7 楼lihao_ningxia(耗子)回复于 2003-09-03 09:59:18 得分 0
你看看DELPHI中的DEMOS中的APPEVENTS这个讲得挺好了Top
8 楼mbzdh(一天一个进步)回复于 2003-09-03 10:01:15 得分 0
我的意思是在窗体上有控件,比如BUTTON,我在它上面击鼠标左键,如何捕获这一消息。Top
9 楼lihao_ningxia(耗子)回复于 2003-09-03 10:36:25 得分 0
function WindowProc(hWnd, uMsg, wParam, lParam: Integer): Integer; stdcall;
begin
Result := DefWindowProc(hWnd, uMsg, wParam, lParam);
{ Checks for messages }
if (lParam = Button1) and (uMsg = WM_COMMAND) then
CheckPassword;
if uMsg = WM_DESTROY then
Halt;
end;Top
10 楼lihao_ningxia(耗子)回复于 2003-09-03 10:44:46 得分 0
或者说,你的windowproc(msg:Tmsg);
msg.message就是如左键呀等
msg.lparam就是具体的某个控件Top
11 楼lihao_ningxia(耗子)回复于 2003-09-03 10:45:08 得分 20
Determines whether an application message requires hiding the hint window.
type
TMsg = packed record
hwnd: HWND;
message: UINT;
wParam: WPARAM;
lParam: LPARAM;
time: DWORD;
pt: TPoint;
end;
function IsHintMsg(var Msg: TMsg): Boolean; virtual;
Description
Call IsHintMsg to determine whether the message specified by the Msg parameter requires hiding the hint window. Upon seeing a mouse, keyboard, command, or activation message, IsHintMsg returns True. The global Application object calls IsHintMsg to check messages while the hint window is on the screen, and hides the window if IsHintMsg returns True.Top
12 楼XXSingle(心无了然)回复于 2003-09-03 11:03:27 得分 0
up,同意楼上Top
13 楼mbzdh(一天一个进步)回复于 2003-09-03 11:24:28 得分 0
tmessage和tmsg有何区别,
上边的意思是LPARM指某个控件,那WPARM指什么,谢谢!!Top
14 楼mbzdh(一天一个进步)回复于 2003-09-03 11:50:27 得分 0
有点眉目了,但还是不太清楚,解决了另外开贴给分!Top



