Hello VC++

Swap Mouse Button

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#include <tchar.h>
#include "windows.h"


using namespace std;

int main() {

// a handler
HKEY key;
// target key
LPCTSTR SubKey = _T("Control Panel\\Mouse");
if (::RegOpenKeyEx(HKEY_CURRENT_USER, SubKey, 0, KEY_ALL_ACCESS, &key) !=
ERROR_SUCCESS) {
::RegCloseKey(key);
}

char dwValue[256];
DWORD dwSize = sizeof(DWORD);
DWORD dwType = REG_SZ;
if (::RegQueryValueEx(key, _T("SwapMouseButtons"), 0, &dwType,
(LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS) {
::RegCloseKey(key);
}


if (dwValue[0] == '1') {

DWORD value = '0';
if (ERROR_SUCCESS
!= ::RegSetValueEx(key, _T("SwapMouseButtons"), 0, REG_SZ,
(CONST BYTE*) &value, 1)) {
::RegCloseKey(key);
}

SwapMouseButton(0);
} else {
DWORD value = '1';
if (ERROR_SUCCESS
!= ::RegSetValueEx(key, _T("SwapMouseButtons"), 0, REG_SZ,
(CONST BYTE*) &value, 1)) {
::RegCloseKey(key);
}

SwapMouseButton(1);
}
// release
::RegCloseKey(key);

}
  • RegOpenKeyEx/ RegQueryValueEx/ RegSetValueEx is register table function.
  • RegCloseKey when function above failed release handler.
  • SwapMouseButton used to control which button’s left right.

LPCTSTR

  • L Long no actual meaning
  • P pointer
  • C constant
  • T base on UNICODE macro, define char or wchar_t
  • STR string char*

So LPCTSTR almost const char*

If macro not define UNICODE, lead to

const char* value can’t initial LPCTSTR instance class.

Solution

1
2
#include <tchar.h>
LPCTSTR SubKey = _T("Control Panel\\Mouse");

or

1
LPCWSTR SubKey = L"Control Panel\\Mouse";

_T("ABC") the letter in _T() obtain 2 bytes same as chinese characters.