您的位置:首页 > 其它

窗体皮肤实现 - 在VC中简单实现绘制(五)

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

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

// 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 = 0;
BOOL gChangeSizeCalled = FALSE;
RECT gWindowSize = {0,0,0,0};

// 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, 0, 0))
{
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        = 0;
wcex.cbWndExtra        = 0;
wcex.hInstance        = hInstance;
wcex.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SKIN));
wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
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, 0, CW_USEDEFAULT, 0, 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[0].left += 3;
((LPNCCALCSIZE_PARAMS)lParam)->rgrc[0].top += 55;
((LPNCCALCSIZE_PARAMS)lParam)->rgrc[0].right -= 4;
((LPNCCALCSIZE_PARAMS)lParam)->rgrc[0].bottom -= 4;
break;

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

case WM_WINDOWPOSCHANGING:
bChanged = FALSE;

if (!gChangeSizeCalled) {
bChanged = (((LPWINDOWPOS)lParam)->flags & SWP_FRAMECHANGED);

if ((((LPWINDOWPOS)lParam)->flags & SWP_NOMOVE) == 0){
gWindowSize.left = ((LPWINDOWPOS)lParam)->x;
gWindowSize.top = ((LPWINDOWPOS)lParam)->y;
}
if ((((LPWINDOWPOS)lParam)->flags & SWP_NOSIZE) == 0){
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) != 0);

if (bChanged)
{
gChangeSizeCalled = TRUE;
__try {
hTmp = gRegion;
gRegion = CreateRoundRectRgn(0, 0, gWindowSize.right, gWindowSize.bottom, 3, 3);
SetWindowRgn(hWnd, gRegion, TRUE);
if (hTmp)
DeleteObject(hTmp);
}
__finally {
gChangeSizeCalled = FALSE;
}
}
}

if (!bChanged)
return DefWindowProc(hWnd,message, wParam, lParam);

break;

case WM_DESTROY:
PostQuitMessage(0);
break;

case WM_QUIT:
if (gRegion) {
DeleteObject(gRegion);
gRegion = 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: