我做了一个小测试程序,点“退出“按钮结束程序。
按钮为IDCANCEL,设计模式下文本为“退出”。
我记得没有后对这个按钮进行SetWindowText等修改按钮文本的操作,但是程序一启动时这个按钮上的文本就变成了“0”,我把这个按钮删掉另外一个按钮启动时也变成了“0”,不知道是什么地方出了错。
麻烦高手指点一下。
按钮为IDCANCEL,设计模式下文本为“退出”。
我记得没有后对这个按钮进行SetWindowText等修改按钮文本的操作,但是程序一启动时这个按钮上的文本就变成了“0”,我把这个按钮删掉另外一个按钮启动时也变成了“0”,不知道是什么地方出了错。
麻烦高手指点一下。
MFC的源码太难贴也太难看,我主要想请教大家是否也遇过类似的问题,主要靠经验判断。
在那些情况下可能不小心给修改掉。
我想得到的就是类似Mackz(在相互) 的答案。我查找了一下SetWindowText、SetDlgItemText、SetDlgItemInt这三个函数,SetWindowText、SetDlgItemInt没找到,SetWindowText只用了两处而且与这个按钮无关。
----------------------------------------
贴出来看,也许你判断错了
//#include "stdafx.h"
#include "CH341DLL.h"
#include <fstream.h>
#include <string>
#include "USB.h"
#include "USBDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
{
public:
CAboutDlg();// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CUSBDlg dialogCUSBDlg::CUSBDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUSBDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUSBDlg)
m_Pos = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CUSBDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUSBDlg)
DDX_Control(pDX, IDC_LIST_RECV, m_List_Recv);
DDX_Control(pDX, IDC_SPIN, m_Spin);
DDX_Control(pDX, IDC_CMB_TIME, m_Comb);
DDX_Control(pDX, IDC_LIST_COMMAND, m_List);
DDX_Control(pDX, IDC_EDT_VALUE, m_Edt);
DDX_Text(pDX, IDC_EDT_VALUE, m_Pos);
DDV_MinMaxInt(pDX, m_Pos, 0, 1000);
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CUSBDlg, CDialog)
//{{AFX_MSG_MAP(CUSBDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_CONNECT, OnBtnConnect)
ON_BN_CLICKED(IDC_BTN_LOCAL, OnBtnLocal)
ON_BN_CLICKED(IDC_BUTTON_RUN, OnButtonRun)
ON_BN_CLICKED(IDC_BTN_PAUSE, OnBtnPause)
ON_BN_CLICKED(IDC_BTN_STOP, OnBtnStop)
ON_WM_TIMER()
ON_CBN_SELCHANGE(IDC_CMB_TIME, OnSelchangeCmbTime)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BTN_CREATEFILE, OnBtnCreatefile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CUSBDlg message handlersBOOL CUSBDlg::OnInitDialog()
{
CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
} // Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_CommandCount = 0;
m_CurrCommand = 0;
m_Comb.AddString("毫秒");
m_Comb.AddString("秒");
m_Comb.SetCurSel(0); m_Spin.SetBuddy(&m_Edt);
m_Spin.SetRange(1, 1000);
m_Spin.SetPos(100); if( 0== m_Comb.GetCurSel() ) // ms
m_TimeType = 1;
else if( 1== m_Comb.GetCurSel() ) // s
m_TimeType = 1000;
m_Index = 0;
if(CH341OpenDevice(m_Index) == INVALID_HANDLE_VALUE)
{
MessageBox("打开设备失败","CH341PAR",MB_OK|MB_ICONSTOP);
}
else
{
CH341SetupSerial(0, NOPARITY,9600);
}
return TRUE; // return TRUE unless you set the focus to a control
}void CUSBDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.void CUSBDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CUSBDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CUSBDlg::OnBtnConnect()
{
// TODO: Add your control notification handler code here
unsigned char buf[mMAX_BUFFER_LENGTH] = "SYST:REM";
unsigned long nLength = 8;
buf[nLength] = 13;
buf[nLength+1] = 10;
buf[nLength+2] = 13;
buf[nLength+3] = 10;
buf[nLength+4] = 10;
nLength += 4;
if(!CH341WriteData(0,&buf[0],&nLength))
{
MessageBox((LPCSTR)"打开远程模式失败!","提示",MB_ICONSTOP);
return;
}
}void CUSBDlg::OnBtnLocal()
{
// TODO: Add your control notification handler code here
unsigned char buf[mMAX_BUFFER_LENGTH] = "SYST:LOC";
unsigned long nLength = 8;
buf[nLength] = 13;
buf[nLength+1] = 10;
buf[nLength+2] = 13;
buf[nLength+3] = 10;
buf[nLength+4] = 10;
nLength += 4; if(!CH341WriteData(0,&buf[0],&nLength))
{
MessageBox((LPCSTR)"打开远程模式失败!","提示",MB_ICONSTOP);
return;
}
}void CUSBDlg::OnButtonRun()
{
// TODO: Add your control notification handler code here
char cBuffer[100]; // store read data
fstream fin; // fstream object
memset(cBuffer, 0, 100); // set buffer 0
fin.open(".\\Data.txt", ios::in); // open a text file
while(fin.getline(cBuffer, 100))
{
cBuffer[strlen(cBuffer)] = 10;
m_sCommand[m_CommandCount] = cBuffer;
m_CommandCount++;
memset(cBuffer, 0, 100);
}
UpdateData(TRUE);
SetTimer(1, m_Pos * m_TimeType, 0); // SetTimer
}void CUSBDlg::OnBtnPause()
{
// TODO: Add your control notification handler code here
static bool Run_Status = true;
if( Run_Status )
{
GetDlgItem(IDC_BTN_PAUSE)->SetWindowText("继续");
KillTimer(1); // 暂停运行
Run_Status = false;
}
else
{
GetDlgItem(IDC_BTN_PAUSE)->SetWindowText("暂停");
SetTimer(1, m_Pos * m_TimeType, 0); // SetTimer
Run_Status = true;
}
}void CUSBDlg::OnBtnStop()
{
// TODO: Add your control notification handler code here
KillTimer(1);
m_CurrCommand = 0;
m_CommandCount = 0;
m_List.ResetContent();
m_List_Recv.ResetContent();
}void CUSBDlg::OnCancel()
{
// TODO: Add extra cleanup here CDialog::OnCancel();
}void CUSBDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
using namespace std;
string sCmd;
if( m_CurrCommand < m_CommandCount )
{
sCmd.assign( m_sCommand[m_CurrCommand].GetBuffer(0) );
SendData((char*)sCmd.c_str());
m_CurrCommand++;
}
else
{
KillTimer(1);
m_List.AddString("命令处理完毕.");
} CDialog::OnTimer(nIDEvent);
}void CUSBDlg::SendData(char *Buffer)
{
unsigned long nLength = strlen(Buffer);
CString sContext;
sContext.Format("%d", m_CurrCommand);
sContext += ": ";
sContext += Buffer;
m_List.AddString(sContext);
Buffer[nLength] = 13;
Buffer[nLength+1] = 10;
Buffer[nLength+2] = 13;
Buffer[nLength+3] = 10;
Buffer[nLength+4] = 10;
nLength += 4; if(!CH341WriteData(0,&Buffer[0],&nLength))
{
MessageBox((LPCSTR)"写命令错误!","提示",MB_ICONSTOP);
return;
} if( -1 != sContext.Find( "?" ))
Recv();
}void CUSBDlg::OnSelchangeCmbTime()
{
// TODO: Add your control notification handler code here if( 0== m_Comb.GetCurSel() ) // ms
m_TimeType = 1;
else if( 1== m_Comb.GetCurSel() ) // s
m_TimeType = 1000;
}void CUSBDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
}void CUSBDlg::Recv()
{
char cBuffer[100] = "";
unsigned long ulLen = sizeof(cBuffer);
CH341ReadData(0, cBuffer, &ulLen);
m_List_Recv.AddString( cBuffer );
}void CUSBDlg::OnBtnCreatefile()
{
// TODO: Add your control notification handler code here
ShellExecute( NULL, "open", "CreateFile.exe", NULL, NULL, SW_SHOWNORMAL );
}