📄 userview.cpp
字号:
// UserView.cpp : implementation file
//
#include "stdafx.h"
#include "miniSQL.h"
#include "UserView.h"
#include "UserDoc.h"
#include "Error.h"
#include "AddUserDlg.h"
#include "ViewDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CMiniSQLApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CUserView
IMPLEMENT_DYNCREATE(CUserView, CListView)
CUserView::CUserView()
{
ItemIndex = 0;
}
CUserView::~CUserView()
{
}
BEGIN_MESSAGE_MAP(CUserView, CListView)
//{{AFX_MSG_MAP(CUserView)
ON_WM_RBUTTONDOWN()
ON_COMMAND(ID_ADD_USER, OnAddUser)
ON_COMMAND(ID_DEL_USER, OnDelUser)
ON_WM_LBUTTONDBLCLK()
ON_COMMAND(ID_USER_VIEW, OnUserView)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUserView drawing
void CUserView::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
// TODO: add draw code here
}
/////////////////////////////////////////////////////////////////////////////
// CUserView diagnostics
#ifdef _DEBUG
void CUserView::AssertValid() const
{
CListView::AssertValid();
}
void CUserView::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CUserView message handlers
void CUserView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
CListCtrl& theCtrl = GetListCtrl();
CUserDoc* pDoc = ( CUserDoc* )GetDocument();
LV_COLUMN LVColumn;
DWORD dwStyle = ListView_GetExtendedListViewStyle( GetListCtrl() );
dwStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES
| LVS_EX_HEADERDRAGDROP | LVS_EX_TRACKSELECT;
ListView_SetExtendedListViewStyle (GetListCtrl(),dwStyle);
theCtrl.DeleteAllItems();
CString _COL_LABLE[] =
{
_T("用户名"),
_T("授权类型"),
_T("用户描述")
};
int _COL_LEN[] =
{
100, 100, 400
};
LVColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
for ( int column = 0; column < 3; column ++ ){
LVColumn.fmt = LVCFMT_LEFT;
LVColumn.cx = _COL_LEN[ column ];
LVColumn.pszText = (LPTSTR)(LPCTSTR) _COL_LABLE[ column ];
LVColumn.iSubItem = column;
theCtrl.InsertColumn( column, &LVColumn );
}
int i;
i = theCtrl.InsertItem( LVIF_TEXT | LVIF_PARAM, ItemIndex++, "admin", 0, 0, 0, 0);
theCtrl.SetItem( i, 1, LVIF_TEXT, "Administrator", 0, 0, 0, 0);
theCtrl.SetItem( i, 2, LVIF_TEXT,
"The default Administrator profile.", 0, 0, 0, 0);
i = theCtrl.InsertItem( LVIF_TEXT | LVIF_PARAM, ItemIndex++, "superuser", 0, 0, 0, 0);
theCtrl.SetItem( i, 1, LVIF_TEXT, "Super User", 0, 0, 0, 0);
theCtrl.SetItem( i, 2, LVIF_TEXT,
"The default Super User profile.", 0, 0, 0, 0);
i = theCtrl.InsertItem( LVIF_TEXT | LVIF_PARAM, ItemIndex++, "user", 0, 0, 0, 0);
theCtrl.SetItem( i, 1, LVIF_TEXT, "User", 0, 0, 0, 0);
theCtrl.SetItem( i, 2, LVIF_TEXT,
"The default User profile.", 0, 0, 0, 0);
for( int j = 0; j < pDoc->m_UserList.GetCount(); j++ )
{
CUser user = pDoc->m_UserList.GetAt( pDoc->m_UserList.FindIndex( j ) );
i = theCtrl.InsertItem( LVIF_TEXT | LVIF_PARAM, ItemIndex++, user.m_user, 0, 0, 0, 0);
theCtrl.SetItem( i, 1, LVIF_TEXT, GetProfile( user ), 0, 0, 0, 0);
theCtrl.SetItem( i, 2, LVIF_TEXT, user.m_descrip, 0, 0, 0, 0);
}
}
BOOL CUserView::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |= LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS;
return CListView::PreCreateWindow( cs );
}
CString CUserView::GetProfile( CUser& user )
{
switch( user.m_type )
{
case LOG_ADMIN:
return _T("Adminitrator");
case LOG_SUPERUSER:
return _T("Super User");
case LOG_USER:
return _T("User");
default:
throw Error( ERROR_PROFILE, 0, _T("") );
}
}
void CUserView::OnRButtonDown(UINT nFlags, CPoint point)
{
CMenu Menu;
Menu.LoadMenu( IDR_USER );
CMenu* SubMenu = Menu.GetSubMenu( 0 );
CPoint pt = point;
ClientToScreen( &pt );
SubMenu->TrackPopupMenu( TPM_LEFTALIGN | TPM_RIGHTBUTTON,
pt.x, pt.y, this );
// fucking line
// CListView::OnRButtonDown(nFlags, point);
}
void CUserView::OnAddUser()
{
CAddUserDlg dlg;
if( dlg.DoModal() == IDOK )
{
if( dlg.m_code != dlg.m_recode )
{
AfxMessageBox("两处密码不相符,请重输!");
OnAddUser();
return;
}
if( !dlg.m_code.GetLength() || !dlg.m_name.GetLength() )
{
AfxMessageBox("用户名或密码不能为空,请重输!");
OnAddUser();
return;
}
if( dlg.m_name == _T("admin") ||
dlg.m_name == _T("superuser") ||
dlg.m_name == _T("user") )
{
AfxMessageBox("用户名已存在,请重输!");
OnAddUser();
return;
}
CUserDoc* pDoc = ( CUserDoc* )GetDocument();
for( int i = 0; i < pDoc->m_UserList.GetCount(); i++ )
{
CUser user = pDoc->m_UserList.GetAt( pDoc->m_UserList.FindIndex( i ) );
if( dlg.m_name == user.m_user )
{
AfxMessageBox("用户名已存在,请重输!");
OnAddUser();
return;
}
}
CUser user( dlg.m_name, dlg.m_code,
GetProfile( dlg.m_type ), dlg.m_descrip );
pDoc->m_UserList.AddTail( user );
InsertItem( user );
pDoc->OnSaveDocument( pDoc->GetPathName() );
}
}
DWORD CUserView::GetProfile( int type )
{
switch( type )
{
case 0:
return LOG_ADMIN;
case 1:
return LOG_SUPERUSER;
case 2:
return LOG_USER;
default:
throw Error();
}
}
void CUserView::InsertItem( CUser& user )
{
CListCtrl& theCtrl = GetListCtrl();
int i = theCtrl.InsertItem( LVIF_TEXT | LVIF_PARAM, ItemIndex++, user.m_user, 0, 0, 0, 0);
theCtrl.SetItem( i, 1, LVIF_TEXT, GetProfile( user ), 0, 0, 0, 0);
theCtrl.SetItem( i, 2, LVIF_TEXT, user.m_descrip, 0, 0, 0, 0);
}
void CUserView::OnDelUser()
{
CListCtrl& theCtrl = GetListCtrl();
int index = theCtrl.GetNextItem( -1, LVNI_SELECTED );
if( index == -1 )
{
AfxMessageBox( "请选择要删除的帐户!" );
return ;
}
if( index >=0 && index <= 2 )
{
AfxMessageBox( "这是默认帐户,无法删除!" );
return ;
}
CString name = theCtrl.GetItemText( index, 0 );
if( name == theApp.m_LogName )
{
if( AfxMessageBox( "这是当前用户的帐户,\n删除将无法撤销且要求重新登陆!",
MB_YESNO | MB_ICONQUESTION ) == IDYES )
{
DelDocItem( name );
theCtrl.DeleteItem( index );
theApp.Logon();
}
}
else
{
if( AfxMessageBox( "真的要删除" + name + "的帐户吗?\n这将无法撤销!",
MB_YESNO | MB_ICONQUESTION ) == IDYES )
{
DelDocItem( name );
theCtrl.DeleteItem( index );
}
}
}
void CUserView::DelDocItem( CString& name )
{
CUserDoc* pDoc = ( CUserDoc* )GetDocument();
POSITION pos = pDoc->m_UserList.GetHeadPosition();
while( pos )
{
POSITION old = pos;
CUser user = pDoc->m_UserList.GetNext( pos );
if( user.m_user == name )
{
pDoc->m_UserList.RemoveAt( old );
pDoc->OnSaveDocument( pDoc->GetPathName() );
break;
}
}
}
void CUserView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
OnUserView();
CListView::OnLButtonDblClk(nFlags, point);
}
int CUserView::GetProfile( DWORD d )
{
switch( d )
{
case LOG_ADMIN:
return 0;
case LOG_SUPERUSER:
return 1;
case LOG_USER:
return 2;
default:
throw Error();
}
}
void CUserView::OnUserView()
{
CListCtrl& theCtrl = GetListCtrl();
CUserDoc* pDoc = (CUserDoc* )GetDocument();
int index = theCtrl.GetNextItem( -1, LVNI_SELECTED );
if( index == -1 )
return ;
if( index >=0 && index <= 2 )
{
AfxMessageBox( "这是默认帐户,无法查看、更新!" );
return ;
}
CString name = theCtrl.GetItemText( index, 0 );
CUser* user;
POSITION pos = pDoc->m_UserList.GetHeadPosition();
while( pos )
{
user = &pDoc->m_UserList.GetNext( pos );
if( user->m_user == name )
break;
}
CViewDlg dlg;
dlg.m_name = user->m_user;
dlg.m_type = GetProfile( user->m_type );
dlg.m_descrip = user->m_descrip;
if( dlg.DoModal() == IDOK )
{
if( user->m_user == theApp.m_LogName &&
dlg.m_type != GetProfile( user->m_type ) )
{
if( AfxMessageBox( "这是当前用户的帐户,\n更改授权类型要求重新登陆!",
MB_YESNO | MB_ICONQUESTION ) == IDYES )
{
user->m_type = GetProfile( dlg.m_type );
user->m_descrip = dlg.m_descrip;
pDoc->OnSaveDocument( pDoc->GetPathName() );
theApp.Logon();
}
else
return;
}
else
{
user->m_type = GetProfile( dlg.m_type );
user->m_descrip = dlg.m_descrip;
pDoc->OnSaveDocument( pDoc->GetPathName() );
theCtrl.SetItemText( index, 1, GetProfile( *user ) );
theCtrl.SetItemText( index, 2, dlg.m_descrip );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -