窗体皮肤实现 - 在VC中简单实现绘制(五)
到第四部分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
窗体皮肤实现 - 在VC中简单实现绘制(五)的更多相关文章
- VC++ 中简单操作MP3音乐的方法,小结
#include <windows.h> #include <stdio.h> #include <mmsystem.h> #include <shellap ...
- VC中实现文字竖排的简单方法
好多人都觉得在VC中实现文字竖排是一件很难的事情,其实可以使用“躺”着的字体很方便的实现文字竖排. Windows中有一些字体是“躺”着的,例如:@Fixedsys.@System.@宋体.@黑体等等 ...
- 窗体皮肤实现 - 增加Toolbar的交互性
稍微改造一下,让交互性更好点.增加提示和动态效果. 控件实现内容: 1.加入Hint提示 2.加入了简易动画效果,鼠标进入和离开会有个渐变效果. 实现方案: 1.基类选用 2.Action的关联 3. ...
- VC++编写简单串口上位机程序
VC++编写简单串口上位机程序 转载: http://blog.sina.com.cn/s/articlelist_1809084904_0_1.html VC++编写简单串口上位机程序 串口通信 ...
- 深度解析VC中的消息传递机制
摘要:Windows编程和Dos编程,一个很大的区别就是,Windows编程是事件驱动,消息传递的.所以,要学好Windows编程,必须 对消息机制有一个清楚的认识,本文希望能够对消息的传递做一个全面 ...
- [转]VC中调用外部exe程序方式
本文转自:http://blog.sina.com.cn/s/blog_486285690100ljwu.html 目前知道三种方式:WinExec,ShellExecute ,CreateProce ...
- 深度解析VC中的消息
消息是指什么? 消息系统对于一个win32程序来说十分重要,它是一个程序运行的动力源泉.一个消息,是系统定义的一个32位的值,他唯一的定义了一个事件,向Windows发出一个通知,告诉应用程序某个事情 ...
- 深度解析VC中的消息(转发)
http://blog.csdn.net/chenlycly/article/details/7586067 这篇转发的文章总结的比较好,但是没有告诉我为什么ON_MESSAGE的返回值必须是LRES ...
- VC++中StretchBlt图像失真问题的解决办法
在 VC 中使用 StretchBlt 会碰到一些与点阵图大小缩放相关的一些问题.在扩展一个点阵图时,StretchBlt必须复制图素行或列.如果放大倍数不是原图的整数倍,那么此操作会造成产生的图像有 ...
随机推荐
- 怎样对CODESOFT中的条形码进行黑白转换
CODESOFT 2015标签设计软件能 够提供无与伦比的灵活性.功能和支持,其面对的用户也是极其的广泛.对于一些需要打印黑白反转条形码的特殊用户,例如使用黑色标签纸的用 户,CODESOFT 2 ...
- c# 控制服务启动停止
public string StartService(string serviceName, bool serviceFlag) { try { using (System.ServiceProces ...
- Oracle 事务
begin begin savepoint p1; DELETE FROM sys_re_xxx; //红色部分替换为需要一起执行的SQL即可 DELETE FROM SYS_xxxx; ...... ...
- MSP430推荐网站
http://www.amobbs.com/thread-5092914-1-1.html http://www.amobbs.com/thread-4701106-1-1.html
- yii学习小结
对yii框架搭建的平台运维过程中,会不断地发现很多新的特性和问题,现一一记录下来,便于后续学习~ 1.日志 在/runtime目录中 参考:http://www.cnblogs.com/you ...
- OpenJudge 取数游戏
描述 我们来玩一个游戏:自然数1到N,按顺序列成一排,你可以从中取走任意个数,但是相邻的两个不可以同时被取走.如果你能算出一共有多少种取法,那么你会被天神Lijiganjun奖励. 输入 仅包含一个数 ...
- ASPxTreeList控件去根节点的新增修改操作(写在onCommandColumnButtonInitialize()事件中)
treelist去掉根节点按钮效果图: //去掉父节点及子节点旁的新增.修改.删除操作(写在onCommandColumnButtonInitialize事件中) protected void Tre ...
- CSS常用布局整理(二)
1-2-1单列变宽布局 side列定宽300px,content列变宽,尺寸是100%-300px.核心的问题就是浮动列的宽度应该等于“100% - 300px”,而CSS显然不支持这种带有减法运算的 ...
- Compass是什么
Compass是一个强大的Sass框架,他的设计目标是顺畅.搞笑地装扮互联网,使用它的人可以写出可维护性更高的样式表. Compass由三个主要部分组成:混混合器和实用工具类库,能够集成到应用开发环境 ...
- max subquence sum(n^2)
#include<cstdio>#include<cstring>const int maxn=100005;int buf[maxn];int main(){ freopen ...