How to check mouse button status without message handler?

 

We can map WM_LBUTTON or WM_RBUTTON down to know whether user has pressed the left or right button respectively on top of the window or not. But sometimes, we have to know this information while we’re processing other handlers like painting/drawing the window.

#define SHIFTED 0x8000
void GetMouseButtonState()
{
   if ((GetKeyState(VK_LBUTTON) & SHIFTED))
   {
      printf( "Left button is pressed" );
   }

   if ((GetKeyState(VK_RBUTTON) & SHIFTED))
   {
      printf( "Right button is pressed" );
   }
}

Please note that this information is a system wide information. By knowing this status doesn’t mean that the button is pressed on top of your window. For that you will have to know the current cursor position using GetCursorPosition and check if it’s within the Window Rect or not.

You can see the similar keyboard sample at MSDN website. – Using Keyboard Input

 

Why I can’t set cursor position on a child control during OnInitDialog?

 

One of my colleague asked me. “Why I can’t set the cursor position on a button during OnInitDialog? I need to set the cursor over a button inside the dialog during startup.” The snippet looks like below

BOOL CCursorPositionDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CRect rect;
	GetDlgItem( IDC_BUTTON2 )->GetWindowRect( rect );
	SetCursorPos(rect.left, rect.top );
}

The reason is, the Window initialization has not completed during OnInitDialog operation (even window is not displayed). Once after OnInitDialog returns, the dialog will be positioned, the input focus will be set the child control and finally the dialog is displayed. Also if you notice the, GetWindowRect returns incorrect window cordinate during OnInitDialog execution.

To solve this issue, it’s necessary to rely on other activation functions like WM_ACTIVATE or WM_WINDOWPOSCHANGED (only once) function

void CCursorPositionDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
	CDialog::OnActivate(nState, pWndOther, bMinimized);

	if( WA_ACTIVE == nState )
		SetMousePosition();
}

Alternate method (not recommended)

void CCursorPositionDlg::OnWindowPosChanged(WINDOWPOS* lpwndpos)
{
	CDialog::OnWindowPosChanged(lpwndpos);

 	static bool bFirst = true;
 	if( bFirst) { PositionButton(); bFirst = false;}
}
 

Windows Registry Virtualization

 

Registry virtualization is implemented in Windows Vista and 7 for providing application support for legacy applications. The legacy application (till Windows XP) runs in administrator privilege by default which enables the crappy applications to access the sensitive system area without user knowledge. UAC is the solution introduced to defend this kind of attacks. By default the applications will be launched in user privileged mode and the application can request user if it requires to have elevated privilege.

Registry Virtualization emulates the HKEY_LOCAL_MACHINE\Software write operations under HKEY_USER\<User SID>_Classes\VirtualStore\Machine\Software path.

Open Registry Virtualization – If the application doesn’t have enough privilege to open a specific key, the virtualized key will be opened with maximum privilege.

Write Registry Virtualization – If the application doesn’t have enough privilege to write to the specified location, the write operation will be performed under virtual store path.

Read Registry Virtualization – System provides a merged copy of information from Virtual store and the original global store together.

Virtualization is provided for

  • 32 bit interactive process
  • Keys in HKEY_LOCAL_MACHINE\Software
  • Keys that an administrator write to (if the application doesn’t have privilege)

Virtualization is disabled for

  • 64 bit processes
  • Processes which doesn’t have GUI (e.g Services)
  • Process that impersonate a user
  • Kernel mode processes such as drivers
  • Process that has specified requestedExecutionLevel in their manifest
  • Keys and subkeys of HKEY_LOCAL_MACHINE\Software\Classes, HKEY_LOCAL_MACHINE\Software\Microsoft\Windows, and HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT.

The applications which required to run under Windows Vista and 7 must follow the UAC guidelines and should not use registry to store the applications information. Microsoft may stop supporting this feature in the upcoming versions of Windows. So keep your applications compatible with the latest platform.

See more