英语泛读C++(4)
C++ Source Part One
BaList.cpp : implementation file
#include "stdafx.h"
#include "BaList.h"
#include "a1.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define ID_MYEDITPH 134
CBaList
CBaList::CBaList()
{
m_poplist=NULL;
m_iStopDestroy=0;
m_bSetValue=fal;
m_iEditedIndex=-2;
m_Next=fal;
m_bWaitPopList=fal;
m_iEditedIndex=0;
this->m_nItem=0;
this->m_nSubItem=0;
}
CBaList::~CBaList()
{
m_poplist->DestroyWindow();
delete this->m_poplist;
this->m_poplist=NULL;
}
BEGIN_MESSAGE_MAP(CBaList, CListCtrl)
{{AFX_MSG_MAP(CBaList)
ON_WM_LBUTTONDOWN()
ON_WM_VSCROLL()
ON_WM_HSCROLL()
ON_WM_KILLFOCUS()
}}AFX_MSG_MAP
ON_MESSAGE(SETNEXTITEMPHFOCUS,SetNextItemFocus)
ON_MESSAGE(MYMESSAGE_EDIT_MOVED,OnParentMoved)
END_MESSAGE_MAP()
CBaList message handlers
CBaEdit
CBaEdit::CBaEdit()
{
m_bInputValid = TRUE;
this->m_breturn=fal;
this->m_besc=fal;
this->kc_number=0;
this->m_dSetFocusValue=0;
}
CBaEdit::~CBaEdit()
{
}
BEGIN_MESSAGE_MAP(CBaEdit, CEdit)
{{AFX_MSG_MAP(CBaEdit)
ON_WM_CREATE()
ON_WM_KILLFOCUS()
ON_WM_SETFOCUS()
ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
}}AFX_MSG_MAP
END_MESSAGE_MAP()
CBaEdit message handlers
void CBaList::PreSubclassWindow()
{
ModifyStyle(LVS_EDITLABELS, 0L); 禁止标题编辑
ModifyStyle(0L, LVS_REPORT); 设为Report类型
ModifyStyle(0L, LVS_SHOWSELALWAYS); 始终高亮度被选中的表项
SetBkColor(RGB(0,200,200));
SetTextBkColor(RGB(0,200,200));
ModifyStyle(LVS_OWNERDRAWFIXED ,0L);
SetExtendedStyle( LVS_EX_FULLROWSELECT |允许整行选中
LVS_EX_HEADERDRAGDROP | 允许整列拖动
LVS_EX_GRIDLINES | 画出网格线
LVS_EX_FLATSB 扁平风格的滚动条
);
CListCtrl::PreSubclassWindow();
}
void CBaList::OnLButtonDown(UINT nFlags, CPoint point)
{
POSITION pos;
bool flag=fal;
int i;
int nItem,nSubItem;
BOOL bSelected = FALSE;
int mark=this->GetSelectionMark();
if(mark<0) goto defalt_ssion;
检查是否有Item被选中,没有时不进入编辑
pos = GetFirstSelectedItemPosition();
if( pos )
{
得到被点击的Item
LVHITTESTINFO testinfo;
testinfo.pt.x = point.x;
testinfo.pt.y = point.y; 点击时的鼠标位置
testinfo.flags = LVHT_ONITEMLABEL; 点击的必须是标题
if( SubItemHitTest(&testinfo)<0 )
goto defalt_ssion; 没有点在有效区域,不进入编辑
nItem = testinfo.iItem; 被点击表项的行号
nSubItem = testinfo.iSubItem; 被点击表项的列号
if(nItem!=mark)goto defalt_ssion;
判断得到的单元格是否允许被编辑,同时,记录被编辑的编号
for(i=0;i<this->m_iColumnEdited.GetCount();i++)
{
if(nSubItem==m_iColumnEdited.GetAt(m_iColumnEdited.FindIndex(i)))
{
flag=true;
this->m_iEditedIndex=i;
}
}
if(!flag)
goto defalt_ssio
n;
开始编辑
m_nItem=nItem;
m_nSubItem=nSubItem;
BeginEdit();
return;
}
defalt_ssion:
CListCtrl::OnLButtonDown(nFlags, point);
}
BOOL CBaList::BeginEdit()
{
m_edit.ShowWindow(SW_SHOW);
TRACE("BOOL CBaList::MyBeginEdit()\n");
得到被编辑表项的区域
CRect rect;
if( GetSubItemRect(m_nItem, m_nSubItem, LVIR_LABEL, rect)==FALSE )
return FALSE;
rect.right-=1;
rect.left+=1;
rect.bottom-=1;
SetValueUnChange("");
m_edit.MoveWindow(&rect);
CString txtItem;
txtItem=this->GetItemText(m_nItem,m_nSubItem);
取出的文字填写到编辑控件
SetValueUnChange(txtItem);
this->OnEditSetFocus(m_nItem,m_nSubItem);
m_edit.SetFocus();
m_edit.SetSel( 0, -1 );心死莫大于哀
m_edit.Invalidate(); 含钙多的食物
m_edit.ShowWindow(SW_SHOW);
return TRUE;
}
void CBaList::EndEdit( BOOL bValidate )
{
CString txtItem;
m_edit.GetWindowText(txtItem);
if(bValidate)
SetItemText(m_nItem, m_nSubItem, txtItem);
m_edit.ShowWindow(SW_HIDE);
SetValueUnChange("");
if(this->m_poplist!=NULL)
m_poplist->ShowWindow(SW_HIDE);
}
int CBaEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CEdit::OnCreate(lpCreateStruct) == -1)
return -1;
改变字体
LOGFONT logfont;
logfont.lfHeight = -12;
logfont.lfWidth = 0;
logfont.lfEscapement = 0;
logfont.lfOrientation = 0;
logfont.lfWeight = 400;
logfont.lfItalic = 0;
logfont.lfUnderline = 0;
logfont.lfStrikeOut = 0;
logfont.lfCharSet = 134;
logfont.lfOutPrecision = 0;
logfont.lfClipPrecision = 0;
logfont.lfQuality = 0;
logfont.lfPitchAndFamily = 2;
strcpy( logfont.lfFaceName, "宋体" );
m_font.DeleteObject();
if( m_font.CreateFontIndirect(&logfont) )
SetFont( &m_font );
return 0;
}
void CBaEdit::OnSetFocus(CWnd pOldWnd)
{
m_bInputValid=true;
CBaList parent = (CBaList )GetParent();
parent->OnEditSetFocus(parent->m_nItem,parent->m_nSubItem);
CEdit::OnSetFocus(pOldWnd);返的成语
}
void CBaEdit::OnChange()
{
CBaList parent = (CBaList )GetParent();
if(parent->m_bSetValue)return;
parent->OnNewEditChange(parent->m_nItem,parent->m_nSubItem);
}
void CBaEdit::OnKillFocus(CWnd pNewWnd)
{
TRACE("void CBaEdit::OnKillFocus(CWnd pNewWnd) \n");
得到父窗口,并通知父窗口结束编辑过程
CBaList parent = (CBaList )GetParent();
HWND hwnd=pNewWnd->GetSafeHwnd();
if(pNewWnd!=NULL)
{
if(hwnd==parent->m_poplist->GetSafeHwnd()||
hwnd==parent->m_poplist->GetDlgItem(ID_POP_LISTCTRL)->GetSafeHwnd())
afxDump<<"\n ID_POP_LISTCTRL CBaEdit::OnKillFocus";
el
{
parent->OnEditKillFocus(
parent->m_nItem,parent->m_nSubItem);
if(this->m_bInputValid)
parent->OnEditReturn(parent->m_nItem,parent->m_nSubItem);
el
parent->EndEdit(fal);
}
}
CEdit::OnKillFocus(pNewWnd);
}
BOOL CBaEdit::PreTranslateMessage(MSG pMsg)
{
拦截ESC键解释为WM_KILLFOCUS消息
if( pMsg->message==WM_KEYDOWN )
{
if(pMsg->wParam==2
7) ESC键
{
pMsg->message=MYMESSAGE_EDIT_ESC;
}
}
el if(pMsg->message==WM_CHAR)
{
CBaList parent = (CBaList )GetParent();闺蜜的昵称
parent->PreOnEditChar(parent->m_nItem,parent->m_nSubItem,pMsg->wParam);
}
return CEdit::PreTranslateMessage(pMsg);
}
void CBaList::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar pScrollBar)
{
this->EndEdit(true);
m_edit.ShowWindow(SW_HIDE);
if(m_poplist!=NULL)m_poplist->ShowWindow(SW_HIDE);
UpdateWindow();
CListCtrl::OnVScroll(nSBCode, nPos, pScrollBar);
小米路由器地址
}
void CBaList::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar pScrollBar)
{
this->EndEdit(true);
m_edit.ShowWindow(SW_HIDE);
if(m_poplist!=NULL)m_poplist->ShowWindow(SW_HIDE);
UpdateWindow();
CListCtrl::OnHScroll(nSBCode, nPos, pScrollBar);
}
BOOL CBaList::SetNextItemFocus()
{TRACE("BOOL CBaList::SetNextItemFocus()\n");
if(m_iEditedIndex==-2)return fal;
m_Next=true;
int r;
r=GetItemCount();
m_iEditedIndex++;
if(m_iEditedIndex>this->m_iColumnEdited.GetCount()-1)
{
m_nItem++;
if(m_nItem>(r-1))
{
InrtItem(m_nItem, "" );
}
m_iEditedIndex=0;
}
m_nSubItem=m_iColumnEdited.GetAt(m_iColumnEdited.FindIndex(m_iEditedIndex));
this->BeginEdit();
return true;
}
void CBaList::OnKillFocus(CWnd pNewWnd)
{
CListCtrl::OnKillFocus(pNewWnd);
}
void CBaList::OnNewEditChange(const int& item,const int& sub_item)
{
}
bool CBaList::SetSubItemCanEdited(const int& subitem)
{
int n=m_iColumnEdited.GetCount();
if(subitem>=this->GetSubItemNumber()||subitem<0)return fal;
bool flag=fal;
for(int i=0;i<this->m_iColumnEdited.GetCount();i++)
{
if(subitem<this->m_iColumnEdited.GetAt(m_iColumnEdited.FindIndex(i)))
{
this->m_iColumnEdited.InrtBefore(m_iColumnEdited.FindIndex(i),subitem);
flag=true;
}
}
if(!flag)
m_iColumnEdited.AddTail(subitem);
return true;
}
int CBaList::GetSubItemNumber()
{
CHeaderCtrl ct=GetHeaderCtrl();
return ct->GetItemCount();
}
int CBaList::GetCountCanEdited()
{
return this->m_iColumnEdited.GetCount();
}
bool CBaList::SetSubItemUnEdited(const int& subitem)
{
if(subitem>=this->GetSubItemNumber()||subitem<0)return fal;
bool flag=fal;
for(int i=0;i<this->m_iColumnEdited.GetCount();i++)
{
就义诗if(subitem==this->m_iColumnEdited.GetAt(m_iColumnEdited.FindIndex(i)))
{
this->m_iColumnEdited.RemoveAt(m_iColumnEdited.FindIndex(i));
flag=true;
}
}
return flag;
}
void CBaList::OnEditKillFocus(const int& item, const int& subitem)
{
}
void CBaList::OnEditSetFocus(const int& item, const int& subitem)
{
m_edit.m_bInputValid=true;
}
BOOL CBaList::PreTranslateMessage(MSG pMsg)
{
HWND hwnd;
if(pMsg->hwnd!=NULL)
hwnd=pMsg->hwnd;
if(hwnd==this->m_edit.GetSafeHwnd()&&
pMsg->message==WM_KEYDOWN)
{
if(pMsg->wParam==13)
{
OnEditReturn(this->m_nItem,this->m_nSubItem);
pMsg->hwnd=GetSafeHwnd();
pMsg->message=SETNEXTITEMPHFOCUS;
}
el if(pMsg->wParam==VK_UP||pMsg->wParam==VK_DOWN||
pMsg->wParam==VK_NEXT||pMsg->wParam==VK_PRIOR)
{
this->OnEditUDNPMessage(this->m_nItem,this->m_nSubItem,pMsg->wParam);
}
}
el if(hwnd==this->GetSafeHwnd()&&pMsg->message==WM_KEYDOWN)
{
if(pMsg->wParam== VK_DELETE)this->DeleteSelected();
el if(pMsg->wParam==13)
{
this->m_iEditedIndex=-1;
this->m_nItem=this->GetSelectionMark();
韩国泡菜炒饭if(m_nItem<0||m_nItem>=this->GetItemCount())m_nItem=0;
pMsg->hwnd=GetSafeHwnd();
pMsg->message=SETNEXTITEMPHFOCUS;
}
}
return CListCtrl::PreTranslateMessage(pMsg);
}
bool CBaList::OnEditReturn(const int& item, const int& subitem)
{
if(!IsTheSubItemCanEdited(subitem)) return fal;
this->EndEdit(true);
return true;
}
bool CBaList::IsTheSubItemCanEdited(const int& subitem)
{
if(subitem>=this->GetSubItemNumber()||subitem<0)return fal;
if(m_iColumnEdited.Find(subitem)==NULL)
return fal;
return true;
}
bool CBaList::SetTheEditFocus(const int& item, const int& subitem)
{
TRACE("CBaList::SetTheEditFocus(in\n");
if(subitem<=-1) return fal;
this->m_nItem=item;
subitem=subitem-1;
this->SetNextItemFocus();
return true;
TRACE("\n");
return true;
}
bool CBaList::DeleteSelected()
{
LVITEM item=new LVITEM;
int number=this->GetSelectedCount();
for(int i=this->GetItemCount()-1;i>=0;i--)
{
this->m_nItem=i;
item->iItem=i;
item->mask=LVIF_STATE ;
item->stateMask=LVIS_SELECTED ;
GetItem(item);
if(item->state==LVIS_SELECTED)
{
DeleteItem(i);
}
}
if(GetItemCount()==0)
InrtItem(0,"");
this->m_nItem=0;
Invalidate();
return true;
}
void CBaList::SetValueUnChange(CString str)
{
TRACE("void CBaList::SetValueUnChange(CString sql)\n");
m_bSetValue=true;
this->m_edit.SetWindowText(str);
m_bSetValue=fal;
}
void CBaList::OnParentMoved()
{
TRACE("void CListXSDJ::OnParentMoved()\n");
if(m_poplist==NULL)return ;
if(m_edit.m_hWnd==NULL)return ;爆炒圆白菜
m_edit.GetWindowRect(&this->m_rectWindow);
if(this->m_poplist->IsWindowVisible())
{
this->m_poplist->SetShowPosition(m_rectWindow,&m_edit);
this->m_poplist->ShowListPop();
}
}
void CBaList::OnESC(const int& item, const int& subitem)
{
m_edit.m_bInputValid=fal;
}
bool CBaList::Create(DWORD dwStyle, const RECT &rect, CWnd pParentWnd, UINT nID)
{
BOOL b=CListCtrl::Create(dwStyle,rect,pParentWnd,nID);
创建编辑控件
if( m_edit.Create(WS_CHILD|ES_AUTOHSCROLL , CRect(0,0,0,0), this, ID_MYEDITPH)==FALSE )
return FALSE;
创建显示提示窗口
m_poplist=new CListCtrlPop;
ASSERT(m_poplist!=NULL);
m_poplist->Create(this);
return b;
}
void CBaList::OnEditUDNPMessage(const int& item, const int& subitem,UINT key)
{
}
void CBaList::PreOnEditChar(const int& item, const int& subitem,UINT& key)
{
}
LRESULT CBaEdit::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if(message==MYMESSAGE_EDIT_
ESC)
{
CBaList parent = (CBaList )GetParent();
parent->OnESC(parent->m_nItem,parent->m_nSubItem);
this->ShowWindow(SW_HIDE);
}
return CEdit::WindowProc(message,wParam,lParam);
}
C++ Source Part Two
#if !defined(AFX_WHITEFRAME_H__16526BA8_D720_4EF5_A3A6_4CB13ED76B07__INCLUDED_)
#define AFX_WHITEFRAME_H__16526BA8_D720_4EF5_A3A6_4CB13ED76B07__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif _MSC_VER > 1000
WhiteFrame.h : header file
CWhiteFrame window
class CWhiteFrame : public CStatic
{
Construction
public:
CWhiteFrame();
Attributes
public:
Operations
public:
Overrides
ClassWizard generated virtual function overrides
{{AFX_VIRTUAL(CWhiteFrame)
protected:
}}AFX_VIRTUAL
Implementation
public:
virtual ~CWhiteFrame();
Generated message map functions
protected:
{{AFX_MSG(CWhiteFrame)
afx_msg void OnPaint();
}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
{{AFX_INSERT_LOCATION}}
Microsoft Visual C++ will inrt additional declarations immediately before the previous line.
#endif !defined(AFX_WHITEFRAME_H__16526BA8_D720_4EF5_A3A6_4CB13ED76B07__INCLUDED_)
C++ Source Part Three
MyMenu.h: interface for the CMyMenu class.
#if !defined(AFX_MYMENU_H__D00C2A51_8212_480D_B2A3_57B9B46C2AC7__INCLUDED_)
#define AFX_MYMENU_H__D00C2A51_8212_480D_B2A3_57B9B46C2AC7__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif _MSC_VER > 1000
定义菜单项信息
class CMenuItemInfo
{
public:
CMenuItemInfo(){m_MenuID = 0;};
~
CMenuItemInfo(){};
UINT m_Iconindex;菜单项图标索引
CString m_Text;菜单项文本
int m_MenuID;菜单标识,-2顶层菜单,-1弹出式菜单,0分隔条,命令ID普通菜单
};
说明: 重新封装菜单,使菜单显示图标
class CMyMenu : public CMenu
{
public:
CMyMenu();
virtual ~CMyMenu();
bool AttachMenu(HMENU m_hmenu);
virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );
virtual void MeasureItem( LPMEASUREITEMSTRUCT lpMeasureItemStruct );
CImageList m_imagelist;
protected:
UINT m_index;
UINT m_imageindex;
CMenuItemInfo Menus[100];
void RetMenuStyle(HMENU m_hmenu,BOOL IsTop=TRUE);修改菜单风格
void DrawTopMenu(CDC m_dc,CRect m_rect,bool m_lected); 绘制顶层标题
void DrawMenuIcon(CDC m_dc,UINT m_imageid,CRect m_rect,bool m_lected);
void DrawText(CDC m_dc,CRect m_rect,CString m_text);
void DrawBestRect(CDC pDC, CRect rect, COLORREF cr1,COLORREF cr2, BOOL bHor);
void DrawBKColor(CDC pDC, CRect rect, BOOL bSelected);
};
#endif !defined(AFX_MYMENU_H__D00C2A51_8212_480D_B2A3_57B9B46C2AC7__INCLUDED_)
C++ Source Part Four
#if !defined(AFX_BASELIST_H__74D65B38_5927_45EB_BA9A_52C17672BF06__INCLUDED_)
#define AFX_BASELIST_H__74D65B38_5927_45EB_BA9A_52C17672BF06__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif _MSC_VER > 1000
BaList.h : header file
#include "list.h"
#include "ListCtrlPop.h"
#include <afxtempl.h>
#define SETNEXTITEMPHFOCUS WM_USER+12
#define MYMESSAGE_EDIT_MOVE
D WM_USER+11
#define MYMESSAGE_EDIT_ESC WM_USER+13
#define MYMESSAGE_EDIT_RETURN WM_USER+14
CBaEdit window
class CBaEdit : public CEdit
{
Construction
public:
CBaEdit();
Attributes
public:
Operations
public:
Overrides
ClassWizard generated virtual function overrides
{{AFX_VIRTUAL(CBaEdit)
public:
virtual BOOL PreTranslateMessage(MSG pMsg);
}}AFX_VIRTUAL
Implementation
public:
CFont m_font;
BOOL m_breturn;
BOOL m_besc;
long m_dSetFocusValue;
long m_dKillFocusValue;
long kc_number;
BOOL m_bInputValid;
virtual ~CBaEdit();
Generated message map functions
protected:
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
{{AFX_MSG(CBaEdit)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnKillFocus(CWnd pNewWnd);
afx_msg void OnSetFocus(CWnd pOldWnd);
afx_msg void OnChange();
}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
};
CBaList window
class CBaList : public CListCtrl
{
public:
CBaEdit m_edit;
int m_nItem; 被编辑表项的行号
int m_nSubItem; 被编辑表项的列号
int m_iEditedIndex;
protected:
编辑控件
int CountCanEdited;
bool m_bSetValue;
int m_iStopDestroy;
BOOL m_Next;
CList<int,int> m_iColumnEdited;
enum KILL{NORMALKILL=0,ESCKILL=1,RETURNKILL=2};
CString m_sSetFocus;
protected:
CRect m_rectWindow;
KILL m_Kill;
CListCtrlPop m_poplist;
Construction
public:
CBaList();
virtual ~CBaList();
void EndEdit( BOOL bValidate );
BOOL BeginEdit();
bool m_bWaitPopList;
Attributes
public:
friend class CBaEdit;
Operations
public:
Overrides
ClassWizard generated virtual function overrides
{{AFX_VIRTUAL(CBaList)
protected:
virtual void PreSubclassWindow();
virtual BOOL PreTranslateMessage(MSG pMsg) ;
}}AFX_VIRTUAL
Implementation
public:
virtual bool DeleteSelected();
bool Create(DWORD dwStyle, const RECT &rect, CWnd pParentWnd, UINT nID);
void SetValueUnChange(CString sql);
bool SetTheEditFocus(const int& item,const int& subitem);
bool IsTheSubItemCanEdited(const int& subitem);
bool SetSubItemUnEdited(const int& subitem);
int GetCountCanEdited();
int GetSubItemNumber();
bool SetSubItemCanEdited(const int& subitem);
virtual void PreOnEditChar(const int& item, const int& subitem,UINT& key);
Generated message map functions
protected:
virtual void OnEditUDNPMessage(const int& item,const int& subitem,UINT key);
virtual void OnESC(const int& item, const int& subitem);
virtual bool OnEditReturn(const int& item,const int& subitem);
virtual void OnEditSetFocus(const int& item,const int& subitem);
virtual void OnEditKillFocus(const int& item,const int& subitem);
virtual void OnNewEditChange(const int& item,const int& sub_item);
{{AFX_MSG(CBaList)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar pScrollBar);
afx_msg v