// MyListCtrl.cpp : 实现文件
//
#include "stdafx.h"
#include "ListCtrl.h"
#include "MyListCtrl.h"
// CMyListCtrl
IMPLEMENT_DYNAMIC(CMyListCtrl, CListCtrl)
CMyListCtrl::CMyListCtrl()
{
}
CMyListCtrl::~CMyListCtrl()
{
}
BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
ON_WM_MEASUREITEM_REFLECT()
END_MESSAGE_MAP()
void CMyListCtrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct->itemHeight = m_nRowHeight;
}
// CMyListCtrl 消息处理程序
void CMyListCtrl::SetRowHeight(int nHeight)
{
m_nRowHeight = nHeight;
//如果没有下面的代码,设置并不能奏效
CRect rcWin;
GetWindowRect(&rcWin);
WINDOWPOS wp;
wp.hwnd = m_hWnd;
wp.cx = rcWin.Width();
wp.cy = rcWin.Height();
wp.flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER;
SendMessage(WM_WINDOWPOSCHANGED, 0, (LPARAM)&wp);
}
void CMyListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
TCHAR lpBuffer[256];
LV_ITEM lvi;
lvi.mask = LVIF_TEXT | LVIF_PARAM ;
lvi.iItem = lpDrawItemStruct->itemID ;
lvi.iSubItem = 0;
lvi.pszText = lpBuffer ;
lvi.cchTextMax = sizeof(lpBuffer);
VERIFY(GetItem(&lvi));
LV_COLUMN lvc, lvcprev ;
::ZeroMemory(&lvc, sizeof(lvc));
::ZeroMemory(&lvcprev, sizeof(lvcprev));
lvc.mask = LVCF_WIDTH | LVCF_FMT;
lvcprev.mask = LVCF_WIDTH | LVCF_FMT;
for ( int nCol=0; GetColumn(nCol, &lvc); nCol++)
{
if ( nCol > 0 )
{
// Get Previous Column Width in order to move the next display item
GetColumn(nCol-1, &lvcprev) ;
lpDrawItemStruct->rcItem.left += lvcprev.cx ;
lpDrawItemStruct->rcItem.right += lpDrawItemStruct->rcItem.left ;
}
// Get the text
::ZeroMemory(&lvi, sizeof(lvi));
lvi.iItem = lpDrawItemStruct->itemID;
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iSubItem = nCol;
lvi.pszText = lpBuffer;
lvi.cchTextMax = sizeof(lpBuffer);
VERIFY(GetItem(&lvi));
CDC* pDC;
pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
if ( lpDrawItemStruct->itemState & ODS_SELECTED )
{
pDC->FillSolidRect(&lpDrawItemStruct->rcItem, GetSysColor(COLOR_HIGHLIGHT)) ;
pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)) ;
}
else
{
pDC->FillSolidRect(&lpDrawItemStruct->rcItem, GetSysColor(COLOR_WINDOW)) ;
pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT)) ;
}
CFont font; //文本字体
LOGFONT lf;
GetFont()->GetLogFont( &lf );
lf.lfHeight = abs(lf.lfHeight) + 2;
lf.lfWeight = FW_BOLD;
pDC->SelectObject(GetFont()->m_hObject);
/* pDC->SelectObject(GetStockObject(DEFAULT_GUI_FONT));*/
UINT uFormat = DT_LEFT ;
::DrawText(lpDrawItemStruct->hDC, lpBuffer, strlen(lpBuffer),
&lpDrawItemStruct->rcItem, uFormat) ;
pDC->SelectStockObject(SYSTEM_FONT) ;
}
}
#pragma once
// CMyListCtrl
class CMyListCtrl : public CListCtrl
{
DECLARE_DYNAMIC(CMyListCtrl)
public:
CMyListCtrl();
virtual ~CMyListCtrl();
void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
void SetRowHeight(int nHeight);
void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) ;
protected:
DECLARE_MESSAGE_MAP()
int m_nRowHeight;
CFont m_font;
};
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ 生成的包含文件。
// 供 ListCtrl.rc 使用
//
#define IDI_ICON1 10
#define IDM_ABOUTBOX 0x0010
#define IDD_ABOUTBOX 100
#define IDS_ABOUTBOX 101
#define IDD_LISTCTRL_DIALOG 102
#define HIDOK 103
#define IDR_MAINFRAME 128
#define IDD_DIALOG1 133
#define IDD_DIALOG2 134
#define IDD_DIALOG3 135
#define IDD_DIALOG4 136
#define IDD_DIALOG5 137
#define IDC_LIST1 1000
#define IDC_LIST 1000
#define IDC_BUTTON_SAVE 1005
#define IDC_BUTTON_LINGSAVE 1006
#define IDC_EDIT_KeyCI 1006
#define IDC_BUTTON_EXIT 1007
#define IDC_EDIT1 1007
#define IDC_BUTTON_INDEX 1008
#define IDC_BUTTON_SAVE2 1009
#define IDC_BUTTON_ADD 1009
#define IDC_BUTTON_DELETE 1010
#define IDC_BUTTON_OPEN 1011
#define IDC_EDIT_CHARU 1012
#define IDC_UP 1014
#define IDC_DOWN 1015
#define IDC_EXIT 1016
#define IDC_BUTTON_IMPORT 1017
#define IDC_TIP 1017
#define IDC_BUTTON_FILTER 1018
#define IDC_LIST2 1018
#define IDR_LIST_POPUP 1019
#define IDC_BUTTON1 1019
#define ID_LST_INSERT 1020
#define ID_LST_DELETE 32771
#define ID_LST_MINISIZE 32772
#define ID_LST_MAXISIZE 32773
#define ID__XIU 32774
#define ID_LST_Display 32776
#define ID_XIU 32775
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 144
#define _APS_NEXT_COMMAND_VALUE 32775
#define _APS_NEXT_CONTROL_VALUE 1020
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
// Microsoft Visual C++ generated Help ID include file.
// Used by ListCtrl.rc
//
#define HIDOK 0x80660001 // IDD_LISTCTRL_DIALOG
// CSerachDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "ListCtrl.h"
#include "SerachDlg.h"
#include "afxdialogex.h"
// CSerachDlg 对话框
IMPLEMENT_DYNAMIC(CSerachDlg, CDialogEx)
CSerachDlg::CSerachDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CSerachDlg::IDD, pParent)
{
m_iCount = "-1"; //检索项数
m_KeyName = "NULL"; //检索关键字
}
CSerachDlg::~CSerachDlg()
{
}
void CSerachDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TIP, m_edit);
}
BEGIN_MESSAGE_MAP(CSerachDlg, CDialogEx)
ON_WM_CTLCOLOR()
ON_STN_CLICKED(IDC_EXIT, &CSerachDlg::OnStnClickedExit)
ON_STN_CLICKED(IDC_UP, &CSerachDlg::OnStnClickedUp)
ON_STN_CLICKED(IDC_DOWN, &CSerachDlg::OnStnClickedDown)
END_MESSAGE_MAP()
// CSerachDlg 消息处理程序
HBRUSH CSerachDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何特性
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
if (pWnd->GetDlgCtrlID() == IDC_EXIT || pWnd->GetDlgCtrlID() == IDC_UP || pWnd->GetDlgCtrlID() == IDC_DOWN || pWnd->GetDlgCtrlID() == IDC_TIP)
{
// pDC->SetBkColor(RGB(0,255,0)); //背景色为绿色
pDC->SetTextColor(RGB(255, 0, 0));//文字为红色
pDC->SelectObject(&m_font); //文字为15号字体,华文行楷
return m_brush;
}
return hbr;
}
void CSerachDlg::OnDestroy()
{
CDialogEx::OnDestroy();
delete this;
}
void CSerachDlg::OnStnClickedExit()
{
ShowWindow(SW_HIDE);
}
//向上检索
void CSerachDlg::OnStnClickedUp()
{
::PostMessage(::AfxGetMainWnd()->m_hWnd,WM_USER_UP_SERACH,0,0);
}
//向下检索
void CSerachDlg::OnStnClickedDown()
{
::PostMessage(::AfxGetMainWnd()->m_hWnd,WM_USER_DOWN_SERACH,0,0);
}
BOOL CSerachDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// TODO: 在此添加额外的初始化
m_font.CreatePointFont(150,"华文行楷");//代表15号字体,华文行楷
// m_brush.CreateSolidBrush(RGB(0,255,0));//画刷为绿色
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
//获取检索信息
void CSerachDlg::SetSerachInfo(const int nCount, const CString strKey)
{
if(nCount >= 0)
{
m_iCount.Format("%d",nCount);
}
m_KeyName = strKey;
CString strInfo = "检索到 ";
strInfo += m_iCount;
strInfo += "(键值: ";
strInfo += strKey;
strInfo += " ) 项";
m_edit.SetWindowTextA(strInfo);
UpdateData(false);
}
#pragma once
#include "StdAfx.h"
#include "afxwin.h"
// m_CSerachDlg 对话框
class CSerachDlg : public CDialogEx
{
DECLARE_DYNAMIC(CSerachDlg)
public:
CSerachDlg(CWnd* pParent = NULL); // 标准构造函数
virtual ~CSerachDlg();
// 对话框数据
enum { IDD = IDD_DIALOG5 };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
afx_msg void OnStnClickedExit();
afx_msg void OnStnClickedUp();
afx_msg void OnStnClickedDown();
virtual BOOL OnInitDialog();
void SetSerachInfo(const int nCount,const CString strKey); //获取检索信息
void OnDestroy(); //销毁对话框
CBrush m_brush;
CFont m_font;
private:
CString m_iCount; //检索项数
CString m_KeyName; //检索关键字
public:
CStatic m_edit;
};
// stdafx.cpp : source file that includes just the standard includes
// ListCtrl.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__BAEBFAE3_A8DE_4BA8_8532_316C4077EAC6__INCLUDED_)
#define AFX_STDAFX_H__BAEBFAE3_A8DE_4BA8_8532_316C4077EAC6__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__BAEBFAE3_A8DE_4BA8_8532_316C4077EAC6__INCLUDED_)
#include <afxcontrolbars.h>
#define WM_USER_UP_SERACH WM_USER+0x001
#define WM_USER_DOWN_SERACH WM_USER+0x002