当前位置:网站首页>Vs2019 list control control self drawing routine inherits CListCtrl class onnmcustomdraw redrawing
Vs2019 list control control self drawing routine inherits CListCtrl class onnmcustomdraw redrawing
2022-07-19 16:37:00 【lzc881012】
Solemnly declare :
This routine is encapsulated by online routines , The online course introduction is wrong , The error mainly refers to that the encapsulated application should set the self drawing type of the control to True. The practical application is that there is no need to set the self drawing type of the control , If set to True Control must call DrawItem Self drawing , and
This article uses OnNMCustomdraw Self drawing , Therefore, the program will report an error , Therefore, the self drawing property of the control does not need any setting . Other versions of VS I don't know whether it needs to be set , This routine uses VS2019 So I found List Control The self drawing property of the control does not need to be set .
Wrapper class H The header file ListCtrlEx.h
#pragma once
class CListCtrlEx : public CListCtrl
{
DECLARE_DYNAMIC(CListCtrlEx)
public:
int m_hoverIndex; // Index of current hot items ( Hot spots are when the mouse stops on a line )
bool m_mouseTrack; // Whether to track mouse movement events
bool m_if_hotLine; // Whether to change the color of the hot line
COLORREF m_oddItemBkColor; // Odd row background color
COLORREF m_oddItemTextColor; // Odd line text color
COLORREF m_evenItemBkColor; // Even row background color
COLORREF m_evenItemTextColor; // Even lines of text color
COLORREF m_hoverItemBkColor; // Hot line background color
COLORREF m_hoverItemTextColor; // Hot line text color
COLORREF m_selectItemBkColor; // Select the line background color
COLORREF m_selectItemTextColor; // Select the line text color
public:
void SetOddItemBkColor(COLORREF color);
void SetOddItemTextColor(COLORREF color);
void SetEvenItemBkColor(COLORREF color);
void SetEvenItemTextColor(COLORREF color);
void SetHoverItemBkColor(COLORREF color);
void SetHoverItemTextColor(COLORREF color);
void SetSelectItemBkColor(COLORREF color);
void SetSelectItemTextColor(COLORREF color);
public:
CListCtrlEx();
virtual ~CListCtrlEx();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnMouseLeave();
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnPaint();
afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
afx_msg void OnLvnColumnclick(NMHDR *pNMHDR, LRESULT *pResult);
struct GetItemColumnText
{
CString nColumn_UserName;
CString nColumn_UserPwd;
CString nColumn_UserType;
}m_UserDataInformation;
//virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
};
Wrapper class CPP file ListCtrlEx.CPP
#include"pch.h"
#include "ListCtrlEx.h"
#pragma warning(disable:26454)
IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)
/**
* Constructors
*/
CListCtrlEx::CListCtrlEx()
{
m_hoverIndex = -1; // Index of current hot items
m_mouseTrack = true; // Track mouse movement events
m_if_hotLine = true; // By default, the hotspot line is not enabled to change color
m_oddItemBkColor = RGB(240, 252, 255); // Odd row background color ( Default white )
m_evenItemBkColor = RGB(255, 251, 240); // Even row background color ( Default white )
m_hoverItemBkColor = RGB(238, 222, 176); // Hot line background color ( Default white )
m_selectItemBkColor = RGB(192, 235, 215); // Select the line background color ( Default blue )
m_oddItemTextColor = RGB(16, 18, 23); // Odd line text color ( Default black )
m_evenItemTextColor = RGB(16, 18, 23); // Even lines of text color ( Default black )
m_hoverItemTextColor = RGB(29,209,165); // Hot line text color ( Default black )
m_selectItemTextColor = RGB(190,0,47); // Select the line text color ( Default black )
}
/**
* Destructor
*/
CListCtrlEx::~CListCtrlEx()
{
}
BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, &CListCtrlEx::OnNMCustomdraw)
ON_WM_MOUSEMOVE()
ON_WM_MOUSELEAVE()
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, &CListCtrlEx::OnLvnColumnclick)
END_MESSAGE_MAP()
/**
* Sort callback function
*/
static int CALLBACK SortFunction(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CString &lp1 = *((CString *)lParam1);
CString &lp2 = *((CString *)lParam2);
int &sort = *(int *)lParamSort;
if (sort == 0)
return lp1.CompareNoCase(lp2);
else
return lp2.CompareNoCase(lp1);
}
/**
* When the mouse moves over the control area
*/
void CListCtrlEx::OnMouseMove(UINT nFlags, CPoint point)
{
if(m_if_hotLine == true) {
// Get which line the mouse is currently on
int newIndex; // One line of the current mouse
int oldIndex; // Record the original line ( That is, the line before leaving )
newIndex = HitTest(point);
if(newIndex != m_hoverIndex) {
CRect rc;
oldIndex = m_hoverIndex;
m_hoverIndex = newIndex;
// Refresh the area after leaving
if(oldIndex != -1) {
GetItemRect(oldIndex, &rc, LVIR_BOUNDS);
InvalidateRect(&rc);
}
// Refresh to a new area
if(m_hoverIndex != -1) {
GetItemRect(m_hoverIndex, &rc, LVIR_BOUNDS);
InvalidateRect(&rc);
}
}
// The mouse should track WM_MOUSELEAVE event
if (m_mouseTrack) {
TRACKMOUSEEVENT csTME;
csTME.cbSize = sizeof(csTME);
csTME.dwFlags = TME_LEAVE; // Specify the events to track
csTME.hwndTrack = m_hWnd; // Specify the window to track
::_TrackMouseEvent(&csTME); // Turn on Windows Of WM_MOUSELEAVE Event support
m_mouseTrack = false; // If it has been tracked , Then stop tracking
}
}
CListCtrl::OnMouseMove(nFlags, point);
}
/**
* When the mouse leaves the control area
*/
void CListCtrlEx::OnMouseLeave()
{
if(m_if_hotLine == true) {
// Start tracking
m_mouseTrack = true;
if(m_hoverIndex != -1) {
CRect rc;
GetItemRect(m_hoverIndex,&rc,LVIR_BOUNDS);
InvalidateRect(&rc);
m_hoverIndex = -1;
m_mouseTrack = true;
}
}
CListCtrl::OnMouseLeave();
}
/**
* Erase background
*/
BOOL CListCtrlEx::OnEraseBkgnd(CDC* pDC)
{
return FALSE;
}
/**
* painting
*/
void CListCtrlEx::OnPaint()
{
CPaintDC dc(this);
CRect rect;
CRect headerRect;
CDC memDC;
CBitmap bitmap;
GetClientRect(&rect);
GetDlgItem(0)->GetWindowRect(&headerRect);
memDC.CreateCompatibleDC(&dc);
bitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
memDC.SelectObject(&bitmap);
memDC.FillSolidRect(&rect, RGB(240, 255, 255));
// Call the default OnPaint(), Draw graphics in memory DC On the table
DefWindowProc(WM_PAINT, (WPARAM)memDC.m_hDC, (LPARAM)0);
// Output to display device
dc.BitBlt(0, headerRect.Height(), rect.Width(), rect.Height(), &memDC, 0, headerRect.Height(), SRCCOPY);
memDC.DeleteDC();
bitmap.DeleteObject();
}
/**
* owner-draw
*/
void CListCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLVCUSTOMDRAW pNMCD = reinterpret_cast<LPNMLVCUSTOMDRAW>(pNMHDR);
int itemIndex = pNMCD->nmcd.dwItemSpec;
if (pNMCD->nmcd.dwDrawStage == CDDS_PREPAINT) {
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if (pNMCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
{
// Select row ( Row selected by mouse )
if(GetItemState(itemIndex,LVIS_SELECTED) == LVIS_SELECTED)
{
pNMCD->nmcd.uItemState = ~CDIS_SELECTED;
pNMCD->clrTextBk = m_selectItemBkColor;
pNMCD->clrText = pNMCD->clrFace = m_selectItemTextColor;
}
// CheckBox Tick line
else if(GetCheck(itemIndex) && (GetExtendedStyle() & LVS_EX_CHECKBOXES)) {
pNMCD->clrTextBk = m_selectItemBkColor;
pNMCD->clrText = m_selectItemTextColor;
}
// Hot line ( Mouse over )
else if(itemIndex==m_hoverIndex) {
pNMCD->clrTextBk = m_hoverItemBkColor;
pNMCD->clrText = m_hoverItemTextColor;
}
// Even number line ( such as 0、2、4、6)
else if(itemIndex % 2==0){
pNMCD->clrTextBk=m_evenItemBkColor;
pNMCD->clrText=m_evenItemTextColor;
}
// Odd line ( such as 1、3、5、7)
else{
pNMCD->clrTextBk = m_oddItemBkColor;
pNMCD->clrText = m_oddItemTextColor;
}
*pResult = CDRF_NEWFONT;
}
}
/**
* Click on the header ( Here we mainly sort the data in the list )
*/
void CListCtrlEx::OnLvnColumnclick(NMHDR *pNMHDR, LRESULT *pResult)
{
int count = 0;
static int sort = 0;
static int subItem = 0;
CArray<CString,CString> itemData;
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
// Get the total number of rows
count = GetItemCount();
itemData.SetSize(count);
// Set the data to be sorted
for (int i = 0; i < count; i++) {
itemData[i] = GetItemText(i, pNMLV->iSubItem);
SetItemData(i, (DWORD_PTR)&itemData[i]); // Set the data to be sorted
}
// If you click in the header of another column
if (subItem != pNMLV->iSubItem) {
sort = 0;
subItem = pNMLV->iSubItem;
}
// If you click in the header of the same column ( In ascending order , Then change to descending order ; In descending order , Then change to ascending order )
else {
sort = (sort == 0 ? 1 : 0);
}
SortItems(SortFunction,(DWORD_PTR)&sort); // Call callback function , Sort
*pResult = 0;
}
/**
* Set the background color of odd rows
*
* @param oddItemBkColor Odd row background color
*/
void CListCtrlEx::SetOddItemBkColor(COLORREF oddItemBkColor)
{
m_oddItemBkColor = oddItemBkColor;
}
/**
* Set the color of odd lines of text
*
* @param oddItemBkColor Odd line text color
*/
void CListCtrlEx::SetOddItemTextColor(COLORREF oddItemTextColor)
{
m_oddItemTextColor = oddItemTextColor;
}
/**
* Set even line background color
*
* @param evenItemBkColor Even row background color
*/
void CListCtrlEx::SetEvenItemBkColor(COLORREF evenItemBkColor)
{
m_evenItemBkColor = evenItemBkColor;
}
/**
* Set the color of even lines of text
*
* @param evenItemTextColor Even text color
*/
void CListCtrlEx::SetEvenItemTextColor(COLORREF evenItemTextColor)
{
m_evenItemTextColor = evenItemTextColor;
}
/**
* Set the background color of the hot line
*
* @param hoverItemBkColor Hot line background color
*/
void CListCtrlEx::SetHoverItemBkColor(COLORREF hoverItemBkColor)
{
m_hoverItemBkColor = hoverItemBkColor;
m_if_hotLine = true;
}
/**
* Set the hot line text color
*
* @param hoverItemTextColor Hot line text color
*/
void CListCtrlEx::SetHoverItemTextColor(COLORREF hoverItemTextColor)
{
m_hoverItemTextColor = hoverItemTextColor;
m_if_hotLine = true;
}
/**
* Set the background color of the selected row
*
* @param selectItemBkColor Select the line background color
*/
void CListCtrlEx::SetSelectItemBkColor(COLORREF selectItemBkColor)
{
m_selectItemBkColor = selectItemBkColor;
}
/**
* Set the text color of the selected line
*
* @param selectItemTextColor Select the line text color
*/
void CListCtrlEx::SetSelectItemTextColor(COLORREF selectItemTextColor)
{
m_selectItemTextColor = selectItemTextColor;
}
Read Access Database routines




边栏推荐
- 啥是图神经网络?
- openresty ngx_ Lua shared memory
- MyCat2启动报[MYCAT-3036][ERR_INIT_CONFIG] start FileMetadataStorageManager fail
- JVM性能调优方式
- Zhang Chaoyang runs 33km at night: live chat on physics reveals the cause of "super moon"
- 梅科尔工作室-DjangoWeb 应用框架+MySQL数据库第四次培训
- Realization of communication between ROS and stm32
- JMeter 21 day clock in Day10
- 对象内存布局和synchronized锁升级
- 软件设计师:11-基础知识例题
猜你喜欢

Clouds want clothes, flowers want looks, spring breeze blows the sill, Revlon (romantic code implementation)

如何下载ScienceDirect(Elsevier)文献的补充材料

JMeter opens Day11 in 21 days

Uniapp authorized login to obtain user information and code

STM32中断梅开二度(一)

Deep learning environment configuration tensorflow2+keras

OS知识点简介(二)

Entropy technology passed the registration: the annual revenue was 1.955 billion, and the book balance of accounts receivable was 290million

flex 布局 justify-content:space-between 最后一行左对齐的解决方案

软件设计师:12-案例分析例题
随机推荐
MATLAB学习第四天(决策语句)
QA机器人召回优化
QA机器人第二节——召回
Cause analysis of the red light of the server and soft switch status on the Vos client
STM32中断梅开二度(一)
MySQL - default value constraint for table fields
JMeter 21 天打卡 day10
为什么很多人都知道打工不挣钱却还在打工?
Which securities company is the account given by Yixue school? Is it safe to open an account
如何由 moment 对象 获取 时间对象
Openresty Lua resty lrucache cache
圆环形材质mask
使用深度學習制作機器人大腦圖紙
All types of code after await are directly thrown into the micro task queue and executed later
03. Dichotomy, complexity, dynamic array, hash table and ordered table
torch dist分布式数据汇总
中信证券网上开户安全吗?开户的流程是什么?
[vscode output is garbled] solution
软件测试周刊(第80期):当你想倾诉的话语已经涌到了舌尖,但是把那些话憋回去的瞬间,从那个瞬间起,你就成为了大人。
分布式笔记(03)— 分布式缓存之 Redis(单机模式、Redis Cluster 模式概述)