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

📄 utils.cpp

📁 虚拟打印机
💻 CPP
字号:
/* * * utils.c *   Copyright (C) 2006 Michael H. Overlin   This program is free software; you can redistribute it and/or modify   it under the terms of the GNU General Public License as published by   the Free Software Foundation; either version 2 of the License, or   (at your option) any later version.   This program is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.   You should have received a copy of the GNU General Public License   along with this program; if not, write to the Free Software   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA      Contact at poster_printer@yahoo.com */#include "utils.h"#include "dialogwindow.h"#include "mathutils.h"#include <tchar.h>#include <strsafe.h>// *************************************************************************************8// **  EnumWindows  ********************************************************************8// *************************************************************************************8EnumWindows::EnumWindows(void) {	// NOTHING TO INIT}EnumWindows::~EnumWindows() {	// NOTHING TO DELETE}BOOL EnumWindows::EnumChildWindows(HWND hwndParent) {	BOOL bRetValue = ::EnumChildWindows(hwndParent, EnumWindows::TheirEnumProc, (LPARAM) this);	return bRetValue;}BOOL CALLBACK EnumWindows::TheirEnumProc(HWND hwndEnum, LPARAM lParam) {	EnumWindows *pew = (EnumWindows *) lParam;	BOOL bRetValue = pew->EnumProc(hwndEnum);	return bRetValue;}// *************************************************************************************8// **  MODULE PUBLIC ROUTINES  *********************************************************8// *************************************************************************************8// CALLER IS RESPONSIBLE FOR FREEING THE MALLOC'D BUFFER RETURNED// lptstrExt MUST INLCUDE A PERIOD// FUNCTION CAN DEAL WITH FILE NAME ENDING IN A SINGLE PERIOD LPTSTR AddExtToFileName(LPCTSTR lptstrFileName, LPCTSTR lptstrExt) {	LPTSTR lptstrRetValue = NULL;	DWORD dwCch = ::lstrlen(lptstrFileName);	if (dwCch > 0 && lptstrFileName[dwCch-1] == L'.') {		lptstrRetValue = MyCatStrings(lptstrFileName, lptstrExt+1, NULL);	} else {		lptstrRetValue = MyCatStrings(lptstrFileName, lptstrExt, NULL);	}	return lptstrRetValue;}BOOL CenterWindowHorizontallyInDialog(HWND hwndCtrl, HWND hDlg) {	RECT rDlg;	::GetClientRect(hDlg, &rDlg);	RECT rCtrl;	::GetWindowRect(hwndCtrl, &rCtrl);	::ScreenToClient(rCtrl, hDlg);	RECT rTemp = rCtrl;	::CenterInRect(rTemp, rDlg);	BOOL bRetValue = ::MoveWindow(hwndCtrl, rTemp.left, rCtrl.top, RW(rCtrl), RH(rCtrl), TRUE);	return bRetValue;}BOOL CenterWindowAboveParent(HWND hwnd) {	HWND hwndParent = ::GetParent(hwnd);	RECT rParent;	if (hwndParent != NULL) {		LONG lStyle = ::GetWindowLong(hwnd, GWL_STYLE);		if ( (lStyle & WS_CHILD) != 0 ) {			::GetClientRect(hwndParent, &rParent);		} else {			::GetWindowRect(hwndParent, &rParent);		}	} else {		::SystemParametersInfo(SPI_GETWORKAREA, 0, (PVOID) &rParent, 0);	}	RECT r;	::GetWindowRect(hwnd, &r);	::CenterInRect(r, rParent);	uint uiFlags = SWP_NOSIZE | SWP_NOZORDER;	BOOL b = ::SetWindowPos(hwnd, NULL, r.left, r.top, 0, 0, uiFlags);	return b;}BOOL FileExists(LPCTSTR lptstrFileName) {	BOOL bRetValue = FALSE;	HANDLE hFile = ::CreateFile( 		lptstrFileName,		GENERIC_READ,           		FILE_SHARE_READ,        		NULL,                   		OPEN_EXISTING,        		FILE_ATTRIBUTE_NORMAL,  		NULL                     		);	if (hFile != INVALID_HANDLE_VALUE) {		bRetValue = TRUE;		::CloseHandle(hFile);	}	return bRetValue;}// FUNCTOIN RETURNS TRUE IF FILE NAME CONTAINS A PERIOD IN THE PORTION AFTER ANY DIRECTORY// NAME AND DRIVE NAMEBOOL FileNameSpecifiesExt(LPCTSTR lptstrFileName) {	DWORD dwCch = ::lstrlen(lptstrFileName);	LPCTSTR lptstr = lptstrFileName + dwCch;	while(dwCch --) {		switch( * -- lptstr ) {			case L'.':				return TRUE;			case L':':			case L'\\':				return FALSE;			default:				break;		}	}	return FALSE;}LPTSTR GetFileNameAndExt(LPCTSTR lptstrFilePath) {	DWORD dwCch = ::lstrlen(lptstrFilePath);	LPCTSTR lptstr = lptstrFilePath + dwCch;	while(dwCch --) {		switch( * -- lptstr ) {			case L':':			case L'\\':				return ::StringDuplicate(lptstr+1);			default:				break;		}	}	return ::StringDuplicate(lptstrFilePath);}LPTSTR GetFileExt(LPCTSTR lptstrFilePath) {	DWORD dwCch = ::lstrlen(lptstrFilePath);	LPCTSTR lptstr = lptstrFilePath + dwCch;	while(dwCch --) {		switch( * -- lptstr ) {			case L'.':			case L':':				return ::StringDuplicate(lptstr);			case L'\\':				return NULL;			default:				break;		}	}	return NULL;}LPTSTR GetSystemErrorMessage(IN DWORD dwErr) { 	LPTSTR lptstrRetValue = NULL;    LPTSTR lpMsgBuff = NULL;    DWORD dw = FormatMessage(        FORMAT_MESSAGE_ALLOCATE_BUFFER |         FORMAT_MESSAGE_FROM_SYSTEM,        NULL,        dwErr,        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),        (LPTSTR) &lpMsgBuff,        0, NULL );	if (dw != 0) {		if (lpMsgBuff != NULL) {			lptstrRetValue = ::StringDuplicate(lpMsgBuff);			LocalFree(lpMsgBuff);		}	}	return lptstrRetValue;}BOOL IsFileNameValid(LPCTSTR lptstrFileName) {	BOOL bRetValue = FALSE;	if ( ! ::FileExists(lptstrFileName) ) {		HANDLE hFile = ::CreateFile( 			lptstrFileName,			GENERIC_WRITE,           			FILE_SHARE_READ,        			NULL,                   			CREATE_NEW,        			FILE_ATTRIBUTE_NORMAL,  			NULL                     			);		if (hFile != INVALID_HANDLE_VALUE) {			bRetValue = TRUE;			::CloseHandle(hFile);			::DeleteFile(lptstrFileName);		}	} else {		bRetValue = TRUE;	}	return bRetValue;}LPTSTR MakeFullFileName(LPCTSTR lptstrDir, LPCTSTR lptstrFileName, IN LPCTSTR lptstrExt) {	LPTSTR lptstrRetValue = MyCatStrings(lptstrDir, TEXT("\\"), lptstrFileName, lptstrExt, NULL);	return lptstrRetValue;}LPTSTR MyCatStrings(LPCTSTR lptstrArg, ...) {	va_list vaList;	va_start(vaList, lptstrArg);	LPTSTR lptstrRetValue = ::vMyCatStringsString(FALSE, lptstrArg, vaList);	va_end(vaList);	return lptstrRetValue;}LPTSTR MyCatStringsString(BOOL bIncludeNullTerms, LPCTSTR lptstrArg, ...) {	va_list vaList;	va_start(vaList, lptstrArg);	LPTSTR lptstrRetValue = ::vMyCatStringsString(bIncludeNullTerms, lptstrArg, vaList);	va_end(vaList);	return lptstrRetValue;}LPTSTR vMyCatStringsString(BOOL bIncludeNullTerms, LPCTSTR lptstrArg, va_list& in_vaList) {	DWORD dwCchExtra = (bIncludeNullTerms ? 1 : 0);	LPTSTR lptstrRetValue = NULL;	va_list vaList = in_vaList;	DWORD dwCch = 0;	for(LPCTSTR lptstr = lptstrArg; lptstr != NULL; lptstr = va_arg(vaList, LPCTSTR)) {		dwCch += ::lstrlen(lptstr) + dwCchExtra;	}	++ dwCch;	lptstrRetValue = (LPTSTR) malloc(dwCch * sizeof(TCHAR));	if (lptstrRetValue != NULL) {		LPTSTR lptstrCtr = lptstrRetValue;		vaList = in_vaList;		for(LPCTSTR lptstr = lptstrArg; lptstr != NULL; lptstr = va_arg(vaList, LPCTSTR)) {			dwCch = ::lstrlen(lptstr) + dwCchExtra;			memcpy(lptstrCtr, lptstr, dwCch * sizeof(TCHAR));			lptstrCtr += dwCch;		}		*lptstrCtr = 0;	}	return lptstrRetValue;}LPTSTR *MyCatStringPtrs(LPTSTR lptstrArg, ...) {	LPTSTR *plptstrRetValue = NULL;	va_list vaList;	va_start(vaList, lptstrArg);	uint nStrings = 0;	for(LPCTSTR lptstr = lptstrArg; lptstr != NULL; lptstr = va_arg(vaList, LPCTSTR)) {		++ nStrings;	}	++ nStrings; // INCLUDING SPACE FOR NULL TERMINATOR	va_end(vaList);	plptstrRetValue = (LPTSTR *) malloc(nStrings * sizeof(LPTSTR));	if (plptstrRetValue != NULL) {		va_start(vaList, lptstrArg);		uint k = 0;		for(LPTSTR lptstr = lptstrArg; lptstr != NULL; lptstr = va_arg(vaList, LPTSTR)) {			plptstrRetValue[k ++] = lptstr;		}		plptstrRetValue[k] = NULL;		va_end(vaList);	}	return plptstrRetValue;}void MyFreeStringPtrs(LPTSTR *plptstr) {	for(uint k = 0; plptstr[k]; ++k) {		free(plptstr[k]);	}	free(plptstr);}LPTSTR MyGetWindowText(IN HWND hwnd) {	DWORD dwCchNeeded = ::SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0) + 1;	LPTSTR lptstr = (LPTSTR) malloc( dwCchNeeded * sizeof(TCHAR) );	if (lptstr != NULL) {		lptstr[0] = 0;		::SendMessage(hwnd, WM_GETTEXT, (WPARAM) dwCchNeeded, (LPARAM) lptstr);	}	return lptstr;}void MyProcessMessage(MSG& msg) {	if ( ! ::DialogWindow::TheirIsDialogMessage(msg)) {		if (!TranslateAccelerator(msg.hwnd, NULL, &msg))		{			TranslateMessage(&msg);			DispatchMessage(&msg);		}	}}BOOL ScreenToClient(OUT RECT& r, IN HWND hwnd) {	BOOL bRetValue = ::ScreenToClient(hwnd, &((POINT *) &r)[0]);	bRetValue = ::ScreenToClient(hwnd, &((POINT *) &r)[1]) && bRetValue;	return bRetValue;}size_t StringBufferSize(IN LPCTSTR lptstr) {	size_t szRetValue = 0;	TCHAR atstr[] = TEXT("");	HRESULT hr = StringCbLength(lptstr, STRSAFE_MAX_CCH * sizeof(TCHAR), &szRetValue);	ASSERT(hr == S_OK);	szRetValue += sizeof(atstr);	return szRetValue;}LPTSTR StringDuplicate(IN LPCTSTR lptstr) {	size_t sz = StringBufferSize(lptstr);	LPTSTR lptstrDup = (LPTSTR) malloc(sz);	CopyMemory(lptstrDup, lptstr, sz);	return lptstrDup;}

⌨️ 快捷键说明

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