一、显示/隐藏控件
m_pTabAutoRunView->GetDlgItem(IDC_AUTORUN_S_DEL_BTN)->ShowWindow(true);
二、响应鼠标单击事件
鼠标左键按下事件函数OnLButtonDown
YourWindowClass.h
class YourWindowClass : public CWnd
{
// 其他成员和函数
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
};
YourWindowClass.cpp
BEGIN_MESSAGE_MAP(YourWindowClass, CWnd)
// 其他消息映射
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
void YourWindowClass::OnLButtonDown(UINT nFlags, CPoint point)
{
CString str;
str.Format(_T("鼠标单击位置:x = %d, y = %d"), point.x, point.y);
MessageBox(str);
// 或者执行其他你需要的操作
}
三、响应鼠标双击事件
鼠标左键按下事件函数OnLButtonDblClk
YourWindowClass.h
class YourWindowClass : public CWnd
{
// 其他成员和函数
public:
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
};
YourWindowClass.cpp
BEGIN_MESSAGE_MAP(YourWindowClass, CWnd)
// 其他消息映射
ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()
void YourWindowClass::OnLButtonDblClk(UINT nFlags, CPoint point)
{
CString str;
str.Format(_T("鼠标双击位置:x = %d, y = %d"), point.x, point.y);
MessageBox(str);
// 或者执行其他你需要的操作
}
四、OnTimer定时器
YourWindowClass.h
class YourWindowClass : public CWnd
{
// 其他成员和函数
public:
afx_msg void OnTimer(UINT_PTR nIDEvent);
};
YourWindowClass.cpp
BEGIN_MESSAGE_MAP(YourWindowClass, CWnd)
// 其他消息映射
ON_WM_TIMER()
END_MESSAGE_MAP()
//监听
void YourWindowClass::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == 1)
{
// 在这里处理定时到达的逻辑
// 例如:
MessageBox(_T("定时到达!"));
KillTimer(1); // 处理完后停止定时器
}
}
//触发,开启定时器
void YourWindowClass::OnBtnClick()
{
SetTimer(1, 1000, NULL);
}
五、输出内容到txt文件
5.1 输出日志信息
YourWindowClass.cpp
#include <fstream>
#include <afx.h>
void YourWindowClass::OnBtnClick()
{
CString filePath = _T("D:\\alanchen.txt");
std::ofstream outFile(filePath,std::ios::app);
CStringA content = "==按钮被点击了== \n";
outFile << content;
//content.Format("picPath= %s %s", picPath,_T("\n"));
}
5.2 输出上位机指令(十进制转十六进制)内容到本地文件txt
#include <fstream>
#include <afx.h>
#include <afxwin.h>
#include <iostream>
#include <iomanip> // 添加此头文件以使用 setfill 和 setw 函数
void YourWindowClass::OutputCmdToFile(unsigned char *lpCmd, int len) {
CString filePath = _T("D:\\alanchen.txt");
std::ofstream outFile(filePath,std::ios::app);
for (int i = 0; i < len; ++i) {
// 将十进制转换为十六进制并输出,不足两位用 0 填充
outFile << std::hex << std::uppercase << std::setfill('0') << std::setw(2) << (int)lpCmd[i] << " ";
}
outFile << "\n";
outFile.close();
}
int main() {
unsigned char lpCmd[] = {10, 20, 255};
int cmdLength = sizeof(lpCmd) / sizeof(lpCmd[0]);
OutputCmdToFile(lpCmd,cmdLength );
return 0;
}
六、OnLButtonDblClk(鼠标双击事件)并排除OnLButtonDown(鼠标单击事件)
YourWindowClass.h
class YourWindowClass : public CWnd
{
// 其他成员和函数
public:
afx_msg void OnTimer(UINT_PTR nIDEvent);
};
YourWindowClass.cpp
BEGIN_MESSAGE_MAP(YourWindowClass, CWnd)
// 其他消息映射
ON_WM_TIMER()
END_MESSAGE_MAP()
bool DB_CLICK = false;
//监听
void YourWindowClass::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == 1)
{
KillTimer(1001); // 停止定时器
if(DB_CLICK == true)
{
//执行双击事件逻辑
}else{
//执行单击事件逻辑
}
}
}
//双击事件
void YourWindowClass::OnLButtonDblClk(UINT nFlags, CPoint point)
{
bool DB_CLICK = false;
}
//单击事件
void YourWindowClass::OnLButtonDown(UINT nFlags, CPoint point)
{
DB_CLICK = false;
SetTimer(1001, 1000, NULL);
}
七、加载本地图片,然后再另存在本地
#include <afxwin.h> // MFC 核心和标准组件
#include <atlimage.h> // CImage 类定义
void LoadAndSaveImage(const CString& inputFilePath, const CString& outputFilePath)
{
// 创建 CImage 对象
CImage image;
// 加载本地图片
HRESULT hr = image.Load(inputFilePath);
if (FAILED(hr))
{
AfxMessageBox(_T("图片加载失败!"));
return;
}
// 保存图片到新位置
hr = image.Save(outputFilePath);
if (FAILED(hr))
{
AfxMessageBox(_T("图片保存失败!"));
}
else
{
AfxMessageBox(_T("图片保存成功!"));
}
}
八、获取当前时间到毫秒并输出
#include <afx.h>
#include <iostream>
void GetCurrentTimeWithMillis()
{
SYSTEMTIME st;
GetLocalTime(&st);
CString strTime;
strTime.Format("%04d-%02d-%02d %02d:%02d:%02d.%03d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
std::cout << "当前时间: " << (LPCTSTR)strTime << std::endl;
}
int main()
{
GetCurrentTimeWithMillis();
return 0;
}
网友评论