键盘鼠标计时器 这几个比较常用:
程序代码:
#include <stdio.h>
#include <windows.h>
#include <math.h>
int nPos = 0;
#define TIMERID 1020
HDC hDC = NULL;
UINT nTimeId = TIMERID;
HWND hConsole = NULL;
HHOOK g_ms_hook = 0;
HHOOK g_kb_hook = 0;
#define WH_KEYBOARD_LL 13
#define WH_MOUSE_LL 14
#if (_WIN32_WINNT < 0x0400) // 以下要看你安装的SDK版本
/*
* Structure used by WH_KEYBOARD_LL
*/
typedef struct tagKBDLLHOOKSTRUCT {
DWORD vkCode;
DWORD scanCode;
DWORD flags;
DWORD time;
DWORD dwExtraInfo;
} KBDLLHOOKSTRUCT, FAR *LPKBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;
/*
* Structure used by WH_MOUSE_LL
*/
typedef struct tagMSLLHOOKSTRUCT {
POINT pt;
DWORD mouseData;
DWORD flags;
DWORD time;
DWORD dwExtraInfo;
} MSLLHOOKSTRUCT, FAR *LPMSLLHOOKSTRUCT, *PMSLLHOOKSTRUCT;
#endif
LRESULT CALLBACK kb_proc (int code, WPARAM w, LPARAM l)
{
printf((w==WM_KEYDOWN)?"按下%c\n":"抬起%c\n",((PKBDLLHOOKSTRUCT)l)->vkCode);
return CallNextHookEx (g_kb_hook, code, w, l);
}
LRESULT CALLBACK ms_proc (int code, WPARAM w, LPARAM l)
{
if(w == WM_LBUTTONDOWN)
printf("按下左键\n");
else if(w == WM_LBUTTONUP)
printf("抬起左键\n");
else
printf("x:%d\ty:%d\n",((PMSLLHOOKSTRUCT)l)->pt.x,((PMSLLHOOKSTRUCT)l)->pt.y);
return CallNextHookEx (g_ms_hook, code, w, l);
}
// 来自:<span style="color: #008000; text-decoration: underline;">http://support.[/color]
// 获取控制台窗口句柄 微软官方网站的程序 直接拿来用了
HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
HWND hwndFound; // This is what is returned to the caller.
char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
wsprintf(pszNewWindowTitle,"%d/%d",
GetTickCount(),
GetCurrentProcessId());
SetConsoleTitle(pszNewWindowTitle);
Sleep(40);
hwndFound=FindWindow(NULL, pszNewWindowTitle);
SetConsoleTitle(pszOldWindowTitle);
return(hwndFound);
}
void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
if (uMsg == WM_TIMER && idEvent == nTimeId)
{
hDC = GetDC(hConsole);// 获得控制台窗口绘图DC
// 随意绘制的一些内容
HPEN hPen = CreatePen(PS_SOLID, 1, RGB(200, nPos%256, (nPos*3)%256)); // 创建一个画笔 相关知识查找MSDN 输入GDI方面的知识 创建填充用的笔刷用Brush
HPEN hOldPen = (HPEN)SelectObject(hDC, hPen); // 让DC选择此画笔
for (int i=0; i<1000; i++)
{
// 画线
MoveToEx(hDC, (i+nPos)%800, (int)(sin(i/10.0)*100+200), NULL);
LineTo(hDC, (i+nPos)%800, (int)(sin((i+1)/10.0)*100+100));
}
if (nPos%70 == 0)
InvalidateRect(hConsole, NULL, TRUE); // 刷新窗口
SelectObject(hDC, hOldPen);// 恢复原有画笔
nPos = (nPos+20)%800;
ReleaseDC(hConsole, hDC);// 释放DC
}
}
int main (void)
{
hConsole = GetConsoleHwnd(); // 获得控制台窗口句柄
nTimeId = SetTimer(NULL, TIMERID, 100, TimerProc); // 为系统定制一个计时器
g_kb_hook = SetWindowsHookEx (WH_KEYBOARD_LL,kb_proc,GetModuleHandle (NULL),0); // 为键盘输入设置一个钩子 来监听用户的键盘输入
g_ms_hook = SetWindowsHookEx (WH_MOUSE_LL, ms_proc,GetModuleHandle(NULL),0); // 为鼠标设置一个钩子 来监听用户的鼠标点击和移动
if (g_kb_hook == NULL || g_ms_hook == NULL) // 判断钩子是否设置成功
{
printf("安装钩子出错\n");
return 0;
}
// 设置消息循环
MSG msg;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
UnhookWindowsHookEx (g_kb_hook);
return 0;
}
[
本帖最后由 cdmalcl 于 2011-12-31 22:54 编辑 ]