怎样用MFC做一个图形界面程序

怎样用MFC做一个图形界面程序,第1张

给你个最最简单的MFC程序。你可以打开2005 新建win32 程序 建立空项目 然后编写如下代码//hellocpp

#include <afxwinh>

// 说明应用程序类

class CHelloApp : public CWinApp

{

  public:

 virtual BOOL InitInstance();

};

// 建立应用程序类的实例

CHelloApp HelloApp;

// 说明主窗口类

class CHelloWindow : public CFrameWnd

{

CStatic cs;

public:

CHelloWindow();

};

// 每当应用程序首次执行时都要调用的初始化函数

BOOL CHelloApp::InitInstance()

{

m_pMainWnd = new CHelloWindow();

m_pMainWnd->ShowWindow(m_nCmdShow);

m_pMainWnd->UpdateWindow();

return TRUE;

}

// 窗口类的构造函数

CHelloWindow::CHelloWindow()

{

// 建立窗口本身

Create(NULL,

  "Hello World!",

  WS_OVERLAPPEDWINDOW,

  CRect(0,0,200,200));

// 建立静态标签

cs = new CStatic();

cs->Create("hello world",

  WS_CHILD|WS_VISIBLE|SS_CENTER,

  CRect(50,80,150,150),

  this);

} 这样就出来了。

>

1、与串口通讯不使用mscomm,难道还要再写个通讯模块吗,vc的话貌似只能用这个~

2、

void CSCommTestDlg::OnComm(){

VARIANT variant_inp;

COleSafeArray safearray_inp;

LONG len,k;

BYTE rxdata[2048]; //设置BYTE数组(8位无符号整数)

CString strtemp;

if(m_ctrlCommGetCommEvent()==2) //事件值为2表示接收缓冲区内有字符

variant_inp=m_ctrlCommGetInput();//读缓冲区

safearray_inp=variant_inp; //VARIANT型变量转换为ColeSafeArray型变量

len=safearray_inpGetOneDimSize(); //得到有效数据长度

for(k=0;k 

safearray_inpGetElement(&k,rxdata+k);//转换为BYTE型数组

for(k=0;k 

PLCData[k]=rxdata[k];

verfun(PLCData,16);//计算校验码

if(PLCData[15]==PLCData[16])

{ //接收数据正确发送确认信号

m_ctrlCommSetOutput(COleVariant(“K@”));

//可以在下面自行加上对数据的处理

if(!m_quit)//如果没有结束信号则续传数据

m_ctrlCommSetOutput(COleVariant(“C@”));

else

m_ctrlCommSetOutput(COleVariant(“Z@”));

else //接收数据出错要求重发数据

m_ctrlCommSetOutput(COleVariant(“R@”));

CFileDialog 打开多个文件2006-12-01 17:28从CFileDialog派生一个类MFileDlg: 头文件://///////////////////////////////////////////////////////////////////////////

// MFileDlg dialogclass MFileDlg : public CFileDialog

{

DECLARE_DYNAMIC(MFileDlg)public:

MFileDlg(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs

LPCTSTR lpszDefExt = NULL,

LPCTSTR lpszFileName = NULL,

DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,

LPCTSTR lpszFilter = NULL,

CWnd pParentWnd = NULL);

int DoModal();

virtual ~MFileDlg();

protected:

//{{AFX_MSG(MFileDlg)

// NOTE - the ClassWizard will add and remove member functions here

//}}AFX_MSG TCHAR m_pszFile;

////////

DECLARE_MESSAGE_MAP()}; 类实现://///////////////////////////////////////////////////////////////////////////

// MFileDlgIMPLEMENT_DYNAMIC(MFileDlg, CFileDialog)MFileDlg::MFileDlg(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,

DWORD dwFlags, LPCTSTR lpszFilter, CWnd pParentWnd) :

CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)

{

m_pszFile = new TCHAR[2048]; //set a 2K buffer to hold selected files

m_pszFile[0] = '\0'; //initialize pointer;

}

BEGIN_MESSAGE_MAP(MFileDlg, CFileDialog)

//{{AFX_MSG_MAP(MFileDlg)

// NOTE - the ClassWizard will add and remove mapping macros here

//}}AFX_MSG_MAP

END_MESSAGE_MAP()MFileDlg::~MFileDlg()

{

if (m_pszFile != NULL)

delete [] m_pszFile; //cleanup

}int MFileDlg::DoModal()

{

ASSERT_VALID(this);

ASSERT(m_ofnFlags & OFN_ALLOWMULTISELECT); //make sure multiple file selection is on m_ofnlpstrFile = m_pszFile; //initialize the OPENFILENAME structure

m_ofnnMaxFile = 2048;

return CFileDialog::DoModal();

} 具体使用: MFileDlg dlg( TRUE,_T("TXT"),_T("TXT"),

OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,

_T("文本文件 (txt)|txt所有文件 ()||"));

if(dlgDoModal()!=IDOK) return FALSE; //取消 *** 作 POSITION pos = dlgGetStartPosition();

while (pos != NULL)

{

CString strPath = dlgGetNextPathName(pos);

if (strPathFind(":\\\\") == 1 && strPathGetLength() > 4)

{

// this means we have an invalid path that looks like this:

// C:\\cdadgl

// get rid of extra slash

CString temp;

temp = strPathLeft(3);

temp += strPathMid(4);

strPath = temp;

}

m_listFileNamesAddTail(strPath); //CStringList m_listFileNames

TRACE(_T("GetOpenFileName returned %s Files\r\n"),strPath);

}

另外,站长团上有产品团购,便宜有保证

int CradioDlg::ReadIniFile()

{

FILE stream;

char line[255];

CStringArray strArray;

stream = fopen("C:\\tttini ", "r ");

if(stream)

{

while (!feof(stream))

{

char ch = fgets(line,2048,stream);

if (ch == NULL) break;

CString strch=(CString)ch;

strchGetBuffer();

CString strLine;

//strLineAdd(strch);

//strchReleaseBuffer();

if (ch[0] == 10)

continue;

//CString testpath = L"C:\\new\\REtestini";

CStdioFile file(strch,CFile::modeCreate|CFile::modeReadWrite);

if(!file)

{

AfxMessageBox(L"文件创建不成功");

}

ReadIniFile();

fileClose();

}

}

return 1;

}

拿你代码随便尝试了下 是可以的啊

C:\\tttini 这里面是 "C:\\new\\REtestini" 你是不是这里面写错了

以上就是关于怎样用MFC做一个图形界面程序全部的内容,包括:怎样用MFC做一个图形界面程序、vs2013不用mscomm控件,可以编写mfc程序控制plc吗,有没有简单的例子提供学习一下,拜托了、MFC怎么将几个Dialog显示在一个dialog里要给用户的感觉是只是在一个窗口中 *** 作!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9695239.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存