C++学习记录(函数指针)word

上传人:文*** 文档编号:386541 上传时间:2018-07-19 格式:DOCX 页数:11 大小:126.26KB
返回 下载 相关 举报
C++学习记录(函数指针)word_第1页
第1页 / 共11页
C++学习记录(函数指针)word_第2页
第2页 / 共11页
C++学习记录(函数指针)word_第3页
第3页 / 共11页
点击查看更多>>
资源描述
.C+学习记录(函数指针)就像数组名是指向数组第一个元素的常指针一样,函数名也是指向函数的常指针。可以声明一个指向函数的指针变量,并且用这个指针来调用这个函数。这可能非常有用;它使你能编写一个根据用户输入来决定调用哪一个函数的程序。有关函数指针唯一的决窍是理解所指向的对象类型。一个指向整型的指针指向一个整型变量,一个指向函数的指针必须指向一个有正确返回类型和签名的函数。1声明函数指针:void (*pfunc)();int (*pfunc)(int Count1,int Count2);两个*pfunc都是指向函数的指针,第一个函数不带参数并返回空类型;第二个函数带有两个整型参数并返回一个整型变量。2声明函数指针数组就像声明一个指向整型变量的指针数组一样,也可以声明一个指向返回特定值类型和具有特定签名的函数指针数组。void (*pfunc5)(int Count1,int Count2);3函数指针(函数指针数组)作为参数函数指针(函数指针数组)可以传递给别的函数(这些函数可能执行动作,然后用这个指针调用正确的函数)。void OnDraw(void (*func)()if(func)func();4成员函数指针创建成员函数指针,可以用与函数指针相同的语法,加上包含类名和作用域运算符(:)。void (tclass:*pfunc)(int Count1,int Count2);成员函数指针的用法和函数指针的用法一样,不同之处是成员函数指针需要一个正确的类对象来调用是针对它的函数。上机练习:学习实例编程:一个应用窗体框架基类程序(C+)-文件名:TCWin.h/*.TCwin.h (C+)窗体框架基类*/#ifndef _TCWIN_H_#define _TCWIN_H_#include #define TCW_MSG static#define tcw_msg static#define tcWinMain() WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow)#define tcMain() WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow)#define tMain() WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow)/ TCMSGFUNC -struct/ 声明窗体消息接口函数/struct TCMSGFUNCint mfId;void (*msgfunc0)();void (*msgfunc1)(int Count);void (*msgfunc2)(int Count1,int Count2);void (*msgfunc3)(HINSTANCE hInstance,HWND hWnd,WPARAM wParam);/ TCMSG -struct/ 声明窗体消息信息/struct TCMSGUINT msg;UINT mId;TCMSGFUNC msgfunc; /TCMSG GetTCMsg(UINT msg,UINT mId,int mfId,void (*func0)(),void (*func1)(int Count),void .(*func2)(int Count1,int Count2),void (*func3)(HINSTANCE hInstance,HWND hWnd,WPARAM wParam)TCMSG tcmsg;tcmsg.mId=mId;tcmsg.msg=msg;tcmsg.msgfunc.mfId=mfId;tcmsg.msgfunc.msgfunc0=func0;tcmsg.msgfunc.msgfunc1=func1;tcmsg.msgfunc.msgfunc2=func2;tcmsg.msgfunc.msgfunc3=func3;return tcmsg;TCMSG GetTCMsg(UINT msg,TCMSG tmsg,int count)TCMSG tcmsg=0,0,0,0,0,0,0;for(int i=0;im_hDC=GetDC(hWnd);_TCF_-m_hWnd=hWnd;SetTimer(hWnd,1,1,NULL);break;case WM_COMMAND:tcmsg=GetTCMsg(WM_COMMAND,_TCF_-m_Message,_TCF_-m_Message_map);if(tcmsg.msg=WM_COMMAND&tcmsg.msgfunc.mfId=3&tcmsg.msgfunc.msgfunc3)tcmsg.msgfunc.msgfunc3(_TCF_-m_hInstance,hWnd,wParam);return DefWindowProc(hWnd,message,wParam,lParam);break;case WM_PAINT:tcmsg=GetTCMsg(WM_PAINT,_TCF_-m_Message,_TCF_-m_Message_map);if(tcmsg.msg=WM_PAINT&tcmsg.msgfunc.mfId=0&tcmsg.msgfunc.msgfunc0)tcmsg.msgfunc.msgfunc0();break;case WM_SIZE:tcmsg=GetTCMsg(WM_SIZE,_TCF_-m_Message,_TCF_-m_Message_map);if(tcmsg.msg=WM_SIZE&tcmsg.msgfunc.mfId=2&tcmsg.msgfunc.msgfunc2).tcmsg.msgfunc.msgfunc2(LOWORD(lParam),HIWORD(lParam);break;case WM_MOUSEMOVE:tcmsg=GetTCMsg(WM_MOUSEMOVE,_TCF_-m_Message,_TCF_-m_Message_map);if(tcmsg.msg=WM_MOUSEMOVE&tcmsg.msgfunc.mfId=2&tcmsg.msgfunc.msgfunc2)tcmsg.msgfunc.msgfunc2(LOWORD(lParam),HIWORD(lParam);break;case WM_LBUTTONDOWN:tcmsg=GetTCMsg(WM_LBUTTONDOWN,_TCF_-m_Message,_TCF_-m_Message_map);if(tcmsg.msg=WM_LBUTTONDOWN&tcmsg.msgfunc.mfId=1&tcmsg.msgfunc.msgfunc1)tcmsg.msgfunc.msgfunc1(wParam);break;case WM_LBUTTONUP:tcmsg=GetTCMsg(WM_LBUTTONUP,_TCF_-m_Message,_TCF_-m_Message_map);if(tcmsg.msg=WM_LBUTTONUP&tcmsg.msgfunc.mfId=1&tcmsg.msgfunc.msgfunc1)tcmsg.msgfunc.msgfunc1(wParam);break;case WM_RBUTTONDOWN:tcmsg=GetTCMsg(WM_RBUTTONDOWN,_TCF_-m_Message,_TCF_-m_Message_map);if(tcmsg.msg=WM_RBUTTONDOWN&tcmsg.msgfunc.mfId=1&tcmsg.msgfunc.msgfunc1)tcmsg.msgfunc.msgfunc1(wParam);break;case WM_RBUTTONUP:tcmsg=GetTCMsg(WM_RBUTTONUP,_TCF_-m_Message,_TCF_-m_Message_map);if(tcmsg.msg=WM_RBUTTONUP&tcmsg.msgfunc.mfId=1&tcmsg.msgfunc.msgfunc1)tcmsg.msgfunc.msgfunc1(wParam);break;case WM_KEYDOWN:tcmsg=GetTCMsg(WM_KEYDOWN,_TCF_-m_Message,_TCF_-m_Message_map);if(tcmsg.msg=WM_KEYDOWN&tcmsg.msgfunc.mfId=1&tcmsg.msgfunc.msgfunc1)tcmsg.msgfunc.msgfunc1(wParam);break;case WM_KEYUP:tcmsg=GetTCMsg(WM_KEYUP,_TCF_-m_Message,_TCF_-m_Message_map);if(tcmsg.msg=WM_KEYUP&tcmsg.msgfunc.mfId=1&tcmsg.msgfunc.msgfunc1)tcmsg.msgfunc.msgfunc1(wParam);break;case WM_CHAR:tcmsg=GetTCMsg(WM_CHAR,_TCF_-m_Message,_TCF_-m_Message_map);if(tcmsg.msg=WM_CHAR&tcmsg.msgfunc.mfId=1&tcmsg.msgfunc.msgfunc1)tcmsg.msgfunc.msgfunc1(wParam);break;case WM_TIMER:InvalidateRect(hWnd,NULL,FALSE);.break;case WM_DESTROY:PostQuitMessage(0);break;return DefWindowProc(hWnd,message,wParam,lParam);void TCWndFrame:MoveWindow(int x,int y,int width,int height):MoveWindow(m_hWnd,x,y,width,height,true);void TCWndFrame:SetWindowTextW(TCHAR *wname):SetWindowTextW(m_hWnd,wname);void TCWndFrame:SetWindowPos(int x,int y,int cx,int cy):SetWindowPos(m_hWnd,NULL,x,y,cx,cy,0);void TCWndFrame:SetWindowMenu(LPCTSTR menu)m_mEnu=menu;void TCWndFrame:GetClassName(HWND hWnd,LPWSTR lpClassName,int nMaxCount):GetClassName(hWnd,lpClassName,nMaxCount);HWND TCWndFrame:FindWindow(LPCWSTR lpClassName,LPCWSTR lpWindowName)return :FindWindow(lpClassName,lpWindowName);void TCWndFrame:CloseWindow(HWND hWnd):CloseWindow(hWnd);void TCWndFrame:GetWindowText(HWND hWnd,LPWSTR lpString,int nMaxCount):GetWindowText(hWnd,lpString,nMaxCount);/ 宏定义窗体消息信息(主要把派生类的消息函数写入到基类的消息指针处)/ 把基类的全局指针指向当前派生类的指针(正确调用窗体消息接口函数)/#define DECLARE_MESSAGE_MAP() public:TCMSG _message20;int _message_map;#define BEGIN_MESSAGE_MAP(T) T-_message_map=0;_TCF_=T;#define ON_WM_PAINT(T)T-_messageT-_message_map=GetTCMsg(WM_PAINT,T-_message_map,0,T-OnPaint,NULL,NULL,NULL);T-_message_map+;#define ON_WM_SIZE(T)T-_messageT-_message_map=GetTCMsg(WM_SIZE,T-_message_map,2,NULL,NULL,T-.OnSize,NULL);T-_message_map+;#define ON_WM_MOUSEMOVE(T)T-_messageT-_message_map=GetTCMsg(WM_MOUSEMOVE,T-_message_map,2,NULL,NULL,T-OnMouseMove,NULL);T-_message_map+;#define ON_WM_LBUTTONDOWN(T)T-_messageT-_message_map=GetTCMsg(WM_LBUTTONDOWN,T-_message_map,1,NULL,T-OnLButtonDown,NULL,NULL);T-_message_map+;#define ON_WM_LBUTTONUP(T)T-_messageT-_message_map=GetTCMsg(WM_LBUTTONUP,T-_message_map,1,NULL,T-OnLButtonUp,NULL,NULL);T-_message_map+;#define ON_WM_RBUTTONDOWN(T)T-_messageT-_message_map=GetTCMsg(WM_RBUTTONDOWN,T-_message_map,1,NULL,T-OnRButtonDown,NULL,NULL);T-_message_map+;#define ON_WM_RBUTTONUP(T)T-_messageT-_message_map=GetTCMsg(WM_RBUTTONUP,T-_message_map,1,NULL,T-OnRButtonUp,NULL,NULL);T-_message_map+;#define ON_WM_KEYDOWN(T)T-_messageT-_message_map=GetTCMsg(WM_KEYDOWN,T-_message_map,1,NULL,T-OnKeyDown,NULL,NULL);T-_message_map+;#define ON_WM_KEYUP(T)T-_messageT-_message_map=GetTCMsg(WM_KEYUP,T-_message_map,1,NULL,T-OnKeyUp,NULL,NULL);T-_message_map+;.;#define ON_WM_CHAR(T)T-_messageT-_message_map=GetTCMsg(WM_CHAR,T-_message_map,1,NULL,T-OnChar,NULL,NULL);T-_message_map+;#define ON_WM_COMMAND(T)T-_messageT-_message_map=GetTCMsg(WM_COMMAND,T-_message_map,3,NULL,NULL,NULL,T-OnCommand);T-_message_map+;#define END_MESSAGE_MAP(T)T-m_Message_map=T-_message_map;T-m_Message=;/#define TCWinApp(T,W)T-Create(NULL,1);T-SetWindowText(W);T-ShowWindow();T-UpdateWindow();#endif文件 :stdtc.h内容#ifndef _STDTC_H#define _STDTC_H#ifndef _stdtc_h#define _stdtc_h#include #include #include #include .#endif#endif把上面两个文件保存在include文件夹根目录下。下面编程测试TCWin.h文件的执行情况。打开VC2010建立一个Win32空项目,编写程序源码如下:程序源码屏幕截图.程序运行结果屏幕截图
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 群众团体


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!