1. SendMessage、PostMessage
写过Windows程序的同学都知道PostMessage、SendMessage的区别,PostMessage函数调用发送之后,立即返回,不等待消息处理完成。而SendMessage则让调用的线程处于阻塞(BLOCk)状态,直到消息处理完成。
2. UpdateData()函数
UpdateData(true);//用于将屏幕上控件中的数据交换到变量中。
UpdateData(false);//用于将数据在屏幕中对应控件中显示出来。
3. CFile文件读写
CString cfg_file_name = L".\\bin\\gvo_spr.cfg";
CFile m_file;
CFileException fileException;
if (0 == m_file.Open(cfg_file_name, CFile::modeNoTruncate | CFile::modeWrite, &fileException)) {
TRACE("Can't open file %s, error = %u\n", cfg_file_name, fileException.m_cause);
}
else {
m_file.SeekToEnd(); // 文件读写指针指向文件末尾
#文件读写过程
}
CString str = L"aaaaabbb";
m_file.Write(str , 2*str .GetLength()); #写入CString字符,字符长度需要乘以2(因为是默认宽字符)
m_file.close();
4.宽字符问题
通过WideCharToMultiByte函数进行转换
CString str_cfg = L"adabsfsfd.txt";
int length = WideCharToMultiByte(CP_ACP,0, str_cfg,-1,NULL,0,NULL,NULL);
char *char_cfg = new char[length + 1];
memset(char_cfg,0,length+1);
WideCharToMultiByte(CP_ACP, 0, str_cfg, -1, char_cfg, length, NULL, NULL);
m_file.Write(char_cfg,length);
5.动态设置Button文字
#button相应函数
SetDlgItemText(IDC_BUTTON_LPF_OR_SAVE,L"LPF");
#或者
GetDlgItem(IDC_BUTTON_LPF_OR_SAVE)->SetWindowText(L"LPF");
6.响应Combox Select事件
在需要响应的combox控件上添加ON_CBN_SELCHANGE消息响应函数功能即可
7.CString转UINT
CString str = L"1000";
CStringA strA = CW2A(str );
UINT str_num = atol(strA.GetBuffer());
8.UINT转CString
CString str = L"";
UINT n = 10;
UINT str.Format("%d",n);
9. OnInitialUpdate()函数
主要初始化视图中控件等,对各个变量进行初始化操作。
void xxxView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
if (IsWindow(GetDlgItem(IDC_EDIT_COORDX0)->GetSafeHwnd())) {
m_strCoordX[0] = L"0";
UpdateData(FALSE);
}
if (IsWindow(GetDlgItem(IDC_EDIT_COORDY0)->GetSafeHwnd())) {
m_strCoordY[0] = L"0";
UpdateData(FALSE);
}
}
10. Invalidate() 和UpdateWindow()的用法
https://blog.csdn.net/foreverhuylee/article/details/20859443
11.CSpinButtonCtrl控件
建议使用对应的32位版本的功能函数
CSpinButtonCtrl Wrap=TRUE 表示滚轮可以循环
UDACCEL accel[2];
accel[0].nInc = 1;
accel[0].nSec = 0;
accel[1].nInc = 4;
accel[1].nSec = 2;
if (IsWindow(GetDlgItem(IDC_SPIN_COORDX0)->GetSafeHwnd())) {
((CSpinButtonCtrl*)GetDlgItem(IDC_SPIN_COORDX0))->SetRange(0, 1);
m_spinCoordX.SetAccel(2, accel); //加速度设置
m_spinCoordX.SetRange32(0, 100); //范围设置 SetRange支持16位的
m_spinCoordX.SetPos32(10); //位置设置 SetPos支持16位的
}
m_spinCoordX.GetPos32(10); //位置设置 GetPos支持16位的
处理事件时只需要添加响应spin控件的OnVScroll或OnHScroll消息即可
void xxxView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (m_spinCoordX.GetSafeHwnd() == pScrollBar->GetSafeHwnd()) {
SendMessage(MsgUpdateCoordXY, m_spinCoordX.GetPos32(), m_spinCoordY.GetPos32());
}
CFormView::OnVScroll(nSBCode, nPos, pScrollBar);
}
12.对话框中控件的Tab顺序设置
13.发送menu command消息
假如界面上有个button,按钮响应时触发menu菜单中的某个消息(例如ID_MENU_PLAY),如下方法:
theApp.m_pMainWnd->SendMessage(WM_COMMAND, ID_MENU_PLAY, 0);
或者
SendMessage(WM_COMMAND, ID_MENU_PLAY, 0);
或者
::SendMessage(theApp.m_pMainWnd->GetSafeHwnd(), WM_COMMAND, ID_MENU_PLAY, 0);
14.获取mac address
当软件需要验证身份信息或者绑定PC唯一性,需要进行mac address信息的获取和验证,以下提供mac信息的获取方法
#include <stdio.h>
#include <Windows.h>
#include <iphlpapi.h>
#include <Assert.h>
#pragma comment(lib, "iphlpapi.lib")
char* getMAC();
int main() {
char* pMac = getMAC();
system("pause");
free(pMac);
}
char* getMAC() {
PIP_ADAPTER_INFO AdapterInfo;
DWORD dwBufLen = sizeof(IP_ADAPTER_INFO);
char *mac_addr = (char*)malloc(18);
AdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
if (AdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
free(mac_addr);
return NULL; // it is safe to call free(NULL)
}
// Make an initial call to GetAdaptersInfo to get the necessary size into the dwBufLen variable
if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == ERROR_BUFFER_OVERFLOW) {
free(AdapterInfo);
AdapterInfo = (IP_ADAPTER_INFO *)malloc(dwBufLen);
if (AdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
free(mac_addr);
return NULL;
}
}
if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == NO_ERROR) {
// Contains pointer to current adapter info
PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
do {
// technically should look at pAdapterInfo->AddressLength
// and not assume it is 6.
sprintf(mac_addr, "%02X:%02X:%02X:%02X:%02X:%02X",
pAdapterInfo->Address[0], pAdapterInfo->Address[1],
pAdapterInfo->Address[2], pAdapterInfo->Address[3],
pAdapterInfo->Address[4], pAdapterInfo->Address[5]);
printf("Address: %s, mac: %s\n", pAdapterInfo->IpAddressList.IpAddress.String, mac_addr);
// print them all, return the last one.
// return mac_addr;
printf("\n");
pAdapterInfo = pAdapterInfo->Next;
} while (pAdapterInfo);
}
free(AdapterInfo);
return mac_addr; // caller must free.
}
15.多视图窗口
https://blog.csdn.net/kingsollyu/article/details/8312910
https://veviz.github.io/2015/12/09/Multiple View in MFC/
http://www.11.re/VC/725.html
16.自定义msg消息
static const UINT MsgUpdatePanelMode = ::RegisterWindowMessage(_T("Update the PanelMode"));
17.多线程
::AfxBeginThread(create_windows_thread, (void*)this); #create_windows_thread为自定义的线程处理函数