擴充你的WIndows標準控制項

來源:互聯網
上載者:User

      擴充你的WIndows標準控制項
 作者:ShellEx
 www.shellex.cn && blog.csdn.net/shellex 著作權。

標準控制項嘛,意味著他們預設的功能和表現力都是有限的.比如說,當滑鼠點擊Edit控制項時,雖然
作為子表單的Edit控制項(Edit也是表單)會接受到WM_LBUTTONUP和WM_LBUTTONDOWN訊息,
但是它並不會向父表單發送相關的通知。也就是說,我們在處理WM_COMMAND訊息時,不會
在Edit的通知中獲知Edit是否被點擊了。
下列是標準Edit所支援的通知,它們將被放在wParam的高字位中,其中並沒有我想要的
WM_LBUTTONUP或者類似的訊息。

EN_CHANGE :The user has modified text in an edit control. Windows updates the display before sending this message (unlike EN_UPDATE).
EN_ERRSPACE :The edit control cannot allocate enough memory to meet a specific request.
EN_HSCROLL : The user has clicked the edit control's horizontal scroll bar. Windows sends this message before updating the screen.
EN_KILLFOCUS :The user has selected another control.
EN_MAXTEXT  :  While inserting text, the user has exceeded the specified number of characters for the edit control. Insertion has been truncated. This message is also sent either when an edit control does not have the ES_AUTOHSCROLL : style and the number of characters to be inserted exceeds the width of the edit control or when an edit control does not have the ES_AUTOVSCROLL style and the total number of lines to be inserted exceeds the height of the edit control.
EN_SETFOCUS  :  The user has selected this edit control.
EN_UPDATE  :  The user has altered the text in the edit control and Windows is about to display the new text. Windows sends this message after formatting the text, but before displaying it, so that the application can resize the edit control window.
EN_VSCROLL  :  The user has clicked the edit control's vertical scroll bar. Windows sends this message before updating the screen.

想實現能對Edit單擊滑鼠事件發出響應,有個很簡單的方法,那就是只需子類化Edit,自
定義Edit的訊息處理過程就可以了。有兩個相關函數:
LONG GetWindowLong(

    HWND hWnd, // handle of window
    int nIndex  // offset of value to retrieve
   );
  
LONG SetWindowLong(

    HWND hWnd, // handle of window
    int nIndex, // offset of value to set
    LONG dwNewLong  // new value
   );
前者可以得到表單的若干資訊,後者可以設定表單的若干屬性。包括處理訊息的回呼函數。
關於函數的參數,請查閱MSDN。下面是具體實現。
我先建立一個基於對話方塊的Win32 GUI工程,在相應WM_INITDIALOG訊息的函數
Main_OnInitDialog中寫入如下代碼:

////////////////////////////////////////////////////////////////////////////////

//先得到原來的Edit的訊息處理回呼函數,最後一個參數使用GWL_WNDPROC
oldEditProcAddr = GetWindowLong(GetDlgItem(hwnd,IDC_EDIT_PROCESS),GWL_WNDPROC);
//子類化Edit,讓他能發送EN_LBUTTONUP通知。
SetWindowLong(GetDlgItem(hwnd,IDC_EDIT_PROCESS),GWL_WNDPROC ,(long) _NewEditProc);

////////////////////////////////////////////////////////////////////////////////

EN_LBUTTONUP是我自訂的一個常量,是WM_USER + 101,IDC_EDIT_PROCESS是一個Edit
控制項的ID。hwnd是父表單的控制代碼。_NewEditProc是我自己寫的回呼函數,以後發給Edit的
訊息會被送到_NewEditProc處理。
下面是_NewEditProc函數的代碼:

////////////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK _NewEditProc(HWND hwnd, UINT uMsg, WPARAM wParam,LPARAM lParam ) {
 //如果滑鼠左鍵在Edit上放開
 if ( WM_LBUTTONUP == uMsg ) {
  //將本Edit的ID放到wparam的低字位,自訂的響應通知放在高字位
  WPARAM wp = MAKEWPARAM(IDC_EDIT_PROCESS, EN_LBUTTONUP);
  //lparam放著Edit的控制代碼
  LPARAM lp = (LPARAM)hwnd;
  把這個訊息組合一下,作為WM_COMMAND發給父表單
  SendMessage(GetParent(hwnd), WM_COMMAND, wp, lp);
 }
 //其他的訊息由預設的處理函數處理
 return CallWindowProc((WNDPROC)oldEditProcAddr, hwnd, uMsg, wParam, lParam);
}
////////////////////////////////////////////////////////////////////////////////

現在,我在主表單的訊息迴圈裡就可以輕易地得到按一下滑鼠的通知了:

////////////////////////////////////////////////////////////////////////////////
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) {
    switch(id) {
  case IDC_EDIT_PROCESS:
   if (codeNotify == EN_LBUTTONUP)
    Main_OnEditProcess_Click(hwnd,id,hwndCtl,codeNotify);
  break;
        default:break;
    }
}
////////////////////////////////////////////////////////////////////////////////

在函數Main_OnEditProcess_Click中可以輕鬆實現你希望實現的功能了。: )
    www.shellex.cn && blog.csdn.net/shellex 著作權。

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.