Windows Programming(八)_ChilldWindow

Send a message to parent

    hwndParent = GetParent(hwnd);
    SendMessage(hwndParent, message, wParam, lParam);

message could be set to WM_USER or above. Perhaps for this message the child window could set wParam to its child window ID. The lParam could be set to a 1 if the child window were being checked and a 0 if it were being unchecked.That's one possibility

子窗口要发送一个消息给父窗口,只要知道自己的窗口句柄即可

    hwndParent = GetParent(hwnd);
    SendMessage(hwndParent, message, wParam, lParam);

为了不与系统消息 WM_message 冲突,发送的message 一般定义为
    
    #define message WM_USER + constant

发送的信息则通过message的 wParam 和 lParam附带信息传达给父窗口,例如:wParam 可定义为窗口ID,让父窗口知道收到的是谁发的消息
lParam可定义为当前可根据当前窗口的状态来设置0或1

This in effect creates a "child window control." The child window processes mouse and keyboard messages and notifies the parent window when the child window's state has changed. In this way, the child window becomes a high-level input device for the parent window. It encapsulates a specific functionality with regard to its graphical appearance on the screen, its response to user input, and its method of notifying another window when an important input event has occurred.

使用子窗口的方法实际上是更高一级的封装,子窗口处理键盘、鼠标等外部输入,当子窗口的状态发生变化时,通过消息的方式通知到父窗口。这样子窗口实际上是相对父窗口的更上一层的输入设备。

Child talks to Its Parent

When you click a button with the mouse, the child window control sends a WM_COMMAND message to its parent window
  LOWORD(wParam)         Child window ID
  HIGHWORD(wParam)     Notification code
  lParam                           Child window handle

Parent talks to its Child

id = GetWindowLong(hwndChild, GWL_ID);
id = GetDlgCtrlID(hwndChild);
hwndChild = GetDlgItem(hwndParent, id);
SendMessage(hwndChild, message, wParam, lParam);

Push Button
    SendMessage(hwndButton, BM_SETSTATE, 1, 0);    // Cause the button to be pressed
    SendMessage(hwndButton, BM_SETSTATE, 0, 0);    // Cause the button to return to normal

Check Boxes
    SendMessage(hwndButton, BM_SETCHECK, 1, 0);
    SendMessage(hwndButton, BM_SETCHECK, 0, 0);
    iChecked = SendMessage(hwndButton, BM_GETCHECK, 0, 0);

Radio Buttons
    SendMessage(hwndButton, BM_SETCHECK, 1, 0);
    SendMessage(hwndButton, BM_SETCHECK, 0, 0);

Edit Control
    SendMessage(hwndEdit, WM_CUT, 0, 0);
    SendMessage(hwndEdit, WM_COPY, 0, 0);
    SendMessage(hwndEdit, WM_CLEAR, 0, 0);


    SendMessage(hwndEdit, EM_GETSEL, (WPARAM) &iStart, (LPARM) &iEnd);
    SendMessage(hwndEdit, EM_SETSEL, iStart, iEnd);
    SendMessage(hwndEdit, EM_REPLACESEL, 0, (LPARAM)szString);

    iCount = SendMessage(hwndEdit, EM_GETLINECOUNT, 0, 0);
    iOffset = SendMessage(hwndEdit, EM_LINEINDEX, iLine, 0);
    iLength = SendMessage(hwndEdit, EM_LINELENGTH, iLine, 0);

    iLength = SendMessage(hwndEidt, EM_GETLINE, iLene, (LPARAM)szBuffer); //Copy the line itself into a buffer

List Box
    SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)szString);
    SendMessage(hwndList, LB_INSERTSTRING, iIndex, (LPARAM)szString);
    SendMessage(hwndList, LB_DELETESTR, iIndex,  0);
    SendMessage(hwndList, LB_RESETCONTENT,  0, 0);
    SendMessage(hwndList, WM_SETRERDRAW, FALSE, 0);
    SendMessage(hwndList, LB_SETCURSEL, iIndex, 0);
    SendMessage(hwndList, LB_SELECTSTRING, iIndex, (LPARAM)szSearchString);
    iIndex = SendMessage(hwndList, LB_GETCURSEL, 0, 0);
    iSelect = SendMessage(hwndList, LB_GETSEL, iIndex, 0);    
    SendMessage(hwndList, LB_DIR, iAttr, (LPARAM)szFileSpec);

Change the button text
    SetWindowText(hwnd, pszString);
    iLength = GetWindowText(hwnd, pszBuffer, iMaxLength);
    iLength = GetWindowTextLength(hwnd);

Visible and Enabled Buttons
    ShowWindow(hwndChild, SW_SHOWNORMAL);
    ShowWindow(hwndChild, SW_HIDE);
    IsWindowVisiable(hwndChild);

    EnableWindow(hwndChild, FALSE);
    EnableWindow(hwndChild, TRUE);
    IsWindowEnabled(hwndChild);

SubClassing

Use the CallWindowProc function for window subclassing. Usually, all windows with the same class share one window procedure. A subclass is a window or set of windows with the same class whose messages are intercepted and processed by another window procedure (or procedures) before being passed to the window procedure of the class. 

The SetWindowLong function creates the subclass by changing the window procedure associated with a particular window, causing the system to call the new window procedure instead of the previous one. An application must pass any messages not processed by the new window procedure to the previous window procedure by calling CallWindowProc. This allows the application to create a chain of window procedures.

 

 

The SetWindowLong function is used to subclass an instance of a window. The application must have the address of the subclass function. The subclass function is the function that receives the messages from Windows and passes the messages to the original window procedure. The subclass function must be exported in the application's or the DLL's module definition file.

The application subclassing the window calls SetWindowLong with the handle to the window the application wants to subclass, the GWL_WNDPROC option (defined in WINDOWS.H), and the address of the new subclass function. SetWindowLong returns a DWORD, which is the address of the original window procedure for the window. The application must save this address to pass the intercepted messages to the original window procedure and to remove the subclass from the window. The application passes the messages to the original window procedure by calling CallWindowProc with the address of the original window procedure and the hWndMessagewParam, and lParam parameters used in Windows messaging. Usually, the application simply passes the arguments it receives from Windows to CallWindowProc.

The application also needs the original window procedure address for removing the subclass from the window. The application removes the subclass from the window by callingSetWindowLong again. The application passes the address of the original window procedure with the GWL_WNDPROC option and the handle to the window being subclassed.

 

The following code subclasses and removes a subclass to an edit control:

 

LRESULT FAR PASCAL SubClassFunc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam);
FARPROC lpfnOldWndProc;
HWND hEditWnd;

//
// Create an edit control and subclass it.
// The details of this particular edit control are not important.
//
hEditWnd = CreateWindow("EDIT", "EDIT Test",
                        WS_CHILD | WS_VISIBLE | WS_BORDER ,
                        0, 0, 50, 50,
                        hWndMain,
                        NULL,
                        hInst,
                        NULL);
//
// Now subclass the window that was just created.
//
lpfnOldWndProc = (FARPROC)SetWindowLong(hEditWnd,
                 GWL_WNDPROC, (DWORD) SubClassFunc);

//
// Remove the subclass for the edit control.
//
SetWindowLong(hEditWnd, GWL_WNDPROC, (DWORD) lpfnOldWndProc);

//
// Here is a sample subclass function.
//
LRESULT FAR PASCAL SubClassFunc(   HWND hWnd, UINT Message,WPARAM wParam,LPARAM lParam)
{
   //
   // When the focus is in an edit control inside a dialog box, the
   //  default ENTER key action will not occur.
   //

   if ( Message == WM_GETDLGCODE )
       return DLGC_WANTALLKEYS;

   return CallWindowProc(lpfnOldWndProc, hWnd, Message, wParam,lParam);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值