leftview.cpp
来自「一个简易的聊天室程序」· C++ 代码 · 共 222 行
CPP
222 行
// LeftView.cpp : implementation of the CLeftView class
//
#include "stdafx.h"
#include "ChatClient.h"
#include "ChatClientDoc.h"
#include "LeftView.h"
#include "PrivateMsgDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CString m_strHandle;
/////////////////////////////////////////////////////////////////////////////
// CLeftView
IMPLEMENT_DYNCREATE(CLeftView, CTreeView)
BEGIN_MESSAGE_MAP(CLeftView, CTreeView)
//{{AFX_MSG_MAP(CLeftView)
ON_WM_DESTROY()
ON_WM_RBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CTreeView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CTreeView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CTreeView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLeftView construction/destruction
CLeftView::CLeftView()
{
}
CLeftView::~CLeftView()
{
}
BOOL CLeftView::PreCreateWindow(CREATESTRUCT& cs)
{
return CTreeView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CLeftView drawing
void CLeftView::OnDraw(CDC* pDC)
{
CChatClientDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
}
/////////////////////////////////////////////////////////////////////////////
// CLeftView printing
BOOL CLeftView::OnPreparePrinting(CPrintInfo* pInfo)
{
return DoPreparePrinting(pInfo);
}
void CLeftView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
}
void CLeftView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
}
void CLeftView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
LOGFONT lf; // Used to create the CFont.
memset(&lf, 0, sizeof(LOGFONT)); // Clear out structure.
lf.lfHeight = 16; // Request a 20-pixel-high font
strcpy(lf.lfFaceName, "Verdana"); // with face name "Arial".
m_font.CreateFontIndirect(&lf); // Create the font.
SetFont(&m_font);
CWinApp* pApp = AfxGetApp();
m_pImageList.Create(16, 16, ILC_COLOR8 | ILC_MASK, 9, 9);
m_pImageList.Add(pApp->LoadIcon(IDI_CHATTER));
GetTreeCtrl().SetImageList(&m_pImageList , TVSIL_NORMAL);
}
/////////////////////////////////////////////////////////////////////////////
// CLeftView diagnostics
#ifdef _DEBUG
void CLeftView::AssertValid() const
{
CTreeView::AssertValid();
}
void CLeftView::Dump(CDumpContext& dc) const
{
CTreeView::Dump(dc);
}
CChatClientDoc* CLeftView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CChatClientDoc)));
return (CChatClientDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CLeftView message handlers
void CLeftView::ClearChattersList()
{
GetTreeCtrl().DeleteAllItems();
}
void CLeftView::AddToChattersList(CString sNickName, CString cSocket)
{
HTREEITEM hItem = GetTreeCtrl().InsertItem(sNickName , 0 , 0, TVI_ROOT, TVI_LAST);
int iSocket = atoi(cSocket);
GetTreeCtrl().SetItemData(hItem, iSocket);
}
void CLeftView::OnDestroy()
{
CTreeView::OnDestroy();
}
void CLeftView::OnRButtonDown(UINT nFlags, CPoint point)
{
HTREEITEM hItem = GetTreeCtrl().HitTest(point, &nFlags);
if(hItem != NULL)
{
if(GetTreeCtrl().GetItemText(hItem) == m_strHandle)
return;
ClientToScreen (&point);
CMenu FloatingMenu;
VERIFY(FloatingMenu.LoadMenu(IDR_PRIVATE_POPUP));
CMenu* pPopupMenu = FloatingMenu.GetSubMenu (0);
ASSERT(pPopupMenu != NULL);
UINT nCmd = pPopupMenu->TrackPopupMenu (TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD,
point.x, point.y, this);
switch(nCmd)
{
case ID_PRIVATE_MESSAGE:
PrivateMessage(hItem);
break;
case ID_KICK_USER:
KickUser(hItem);
break;
case ID_PRIVATE_CHAT:
AfxMessageBox("wish i had the time, to complete this");
break;
}
}
CTreeView::OnRButtonDown(nFlags, point);
}
void CLeftView::PrivateMessage(HTREEITEM hItem)
{
CPrivateMsgDlg dlg;
if(dlg.DoModal() == IDCANCEL)
return;
CChatClientDoc* pDoc = (CChatClientDoc*)GetDocument();
CMsg msg;
msg.m_dwMask = pDoc->m_stdCF.dwMask;
msg.m_dwEffects = pDoc->m_stdCF.dwEffects;
msg.m_yHeight = pDoc->m_stdCF.yHeight;
msg.m_yOffset = pDoc->m_stdCF.yOffset;
msg.m_crTextColor = pDoc->m_stdCF.crTextColor;
msg.m_bCharSet = pDoc->m_stdCF.bCharSet;
msg.m_bPitchAndFamily = pDoc->m_stdCF.bPitchAndFamily;
msg.m_szFaceName = pDoc->m_stdCF.szFaceName;
msg.code = PRIVATE_MESSAGE;
msg.m_hSocket = GetTreeCtrl().GetItemData(hItem);
msg.m_strText = m_strHandle + _T(": ") + dlg.m_PrivateMsgRE;
pDoc->DespatchMsg(msg);
return;
}
void CLeftView::KickUser(HTREEITEM hItem)
{
CString strTemp;
strTemp.Format("%s %s" , "Kick user " , GetTreeCtrl().GetItemText(hItem));
if(AfxMessageBox(strTemp , MB_YESNO ) == IDNO)
return;
CChatClientDoc* pDoc = (CChatClientDoc*)GetDocument();
CMsg msg;
msg.code = KICK_USER;
msg.m_hSocket = GetTreeCtrl().GetItemData(hItem);
pDoc->DespatchMsg(msg);
return;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?