// MyInfo2Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "MyInfo2.h"
#include "MyInfo2Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class 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()
/////////////////////////////////////////////////////////////////////////////
// CMyInfo2Dlg dialog
CMyInfo2Dlg::CMyInfo2Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyInfo2Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyInfo2Dlg)
m_strCommand = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyInfo2Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyInfo2Dlg)
DDX_Control(pDX, IDC_LIST_ADD, m_listInfo);
DDX_Control(pDX, IDC_RICHEDIT1, m_reditInfo);
DDX_Text(pDX, IDC_EDIT_COMMAND, m_strCommand);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyInfo2Dlg, CDialog)
//{{AFX_MSG_MAP(CMyInfo2Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_APPEND, OnButtonAppend)
ON_BN_CLICKED(IDC_BUTTON_COMMAND, OnButtonCommand)
ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_ADD, OnItemchangedListAdd)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyInfo2Dlg message handlers
BOOL CMyInfo2Dlg::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
InitListCtrl();
//return TRUE; // return TRUE unless you set the focus to a control
}
void CMyInfo2Dlg::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 CMyInfo2Dlg::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 CMyInfo2Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyInfo2Dlg::OnButtonAppend()
{
// TODO: Add your control notification handler code here
CString strOne = _T("Left is what you can make it, so try to make it beautiful!");
CString strTwo = _T("Things can only get better!");
static int times = 0;
CString strMsg = _T("");
// Initialize character format structure
CHARFORMAT cf;
m_reditInfo.GetSelectionCharFormat(cf);
static int iHeight = 200;
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask |= CFM_BOLD;
cf.dwEffects |= CFE_BOLD;//设置粗体,取消用cf.dwEffects&=~CFE_BOLD;
cf.dwEffects &= ~CFE_AUTOCOLOR;
cf.dwMask |= CFM_COLOR;
cf.crTextColor = RGB(255, 0, 0);
cf.dwMask|= CFM_SIZE;
cf.yHeight = iHeight;//设置高度
cf.dwMask |= CFM_FACE;
_tcscpy(cf.szFaceName ,_T("微软雅黑"));//设置字体
for (int i = 0; i < 10; i++)
{
if (times++ % 2 == 0)
{
strMsg = strOne;
}
else
{
strMsg = strTwo;
}
int pos = m_reditInfo.GetWindowTextLength();
m_reditInfo.SetSel(pos, -1);
// Set the character format
m_reditInfo.SetSelectionCharFormat(cf);
//strMsg = CString::Format("%5d ", i) + strMsg + _T("\r\n");
CString strIndex;
strIndex.Format(_T("%5d: "), i);
strMsg = strIndex + strMsg + _T("\r\n");
m_reditInfo.ReplaceSel(strMsg);
m_reditInfo.PostMessage(WM_VSCROLL, SB_BOTTOM, 0); // 滚动条滚动到最下方
Sleep(200);
UpdateData(FALSE);
}
// ShellExecute(NULL, "open", "ping", "127.0.0.1", NULL, SW_SHOW);
// ShellExecute(NULL, "open", "route", "print", NULL, SW_SHOWNORMAL);
WinExec("Cmd.exe ping 192.168.1.1 -t", SW_SHOWNORMAL);
}
void CMyInfo2Dlg::PrintMessage(CString strMsg)
{
CHARFORMAT cf;
m_reditInfo.GetSelectionCharFormat(cf);
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask |= CFM_BOLD;
cf.dwEffects |= CFE_BOLD;
cf.dwEffects &= ~CFE_AUTOCOLOR;
cf.dwMask |= CFM_COLOR;
cf.crTextColor = RGB(255, 0, 0);
cf.dwMask |= CFM_SIZE;
cf.yHeight = 200;//设置高度
cf.dwMask |= CFM_FACE;
_tcscpy(cf.szFaceName ,_T("微软雅黑"));//设置字体
int pos = m_reditInfo.GetWindowTextLength();
m_reditInfo.SetSel(pos, -1);
m_reditInfo.SetSelectionCharFormat(cf);
//strMsg = CString::Format("%5d ", i) + strMsg + _T("\r\n");
// CString strIndex;
// strIndex.Format(_T("%5d: "), i);
// strMsg = strIndex + strMsg + _T("\r\n");
m_reditInfo.ReplaceSel(strMsg /*+ _T("\r\n")*/);
m_reditInfo.PostMessage(WM_VSCROLL, SB_BOTTOM, 0); // 滚动条滚动到最下方
UpdateData(FALSE);
}
TCHAR* CMyInfo2Dlg::StringToChar(CString& str)
{
TCHAR *tr = str.GetBuffer(str.GetLength());
str.ReleaseBuffer();
return tr;
}
CONST int MAX_BUF_SIZE = 4096;
HANDLE hReadPipe1 = NULL;
HANDLE hWritePipe1 = NULL;
HANDLE hReadPipe2 = NULL;
HANDLE hWritePipe2 = NULL;
void CMyInfo2Dlg::OnButtonCommand()
{
// TODO: Add your control notification handler code here
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (!CreatePipe(&hReadPipe1, &hWritePipe1, &sa, 0))
{
MessageBox("Error on CreatePipe()!");
return ;
}
if (!CreatePipe(&hReadPipe2, &hWritePipe2, &sa, 0))
{
MessageBox("Error on CreatePipe()!");
return ;
}
// CString strComLine = _T("ping 192.168.1.1");
// TCHAR* cCmdLine = StringToChar(strComLine);
TCHAR cCmdLine[256] = {0};
GetSystemDirectory(cCmdLine, sizeof(cCmdLine));
_tcscat(cCmdLine, _T("\\cmd.exe"));
STARTUPINFO si = {sizeof(si)};
PROCESS_INFORMATION pi;
si.hStdInput = hReadPipe1;
si.hStdOutput = hWritePipe2;
si.hStdError = hWritePipe2;
si.wShowWindow = SW_HIDE/*SW_SHOWNORMAL*/;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
if (!CreateProcess(NULL, cCmdLine, NULL, NULL, TRUE ,NULL,
NULL, NULL, &si, &pi))
{
MessageBox("Error on CreateProcess()!");
return;
}
HANDLE mythread = CreateThread(NULL, 0, GetEchoData, (LPVOID)this, 0, NULL);
CloseHandle(mythread);
// AfxBeginThread(GetEchoData, (LPVOID)m_hWnd);
// TCHAR buffer[MAX_BUF_SIZE] = {0};
// DWORD byteRead = 0;
//
// while(true)
// {
// // 没有输入数据时,会阻塞
// if (!ReadFile(hReadPipe2, buffer, MAX_BUF_SIZE - 1, &byteRead, NULL))
// {
// break;
// }
// PrintMessage(buffer);
// memset(buffer, 0, MAX_BUF_SIZE);
// }
// CloseHandle(hRead);
// CloseHandle(hWrite);
}
// 线程, 读取管道
DWORD WINAPI CMyInfo2Dlg::GetEchoData(LPVOID lpParam)
{
// CMyInfo2Dlg *pDlg = static_cast(
// CWnd::FromHandle(reinterpret_cast(lpParam)));
CMyInfo2Dlg *pDlg = (CMyInfo2Dlg *)lpParam; // 这里debug时会有问题
TCHAR szBuf[MAX_BUF_SIZE] = {0};
BOOL bRet = FALSE;
DWORD dwRead = 0;
DWORD dwAvail = 0;
CString strText = _T("");
while(true)
{
bRet = PeekNamedPipe(hReadPipe2, szBuf, MAX_BUF_SIZE, &dwRead, &dwAvail, 0);
if (bRet && dwAvail > 0)
{
memset(szBuf, 0, MAX_BUF_SIZE);
bRet = ReadFile(hReadPipe2, szBuf, MAX_BUF_SIZE, &dwRead, NULL);
if (bRet && dwRead > 0)
{
strText.Format(_T("%s"), szBuf);
pDlg->PrintMessage(strText);
memset(szBuf, 0, MAX_BUF_SIZE);
}
Sleep(50);
}
}
return true;
}
void CMyInfo2Dlg::OnButtonSend()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
DWORD dwWrite = 0;
// TCHAR szBuf[MAX_BUF_SIZE] = StringToChar(m_strCommand);
m_strCommand += _T("\r\n");
if (!WriteFile(hWritePipe1, StringToChar(m_strCommand), m_strCommand.GetLength(), &dwWrite, NULL))
{
MessageBox(_T("WriteFile() error!"));
}
}
void CMyInfo2Dlg::InitListCtrl()
{
// 设置风格
LONG lStyle = GetWindowLong(m_listInfo.m_hWnd, GWL_STYLE); //获取当前窗口style ;
lStyle &= ~LVS_TYPEMASK; // 清除显示方式位
lStyle |= LVS_REPORT; // 设置style
SetWindowLong(m_listInfo.m_hWnd, GWL_STYLE, lStyle); // 设置style
DWORD dwStyle = m_listInfo.GetExtendedStyle();
dwStyle |= LVS_EX_FULLROWSELECT; // 选中某行使整行高亮(只适用与report风格的listctrl)
dwStyle |= LVS_EX_GRIDLINES; // 网格线(只适用与report风格的listctrl)
dwStyle |= LVS_EX_CHECKBOXES; // item前生成checkbox控件
dwStyle |= LVS_EX_INFOTIP;
m_listInfo.SetExtendedStyle(dwStyle); // 设置扩展风格
// 插入数据(插入列)
m_listInfo.InsertColumn(PERSON_INFO_INDEX, _T("Index"), LVCFMT_LEFT, INFO_INDEX_LEN);
m_listInfo.InsertColumn(PERSON_INFO_NAME, _T("Nane"), LVCFMT_LEFT, INFO_NAME_LEN);
m_listInfo.InsertColumn(PERSON_INFO_ADDR, _T("Add"), LVCFMT_LEFT, INFO_ADDR_LEN);
m_listInfo.InsertColumn(PERSON_INFO_ID, _T("ID"), LVCFMT_LEFT, INFO_ID_LEN);
}
void CMyInfo2Dlg::OnButtonAdd()
{
// TODO: Add your control notification handler code here
for (int i = 0; i < 10; i++)
{
m_listInfo.InsertItem(i, _T("")); // 插入一项
CString strIndex; // 填充数据
strIndex.Format(_T("%4d"), i + 1);
m_listInfo.SetItemText(i, PERSON_INFO_INDEX, strIndex);
m_listInfo.SetItemText(i, PERSON_INFO_NAME, _T("name:hello, world!"));
m_listInfo.SetItemText(i, PERSON_INFO_ADDR, _T("addr"));
m_listInfo.SetItemText(i, PERSON_INFO_ID, _T("ID"));
// m_listInfo.EnsureVisible(i, FALSE); // 使插入项可见
}
}
void CMyInfo2Dlg::OnItemchangedListAdd(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
CString strMsg = _T("");
// TODO: Add your control notification handler code here
LPNMITEMACTIVATE pNMLV = (LPNMITEMACTIVATE)pNMHDR;
if((pNMLV->uOldState & INDEXTOSTATEIMAGEMASK(1)) /* old state : unchecked */
&& (pNMLV->uNewState & INDEXTOSTATEIMAGEMASK(2)) /* new state : checked */
)
{
// Item被选中(pNMLV->iItem)
strMsg.Format(_T("%d selected!\r\n"), pNMLV->iItem);
PrintMessage(strMsg);
m_listInfo.SetCheck(1);
}
else if((pNMLV->uOldState & INDEXTOSTATEIMAGEMASK(2)) /* old state : checked */
&& (pNMLV->uNewState & INDEXTOSTATEIMAGEMASK(1)) /* new state : unchecked */
)
{
strMsg.Format(_T("%d not selected!\r\n"), pNMLV->iItem);
PrintMessage(strMsg);
m_listInfo.SetCheck(1, FALSE);
}
else
{
// 未改变选中状态(pNMLV->iItem)
}
*pResult = 0;
}
http://blog.csdn.net/morewindows/article/details/7390441
http://blog.chinaunix.net/uid-24862988-id-3793502.html
(第二个链接处理线程传参断言的问题)