21xrx.com
2024-09-20 00:31:07 Friday
登录
文章检索 我的文章 写文章
VC++6.0源代码分享
2023-07-02 16:18:30 深夜i     --     --
VC++ 0 源代码 分享

VC++6.0是一款广泛使用的集成开发环境,用于Windows平台下的应用程序开发,包括桌面应用、嵌入式系统应用、驱动程序等。它与MFC、ATL等开发框架相结合,可以轻松地实现复杂的项目开发和调试。在这篇文章中,我们将分享一些VC++6.0源代码,为开发者提供一些参考和借鉴。

1. 经典的“Hello World”程序

#include

class CMyApp : public CWinApp

{

public:

  virtual BOOL InitInstance();

};

class CMainWindow : public CFrameWnd

{

public:

  CMainWindow()

  {

    Create(NULL, "My First Window");

  }

};

BOOL CMyApp::InitInstance()

{

  CMainWindow* pWnd = new CMainWindow();

  m_pMainWnd = pWnd;

  m_pMainWnd->ShowWindow(SW_SHOW);

  m_pMainWnd->UpdateWindow();

  return TRUE;

}

CMyApp theApp;

这个程序采用了MFC框架,它创建了一个窗口并在窗口中显示文字“Hello World”。用于初学者练手和了解MFC框架。

2. 简单的计算器程序

#include

class CMyApp : public CWinApp

{

public:

  virtual BOOL InitInstance();

};

class CMainWindow : public CFrameWnd

{

public:

  CMainWindow()

  {

    Create(NULL, "Calculator");

    nNumber = 0;

    nPrevOperator = 0;

    bNewNumber = TRUE;

  }

  afx_msg void OnButtonPress(UINT nID);

  afx_msg void OnAdd();

  afx_msg void OnSubtract();

  afx_msg void OnMultiply();

  afx_msg void OnDivide();

  afx_msg void OnEquals();

  void DoOperation();

private:

  int nNumber;

  int nPrevOperator;

  BOOL bNewNumber;

  CString strDisplay;

};

BOOL CMyApp::InitInstance()

{

  CMainWindow* pWnd = new CMainWindow();

  m_pMainWnd = pWnd;

  m_pMainWnd->ShowWindow(SW_SHOW);

  m_pMainWnd->UpdateWindow();

  return TRUE;

}

void CMainWindow::OnButtonPress(UINT nID)

{

  if (bNewNumber)

    strDisplay = "";

    bNewNumber = FALSE;

  strDisplay += nID + '0';

  SetWindowText(strDisplay);

}

void CMainWindow::OnAdd()

{

  DoOperation();

  nPrevOperator = 1;

}

void CMainWindow::OnSubtract()

{

  DoOperation();

  nPrevOperator = 2;

}

void CMainWindow::OnMultiply()

{

  DoOperation();

  nPrevOperator = 3;

}

void CMainWindow::OnDivide()

{

  DoOperation();

  nPrevOperator = 4;

}

void CMainWindow::OnEquals()

{

  DoOperation();

  nPrevOperator = 0;

  bNewNumber = TRUE;

}

void CMainWindow::DoOperation()

{

  if (nPrevOperator == 1)

  {

    nNumber += atoi(strDisplay);

  }

  else if (nPrevOperator == 2)

  {

    nNumber -= atoi(strDisplay);

  }

  else if (nPrevOperator == 3)

  {

    nNumber *= atoi(strDisplay);

  }

  else if (nPrevOperator == 4)

  {

    nNumber /= atoi(strDisplay);

  }

  else

  {

    nNumber = atoi(strDisplay);

  }

  strDisplay.Format("%d", nNumber);

  SetWindowText(strDisplay);

}

CMyApp theApp;

这个程序采用了MFC框架,实现了一个简单的计算器程序。在窗口中有数字按钮、加减乘除按钮和等于按钮,用户可以点击它们进行简单的数学计算。用于入门级MFC框架应用练习。

3. 遍历文件夹及其子文件夹实现目录树

#include

#include

#include

class CMyApp : public CWinApp

{

public:

  virtual BOOL InitInstance();

};

class CMainWindow : public CFrameWnd

{

public:

  CMainWindow()

  {

    Create(NULL, "File Explorer");

    CreateTreeControl();

  }

  afx_msg void OnBrowse();

  afx_msg void OnTreeSelectionChanged(NMHDR* pNMHDR, LRESULT* pResult);

  void CreateTreeControl();

  void TraverseDirectory(HTREEITEM hItem, CString strPath);

private:

  CTreeCtrl m_treeCtrl;

};

BOOL CMyApp::InitInstance()

{

  CMainWindow* pWnd = new CMainWindow();

  m_pMainWnd = pWnd;

  m_pMainWnd->ShowWindow(SW_SHOW);

  m_pMainWnd->UpdateWindow();

  return TRUE;

}

void CMainWindow::OnBrowse()

{

  CString strPath;

  CFolderDialog dlg(NULL);

  if (dlg.DoModal() == IDOK)

  {

    strPath = dlg.GetFolderPath();

    TraverseDirectory(TVI_ROOT, strPath);

  }

}

void CMainWindow::OnTreeSelectionChanged(NMHDR* pNMHDR, LRESULT* pResult)

{

  HTREEITEM hItem = m_treeCtrl.GetSelectedItem();

  CString strPath;

  CString strText = m_treeCtrl.GetItemText(hItem);

  while (hItem != NULL)

  {

    strText = m_treeCtrl.GetItemText(hItem);

    if (strPath.IsEmpty())

      strPath = strText;

    else

      strPath = strText + "\\" + strPath;

    hItem = m_treeCtrl.GetParentItem(hItem);

  }

  MessageBox(strPath);

}

void CMainWindow::CreateTreeControl()

{

  CRect rect;

  GetClientRect(rect);

  rect.bottom /= 2;

  m_treeCtrl.Create(WS_VISIBLE | WS_CHILD | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT | TVS_DISABLEDRAGDROP | TVS_SHOWSELALWAYS, rect, this, 1);

}

void CMainWindow::TraverseDirectory(HTREEITEM hItem, CString strPath)

{

  CString strFindPath(strPath + "\\*");

  WIN32_FIND_DATA fd = { 0 };

  HANDLE hFind = FindFirstFile(strFindPath, &fd);

  if (hFind != INVALID_HANDLE_VALUE)

  {

    do

    {

      if (fd.cFileName[0] == '.') continue;

      CString strFilePath(strPath + "\\" + fd.cFileName);

      HTREEITEM hNewItem = m_treeCtrl.InsertItem(fd.cFileName, hItem);

      if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

      {

        TraverseDirectory(hNewItem, strFilePath);

      }

    } while (FindNextFile(hFind, &fd));

    FindClose(hFind);

  }

}

CMyApp theApp;

这个程序采用了MFC框架,实现了一个简单的目录树。程序提供一个浏览按钮,点击之后可以浏览计算机中的所有文件夹及其子文件夹,用户可以在树型目录中选择文件夹,选中文件夹后会弹出对话框显示文件夹的绝对路径。用于熟悉Windows文件管理机制。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复