美文网首页
C++ MFC开发常用代码

C++ MFC开发常用代码

作者: AC编程 | 来源:发表于2024-08-05 19:28 被阅读0次

一、显示/隐藏控件

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;
}

相关文章

  • IOS界面布局到底纯代码和xib哪个好

    IOS界面布局到底纯代码和xib哪个好 我做IOS开发有几年了,之前从C++,MFC转过来的,刚开始做IOS开发,...

  • MFC下实现 灰度图像显示函数代码 C++

    layout: posttitle: "MFC下实现 灰度图像显示函数代码 C++"category: codi...

  • C++面试题

    C++音视频开发 面试1 技能要求:Socket,MFC,Windows 岗位职责:1、参与需求分析、模块开发等相...

  • iOS中的SDK开发

    开发中,有一种开发叫SDK开发,window中的.dll文件,c++语言开发MFC插件,安卓开发的so库或者arr...

  • C++算法之通用数据结构的代码

    把开发过程经常用的代码记录起来,下面代码内容是关于C++算法之通用数据结构的代码。 class calculate...

  • c++混合js编程

    1 背景 有个项目是c++开发(MFC),内嵌一个浏览器容器(IE内核),c++控制该容器浏览器某个html,然后...

  • 学Wpf之前 从事软件开发近6年,用过很多编程语言 C/C++ win32/mfc java android c#...

  • C++和OC混合开发,Xcode环境下,在C++文件里获得 xm

    最近在开发项目的时候,需要调用C++代码,但是C++代码里面,又必须调用外部的 XML 文件,在纯C++开发环境下...

  • C++通过递归进行回文判断的代码

    将开发过程比较常用的一些代码段做个收藏,下面代码是关于C++通过递归进行回文判断的代码,应该能对各位有些用。 #i...

  • windows逆向4

    C++数据结构-mapC++-MFC 程序C++-MFC 程序分析-00010Editor 分析WinRAR 去广...

网友评论

      本文标题:C++ MFC开发常用代码

      本文链接:https://www.haomeiwen.com/subject/semlhjtx.html