MFC中自定义窗口类名技巧_第1页
MFC中自定义窗口类名技巧_第2页
MFC中自定义窗口类名技巧_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、转载MFC 中封装很多常用的控件,把类名也给封装了,没有提供明显的接口出来,用 win api写窗口程序,第一步就是注册窗口类此时类名和标题名是一起注册的, 所以能把标题很好地让用户来设定, 类名也应该是很简单的,可惜的是MFC 没有这样做,原因也许是window name 可以不停的改,而类名不能。窗口的类名是有Create 来确定的,要在Create 前,给窗口选择一个已经注册的窗口类名,作为参数窗口 Create 就 ok 了, CWnd 的 Create 最终还是到了 CreateEx 中来,看看CreateEx就会清楚许多BOOL CWnd:CreateEx(DWord dwExSt

2、yle, LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, LPVOID lpParam /* = NULL */) return CreateEx(dwExStyle, lpszClassName, lpszWindowName, dwStyle, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, pParentWnd-GetSafeHwnd

3、(), (HMENU)(UINT_PTR)nID, lpParam);BOOL CWnd:CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU nIDorHMenu, LPVOID lpParam) ASSERT(lpszClassName = NULL | AfxIsValidString(lpszClassName)| AfxIsValidAtom

4、(lpszClassName); ENSURE_ARG(lpszWindowName = NULL | AfxIsValidString(lpszWindowName); / allow modification of several common create parameters CREATESTRUCT cs; cs.dwExStyle = dwExStyle; cs.lpszClass = lpszClassName; cs.lpszName = lpszWindowName; cs.style = dwStyle; cs.x = x; cs.y = y; cs.cx = nWidth

5、; cs.cy = nHeight; cs.hwndParent = hWndParent; cs.hMenu = nIDorHMenu; cs.hInstance = AfxGetInstanceHandle(); cs.lpCreateParams = lpParam; if (!PReCreateWindow(cs) PostNcDestroy(); returnFALSE; AfxHookWindowCreate(this); HWNDhWnd= :AfxCtxCreateWindowEx(cs.dwExStyle, cs.lpszClass, cs.lpszName, cs.styl

6、e, cs.x, cs.y, cs.cx, cs.cy, cs.hwndParent,cs.hMenu, cs.hInstance, cs.lpCreateParams);#ifdef_DEBUG if (hWnd = NULL) TRACE(traceAppMsg, 0, Warning: Window creationfailed:GetLastError returns 0 x%8.8Xn, GetLastError(); #endif if(!AfxUnhookWindowCreate() PostNcDestroy(); / cleanup if CreateWindowEx fai

7、ls too soon if (hWnd = NULL) return FALSE; ASSERT(hWnd = m_hWnd); / should have been set in send msg hook return TRUE; 可以看到最后到了 :AfxCtxCreateWindowEx , 可以很容易地知道这里调用了 CreateWindowEx 来创建一个窗口在 前 面 有 一 个 PreCreateWindow(cs) ,而 cs 经 过 PreCreateWindow 处 理 后 , 交给:AfxCtxCreateWindowEx 处理:AfxCtxCreateWindowE

8、x 在中转给 CreateWindowEx , cs.lpszClass 就是类名,可以清楚了AfxCtxCreateWindowEx 的用心良苦我们可以重载的PreCreateWindow ,来修改类名,如下的代码:/ TODO:在 此 添 加 专 用 代 码 和 / 或 调 用 基类 /VERIFY(AfxDeferRegisterClass(AFX_WND_REG); /AfxEndDeferRegisterClass(AF X_WND_REG); /cs.lpszClass= AfxRegisterWndClass(NULL); WNDCLASSwndcls; memset(&wndc

9、ls, 0, sizeof(WNDCLASS); / start with NULL / defaults wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW; /you can specify your own window procedure wndcls.lpfnWndProc = :DefWindowProc; wndcls.hInstance = AfxGetInstanceHandle(); wndcls.hIcon = NULL; / or load a different icon wndcls.hCursor =NULL;

10、wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); wndcls.lpszMenuName = NULL; / Specify your own class name for using FindWindow later wndcls.lpszClassName = _T(MyNewClass); / Register the new class and exit if it fails if(!AfxRegisterClass(&wndcls) TRACE(Class Registration Failedn); return FALSE;

11、 cs.lpszClass = wndcls.lpszClassName; return TRUE; /return CWnd:PreCreateWindow(cs);其实就是为了把一个已经注册的类名字符串传给CreateWindowEx ,从上面代码中的注释中来 看 , 我 还 用 了 一 种 让 系 统 来 生 成 className 的 方 法 AfxRegisterWndClass 。CWnd:PreCreateWindow 不符合我的心意,注释掉了,其实里面也没什么就是判断而已。而在 MFC 中 CWnd 其他派生类就不这么简单了, 不过单纯的修改类名, 就重载这个方法大多 就 ok

12、 了。是的,只是大多数可以的,可惜的是这个方法,对于Dialog 来说并不行,因为它不用CWnd:Create ,也就绕不到PreCreateWindow 上来了, 你可以重载对话框的这个方法, 断点, 是断不下来的。 因为 CDialog 的创建可以直接用系统的 api 来搞,不用再劳驾 CWnd 来中转到 CReateWindowEx 了, 所以 就不能够用上述方法来改对话框的类名了。看下它的创建代码了:BOOLCDialog:Create(LPCTSTRlpszTemplateName,CWnd*pParentWnd) ASSERT(IS_INTRESOURCE(lpszTemplate

13、Name)| AfxIsValidString(lpszTemplateName); m_lpszTemplateName = lpszTemplateName; / used for help if (IS_INTRESOURCE(m_lpszTemplateName) & m_nIDHelp = 0) m_nIDHelp = LOWORD(DWORD_PTR)m_lpszTemplateName);#ifdef _DEBUG if (!_AfxCheckDialogTemplate(lpszTemplateName, FALSE) ASSERT(FALSE); / invalid dial

14、og template name PostNcDestroy(); / cleanup if Create fails too soon return FALSE; #endif /_DEBUG HINSTANCEhInst =AfxFindResourceHandle(lpszTemplateName,RT_DIALOG); HRSRChResource= :FindResource(hInst, lpszTemplateName, RT_DIALOG); HGLOBAL hTemplate =LoadResource(hInst, hResource); BOOL bResult = CreateIndirect(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论