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

📄 optionsfont.c

📁 支持Unicode及Uniscribe的多语言输入的文本编辑器源码。
💻 C
📖 第 1 页 / 共 2 页
字号:
LONG CALLBACK PreviewWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	RECT		rect;
	PAINTSTRUCT ps;
	HANDLE		hold;

	switch(msg)
	{
	case WM_ERASEBKGND:
		return 1;

	case WM_PAINT:
		BeginPaint(hwnd, &ps);
		
		GetClientRect(hwnd, &rect);

		FrameRect(ps.hdc, &rect, GetSysColorBrush(COLOR_3DSHADOW));
		InflateRect(&rect, -1, -1);

		SetTextColor(ps.hdc, g_crPreviewFG);
		SetBkColor(ps.hdc, g_crPreviewBG);

		ExtTextOut(ps.hdc, 0, 0, ETO_OPAQUE, &rect, 0, 0, 0);
		hold = SelectObject(ps.hdc, g_hPreviewFont);

		DrawText(ps.hdc, _T("Sample Text"), -1, &rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
		
		SelectObject(ps.hdc, hold);
		EndPaint(hwnd, &ps);
		return 0;
	}

	return CallWindowProc(oldPreviewProc, hwnd, msg, wParam, lParam);
}

void AddColourListItem(HWND hwnd, UINT uItem, int fgIdx, int bgIdx, TCHAR *szName)
{
	HWND hwndCtrl = GetDlgItem(hwnd, uItem);
	int idx = SendMessage(hwndCtrl, LB_ADDSTRING, 0, (LONG)szName);
	SendMessage(hwndCtrl, LB_SETITEMDATA, idx, MAKELONG(fgIdx, bgIdx));
}

void AddColourComboItem(HWND hwnd, UINT uItem, COLORREF col, TCHAR *szName)
{
	HWND hwndCtrl = GetDlgItem(hwnd, uItem);
	int idx = SendMessage(hwndCtrl, CB_ADDSTRING, 0, (LONG)szName);
	SendMessage(hwndCtrl, CB_SETITEMDATA, idx, col);
}

void UpdatePreviewPane(HWND hwnd)
{
	TCHAR szFaceName[200];
	int idx = SendDlgItemMessage(hwnd, IDC_FONTLIST, CB_GETCURSEL, 0, 0);
	int size;
	DWORD data;

	SendDlgItemMessage(hwnd, IDC_FONTLIST, CB_GETLBTEXT, idx, (LONG)szFaceName);

	size = GetDlgItemInt(hwnd, IDC_SIZELIST, 0, FALSE);

	if(g_hPreviewFont != 0)
		DeleteObject(g_hPreviewFont);

	g_hPreviewFont = EasyCreateFont(size, 
								IsDlgButtonChecked(hwnd, IDC_BOLD),
								g_tempFontSmoothing,
								szFaceName);

	idx  = SendDlgItemMessage(hwnd, IDC_ITEMLIST, LB_GETCURSEL, 0, 0);
	data = SendDlgItemMessage(hwnd, IDC_ITEMLIST, LB_GETITEMDATA, idx, 0);


	if((short)LOWORD(data) >= 0)
		g_crPreviewFG = REALIZE_SYSCOL(g_rgbTempColourList[LOWORD(data)]);
	else
		g_crPreviewFG = GetSysColor(COLOR_WINDOWTEXT);

	if((short)HIWORD(data) >= 0)
		g_crPreviewBG = REALIZE_SYSCOL(g_rgbTempColourList[HIWORD(data)]);
	else
		g_crPreviewBG = GetSysColor(COLOR_WINDOW);

	InvalidateRect(GetDlgItem(hwnd, IDC_PREVIEW), 0, TRUE); 
}

BOOL PickColour(HWND hwndParent, COLORREF *col, COLORREF *custCol)
{
	CHOOSECOLOR cc = { sizeof(cc) };
	COLORREF    custTmp[16];

	memcpy(custTmp, custCol, sizeof(custTmp));

	cc.Flags			= CC_ANYCOLOR|CC_FULLOPEN|CC_SOLIDCOLOR|CC_RGBINIT;
	cc.hwndOwner		= hwndParent;
	cc.lpCustColors		= custTmp;
	cc.rgbResult		= *col;

	if(ChooseColor(&cc))
	{
		*col = cc.rgbResult;
		memcpy(custCol, custTmp, sizeof(custTmp));
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

//
//
//
void SelectColorInList(HWND hwnd, UINT uComboIdx, short itemIdx)
{
	HWND hwndCombo = GetDlgItem(hwnd, uComboIdx);
	int  i;

	if(itemIdx == (short)-1)
	{
		SendMessage(hwndCombo, CB_SETCURSEL, 0, 0);
		EnableWindow(hwndCombo, FALSE);
		EnableWindow(GetWindow(hwndCombo, GW_HWNDNEXT), FALSE);
		return;
	}
	else
	{
		EnableWindow(hwndCombo, TRUE);
		EnableWindow(GetWindow(hwndCombo, GW_HWNDNEXT), TRUE);
	}

	if(itemIdx < 0 || itemIdx >= TXC_MAX_COLOURS)
		return;

	// update the Auto item
	SendMessage(hwndCombo, CB_SETITEMDATA, 0, g_rgbAutoColourList[itemIdx]);

	// remove the custom entry (if any)
	SendMessage(hwndCombo, CB_DELETESTRING, NUM_DEFAULT_COLOURS, 0);

	// if an "AUTO" colour
	if((g_rgbTempColourList[itemIdx] & 0x80000000) ||
		g_rgbTempColourList[itemIdx] == g_rgbAutoColourList[itemIdx])
	{
		SendMessage(hwndCombo, CB_SETCURSEL, 0, 0);
	}
	// normal colour
	else
	{
		// try to match current colour with a default colour
		for(i = 1; i < NUM_DEFAULT_COLOURS; i++)
		{
			if(g_rgbTempColourList[itemIdx] == CUSTCOL[i].cr)
			{
				SendMessage(hwndCombo, CB_SETCURSEL, i, 0);
				break;
			}
		}
		
		// if we didn't match the colour, add it as a custom entry
		if(i == NUM_DEFAULT_COLOURS)
		{
			i = SendMessage(hwndCombo, CB_ADDSTRING, 0, (LONG)_T("Custom"));
			SendMessage(hwndCombo, CB_SETITEMDATA, i, g_rgbTempColourList[itemIdx]);
			SendMessage(hwndCombo, CB_SETCURSEL, i, 0);
		}
	}
}

BOOL InitFontOptionsDlg(HWND hwnd)
{
	HWND	hwndPreview;
	int		i;
	LOGFONT lf;
	HFONT   hDlgFont;
	TCHAR	ach[LF_FACESIZE];

	// make a temporary copy of the current colour settings
	memcpy(g_rgbTempColourList, g_rgbColourList, sizeof(COLORREF) * TXC_MAX_COLOURS);

	//
	//	Load the TrueType icon for the font-list
	//
	g_hIcon2 = LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDI_ICON2), IMAGE_ICON, 16, 16, 0);
	g_hIcon3 = LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDI_ICON3), IMAGE_ICON, 16, 16, 0);
	
	//
	//	Create two fonts (normal+bold) based on current dialog's font settings
	//
	hDlgFont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
	GetObject(hDlgFont, sizeof(lf), &lf);
		
	g_hNormalFont = CreateFontIndirect(&lf);
	lf.lfWeight   = FW_BOLD;
	g_hBoldFont   = CreateFontIndirect(&lf);

	//
	//	Manually set the COMBO item-heights because WM_MEASUREITEM has already
	//  been sent and we missed it..
	//
	SetComboItemHeight(GetDlgItem(hwnd, IDC_FGCOLCOMBO), 14);
	SetComboItemHeight(GetDlgItem(hwnd, IDC_BGCOLCOMBO), 14);
	SetComboItemHeight(GetDlgItem(hwnd, IDC_FONTLIST), 16);
	
	g_tempFontSmoothing = g_nFontSmoothing;
	g_hPreviewFont = EasyCreateFont(g_nFontSize, g_fFontBold, g_tempFontSmoothing, g_szFontName);

	// Create the list of fonts
	FillFontComboList(GetDlgItem(hwnd, IDC_FONTLIST));

	// Update the font-size-list
	InitSizeList(hwnd);
	
	//
	//	Subclass the PREVIEW static control so we can custom-draw it
	//
	hwndPreview = GetDlgItem(hwnd, IDC_PREVIEW);
	oldPreviewProc = (WNDPROC)SetWindowLongPtr(hwndPreview, GWLP_WNDPROC, (LONG)PreviewWndProc);
	
//	g_rgbAutoColourList[TXC_LONGLINE]	= MixRGB(GetSysColor(COLOR_3DFACE), GetSysColor(COLOR_WINDOW));
//	g_rgbAutoColourList[TXC_LINENUMBER]	= MixRGB(GetSysColor(COLOR_3DFACE), GetSysColor(COLOR_WINDOW));
//	g_rgbAutoColourList[TXC_LINENUMBER] = MixRGB(g_rgbAutoColourList[TXC_LINENUMBER], GetSysColor(COLOR_WINDOW));

	//g_rgbAutoColourList[TXC_LONGLINE]	= MixRGB(GetSysColor(COLOR_3DFACE), GetSysColor(COLOR_WINDOW));
	//g_rgbAutoColourList[TXC_LINENUMBER]	= MixRGB(GetSysColor(COLOR_3DFACE), GetSysColor(COLOR_WINDOW));
	//g_rgbAutoColourList[TXC_LINENUMBER] = MixRGB(g_rgbAutoColourList[TXC_LINENUMBER], GetSysColor(COLOR_WINDOW));

	AddColourListItem(hwnd, IDC_LIST1, TXC_FOREGROUND,		TXC_BACKGROUND,   _T("Text"));
	AddColourListItem(hwnd, IDC_LIST1, TXC_HIGHLIGHTTEXT,	TXC_HIGHLIGHT,    _T("Selected Text"));
	AddColourListItem(hwnd, IDC_LIST1, TXC_HIGHLIGHTTEXT2,  TXC_HIGHLIGHT2,   _T("Inactive Selection"));
	AddColourListItem(hwnd, IDC_LIST1, TXC_SELMARGIN1,		TXC_SELMARGIN2,   _T("Left Margin"));
	AddColourListItem(hwnd, IDC_LIST1, TXC_LINENUMBERTEXT,  TXC_LINENUMBER,   _T("Line Numbers"));
	AddColourListItem(hwnd, IDC_LIST1, -1,					TXC_LONGLINE,	  _T("Long Lines"));
	AddColourListItem(hwnd, IDC_LIST1, TXC_CURRENTLINETEXT, TXC_CURRENTLINE,  _T("Current Line"));
	
	SendDlgItemMessage(hwnd, IDC_ITEMLIST, LB_SETCURSEL, 0, 0);
	PostMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDC_ITEMLIST, LBN_SELCHANGE), (LPARAM)GetDlgItem(hwnd, IDC_ITEMLIST));

	for(i = 0; i < NUM_DEFAULT_COLOURS; i++)
	{
		AddColourComboItem(hwnd, IDC_FGCOLCOMBO, CUSTCOL[i].cr, CUSTCOL[i].szName);
		AddColourComboItem(hwnd, IDC_BGCOLCOMBO, CUSTCOL[i].cr, CUSTCOL[i].szName);
	}
	
	SendDlgItemMessage(hwnd, IDC_FGCOLCOMBO, CB_SETCURSEL, 1, 0);
	SendDlgItemMessage(hwnd, IDC_BGCOLCOMBO, CB_SETCURSEL, 0, 0);
	
	SendDlgItemMessage(hwnd, IDC_SPIN1, UDM_SETRANGE, 0, MAKELONG(10,0));
	SendDlgItemMessage(hwnd, IDC_SPIN2, UDM_SETRANGE, 0, MAKELONG(10,0));

	//
	//	Select
	//
	_stprintf(ach, _T("%d"), g_nFontSize);

	SendDlgItemMessage(hwnd, IDC_SIZELIST, CB_SELECTSTRING, -1, (LONG)ach);
	SendDlgItemMessage(hwnd, IDC_FONTLIST, CB_SELECTSTRING, -1, (LONG)g_szFontName);

	SetDlgItemInt(hwnd, IDC_PADDINGA, g_nPaddingAbove, 0);
	SetDlgItemInt(hwnd, IDC_PADDINGB, g_nPaddingBelow, 0);

	if((g_fPaddingFlags & COURIERNEW) && lstrcmpi(g_szFontName, _T("Courier New")) == 0)
	{
		SetDlgItemInt(hwnd, IDC_PADDINGA, g_nPaddingAbove, 0);
		SetDlgItemInt(hwnd, IDC_PADDINGB, g_nPaddingBelow, 1);
	}

	if((g_fPaddingFlags & LUCIDACONS) && lstrcmpi(g_szFontName, _T("Lucida Console")) == 0)
	{
		//SetDlgItemInt(hwnd, IDC_PADDINGA, g_nPaddingAbove, 2);
		//SetDlgItemInt(hwnd, IDC_PADDINGB, g_nPaddingBelow, 1);
		//SendDlgItemMessage(hwnd, IDC_
	}

	CheckDlgButton(hwnd, IDC_BOLD,    g_fFontBold);

	UpdatePreviewPane(hwnd);

	return TRUE;
}

BOOL CALLBACK AdvancedDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	char lookup[] = 
	{ 
		1,		// DEFAULT_QUALITY
		1,		// ??
		1,		// ??
		0,		// NONANTIALIASED_QUALITY
		2,		// ANTIALIASED_QUALITY
		3,		// CLEARTYPE_QUALITY 
	};

	switch(msg)
	{
	case WM_INITDIALOG:
		
		AddComboStringWithData(hwnd, IDC_COMBO1, _T("None"),		NONANTIALIASED_QUALITY);
		AddComboStringWithData(hwnd, IDC_COMBO1, _T("Default"),		DEFAULT_QUALITY);
		AddComboStringWithData(hwnd, IDC_COMBO1, _T("Antialiased"), ANTIALIASED_QUALITY);
		AddComboStringWithData(hwnd, IDC_COMBO1, _T("ClearType"),	CLEARTYPE_QUALITY);

		SendDlgItemMessage(hwnd, IDC_COMBO1, CB_SETCURSEL, lookup[g_tempFontSmoothing], 0);

		return TRUE;

	case WM_CLOSE:
		EndDialog(hwnd, FALSE);
		return TRUE;

	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDCANCEL:
			EndDialog(hwnd, FALSE);
			return TRUE;

		case IDOK:
			g_tempFontSmoothing = GetCurrentComboData(hwnd, IDC_COMBO1);
			EndDialog(hwnd, TRUE);
			return TRUE;
		}

		return FALSE;
	}

	return FALSE;
}

//BOOL FontOptions(HWND hwnd, WPARAM wParam, 
//
//	Dialogbox procedure for the FONT pane
//
BOOL CALLBACK FontOptionsDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	PSHNOTIFY *pshn;

	switch(msg)
	{
	case WM_INITDIALOG:
		CenterWindow(GetParent(hwnd));
		return InitFontOptionsDlg(hwnd);

	case MSG_UPDATE_PREVIEW:
		UpdatePreviewPane(hwnd);
		return TRUE;

	case WM_DESTROY:
		DeleteObject(g_hNormalFont);
		DeleteObject(g_hBoldFont);
		DeleteObject(g_hPreviewFont);
		return TRUE;

	case WM_MEASUREITEM:
		// can't do anything here because we haven't created
		// the fonts yet, so send a manual CB_SETITEMHEIGHT instead
		return FALSE;

	case WM_DRAWITEM:

		if(wParam == IDC_FONTLIST)
			return FontCombo_DrawItem(hwnd, (DRAWITEMSTRUCT *)lParam);

		else if(wParam == IDC_FGCOLCOMBO || wParam == IDC_BGCOLCOMBO)
			return ColourCombo_DrawItem(hwnd, wParam, (DRAWITEMSTRUCT *)lParam, FALSE);

		return FALSE;

	case WM_NOTIFY:

		pshn = (PSHNOTIFY *)lParam;

		if(pshn->hdr.code == NM_CUSTOMDRAW)
		{
			if(pshn->hdr.idFrom == IDC_FONTLIST)
			{
				return FALSE;
			}
			return FALSE;
		}		
		else if(pshn->hdr.code == PSN_APPLY)
		{
			g_nFontSize = GetDlgItemInt(hwnd, IDC_SIZELIST, 0, 0);
			g_fFontBold = IsDlgButtonChecked(hwnd, IDC_BOLD);

			GetDlgItemText(hwnd, IDC_FONTLIST, g_szFontName, LF_FACESIZE);	

			g_nPaddingAbove = GetDlgItemInt(hwnd, IDC_PADDINGA, 0, 0);
			g_nPaddingBelow = GetDlgItemInt(hwnd, IDC_PADDINGB, 0, 0);

			memcpy(g_rgbColourList, g_rgbTempColourList, sizeof(COLORREF) * TXC_MAX_COLOURS);

			g_nFontSmoothing = g_tempFontSmoothing;
			
			return TRUE;
		}

		return FALSE;

	case WM_COMMAND:

		switch(LOWORD(wParam))
		{
		case IDC_ADVANCED:
	
			if(DialogBoxParam(g_hResourceModule, MAKEINTRESOURCE(IDD_FONTEXTRA), hwnd, AdvancedDlgProc, 0))
			{
				UpdatePreviewPane(hwnd);
			}

			return TRUE;

		case IDCANCEL:
			return TRUE;

		case IDC_FONTLIST:
			if(HIWORD(wParam) == CBN_SELCHANGE)
			{
				InitSizeList(hwnd);
			}
				
			PostMessage(hwnd, MSG_UPDATE_PREVIEW, 0, 0);
			return TRUE;

		case IDC_ITEMLIST:

			if(HIWORD(wParam) == CBN_SELCHANGE)
			{
				DWORD itemidx = GetCurrentListData(hwnd, IDC_ITEMLIST);

				SelectColorInList(hwnd, IDC_FGCOLCOMBO, LOWORD(itemidx));
				SelectColorInList(hwnd, IDC_BGCOLCOMBO, HIWORD(itemidx));

				UpdatePreviewPane(hwnd);
			}

			return TRUE;

		case IDC_SIZELIST:
			
			if(HIWORD(wParam) == CBN_SELCHANGE || 
			   HIWORD(wParam) == CBN_EDITCHANGE)
			{
				PostMessage(hwnd, MSG_UPDATE_PREVIEW, 0, 0);
			}

			return TRUE;

		case IDC_FGCOLCOMBO:
		case IDC_BGCOLCOMBO:
			
			if(HIWORD(wParam) == CBN_SELCHANGE)
			{
				short fgidx = LOWORD(GetCurrentListData(hwnd, IDC_ITEMLIST));
				short bgidx = HIWORD(GetCurrentListData(hwnd, IDC_ITEMLIST));

				if(fgidx >= 0)
					g_rgbTempColourList[fgidx] = GetCurrentComboData(hwnd, IDC_FGCOLCOMBO);
				
				if(bgidx >= 0)
					g_rgbTempColourList[bgidx] = GetCurrentComboData(hwnd, IDC_BGCOLCOMBO);

				PostMessage(hwnd, MSG_UPDATE_PREVIEW, 0, 0);
			}

			return TRUE;
			
		case IDC_BOLD:
			PostMessage(hwnd, MSG_UPDATE_PREVIEW, 0, 0);
			return TRUE;

		case IDC_CUSTBUT1:
			{
				COLORREF col = 0;
				int idx = LOWORD(GetCurrentListData(hwnd, IDC_ITEMLIST));

				if(idx >= 0)
					col = REALIZE_SYSCOL(g_rgbTempColourList[idx]);
				
				if(PickColour(hwnd, &col, g_rgbCustColours))
				{
					g_rgbTempColourList[idx] = col;
					SelectColorInList(hwnd, IDC_FGCOLCOMBO, (short)idx);
					UpdatePreviewPane(hwnd);
				}
			}
			return TRUE;

		case IDC_CUSTBUT2:
			{
				COLORREF col = 0;
				int idx = HIWORD(GetCurrentListData(hwnd, IDC_ITEMLIST));
				
				if(idx >= 0)
					col = REALIZE_SYSCOL(g_rgbTempColourList[idx]);
				
				if(PickColour(hwnd, &col, g_rgbCustColours))
				{
					g_rgbTempColourList[idx] = col;
					SelectColorInList(hwnd, IDC_BGCOLCOMBO, (short)idx);
					UpdatePreviewPane(hwnd);
				}
			}
			return TRUE;
		}

		return FALSE;
	}

	return FALSE;
}

⌨️ 快捷键说明

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