c++ 问题记录

1 篇文章 0 订阅

MFC项目中问题汇总

1.使用ffmpeg.exe命令转码音视频文件,代码使用CMD命令一样适用
遇到文件路径中有空格的时候,路径要使用""包括起来

//将m4a转为mp3
std::string S2 = "ffmpeg -i \"" + input + "\" -f mp3 \"" + output + "\" -y";

2.执行CMD命令,不显示CMD窗体

//执行CMD命令
void startCmd(char* cmd, bool wait)
{
	STARTUPINFOA si;
	PROCESS_INFORMATION pi;
	SECURITY_ATTRIBUTES sa;
	sa.nLength = sizeof(sa);
	sa.lpSecurityDescriptor = NULL;
	sa.bInheritHandle = TRUE;
	HANDLE handle = NULL;
	memset(&si, 0, sizeof(STARTUPINFO));
	memset(&pi, 0, sizeof(PROCESS_INFORMATION));
	si.cb = sizeof(STARTUPINFO);
	si.dwFlags |= STARTF_USESTDHANDLES;
	si.wShowWindow = SW_HIDE;
	si.hStdInput = NULL;
	si.hStdError = NULL;
	si.hStdOutput = handle;

	std::string cmdString = cmd;

	if (!CreateProcessA(NULL,
		(LPSTR)cmdString.c_str(),
		NULL,
		NULL,
		TRUE,
		CREATE_NO_WINDOW,
		NULL,
		NULL,
		&si,
		&pi))
	{
		CCLOG("CreateProcess failed:%d", GetLastError());
		return;
	}

	//是否阻塞
	if (wait)
	{
		WaitForSingleObject(pi.hProcess, INFINITE);
	}

	CCLOG("WaitForSingleObject finish return:%d", GetLastError());
	CloseHandle(handle);
	CloseHandle(pi.hThread);
	CloseHandle(pi.hProcess);
}

3.mfc改变鼠标样式

void setCursor(std::string str) {
	HWND parent = getCocosWindow();
	SetCapture(parent);
	HCURSOR hCur = LoadCursor(NULL, MAKEINTRESOURCE(std::atoll(str.c_str())));
	SetCursor(hCur);
	if (str == "32512") {
		ReleaseCapture();
	}
}

4.获取CPUID

std::string GetId()
{
	std::string strCPUId;

	unsigned long s1, s2, s3, s4;
	char buf[32] = { 0 };

	__asm
	{
		mov eax, 01h
		xor edx, edx
		cpuid
		mov s1, eax
		mov s2, ebx
		mov s3, ecx
		mov s4, edx
	}

	if (s1)
	{
		memset(buf, 0, 32);
		sprintf_s(buf, 32, "%08X", s1);
		strCPUId += buf;
	}

	//if (s2)
	//{
	//	memset(buf, 0, 32);
	//	sprintf_s(buf, 32, "%08X", s2);
	//	strCPUId += buf;
	//}

	if (s3)
	{
		memset(buf, 0, 32);
		sprintf_s(buf, 32, "%08X", s3);
		strCPUId += buf;
	}

	if (s4)
	{
		memset(buf, 0, 32);
		sprintf_s(buf, 32, "%08X", s4);
		strCPUId += buf;
	}

	__asm
	{
		mov eax, 03h
		xor ecx, ecx
		xor edx, edx
		cpuid
		mov s1, edx
		mov s2, ecx
	}

	if (s1)
	{
		memset(buf, 0, 32);
		sprintf_s(buf, 32, "%08X", s1);
		strCPUId += buf;
	}

	if (s2)
	{
		memset(buf, 0, 32);
		sprintf_s(buf, 32, "%08X", s2);
		strCPUId += buf;
	}

	return strCPUId;
}

5.去windows窗体标题栏

HWND parent = glfwGetWin32Window(_mainWindow);
CWnd * pCWnd;
pCWnd = CWnd::FromHandle(parent);
pCWnd->ModifyStyle(WS_CAPTION | WS_THICKFRAME, 0, SWP_FRAMECHANGED);

另外还有如下单独效果

pCWnd->ModifyStyle(WS_MAXIMIZEBOX, 0);//禁止最大化按钮
pCWnd->ModifyStyle(WS_MINIMIZEBOX, 0);//禁止最小化按钮
pCWnd->ModifyStyle(WS_SYSMENU, 0);//禁止最大最小关闭按钮

6.获取屏幕分辨率

HWND hWnd = GetDesktopWindow();
HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
// 获取监视器逻辑宽度与高度
MONITORINFOEX miex;
miex.cbSize = sizeof(miex);
GetMonitorInfo(hMonitor, &miex);
// 获取监视器物理宽度与高度
DEVMODE dm;
dm.dmSize = sizeof(dm);
dm.dmDriverExtra = 0;
EnumDisplaySettings(miex.szDevice, ENUM_CURRENT_SETTINGS, &dm);
int cxPhysical = dm.dmPelsWidth;
int cyPhysical = dm.dmPelsHeight;

7.判断文件是否存在

static std::wstring StringUtf8ToWideChar(const std::string& strUtf8)
{
	std::wstring ret;
	if (!strUtf8.empty())
	{
		int nNum = MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, nullptr, 0);
		if (nNum)
		{
			WCHAR* wideCharString = new WCHAR[nNum + 1];
			wideCharString[0] = 0;

			nNum = MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, wideCharString, nNum + 1);

			ret = wideCharString;
			delete[] wideCharString;
		}
		else
		{
			CCLOG("Wrong convert to WideChar code:0x%x", GetLastError());
		}
	}
	return ret;
}
DWORD attr = GetFileAttributesW(StringUtf8ToWideChar("../dygame.sln").c_str());
if (attr == INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY)) {
	//不存在
}

8.计算进程数
需要引入
#include <tlhelp32.h>

static int find_process(std::string process_name)
{
	int count = 0;//进程计数 
	PROCESSENTRY32 pe32;

	pe32.dwSize = sizeof(PROCESSENTRY32);
	HANDLE process_snapshot_handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);//创建进程快照句柄

	if (process_snapshot_handle == INVALID_HANDLE_VALUE) return -1;//创建句柄失败

	bool is_exist = Process32First(process_snapshot_handle, &pe32);//找第一个
	while (is_exist)
	{
		CString asdf = "weiqi.exe";
		if (pe32.szExeFile == asdf) {
			CCLOG("%s", pe32.szExeFile);
		}
		const char* fir = process_name.c_str();
		char* sec = (char*)malloc(512);
		sprintf(sec, "%ws", pe32.szExeFile);
		int ret = stricmp(fir, sec);
		if (ret == 0) {
			count++;//进程名不区分大小写
		}
		is_exist = Process32Next(process_snapshot_handle, &pe32);//找下一个
	}

	return count;
}

9.获取程序所在目录

CString strExePath;
CString strPath;
GetModuleFileNameW(NULL, strPath.GetBufferSetLength(260 + 1), 260 + 1);

int nPos = strPath.ReverseFind(_T('\\'));
strExePath = strPath.Left(nPos + 1);
std::string installdir = CString2string(strExePath);

10.最大化窗体

HWND parent = GetDesktopWindow();
ShowWindow(parent, SW_MAXIMIZE);

11.通过HWND获取CWnd

HWND parent = GetDesktopWindow();
CWnd * pCWnd;
pCWnd = CWnd::FromHandle(parent);

12.快速创建窗体

//创建static窗体,还有EDIT等其他类型
HWND localVideoHwnd = CreateWindowEx(
	WS_EX_WINDOWEDGE,
	L"STATIC",
	NULL,
	WS_POPUP,
	200,
	200,
	_oriWidth,
	_oriHeight,
	parent,
	0,
	NULL,
	NULL);

ShowWindow(localVideoHwnd, SW_SHOW);
//通过HDC获取CDC
HDC hdc = GetDC(localVideoHwnd);
CDC dc;
dc.Attach(hdc);

13.opengl窗体居中

//(屏幕宽度 - 窗体宽度) / 2,(屏幕高度 - 窗体高度) / 2
glfwSetWindowPos(_mainWindow, (_mode->width - _oriWidth) / 2, (_mode->height - _oriHeight) / 2);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值