本文介绍了纯Win32 C开发中的视觉样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,亲爱的Codeproject读者和开发人员,

我想提出一个问题,因为我想发展自己的技能和知识,我希望从Windows 2000开始就使用Microsoft提供的有关Visual Style的一些信息,以在按钮,边框等上添加一些整洁的效果.

当我使用CreateWindow创建一个不使用MS Shell Dlg2替代字体的窗口时,如何在整个应用范围内添加该应用?

当我在窗口上创建子控件并激活视觉样式时,我得到了一个丑陋的白色边框(应该是透明的),我尝试使用WM_CTLCOLORBTN,WM_CTLCOLOREDIT进行严格的方式,但是我认为唯一的方法是对每个子类进行子类化子控件(使用WM_NCPAINT/WM_NCCALCSIZE)并自己绘制视觉样式,这是要走的路吗?


请,如果您需要一些我可以提供的示例代码,我只是不想让该问题向代码中发送垃圾邮件,这会使阅读起来变得更困难.

Hello dear fellow Codeproject readers and developers,

I got a question, as I like to develope my skills and knowledge I would like some input about Visual Style from Microsoft used since Windows 2000 to add some neat effects on buttons, borders etc.

When I create a window using CreateWindow its not using MS Shell Dlg2 substitute font, how can I add that app wide?

When I create child controls on the window and activate visual style then I get an ugly white border, where it is supposed to be transparent, I have tried severeal ways using WM_CTLCOLORBTN, WM_CTLCOLOREDIT, but I think the only way is to subclass each and every child control (using WM_NCPAINT/WM_NCCALCSIZE) and paint the visual style myself, is that the way to go ?


Please, if you need some sample code I can provide it, I just dont feel like spamming this question with code that will make it harder to read

推荐答案


#include <Windows.h>
#include <CommCtrl.h>
#include <Uxtheme.h>
#include <vsstyle.h>
#include <Richedit.h>
#pragma comment(lib,"comctl32.lib")
#pragma comment(lib,"uxtheme.lib")
#pragma comment(linker,"\"/manifestdependency:type=''win32'' name=''Microsoft.Windows.Common-Controls'' version=''6.0.0.0'' processorArchitecture=''*'' publicKeyToken=''6595b64144ccf1df'' language=''*''\"")
LRESULT CALLBACK MyWndProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK MyStaticProc(HWND,UINT,WPARAM,LPARAM);
static TCHAR szAppName[] = TEXT("Jubii");
static HINSTANCE g_hInst = NULL;
WNDPROC MyOriginalStaticProc;
int _stdcall WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpszCmdLine,int nCmdShow)
{
	WNDCLASS wndclass;
	MSG msg;
	HWND hwnd;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
	wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
	wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wndclass.hInstance = hInst;
	wndclass.lpfnWndProc = &MyWndProc;
	wndclass.lpszClassName = szAppName;
	wndclass.lpszMenuName = L"AllControlsInForm";
	wndclass.style = CS_HREDRAW | CS_VREDRAW;

	if(!RegisterClass(&wndclass))
	{
		MessageBox(NULL,L"Couldn''t register window",szAppName,MB_OK|MB_ICONERROR);
		return -1;
	}

	hwnd = CreateWindow(szAppName, // lpClassName
		L"AllControlsInOneForm",
		WS_OVERLAPPEDWINDOW | WS_SYSMENU,
		50, // X
		50, // Y
		640, // Width
		480, // Height
		NULL, // HWNDPARENT
		(HMENU) NULL, // (HMENU) menu
		hInst, // (HINSTANCE) hInst
		NULL // lParam
		);

	g_hInst = hInst;
	InitCommonControls();
	ShowWindow(hwnd,nCmdShow);
	UpdateWindow(hwnd);
	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

// Add MS_SHELLFnt
LRESULT CALLBACK MyStaticProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	static RECT windowRect;
	static RECT borderRect;
	static HTHEME hTheme;
	switch(msg)
	{
	case WM_STYLECHANGED:
	case WM_THEMECHANGED:
		{
			if(::IsThemeActive()&&GetWindowLong(hwnd,GWL_EXSTYLE)&WS_EX_CLIENTEDGE)
			{
				SetWindowLong(hwnd,GWL_EXSTYLE,GetWindowLong(hwnd,GWL_EXSTYLE)^WS_EX_CLIENTEDGE);
			}
			SetWindowPos(hwnd,NULL,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED);
			RedrawWindow(hwnd,NULL,NULL,RDW_INVALIDATE|RDW_NOCHILDREN|RDW_UPDATENOW|RDW_FRAME);
		}
		return 0;
	case WM_NCPAINT:
		{
			LRESULT lr = CallWindowProc(::MyOriginalStaticProc,hwnd,msg,wParam,lParam);
			hTheme = OpenThemeData(hwnd,L"BUTTON");
			if(hTheme)
			{
				RECT rcBorder;
				RECT rcClient;
				int nState;
				HDC hdc = GetWindowDC(hwnd);
				GetWindowRect(hwnd,&rcBorder);
				rcBorder.right -= rcBorder.left; rcBorder.bottom -= rcBorder.top;
				rcBorder.left = rcBorder.top = 0;
				CopyRect(&rcClient,&rcBorder);
				rcClient.left += rcBorder.left;
				rcClient.top += rcBorder.top;
				rcClient.right -= rcBorder.right;
				rcClient.bottom -= rcBorder.bottom;
				ExcludeClipRect(hdc,rcClient.left,rcClient.top,rcClient.right,rcClient.bottom);
				if(::IsThemeBackgroundPartiallyTransparent(hTheme,EP_EDITTEXT,ETS_NORMAL))
				{
					DrawThemeParentBackground(hwnd,hdc,&rcBorder);
				}
				if(!IsWindowEnabled(hwnd))
					nState = ETS_DISABLED;
				else if(SendMessage(hwnd,EM_GETOPTIONS,0,0) & ECO_READONLY)
					nState = ETS_READONLY;
				else
					nState = ETS_NORMAL;
				DrawThemeBackground(hTheme,hdc,EP_EDITTEXT,nState,&rcBorder,NULL);
				CloseThemeData(hTheme);
				ReleaseDC(hwnd,hdc);
				return 0;
			}
			return lr;
		}
	case WM_NCCALCSIZE:
		{
			LRESULT lr = CallWindowProc(::MyOriginalStaticProc,hwnd,msg,wParam,lParam);
			NCCALCSIZE_PARAMS *nsParams = (NCCALCSIZE_PARAMS*)lParam;
			hTheme = OpenThemeData(hwnd,L"EDIT");
			if(hTheme)
			{
				RECT rcClient;
				HDC hdc = GetDC(GetParent(hwnd));
				ZeroMemory(&rcClient,sizeof(RECT));
				if(GetThemeBackgroundContentRect(hTheme,hdc,EP_EDITTEXT,ETS_NORMAL,&nsParams->rgrc[0],&rcClient) == S_OK)
				{
					ReleaseDC(GetParent(hwnd),hdc);
					return WVR_REDRAW;
				}
				ReleaseDC(GetParent(hwnd),hdc);
				CloseThemeData(hTheme);
			}

			return lr;
		}

	default:
		return CallWindowProc(::MyOriginalStaticProc,hwnd,msg,wParam,lParam);
	}
}
LRESULT CALLBACK MyWndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
	static HWND hwndStaticSubClass;
	HDC hdc;
	PAINTSTRUCT ps;
	switch(msg)
	{
	case WM_CREATE:
		{
			HDC hdc = GetWindowDC(hwnd);
			HFONT hFont = CreateFont(
		36,
		20,
		-300,
		0,
		FW_DONTCARE,
		FALSE,
		TRUE,
		FALSE,
		DEFAULT_CHARSET,
		OUT_OUTLINE_PRECIS,
		CLIP_DEFAULT_PRECIS,
		CLEARTYPE_QUALITY,
		VARIABLE_PITCH,
		TEXT("Tahoma")
		);
			SendMessage(hwnd,WM_SETFONT,(WPARAM)hFont,(LPARAM)true);
			hwndStaticSubClass = CreateWindow(L"STATIC",L"Hej",WS_CHILD | WS_VISIBLE, 120,120,200,200,hwnd,(HMENU) NULL,g_hInst,0);
			::MyOriginalStaticProc = (WNDPROC)SetWindowLong(hwndStaticSubClass,GWL_WNDPROC,(LONG)&::MyStaticProc);
			HWND hwndButton = CreateWindow(L"BUTTON",L"Hejsa",WS_CHILD | WS_VISIBLE,75,75,23,23,hwnd,(HMENU) NULL,g_hInst,0);
		}
		return 0;

	case WM_CTLCOLORSTATIC:
		{
			HDC hdcStatic = (HDC)wparam;
			SetTextColor(hdcStatic,RGB(46,46,46));
			SetBkMode(hdcStatic,TRANSPARENT);
			HBRUSH hollow = (HBRUSH) GetStockObject(HOLLOW_BRUSH);
			return (LONG) hollow;
		}
		return 0;
	case WM_PAINT:
		hdc = BeginPaint(hwnd,&ps);
		SetBkMode(hdc,TRANSPARENT);
		TextOut(hdc,10,10,L"Hello World!!!!",::strlen("Hello World!!!!"));
		EndPaint(hwnd,&ps);
		return 0;
	case WM_DESTROY:
		::PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd,msg,wparam,lparam);
	}

}


这篇关于纯Win32 C开发中的视觉样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 20:38