要想用vc编写出一个漂亮的界面通常是比较费时的,主要原因有两个,一是vc中自带的组件少而不亮,二是无法进行可视化设计。不过,这些都只是针对常用的方法罢了,如果你采用子类化,钩子等技术,可以完全改变这处局面,甚至比delphi等rad工具还要快.本文中就将介绍如何编写一个dll,如何来轻松的定制标题栏。
为了使每个窗体的标题栏都能定制,并且不用为每一个窗体类编码,所以本方法采用钩子技术,其核心思想是监控 windows 消息,处理需要重缓标题的消息,以达到定制标题栏的思想。
本文件介绍的方法将在应用程序中安装 wh_callwndproc 钩子,具体的代码如下所示:
在应用程序启动时安装钩子的代码:
| extern "c" bool __declspec(dllexport) installcallwndhook() { g_hcallwndproc = setwindowshookex(wh_callwndproc, callwndproc, afxgetinstancehandle(),getcurrentthreadid()); if (null == g_hcallwndproc) return false; else return true; } |
| #pragma data_seg("shared") static hhook g_hcallwndproc; #pragma data_seg() |
| extern "c" void __declspec(dllexport) uninstallcallwndhook() { if (g_hcallwndproc != null) { unhookwindowshookex(g_hcallwndproc); g_hcallwndproc = null; } } |
| lresult __declspec(dllexport) callback callwndproc( int code, // hook code wparam wparam, // undefined lparam lparam // address of structure with message data (cwpstruct) ) { dword dwthreadid = (dword)wparam; lpcwpstruct pcwpstruct = lpcwpstruct(lparam); if (hc_action == code) { if ((pcwpstruct->message == wm_mousemove) (pcwpstruct->message == wm_setcursor) (pcwpstruct->message == wm_nchittest) (pcwpstruct->message == wm_kickidle) (pcwpstruct->message == wm_ncmousemove) (pcwpstruct->message == wm_mouseactivate) (pcwpstruct->message > wm_user)); else drawframe(pcwpstruct); } return callnexthookex(g_hcallwndproc, code, wparam, lparam); } |
| char szclassname[128] = {0}; ::getclassname(pcwpstruct->hwnd, szclassname, sizeof(szclassname)); if (strcmp(szclassname, "#32770") != 0) return ; |
| if (bactive) hbitmap = (hbitmap)::loadimage(null, _t("active.bmp"), image_bitmap, nwidth, nheight, lr_loadfromfile); else hbitmap = (hbitmap)::loadimage(null, _t("inactive.bmp"), image_bitmap, nwidth, nheight, lr_loadfromfile); if (null == hbitmap) { drawtitlebar(dcwin, rcncclient, 0); } else { dcmem = ::createcompatibledc(dcwin); holdbitmap = (hbitmap)::selectobject(dcmem, hbitmap); ::stretchblt(dcwin,0,0,nwidth, nheight,dcmem,0,0,nwidth, nheight,srccopy); ::selectobject(dcmem, holdbitmap); ::deletedc(dcmem); } |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器