Friday, 23 March 2012

MFC Dialog - Simulate a Mouse Click


Beginner Series.

Sometimes when you are trying to select a row in a listbox (CListBox) which is place in your by name <YourDialogName>, you might do all that should be done to select a row programatically.  But when nothing else works, here is a short cut.  You might notice that if you click on an entry in a list box, you selchanged method will be called.  But when you do a "SetCurSel", it might not be called, because your row will not get selected.  In that case the following short cut works.  You may vary the time interval given in Sleep method based on your mouse configuration.

void <YourDialogName>::SelectRow(int row)
{
     //COMMENT: listBox_URLis the name of the list box, whose row should be selected.


     RECT r; 

     listBox_URL.GetItemRect(row,&r);

     int x=r.left + 10, y= r.top+5;
     const int lParam=y<<16 | x;  // Infuse the X & Y co-ordinates into the lParam
     const int wParam=0;
     listBox_URL.SendMessage(WM_LBUTTONDOWN,wParam,lParam);
     Sleep(20);
     listBox_URL.SendMessage(WM_LBUTTONUP,wParam,lParam); 
}

MFC Dialog - Escape/Enter key disable

Beginner Series


When we start to program using an MFC Dialog, handling the Escape key and Enter key seems to be a challenge.  One way is to override the PreTranslateMessage of the Dialog.
Following is a code snippet


Please replace the <DialogName> with the actual name of the dialog which you have created.  


1. In the header file <DialogName>.h file add the following method, under the protected access modifier. 


virtual BOOL PreTranslateMessage(MSG* msg);


2. In the <DialogName>.cpp file add the following code



BOOL <DialogName>::PreTranslateMessage(MSG* msg)
{
       //COMMENT: Get the ID from which this message is received.
         // If the control matches, then see whether the Windows message is KEYDOWN. 
         // Then check whether the key pressed is Enter key.  Return false.  
         // By returning false, we are instructing the dialog to avoid the default behavior, which might 
         // be to close the dialog.


UINT ctrlID=GetDlgCtrlID(msg->hwnd);
if(ctrlID==<YOUR_CONTROL_ID> )  
{
if(msg->message == WM_KEYDOWN)
{
if(msg->wParam==VK_ENTER)
{
return FALSE;
}
}
}

if(msg->message == WM_KEYDOWN)
{
if(msg->wParam == VK_ESCAPE)
{
return FALSE;
}
}
      // For all the others, please go ahead with the default behavior.  

      return CDialog::PreTranslateMessage(msg);
}

Saturday, 3 March 2012

How to read from registry and display a value.

The Windows Registry is an integral part of the Windows Operating System.  Many of the configurations are not explicitly provided to users.  But folks from Microsoft decide to pin it down only in the registry.  So if you are into Windows programming eventually there will be a need for reading from and writing to registry.  The following examples do just that.  I am newbie to Windows Programming though have a substantial experience and interest in programming.

Code Sample:

The following code sample provides a simple functionality.  This part of code reads the registry key responsible for supressing the java-script error dialog when we browse web pages using Internet Explorer.

Registry Key : HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main

Key Type      : REG_SZ  // Null Terminated string value

void readEntriesFromRegistry()
{
     wchar_t disableScriptDebugger[128]=L"Software\\Microsoft\\Internet Explorer\\Main";
     wchar_t keyValue[3]; // I know the value might be either 'yes' or 'no'
     int keySize=sizeof(wchar_t)*3; // Size in bytes.
     int          errorCode=-1;  // Initialize errorCode
     HKEY hKey=0;             // Initialize key
    // Open Registry key.
 errorCode=RegOpenKeyEx(HKEY_CURRENT_USER,disableScriptDebugger,0,KEY_READ,&hKey);

     if(errorCode==0)
     {
          // Read the value for the registry key.  The value is stored in keyValue
   errorCode=RegQueryValueEx(hKey,L"Disable Script Debugger",0,&keyType,(LPBYTE)keyValue,&keySize);
     }
     CString str(keyValue);
     ATLTRACE(L"The value is : %s \n",str);
    RegCloseKey(hKey);
}  

Note:
If you use the above code snippet repeatedly allocate memory for keyValue and size for keySize everytime, since you may get errorCode as 234 instead of 0.















}