11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5. Common Controls<br />

From this structure, we know which window is going to receive the message (from member hwnd),<br />

what kind of message it is (from member message). Also, we can obtain the message parameters from<br />

members wParam and lParam. If the message is not the one we want to intercept, we can just forward the<br />

message to its original destination by calling the base class version of this function.<br />

Function CWnd::PreTranslateMessage(…)<br />

Sample 5.9\CCtl demonstrates how to trap RETURN keystrokes for combo box. It is based on sample<br />

5.8\CCtl. First, function PreTranslateMessage(…) is overridden. This function can be added by using<br />

Class Wizard through following steps: 1) Open Class Wizard, click “Message Maps” tab, select<br />

“CCCtlDlg” from “Class name” window. 2) Highlight “CCCtlDlg” in window “Object IDs”. 3) Locate and<br />

highlight “PreTranslateMessage” in window “Messages”. 4) Press “Add function” button.<br />

The default member function looks like the following:<br />

BOOL CCCtlDlg::PreTranslateMessage(MSG *pMsg)<br />

{<br />

return CDialog::PreTranslateMessage(pMsg);<br />

}<br />

If we do not want to process the message, we need to call function CDialog::<br />

PreTranslateMessage(…) to let the dialog box process it as usual. Otherwise we need to return a TRUE<br />

value to give the operating system an impression that the message has been processed properly.<br />

In the overridden function, first we need to check if the message is WM_KEYDOWN and the key being<br />

pressed is RETURN:<br />

BOOL CCCtlDlg::PreTranslateMessage(MSG *pMsg)<br />

{<br />

CString str;<br />

HWND hwndDlg;<br />

HWND hwndFocus;<br />

HWND hwndEdit;<br />

CEdit *ptrEdit;<br />

int nVirtKey;<br />

char szClassName[256];<br />

……<br />

if(pMsg->message == WM_KEYDOWN)<br />

{<br />

nVirtKey=(int)pMsg->wParam;<br />

if(nVirtKey == VK_RETURN)<br />

{<br />

}<br />

}<br />

Message WM_KEYDOWN is a standard Windows message for non-system key strokes, and VK_RETURN is<br />

a standard virtue key code defined for RETUN key (For a list of virtual key codes, see appendix A). Some<br />

local variables are declared at the beginning. They will be used throughout this function.<br />

Accessing the Edit Box of a Combo Box<br />

We need to find out which combo box has the current focus in order to decide if we should process this<br />

message. If the item that has the current focus is either IDC_COMBO_DROPDOWN or IDC_COMBO_SIMPLE, we will<br />

update the corresponding list items.<br />

In Windows operating system, windows are managed through using handles. Like menu and bitmap<br />

resources, a window handle is also a number which could be used to identify a window. Each window’s<br />

handle has a different value. As a programmer, we do not need to know the exact value of the handle,<br />

however, we can use handle to access or identify a window.<br />

In <strong>MFC</strong>, there is a function CWnd::GetFocus(), which can be used to obtain a pointer to the child<br />

window that has the current focus. From this pointer, we can obtain that window’s handle. Then we can<br />

108

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!