MFC常用操作 多线程 基本控件使用 常用宏 Cstring转char*转string互转等

 类型转换:MFC需要与C语言或C++交互,字符串不能连续+不方便

  • CString   char*   string之间互转

{
		CString cstring = "hello";
		char pchar[64] = "1122";

		//Cstring转char*
		char *pchar2 = cstring.GetBuffer(); //或	sprintf(pchar,"%s",cstring);
		//char* 转CString  
		CString s="1"+CString(pchar); //能连续加 或cstring = pchar; 或cstring.Format("%s", pchar);
        s.ReleaseBuffer(); //必须要重新计算长度
		
		//char*转string
		string str3="123"+string(pchar);    //能连续加 或 string str = pchar;
		//string转char*
		const char *p = str3.c_str();

		//cstring转string
		string str2 = cstring;
		//string转cstring
		CString cstring2(str.c_str()); //或cstring.Format("%s", str.c_str());

        //int转string 便于连续加 一行拼成一条字符串        
        string str5="8899"+to_string(123);
        //int转CString      
        str.Format(_T("%d"), s);//不能插在中间不方便 //反过来 int data= _ttoi(str); 
        CString str6 = "7788" +CString(to_string(123).c_str()); //这样就可以放中间 连续加了	}	


C/C++常用好用的宏 

#define P(x) (#x"="+to_string(x)) //显示整数等
#define PS(x) (#x"="+x) //显示字符串
#define FUN_LINE __FUNCTION__" "+to_string(__LINE__)+" "
#define Size(myArray) (sizeof(myArray)/sizeof(myArray[0])) //数组成员数量
#define LimitIndex(index,arraySzie) (index <arraySzie ? index : arraySzie-1) //索引不能超出数组范围,超出就重置为最大值



#define ArraySize(myArray) (sizeof(myArray)/sizeof(myArray[0])) //数组成员数量                                                                                                                                                                                  
#define OutArray_Check(index,myArray) ( index>=(ArraySize(myArray)) ? ArraySize(myArray)-1 : index ) //防止数组索引溢出                                                                                                                                                                                                             
                                                                                                                                                                                                                                                      
#define BufferToWord(ucBuff,iOffset) (WORD)((((WORD)(ucBuff[0+iOffset] & 0xFF) << 8) | (WORD)(ucBuff[1+iOffset] & 0xFF))) //缓冲转word LSB低字节在前                                                                                                            
#define BufferToDword(ucBuff,iOffset) (DWORD)((     ((DWORD)(ucBuff[0+iOffset] & 0xFFFFFF) << 24)  |((DWORD)(ucBuff[1+iOffset] & 0xFFFF) << 16)  |((DWORD)(ucBuff[2+iOffset] & 0xFF) << 8)  | (DWORD)(ucBuff[3+iOffset] & 0xFF)   )) //缓冲转dword LSB低字节在前 

#define WordToBuffer(myarray,val,offset) (myarray)[0+offset] = ((val&0xFFFF)>>8); (myarray)[1+offset] = ((val) & 0xFF) //按照LSB格式把一个Word转化为两个字节 
#define DwordToBuffer(myarray,val,offset) (myarray)[0+offset] = ((val&0xFFFFFFFF)>>24); (myarray)[1+offset] = ((val&0xFFFFFF)>>16); (myarray)[2+offset] = ((val&0xFFFF)>>8); (myarray)[3+offset] = ((val) & 0xFF) //按照LSB格式把一个Word转化为两个字节 

#define  Mem_Byte(x) (*((BYTE *)(x))) //得到指定地址上的一个字节
#define  Mem_Word(x) (*((WORD *)(x))) //得到指定地址上的一个字

#define Word_L(x)  ((BYTE) ((WORD)(x) & 0xFF)) //得到一个字的低位字节 
#define Word_H(x)  ((BYTE) ((WORD)(x) >> 8))   //得到一个字的高位字节


#define Decimal_Check(c) ((c)>='0' && (c)<='9')  //判断字符是不是10进值的数字                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
#define UPCASE(c) (((c)>='a' && (c) <= 'z') ? ((c) - 0x20) : (c)) //将一个字母转换为大写
#define hex_to_asc(c) (c-0x30) //转为ASCII   
#define Temperature(temperature) (float)((short)temperature & 0x8000? (-1) * ((~(short)temperature) + 1):temperature)  //温度转换 支持带负数                                                                                                                    


//__VA_ARGS__可变参数,即编译时 替换...,##当可变参数的个数为0时,这里的##起到把前面多余的","去掉的作用,否则会编译出错
#define log_printf(fmt,...) printf("[%s|%d]"fmt"\n",__func__,__LINE__,##__VA_ARGS__)
//可变参数输出到终端和追加到文件等
#define logf_printf(fmt,...)  do { char temp[1024]={0}; sprintf(temp,"[%s|%d]"fmt"\n",__func__,__LINE__,##__VA_ARGS__);  printf(temp);std::ofstream outfile;	outfile.open("log.txt", std::ios::out | std::ios::app);  outfile << std::string(temp); outfile.close();	}while(0)
__LINE__       当前语句所在的行号, 为10进制整数.
__FILE__       当前源文件的文件名, 为字符串常量.
__DATE__       程序被编译的日期, 为"MM dd yyyy"格式的字符串.
__TIME__       程序被编译的时间, 为"hh:mm:ss"格式的字符串, 由asctime返回.
__FUNCTION__   当前语句所在函数名,为字符串常量.


printf("%s %s:%d\n", __FUNCTION__, __FILE__, __LINE__);//打印带函名、完整路径文件名和行号

修改控件字体颜色

HBRUSH CSetConfigDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
	if (pWnd->GetDlgCtrlID() == IDC_STATIC_DEVICE)// IDC_Display为所选文本框ID
	{
		if (g_bDeviceConnectStatu)
		{
			pDC->SetTextColor(GREEN_RGB);//设置字体颜色
		}
		else 
        {
			pDC->SetTextColor(RED_RGB);//设置字体颜色
		}
	}	
	return hbr;
}

多线程中调用控件或MFC函数

CSetConfigDlg *thisDlg = NULL;//定义 本对话框指针


CSetConfigDlg::CSetConfigDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_WriteConfig, pParent)
{
	thisDlg = this;//赋值 本对话框指针
}


线程中更新 对话框界面内容等
thisDlg->ShowInfo(); 
thisDlg->GetDlgItem(IDC_EDIT_SN)->ShowWindow(SW_SHOW);
thisDlg->GetDlgItem(IDC_EDIT_SN)->SetWindowTextA("");
thisDlg->GetDlgItem(IDC_EDIT_SN)->SetFocus();

//checkbox控件是否选中
if(BST_CHECKED == g_KeyLoadWindow->IsDlgButtonChecked(IDC_CHECK_AES_MODE))
{
}

//获取控件整数值
CString cTmpstr;
g_KeyLoadWindow->GetDlgItem(IDC_EDIT_KEK_IDX_2)->GetWindowText(cTmpstr);
int k_index = _ttoi(cTmpstr); //界面上key索引

//获取控件字符串到char array里
char bKeyValue[5120] = { 0 };//界面上key
g_KeyLoadWindow->GetDlgItemText(IDC_EDIT_KEK_DATA, bKeyValue, sizeof(bKeyValue));

//获取控件字符串到char array里
char group[100] = {0};
g_KeyLoadWindow->GetDlgItemText(IDC_COMBO_MKSK_GROUP_V,group,sizeof(group));
memcpy(group, group+6,strlen(group)); //并截取 第6个到最后
//进度条 开始与结束,处理成功或失败 不同颜色
g_KeyLoadWindow->m_cprogressctrl_keyload_progress.ShowWindow(1);
g_KeyLoadWindow->m_cprogressctrl_keyload_progress.SetBarColor(RGB(0, 255, 0));
g_KeyLoadWindow->m_cprogressctrl_keyload_progress.SetRange(0, 100);
g_KeyLoadWindow->m_cprogressctrl_keyload_progress.SetPos(2);



g_KeyLoadWindow->m_cprogressctrl_keyload_progress.SetBarColor(wRet ? RGB(255, 0, 0) : RGB(0, 255, 0));
g_KeyLoadWindow->m_cprogressctrl_keyload_progress.SetPos(100);
//多线程 示例
OnComHandlerUart(_Proc_thread_ft_KLD_CertLoad); //调用线程中处理



//通用串口线程处理函数
void OnComHandlerUart(AFX_THREADPROC pfnThreadProc)
{
	// TODO: Add your control notification handler code here
	if (TRUE == g_KeyLoadWindow->g_my_ComIsOpen)
	{
		if (TRUE == g_KeyLoadWindow->m_Wrok_Thread_Flag)
		{
			AfxMessageBox("There are currently unfinished tasks\n");
			return;
		}

		g_KeyLoadWindow->m_GetSN_Thread_h = AfxBeginThread(pfnThreadProc, g_KeyLoadWindow);
		if (NULL == g_KeyLoadWindow->m_GetSN_Thread_h)
		{
			AfxMessageBox("Create Thread Error\n");
		}
	}
	else
	{
		AfxMessageBox("Com port not open\n");
	}
}

//线程中更新窗口控件UI等
UINT _Proc_thread_ft_KLD_CertLoad(LPVOID pParam)
{
	g_KeyLoadWindow->m_Wrok_Thread_Flag = TRUE;
	printf("*****************************************Thread Start**********************************************************%d\n", __LINE__);
	char certFilePath[5120] = { 0 };
	g_KeyLoadWindow->GetDlgItemText(IDC_EDIT_TLS_CERT_PATH, certFilePath, sizeof(certFilePath)); //线程中获取控件值。也可以更新UI
	
	WORD wRet;
	//wRet = halLoadCert(bFilebuffer, FileSize);
	if (wRet != ERR_SUCCESS)
	{
		printf("注入证书失败!!!!!!!0x%4X\n", wRet);
		goto ThreadExit;
	}
	printf("注入证书成功!!!!!!!\n");
ThreadExit:
	printf("*****************************************Thread Exit**********************************************************%d\n\n", __LINE__);
	g_KeyLoadWindow->m_Wrok_Thread_Flag = FALSE;
	return wRet;
}

//几个变量
CWinThread *m_GetSN_Thread_h; //一般不用。只有需要在线程过程中,对对线程处理才用到。
FTsafe_KeyLoad *g_KeyLoadWindow=NULL; //当前窗口对象,构造函数里赋值this

//AfxMessageBox("注入失败");

//messagebox		
switch(MessageBox("本数据已存在,是否更新保存?","请确认",MB_ICONQUESTION | MB_YESNOCANCEL) )
		{
        case IDOK:
		case IDYES:
			//是处理
			CDialog::OnOK();	//关闭
		case IDNO:
			//否处理
			CDialog::OnCancel();     //关闭
		case  IDCANCEL:
			//取消处理
			break;
		 }
  • INI读写

int SNLength = GetPrivateProfileInt("CFG_FILE", "SNLength", 15, configFileName);



在ini文件中的格式
[Form_DianShueg]
总点数=00000090

LPCTSTR FILE_NAME= ".\\WKA_Param.ini";

//写入INI文件
WritePrivateProfileString("Form_DianShueg", "总点数", str_use2, FILE_NAME);	

//从INI文件中读
GetPrivateProfileString("Form_DianShueg", "总点数", "0", str.GetBuffer(MAX_PATH), MAX_PATH, FILE_NAME);	如果没有读到数据,就读第三个参数
str.RealseBuffer(); //重置长度



 

  •  备份最小的源码包

//删除项目中所有debug和release文件夹
//打包打缩FTsafe_F20_PC_Tools.sln和项目文件夹FTsafe_F20_PC_Tools就是最小的源码包 

窗口专题

  1. 设计一个窗口类
  2. 注册
  3. 创建
  4. 显示及更新
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄人软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值