到第四部分Delphi XE3的代码能基本完成窗体界面的绘制。窗口中的其他控件的处理方法也是相同的,截获消息处理消息。

问题这个编译出来的个头可不小。Release版本竟然2.43M,完全是个胖子。系统中应该加入了大量基础代码(如泛型之类),用Delphi7编译出来应该能小一截。

使用默认Release的配置方案,没有第三方控件。

翻译到C++进行实验,发现明显这个个头没法比。Debug版本88.5K...~~~  这货确实小。可惜再小现公司项目也不会考虑用C++开发,有些悲剧。

C版本只实现了部分代码(第一篇内容),并没有全部实现。思路一样,只是换个表示方法而已。

代码中处理了四个消息,还是比较简单。只有 WM_WINDOWPOSCHANGING 消息的处理稍微长些。

WM_NCPAINT                             --- 绘制非客户区

WM_NCCALCSIZE                       --- 重新设置边缘宽度

WM_NCACTIVATE                       --- 程序切换时重绘非客户去

WM_WINDOWPOSCHANGING      --- 重设界面样式

重新绘制非客户区 WM_NCPAINT

// LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case WM_NCPAINT:
hdc = GetWindowDC(hWnd);
PaintNC(hWnd, hdc);
ReleaseDC(hWnd, hdc);
break; // 非客户去绘制
void PaintNC(HWND hWnd, HDC dc)
{
HBRUSH hBrush;
RECT rw;
RECT rc;
POINT pt; GetWindowRect(hWnd, &rw);
GetClientRect(hWnd, &rc);
pt.x = rc.left;
pt.y = rc.top;
ClientToScreen(hWnd, &pt);
OffsetRect(&rc, pt.x - rw.left, pt.y - rw.top);
ExcludeClipRect(dc, rc.left, rc.top, rc.right, rc.bottom); // 扣除客户区 OffsetRect(&rw, -rw.left, -rw.top);
hBrush = CreateSolidBrush(0xBF7B18);
FillRect(dc, &rw, hBrush);
DeleteObject(hBrush);
}

重新设置窗体边缘宽度 WM_NCCALCSIZE

 //  设置窗体边框宽度
case WM_NCCALCSIZE:
((LPNCCALCSIZE_PARAMS)lParam)->rgrc[].left += ;
((LPNCCALCSIZE_PARAMS)lParam)->rgrc[].top += ;
((LPNCCALCSIZE_PARAMS)lParam)->rgrc[].right -= ;
((LPNCCALCSIZE_PARAMS)lParam)->rgrc[].bottom -= ;
break;

切换程序时重绘非客户去WM_NCACTIVATE

     case WM_NCACTIVATE:
PostMessage(hWnd, WM_NCPAINT, , );
break;

设置窗体样式 WM_WINDOWPOSCHANGING

 // 有修改窗体尺寸时重设样式
// case WM_WINDOWPOSCHANGING:
bChanged = FALSE;
if (!gChangeSizeCalled) {
bChanged = (((LPWINDOWPOS)lParam)->flags & SWP_FRAMECHANGED);
if ((((LPWINDOWPOS)lParam)->flags & SWP_NOMOVE) == ){
gWindowSize.left = ((LPWINDOWPOS)lParam)->x;
gWindowSize.top = ((LPWINDOWPOS)lParam)->y;
}
if ((((LPWINDOWPOS)lParam)->flags & SWP_NOSIZE) == ){
bChanged = bChanged || (((LPWINDOWPOS)lParam)->cx != gWindowSize.right) || (((LPWINDOWPOS)lParam)->cy != gWindowSize.bottom);
gWindowSize.right = ((LPWINDOWPOS)lParam)->cx;
gWindowSize.bottom = ((LPWINDOWPOS)lParam)->cy;
}
bChanged = bChanged && ((gWindowSize.right * gWindowSize.bottom) != ); if (bChanged){
gChangeSizeCalled = TRUE;
__try {
hTmp = gRegion;
gRegion = CreateRoundRectRgn(, , gWindowSize.right, gWindowSize.bottom, , );
SetWindowRgn(hWnd, gRegion, TRUE);
if (hTmp)
DeleteObject(hTmp);
}
__finally {
gChangeSizeCalled = FALSE;
}
}
} if (!bChanged)
return DefWindowProc(hWnd,message, wParam, lParam); break;

完整代码文件

 // skin.cpp : Defines the entry point for the application.
// #include "stdafx.h"
#include "skin.h" #define MAX_LOADSTRING 100 // Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name HRGN gRegion = ;
BOOL gChangeSizeCalled = FALSE;
RECT gWindowSize = {,,,}; // Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); void PaintNC(HWND, HDC); int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine); // TODO: Place code here.
MSG msg;
HACCEL hAccelTable; // Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_SKIN, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance); // Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
} hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SKIN)); // Main message loop:
while (GetMessage(&msg, NULL, , ))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} return (int) msg.wParam;
} //
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = ;
wcex.cbWndExtra = ;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SKIN));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SKIN);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex);
} //
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, , CW_USEDEFAULT, , NULL, NULL, hInstance, NULL); if (!hWnd)
{
return FALSE;
} ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd); return TRUE;
} //
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc; HRGN hTmp;
BOOL bChanged;
LRESULT result; switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break; case WM_NCPAINT:
hdc = GetWindowDC(hWnd);
PaintNC(hWnd, hdc);
ReleaseDC(hWnd, hdc);
break; case WM_NCCALCSIZE:
((LPNCCALCSIZE_PARAMS)lParam)->rgrc[].left += ;
((LPNCCALCSIZE_PARAMS)lParam)->rgrc[].top += ;
((LPNCCALCSIZE_PARAMS)lParam)->rgrc[].right -= ;
((LPNCCALCSIZE_PARAMS)lParam)->rgrc[].bottom -= ;
break; case WM_NCACTIVATE:
PostMessage(hWnd, WM_NCPAINT, , );
break; case WM_WINDOWPOSCHANGING:
bChanged = FALSE; if (!gChangeSizeCalled) {
bChanged = (((LPWINDOWPOS)lParam)->flags & SWP_FRAMECHANGED); if ((((LPWINDOWPOS)lParam)->flags & SWP_NOMOVE) == ){
gWindowSize.left = ((LPWINDOWPOS)lParam)->x;
gWindowSize.top = ((LPWINDOWPOS)lParam)->y;
}
if ((((LPWINDOWPOS)lParam)->flags & SWP_NOSIZE) == ){
bChanged = bChanged || (((LPWINDOWPOS)lParam)->cx != gWindowSize.right) || (((LPWINDOWPOS)lParam)->cy != gWindowSize.bottom);
gWindowSize.right = ((LPWINDOWPOS)lParam)->cx;
gWindowSize.bottom = ((LPWINDOWPOS)lParam)->cy;
} bChanged = bChanged && ((gWindowSize.right * gWindowSize.bottom) != ); if (bChanged)
{
gChangeSizeCalled = TRUE;
__try {
hTmp = gRegion;
gRegion = CreateRoundRectRgn(, , gWindowSize.right, gWindowSize.bottom, , );
SetWindowRgn(hWnd, gRegion, TRUE);
if (hTmp)
DeleteObject(hTmp);
}
__finally {
gChangeSizeCalled = FALSE;
}
}
} if (!bChanged)
return DefWindowProc(hWnd,message, wParam, lParam); break; case WM_DESTROY:
PostQuitMessage();
break; case WM_QUIT:
if (gRegion) {
DeleteObject(gRegion);
gRegion = ;
}
return DefWindowProc(hWnd, message, wParam, lParam); default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return ;
} // Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE; case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
} void PaintNC(HWND hWnd, HDC dc)
{
HBRUSH hBrush;
RECT rw;
RECT rc;
POINT pt; GetWindowRect(hWnd, &rw);
GetClientRect(hWnd, &rc);
pt.x = rc.left;
pt.y = rc.top;
ClientToScreen(hWnd, &pt);
OffsetRect(&rc, pt.x - rw.left, pt.y - rw.top); ExcludeClipRect(dc, rc.left, rc.top, rc.right, rc.bottom); OffsetRect(&rw, -rw.left, -rw.top);
hBrush = CreateSolidBrush(0xBF7B18);
FillRect(dc, &rw, hBrush);
DeleteObject(hBrush);
}

单元 skin.cpp

蘑菇房 (moguf.com)

窗体皮肤实现 - 在VC中简单实现绘制(五)的更多相关文章

  1. VC++ 中简单操作MP3音乐的方法,小结

    #include <windows.h> #include <stdio.h> #include <mmsystem.h> #include <shellap ...

  2. VC中实现文字竖排的简单方法

    好多人都觉得在VC中实现文字竖排是一件很难的事情,其实可以使用“躺”着的字体很方便的实现文字竖排. Windows中有一些字体是“躺”着的,例如:@Fixedsys.@System.@宋体.@黑体等等 ...

  3. 窗体皮肤实现 - 增加Toolbar的交互性

    稍微改造一下,让交互性更好点.增加提示和动态效果. 控件实现内容: 1.加入Hint提示 2.加入了简易动画效果,鼠标进入和离开会有个渐变效果. 实现方案: 1.基类选用 2.Action的关联 3. ...

  4. VC++编写简单串口上位机程序

    VC++编写简单串口上位机程序   转载: http://blog.sina.com.cn/s/articlelist_1809084904_0_1.html VC++编写简单串口上位机程序 串口通信 ...

  5. 深度解析VC中的消息传递机制

    摘要:Windows编程和Dos编程,一个很大的区别就是,Windows编程是事件驱动,消息传递的.所以,要学好Windows编程,必须 对消息机制有一个清楚的认识,本文希望能够对消息的传递做一个全面 ...

  6. [转]VC中调用外部exe程序方式

    本文转自:http://blog.sina.com.cn/s/blog_486285690100ljwu.html 目前知道三种方式:WinExec,ShellExecute ,CreateProce ...

  7. 深度解析VC中的消息

    消息是指什么? 消息系统对于一个win32程序来说十分重要,它是一个程序运行的动力源泉.一个消息,是系统定义的一个32位的值,他唯一的定义了一个事件,向Windows发出一个通知,告诉应用程序某个事情 ...

  8. 深度解析VC中的消息(转发)

    http://blog.csdn.net/chenlycly/article/details/7586067 这篇转发的文章总结的比较好,但是没有告诉我为什么ON_MESSAGE的返回值必须是LRES ...

  9. VC++中StretchBlt图像失真问题的解决办法

    在 VC 中使用 StretchBlt 会碰到一些与点阵图大小缩放相关的一些问题.在扩展一个点阵图时,StretchBlt必须复制图素行或列.如果放大倍数不是原图的整数倍,那么此操作会造成产生的图像有 ...

随机推荐

  1. js对象2--工厂模式的由来--杂志

    一:工厂模式引入前提例子 先看一个案例 <script type="text/javascript"> var person= new Object(); //创建一个 ...

  2. C语言程序设计50例(二)(经典收藏)

    [程序11]题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月 后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?1.程序分析: 兔子的规律为数列1,1 ...

  3. Objective-C的内存管理

    一.Objective-C内存管理的对象 1. 值类型:比如int.float.struct等基本数据类型. 值类型会被放入栈中,在内存中占有一块连续的内存空间,遵循先进后出的原则,故不会产生碎片. ...

  4. Markdown 编辑器语法指南

    基本技巧 代码 如果你只想高亮语句中的某个函数名或关键字,可以使用 `function_name()` 实现 通常编辑器根据代码片段适配合适的高亮方法,但你也可以用 ``` 包裹一段代码,并指定一种语 ...

  5. 003.ASP.NET MVC集中管理Session

    原文链接:http://www.codeproject.com/Tips/790387/Session-in-ASP-NET-MVC 1.前言 今天有得有失啊,看到这篇,专心记下里面的精华吧 2.一般 ...

  6. 国内外CDN服务商CNAME特征串调研

    总结 此篇博文给特定需求的人群使用,通过CNAME的某些特征串,确定其使用的是哪家CDN,大多是国外的CDN,国内的CDN厂商只有几个,格式为:[来源地址]+[截图]+[猜测的特征串],整体博文较长, ...

  7. vss error reading from file 解决方法

    vss error reading from file 解决方法 1 若服务器中存在 vss/data/backup目录,请将该目录删掉2 运行cmd cd.. cd C:\Program Files ...

  8. unknown software exception

    打开cmd 输入下面代码,回车,等跑完即解决for %1 in (%windir%\system32\*.dll) do regsvr32.exe /s %1

  9. phpDoc使用说明

    技术文档可以使人们更好得使用他人的代码库,然而,作为开发人员,编写文档确是一件让人头疼的事情.PHPDoc是PHP文档注释的一个标准,它可以实现为代码生成API文档. PHPDoc注释规范: 块注释的 ...

  10. linx 实用操作命令一

    如果apache安装成为linux的服务的话,可以用以下命令操作:service httpd start 启动service httpd restart 重新启动service httpd stop ...