// xiemUsersDlg.cpp : implementation file
//
#include "stdafx.h"
#include "xiemUsers.h"
#include "xiemUsersDlg.h"
#include "XEIM_Add_Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// 参考飞鸽传书:http://www.freeeim.com
/
// 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()
/
// CXiemUsersDlg dialog
CXiemUsersDlg::CXiemUsersDlg(CWnd* pParent /*=NULL*/)
: CDialog(CXiemUsersDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CXiemUsersDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CXiemUsersDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CXiemUsersDlg)
DDX_Control(pDX, IDC_LIST1, m_lcUserList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CXiemUsersDlg, CDialog)
//{{AFX_MSG_MAP(CXiemUsersDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTNADD, OnBtnadd)
ON_NOTIFY(LVN_GETDISPINFO, IDC_LIST1, OnGetdispinfoList1)
ON_NOTIFY(NM_RCLICK, IDC_LIST1, OnRclickList1)
ON_COMMAND(ID_MENU_DELETE, OnMenuDelete)
ON_COMMAND(ID_MENU_MODIFY, OnMenuModify)
ON_COMMAND(ID_MENUADDNEW, OnMenuaddnew)
ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CXiemUsersDlg message handlers
BOOL CXiemUsersDlg::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
UI_InitList();
UI_InitReportHeader();
if (m_xUser.IsConnected())
{
AddLog("已连接服务器!/r/n");
m_xUser.ReadUsers();
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CXiemUsersDlg::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 CXiemUsersDlg::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 CXiemUsersDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CXiemUsersDlg::OnBtnadd()
{
// TODO: Add your control notification handler code here
/* if (m_xUser.SaveUser())
AddLog("用户添加成功。/r/n");
SetDlgItemText(IDC_EDIT1, "");*/
XEIM_Add_Dlg dlg;
if (IDOK == dlg.DoModal())
{
if (m_xUser.SaveUser(dlg.m_userID, dlg.m_password, dlg.m_group, dlg.m_display))
{
AddUserToList(dlg.m_userID, dlg.m_password, dlg.m_group, dlg.m_display);
}
}
}
void CXiemUsersDlg::UI_InitReportHeader()
{
// Initial extended style for the list control on this dialog
DWORD dwStyle = m_lcUserList.GetExtendedStyle();
dwStyle |= LVS_EX_FULLROWSELECT;
m_lcUserList.SetExtendedStyle(dwStyle);
m_lcUserList.InsertColumn(0, "ID", LVCFMT_LEFT, 88);
m_lcUserList.InsertColumn(1, "用户名", LVCFMT_LEFT, 88);
m_lcUserList.InsertColumn(2, "昵称", LVCFMT_LEFT, 88);
m_lcUserList.InsertColumn(3, "密码", LVCFMT_LEFT, 55);
m_lcUserList.InsertColumn(7, "版本", LVCFMT_LEFT, 80);
m_lcUserList.InsertColumn(8, "状态", LVCFMT_LEFT, 80);
}
void CXiemUsersDlg::AddLog(const char *szLog)
{
CEdit *pEdit = (CEdit*)GetDlgItem(IDC_EDIT5);
pEdit->SetSel(-1); // 光标定位到尾部
pEdit->ReplaceSel(szLog);
pEdit->PostMessage(WM_VSCROLL, SB_BOTTOM); // 滚动到光标*/
}
void CXiemUsersDlg::UI_InitList()
{
// *****************************************************************
// 用户 UI 初始化设置
// *****************************************************************
// 设置在线用户列表------------------------------------------------------------------
CBitmap bm;
m_ilUser.Create (16, 16, ILC_COLOR24 | ILC_MASK , 5,5);
bm.LoadBitmap(IDB_BITMAP1);
m_ilUser.Add(& bm, RGB(255,0,255));
m_lcUserList.SetImageList(& m_ilUser, LVSIL_SMALL);
// Initial extended style for the list control on this dialog
DWORD dwStyle = m_lcUserList.GetExtendedStyle();
dwStyle |= LVS_EX_FULLROWSELECT;
m_lcUserList.SetExtendedStyle(dwStyle);
}
void CXiemUsersDlg::OnGetdispinfoList1(NMHDR* pnmhdr, LRESULT* pResult)
{
LV_DISPINFO* pdi = (LV_DISPINFO *) pnmhdr;
// Fill in the LV_ITEM structure with the image info.
// When an item is selected, the image is set to the first
// image (the new bitmap on the toolbar).
// When it is not selected, the image index is equal to the
// item number (that is, 0=new, 1=open, 2=save, and so on.)
CString str;
str = m_lcUserList.GetItemText(pdi->item.iItem, 3);
if (strcmp((LPCTSTR)str, "离线"))
pdi->item.iImage = 4;
else
pdi->item.iImage = 3;
*pResult = 0;
}
void CXiemUsersDlg::OnRclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
CPoint pt(pNMListView->ptAction);
CMenu menu;
int nID;
POINT p;
p.x = pt.x;
p.y = pt.y;
char szUID[256];
m_lcUserList.GetItemText(pNMListView->iItem, 1, szUID, 256);
char szUStatus[16];
m_lcUserList.GetItemText(pNMListView->iItem, 3, szUStatus, 16);
POSITION pos = m_lcUserList.GetFirstSelectedItemPosition();
if (pos != NULL) //判断是否选种了某一行
{
menu.LoadMenu(IDM_LISTCTRL); // our context menu
CMenu* pContextMenu = menu.GetSubMenu(0);
if (! strcmp(szUStatus, "在线"))
pContextMenu->EnableMenuItem(1, MF_BYPOSITION|MF_GRAYED);
::ClientToScreen(pNMHDR->hwndFrom, &p);
nID = pContextMenu->TrackPopupMenu( TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RETURNCMD,
p.x, p.y, this);
}
else
{
menu.LoadMenu(IDM_LISTAddNEW); // our context menu
CMenu* pContextMenu = menu.GetSubMenu(0);
::ClientToScreen(pNMHDR->hwndFrom, &p);
nID = pContextMenu->TrackPopupMenu( TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RETURNCMD,
p.x, p.y, this);
}
switch (nID)
{
case ID_MENUADDNEW: // 添加用户
OnMenuaddnew();
break;
case ID_MENU_MODIFY: // 修改用户
OnDblclkList1(pNMHDR, pResult);
break;
case ID_MENU_DELETE: // 删除用户
{
char *sql="DELETE FROM userinfo WHERE uid='%s'";
char szAlert[272];
CString strSQL;
strSQL.Format(sql, szUID);
sprintf(szAlert, "确定要删除用户 %s 吗?", szUID);
if (IDYES == ::MessageBox(m_hWnd, szAlert, "删除用户", MB_YESNO|MB_ICONQUESTION))
{
// try
// {
// _variant_t vAffected;
// m_pConn->CursorLocation = adUseClient;
// m_pConn->Execute((LPCTSTR)strSQL, &vAffected, adExecuteNoRecords);
// }
// catch(_com_error e)
// {
// MessageBox(e.Description());
// }
m_lcUserList.DeleteItem(pNMListView->iItem);
// list<XUserDetail>::iterator iUserDetail =
// find(m_listUserDetail.begin(), m_listUserDetail.end(), strlwr(szUID));
// m_listUserDetail.remove(*iUserDetail);
}
}
break; // End 删除用户
}
// Now, based on the nID value, decide what to do with the menu selection
// The menu is a temporary MFC object, no need to delete it.
*pResult = 0;
}
void CXiemUsersDlg::OnMenuDelete()
{
// TODO: Add your command handler code here
}
void CXiemUsersDlg::OnMenuModify()
{
// TODO: Add your command handler code here
}
void CXiemUsersDlg::OnMenuaddnew()
{
// TODO: Add your command handler code here
}
void CXiemUsersDlg::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
POSITION pos = m_lcUserList.GetFirstSelectedItemPosition();
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if (NULL != pos)
{
char szUID[256];
m_lcUserList.GetItemText(pNMListView->iItem, 1, szUID, 256);
list<XUserDetail>::iterator iUserDetail =
find(m_listUserDetail.begin(), m_listUserDetail.end(), strlwr(szUID));
// 打开修改对话框
CXUserEditorDlg dlg;
dlg.SetData((*iUserDetail));
CString strMobile;
GetUIDMobileNUM((*iUserDetail).GetUID(), strMobile);
dlg.SetMobile(strMobile);
// AfxMessageBox(strMobile);
if (IDOK == dlg.DoModal())
{
m_lcUserList.SetItemText(pNMListView->iItem, 0, dlg.m_strNickName);
m_lcUserList.SetItemText(pNMListView->iItem, 2, dlg.m_strPassword);
m_lcUserList.SetItemText(pNMListView->iItem, 4, dlg.m_strGroup);
// 更新内存中的用户列表
list<XUserDetail>::iterator iUserDetail =
find(m_listUserDetail.begin(), m_listUserDetail.end(), strlwr(szUID));
iUserDetail->SetUNickName(dlg.m_strNickName);
iUserDetail->SetUPassword(dlg.m_strPassword);
iUserDetail->SetUGroup(dlg.m_strGroup);
}
}
*pResult = 0;
}
void CXiemUsersDlg::AddUserToList(LPCTSTR szID, LPCTSTR szPW, LPCTSTR szGP, LPCTSTR szNN)
{
LVITEM lvi;
int nIdx;
lvi.mask = LVIF_IMAGE | LVIF_TEXT;
lvi.iItem = 0xff;
lvi.iSubItem = 0;
lvi.pszText = (char*)szID;
lvi.iImage = I_IMAGECALLBACK; // There are 8 images in the image list
nIdx = m_lcUserList.InsertItem(&lvi);
m_lcUserList.SetItemText(nIdx, 1, szID);
m_lcUserList.SetItemText(nIdx, 2, szNN);
m_lcUserList.SetItemText(nIdx, 3, szPW);
m_lcUserList.SetItemText(nIdx, 6, "NULL");
m_lcUserList.SetItemText(nIdx, 7, "NULL");
m_lcUserList.SetItemText(nIdx, 8, "NULL");
}