How to capture mouse states in a non-customer area?

zhaozj2021-02-16  52

In view of many netizens to capture mouse in non-customer districts, the method of using DirectX can be solved.

First create a dialog-based project (setting as a mouser)

Link in engineering attribute -> Object / library modules: 里:

DXGUID.LIB DXERR8.LIB DINPUT8.LIB

(If you have DirectX 8.0 SDK on your machine, you only need to do it as soon as possible.

If you do not install or do not specify the VC library to the SDK, you need to copy these libraries to your project)

At the same time, add: #include // in the head of mouserdlg.cpp.

The following is the original file:

/// cmouserdlg dialog

Class CMouserdlg: Public CDialog {// Constructionpublic: cmouserdlg (cwnd * pparent = null); // Standard Constructionor

// Define three functions here

Void Freedirectinput (); // Release Void ReadimMediatedata (); // Read Void InitDevice (); // Initialization

// Dialog Data // {{AFX_DATA (CMouserdlg) ENUM {IDD = IDD_MOUSER_DIALOG}; // Note: The classwizard will add data members here //}} AFX_DATA

// ClassWizard generated Virtual function overrides // {{AFX_VIRTUAL (CMouserdlg) protected: Virtual Void DodataExchange (CDATAEXCHANGE * PDX); // DDX / DDV Support ///}} AFX_VIRTUAL

// ImplementationProtace: hicon m_hicon;

// Generated message map functions // {{AFX_MSG (CMouseRDlg) virtual BOOL OnInitDialog (); afx_msg void OnSysCommand (UINT nID, LPARAM lParam); afx_msg void OnPaint (); afx_msg HCURSOR OnQueryDragIcon (); afx_msg void OnTimer (UINT nIDEvent) AFX_MSG VOID OONDESTROY (); //}} AFX_MSG DECLARE_MESSAGE_MAP ()};

// {{AFX_INSERT_LOCATION}} // Microsoft Visual C Will Insert Additional Declarations Immediately Before The Previous Line.

#ENDIF / /! Defined (AFX_MOUSERDLG_H__BC9AFF7C_C2BE_439A_9D50_CDC608606DFA__INCLUDED_)

// mouserdlg.cpp: Implementation file //

#include "stdafx.h" #include "mouser.h" #include "mouserdlg.h"

// DirctX # incline

#ifdef _debug # define new debug_new # undef this_filestatic char this_file [] = __file __; # ENDIF

/// Caboutdlg Dialog Used for app about // Defines two small functions #define Safe_Delete (p) {if (p) {delete (p) = null;}} #define safe_release (p) {ix ( p) {(p) -> release (); (p) = null;}}

// # Define Sample_Buffer_Size 16 // Arbitrary Number of Buffer Elements

LPDIRECTINPUT8 g_pdi = null; // Defines two control variables lpdirectinputDevice8 g_pmouse = null;

Class Caboutdlg: public cdialog {public: capoutdlg ();

// Dialog Data // {{AFX_DATA (Caboutdlg) Enum {IDD = IDD_ABOUTBOX}; //}} AFX_DATA

// classwizard generated virtual function overrides // {{AFX_VIRTUAL (Caboutdlg) protected: Virtual Void DodataExchange (CDATAEXCHANGE * PDX); // DDX / DDV Support //}} AFX_VIRTUAL

// ImplementationProtace: // {{AFX_MSG (Caboutdlg) //}} AFX_MSG DECLARE_MESSAGE_MAP ()};

Caboutdlg :: Caboutdlg (): cdialog (caboutdlg :: IDD) {/ {{AFX_DATA_INIT (Caboutdlg) //}} AFX_DATA_INIT}

Void Caboutdlg :: DODATAEXCHANGE (CDataExchange * PDX) {cdialog :: DODATAEXCHANGE (PDX); // {{AFX_DATA_MAP (CaboutDLG) //}} AFX_DATA_MAP}

Begin_MESSAGE_MAP (Caboutdlg, cdialog) // {{AFX_MSG_MAP (Caboutdlg) // No Message Handlers //}} AFX_MSG_MAPEND_MESSAGE_MAP ()

/// cmouserdlg dialog

CMouseRDlg :: CMouseRDlg (CWnd * pParent / * = NULL * /): CDialog (CMouseRDlg :: IDD, pParent) {// {{AFX_DATA_INIT (CMouseRDlg) // NOTE: the ClassWizard will add member initialization here //}} AFX_DATA_INIT m_hicon = AFXGETAPP () -> Loadicon (idR_mainframe);

void CMouseRDlg :: DoDataExchange (CDataExchange * pDX) {CDialog :: DoDataExchange (pDX); // {{AFX_DATA_MAP (CMouseRDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}} AFX_DATA_MAP}

BEGIN_MESSAGE_MAP (CMouseRDlg, CDialog) // {{AFX_MSG_MAP (CMouseRDlg) ON_WM_SYSCOMMAND () ON_WM_PAINT () ON_WM_QUERYDRAGICON () ON_WM_TIMER () ON_WM_DESTROY () //}} AFX_MSG_MAPEND_MESSAGE_MAP () /// CMouseRDlg message handlers

Bool cmouserdlg :: oninitdialog () {cdialog :: onInitdialog ();

// Add "About ..." Menu Item to System Menu.

// IDM_ABOUTBOX MUST BE IN THE SYSTEM Command Range. Assert ((idm_aboutbox & 0xff0) == idm_aboutbox; assert (idm_aboutbox <0xf000);

CMenu * pSysMenu = GetSystemMenu (FALSE); if (pSysMenu = NULL!) {CString strAboutMenu; strAboutMenu.LoadString (IDS_ABOUTBOX); if {pSysMenu-> AppendMenu (MF_SEPARATOR) (strAboutMenu.IsEmpty ()!); PSysMenu-> AppendMenu ( MF_String, IDM_AboutBox, Straboutmenu);}}

Seticon (m_hicon, true); // set big icon seticon (m_hicon, false); // set small icon // Todo: add extra initialization here

INITDEVICE (); setTimer (1, 10, 0); Return true; // Return True Unless}} SECUS TRUE UNSO

void CMouseRDlg :: OnSysCommand (UINT nID, LPARAM lParam) {if ((nID & 0xFFF0) == IDM_ABOUTBOX) {CAboutDlg dlgAbout; dlgAbout.DoModal ();} else {CDialog :: OnSysCommand (nID, lParam);}}

// if you add a minimize button to your dialog, you will need the code Below // to draw the icon. For mfc Applications Using the document / view model, // this is automaticly done for you by the framework.

Void cmouserdlg :: onpaint () {if (isiconic ()) {cpaintdc dc (this); // device context for Painting

SendMessage (WM_ICONERASEBKGND, (WPARAM) dc.getsafehdc (), 0);

// Center icon in client rectangle int cxIcon = GetSystemMetrics (SM_CXICON); int cyIcon = GetSystemMetrics (SM_CYICON); CRect rect; GetClientRect (& rect); int x = (rect.Width () - cxIcon 1) / 2; int y = (Rect.height () - Cyicon 1) / 2; // Draw the icon dc.drawicon (x, y, m_hicon);} else {cdialog :: onpaint ();}}

Hcursor cmouserdlg :: ONQUERYDRAGICON () {Return (hcursor) m_hicon;}

Void cmouserdlg :: initdevice () {hResult HR; freedirectinput ();

if (FAILED (hr = DirectInput8Create (GetModuleHandle (NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID **) & g_pDI, NULL))) return; // Obtain an interface to the system mouse device if (FAILED (hr = g_pDI->. CreateDevice (GUID_SYSMOUSE, & G_PMOUSE, NULL)) Return; IF (Failed (HR = g_pmouse-> setdataformat)) Return; IF (FAILED (HR)) Return; g_pmouse-> acquire ();

}

/// read mouse infovoid cmouserdlg :: readimmediatedata () {HRESULT HR; CSTRING STRNEWTEXT; DIMOUSSTATE2 DIMS2; // DirectInput Mouse State Structure

Cpoint Point;

IF (NULL == g_pmouse) Return; ZeromeMory (& DIMS2, SIZEOF (DIMS2)); HR = g_pmouse-> getDeviceState (SizeOf (dimousestate2), & DIMS2); if (Failed (HR)) {hr = g_pmouse-> acquire () WHILE (HR == Dierr_inputlost) hr = g_pmouse-> acquire ();

IF (hr == dierr_otherapphasprio || hr == dierr_notacquired) setdlgitemtext (IDC_STATIC1, "Failure!");

} GetCursorpos (& POINT); // Get the position of the mouse. This feature is more convenient to use the API, so this is taken // generation DirectX strnewtext.format ("L =% D r =% D m =% D x =% 4D Y =% 4D ", // write result, respectively, L left

// key, R right button, m mid button, x mouse x

// Value, Y mouse y value. The first three values, 1

/ / Indications, 0 is bomb up (DIMS2.RGBB /TTONS [0] & 0x80)? 1: 0, (dims2.rgbbuttons [1] & 0x80)? 1: 0, (DIMS2.RGBB /TTONS [2] & 0x80) ? 1: 0, point.x, point.y);

CSTRING STROLDTEXT; GETDLGITEMTEXT (IDC_STATIC1, STROLDTEXT); if (0! = LSTRCMP (StrNewText, StrnewText)) setdlgitemtext (IDC_STATIC1, STRNEWTEXT); // Display result, IDC_STATIC1 is used

// Show text tag}

Void cmouserdlg :: freedirectinput () // Release {if (g_pmouse) g_pmouse-> unacquire (); SAFE_RELEASE (g_pmouse); Safe_Release (g_pdi);}

Void cmouserdlg :: ONTIMER (uint nidevent) {

Readimmediatedata (); CDIALOG :: ONTIMER (Nidevent);

Void cmouserdlg :: overdestroy () {

Freedirectinput (); killtimer (1); cdialog :: overdestroy ();

OK, running a program, then you will send another place to click on the screen to have a response.

Download Engineering Code:

http://www.7forever.com/rwdata/temp/mouser.rar

Author mailbox:

Zhucde@163.com

转载请注明原文地址:https://www.9cbs.com/read-23339.html

New Post(0)