在C ++ win32中捕获鼠标指针 [英] Capture mouse pointer in C++ win32
本文介绍了在C ++ win32中捕获鼠标指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已将此代码编写为捕获屏幕,它可以100%工作,但它不会捕获鼠标指针
i希望它也能捕获鼠标指针
我的代码如下所示
#includestdafx.h
#includescreenshot2.h
#include< Windows.h>
#include< stdio.h>
void ScreenShot(){
int x1,y1,x2,y2,w,h;
//获取屏幕尺寸
x1 = GetSystemMetrics(SM_XVIRTUALSCREEN);
y1 = GetSystemMetrics(SM_YVIRTUALSCREEN);
x2 = GetSystemMetrics(SM_CXVIRTUALSCREEN);
y2 = GetSystemMetrics(SM_CYVIRTUALSCREEN);
w = x2 - x1;
h = y2 - y1;
//将屏幕复制到位图
HDC hScreen = GetDC(NULL);
HDC hDC = CreateCompatibleDC(hScreen);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreen,w,h);
HGDIOBJ old_obj = SelectObject(hDC,hBitmap);
BOOL bRet = BitBlt(hDC,0,0,w,h,hScreen,x1,y1,SRCCOPY);
//将位图保存到剪贴板
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_BITMAP,hBitmap);
CloseClipboard();
//清理
SelectObject(hDC,old_obj);
DeleteDC(hDC);
ReleaseDC(NULL,hScreen);
DeleteObject(hBitmap);
}
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
ScreenShot();
返回0;
}
我的尝试:
只使用GetCusorPos()尝试我不知道实现它的实际方法
解决方案
参见使用鼠标输入| Microsoft Docs [ ^ ]
i have written this code to capture screen, it works 100% but it does not capture the mouse pointer
i wanted it to capture the mouse pointer as well
My code looks like this
#include "stdafx.h" #include "screenshot2.h" #include <Windows.h> #include <stdio.h> void ScreenShot() { int x1, y1, x2, y2, w, h; // get screen dimensions x1 = GetSystemMetrics(SM_XVIRTUALSCREEN); y1 = GetSystemMetrics(SM_YVIRTUALSCREEN); x2 = GetSystemMetrics(SM_CXVIRTUALSCREEN); y2 = GetSystemMetrics(SM_CYVIRTUALSCREEN); w = x2 - x1; h = y2 - y1; // copy screen to bitmap HDC hScreen = GetDC(NULL); HDC hDC = CreateCompatibleDC(hScreen); HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, w, h); HGDIOBJ old_obj = SelectObject(hDC, hBitmap); BOOL bRet = BitBlt(hDC, 0, 0, w, h, hScreen, x1, y1, SRCCOPY); // save bitmap to clipboard OpenClipboard(NULL); EmptyClipboard(); SetClipboardData(CF_BITMAP, hBitmap); CloseClipboard(); // clean up SelectObject(hDC, old_obj); DeleteDC(hDC); ReleaseDC(NULL, hScreen); DeleteObject(hBitmap); } int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { ScreenShot(); return 0; }
What I have tried:
Tried using GetCusorPos() only i dont know the actual method to have it implemented
解决方案
See Using Mouse Input | Microsoft Docs[^]
这篇关于在C ++ win32中捕获鼠标指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文