Windows窗口子类化



 利用子类化技术拓展Edit控件功能

认识子类化技术

把一个窗体子类化其实就是使用SetwindowLong为该窗体设计一个新的窗体过程,然后可以在该窗体函数中处理特定消息,而那些不关心的消息可以通过CallWindowProc传递给默认处理

从功能上说,子类化技术有点类似于钩子函数(SetWindowHookEx),都可以对一些特定的消息进行处理。但两者有本质区别,钩子函数是针对系统的消息传递机制,而子类化是针对特定的窗体。利用钩子可以处理某一类或几类消息,而利用子类化技术则能处理特定窗体的绝大部分消息。

一个对话框(IDD_TEST_SAMPLE)和一个EDIT控件(IDC_TEST)

#include "resource.h"

#include <tchar.h>

#include <windows.h>

LONG DefWndProc=NULL;

 

LRESULT CALLBACK EditNewProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);

BOOL CALLBACK TestSampleProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)

{

         DialogBox(hInstance,MAKEINTRESOURCE(IDD_TEST_SAMPLE,0,TestSampleProc);

         return 0;

}

LRESULT CALLBACK EditNewProc(HWND hwnd,UINT msg,WPARAM  wParam,LPARAM lParam)

{

 

//        return CallWindowProc((WNDPROC)DefWndProc,hwnd,msg,wParam,lParam);

          if(msg==WM_CHAR)

          {

                       .......

          }

}

BOOL CALLBACK TestSampleProc(HWND hwnd,MSG msg,WPARAM wParam,LPARAM lParam)

{

           LONG DlgWndProc,EditWndProc;

           TCHAR buf[128];

            switch(msg)

            {

                            case WM_INITDIALOG:

                                           DlgWndProc=GetWindowLong(hwnd,GWL_WNDPROC);

                                           EditWndProc=GetWindowLong(GetDlgItem(hwnd,IDC_TEST),GWL_WNDPROC);

                                           wsprintf(buf,_T("DlgWndProc  :%08X\nEditWndProc  :  %08X\nDefWndProc:%08X\n"),

                                                             DlgWndProc,EditWndProc,DefWndProc);

                                            MessageBox(hwnd,buf,_T("zileihuazhiqian"),0);

                                            DefWndProc=GetDlgItem(hwnd,IDC_TEST),GWL_WNDPROC,(LONG)EditNewProc);

                                            return true;

                                  case WM_COMMAND:

                                             switch(LOWORD(wParam))

                                               {

                                                               case IDCANCEL:

                                                                        EndDialog(hwnd,0);

                                                                        return true;

                                               }

                                               break;

                                   case WM_LBUTTONDOWN:

                                                         DlgWndProc=GetWindowLong(hwnd,GWL_WNDPROC);

                                                          EditWndProc=GetWindowLong(GetDlgItem(hwnd,IDC_TEST),GWL_WNDPROC);

                                                           wsprintf(buf,_T("DlgWndProc : %08X\nEditWndProc: 08X\nDefWndProc : 08X\n"),

                                                                               DlgWndProc,EditWndProc,DefWndProc);

                                                            MessageBox(hwnd,buf,_T("zileihuazhihou"),0);

                                                            return false;

            }

           return false;

          

}

 

阅读更多
文章标签: windows callback command
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭