⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fontcontrols.cpp

📁 在vc环境下通过比较简单的方法实现了修改字体、字体颜色和修改背景颜色等基本功能。
💻 CPP
字号:
/*
FontControl.cpp
coded by: Hound
This is built from the ColorControls example
just added ability to change the fonts as well.
*/
#include "FontControls.h"

LRESULT CALLBACK WndProc(HWND hWnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
	static COLORREF		customcolors[16];
	HINSTANCE			hInst = (HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE);
	static LOGBRUSH		logbrush;

	switch (iMsg)
	{
	case WM_CREATE:
		//here is our combobox we do not need the HMENU parameter in this example
		//since im not checking for any notifications here
		combo = CreateWindow("combobox",NULL,WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST,
				15,15,130,200,hWnd,(HMENU)NULL,hInst,NULL);
				SendMessage(combo,CB_ADDSTRING,0,(LPARAM)(LPCTSTR)"item1");
				SendMessage(combo,CB_ADDSTRING,0,(LPARAM)(LPCTSTR)"item2");
		//here is our edit control
		edit = CreateWindow("edit","edit control",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,
							15,40,130,25,hWnd,(HMENU)NULL,hInst,NULL);
		//here is the listbox
		list = CreateWindow("listbox","",WS_CHILD|WS_VISIBLE|LBS_STANDARD,15,70,100,150,
							hWnd,(HMENU)NULL,hInst,NULL);
				SendMessage(list,LB_ADDSTRING,0,(LPARAM)(LPCTSTR)"item1");
				SendMessage(list,LB_ADDSTRING,0,(LPARAM)(LPCTSTR)"item2");
		//here is the button
		button = CreateWindow("button","Checkbox",WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,15,230,100,20,hWnd,
							(HMENU)NULL,hInst,NULL);
		//static:
		stat = CreateWindow("static","Hi, i'm a static\nColor me!",WS_CHILD|WS_VISIBLE|SS_CENTER,
							150,105,150,50,hWnd,(HMENU)NULL,hInst,NULL);
		//here is the buttons to change to colors
		changetxtcolor = CreateWindow("button","Change Txt Color",WS_VISIBLE|WS_CHILD,
						150,15,150,25,hWnd,(HMENU)CHANGETEXT,hInst,NULL);
		changebkcolor = CreateWindow("button","Change BK Color",WS_VISIBLE|WS_CHILD,
						150,45,150,25,hWnd,(HMENU)CHANGEBKG,hInst,NULL);
		//we need to set up the brush
		logbrush.lbHatch = 0;
		logbrush.lbStyle = BS_SOLID;
		logbrush.lbColor = RGB(255,255,255);	//initially set it to black
		hbrush = CreateBrushIndirect(&logbrush);
			//and finally we set up the choosecolor struct for that common dialog
		SetupChooseColor(&choosecolor,customcolors,hWnd,currentcolor);
		//NEW CODE
		changefont = CreateWindow("button","Change Font",WS_VISIBLE|WS_CHILD,150,75,150,25,
									hWnd,(HMENU)CHANGEFONT,hInst,NULL);
		SetupChooseFont(hWnd,currentfont,logfont);
		currentfont = CreateFontIndirect(&logfont);
			UpdateFonts();
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd,&ps);
			InvalidateRect(hWnd,NULL,true);
		EndPaint(hWnd,&ps);
		break;
		//since all of the controls will have the same color attributes i can
		//switch them all at once like so, use seperate ones if you need but return
		//the brush for each of them
	case WM_CTLCOLORSTATIC:
	case WM_CTLCOLOREDIT:
	case WM_CTLCOLORLISTBOX:
	case WM_CTLCOLORBTN:
		SetTextColor((HDC)wParam,textcolor);
		SetBkMode((HDC)wParam,OPAQUE);	//*look on the bottom for more info
		SetBkColor((HDC)wParam,backcolor);
		logbrush.lbColor = backcolor;
		//now because ive changed the logbrush  have to update the brush
		//if your keeping your control at a constant color just set the text and
		//return the brush, no need for this in that case
		hbrush = CreateBrushIndirect(&logbrush);
		return (long)hbrush;
		break;
	case WM_CLOSE:
		DeleteObject(hbrush);
		DeleteObject(currentfont);
		break;
	case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
		break;
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case CHANGETEXT:
			if(ChooseColor(&choosecolor))
			{     
				currentcolor = choosecolor.rgbResult;//set currentcolor cause
							//we need that variable to have value for the RGB_INIT
				textcolor = currentcolor;
				SendMessage(hWnd,WM_PAINT,0,0);
			}
			break;
		case CHANGEBKG:
			if(ChooseColor(&choosecolor))
			{     
				currentcolor = choosecolor.rgbResult;//set currentcolor cause
							//we need that variable to have value for the RGB_INIT
				backcolor = currentcolor;
				SendMessage(hWnd,WM_PAINT,0,0);
			}
			break;
			//NEW CODE
		case CHANGEFONT:
			if(ChooseFont(&choosefont))//call the dialog for fonts
			{
				currentfont = CreateFontIndirect(&logfont);	//create font from returned values
				UpdateFonts();								//update windows
				SendMessage(hWnd,WM_PAINT,0,0);
			}
			break;

		}
	}
	return DefWindowProc(hWnd,iMsg,wParam,lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
				   PSTR szCmdLine, int iCmdShow)
{
	const char *ClassName = "PCPP_ColorControls";
	WNDCLASSEX wclass;			//here we set up a variable for our window class
	MSG msg;					//this will hold the window messages later on
	//Next we set up the properties of the window class
	wclass.cbSize = sizeof(wclass);			//the size
	wclass.style = CS_HREDRAW | CS_VREDRAW;	//style
	wclass.lpfnWndProc = WndProc;			//this tells windows the function to send messages to
	wclass.cbClsExtra = 0;
	wclass.cbWndExtra = 0;
	wclass.hInstance = hInstance;			//the instance of your program
	wclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);	//which icon to use
	wclass.hCursor = LoadCursor(NULL,IDC_ARROW);	//the cursor to use
	wclass.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);	//background color
	wclass.lpszMenuName = NULL;				//set the menu
	wclass.lpszClassName = ClassName;		//set classname
	wclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION);	//set small icon

	RegisterClassEx(&wclass);		//this registers your window with windows

	main = CreateWindow(ClassName, "Neato Fonts :-)",
						WS_OVERLAPPEDWINDOW|WS_VISIBLE, 200, 200, 320, 300,NULL, NULL, hInstance, NULL);
		

while(GetMessage(&msg,NULL,0,0))	//getmessage loops until a message is on the queue
{									//then it returns focus,peekmessage does the same except returns focus no matter what
	TranslateMessage(&msg);			//translate the message into its char equivelent
	DispatchMessage(&msg);			//dispatch to the window
}
	return msg.wParam;
}
/*
*the reason i made the back of the text OPAQUE and set it to the same color as the
back of the control is only because the results are much better. the color you set to 
the logbrush is the controls color, the color you set when you use SetBkColor is not,
that is the color of the text's background color. you CAN set the bacground mode to TRANSPARENT
and not worry about it but you might get wierd results at times like in the combo box.
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -