里面涉及到很多知识

包括3D样式的去除,重绘ListView控件,以及处理控件的边框颜色

// Test_listview_1.cpp : Defines the entry point for the application.
// #include "stdafx.h"
#include "Test_listview_1.h"
#include <commctrl.h>
#include <uxtheme.h> #pragma comment (lib,"Comctl32.lib")
#pragma comment (lib,"UxTheme.lib")
//#pragma comment(linker,"\"/manifestdependency:type='win32' \
//name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
//processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#define MAX_LOADSTRING 100 // Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
HWND hwndList1;
int middle = , middleH = , width = , height = ;
BOOL S_1 = FALSE;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ListProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, UINT_PTR, DWORD_PTR);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine); // TODO: Place code here. // Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_TESTLISTVIEW1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance); // Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
} HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTLISTVIEW1)); MSG msg; // Main message loop:
while (GetMessage(&msg, nullptr, , ))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} return (int) msg.wParam;
} //
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = ;
wcex.cbWndExtra = ;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TESTLISTVIEW1));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_TESTLISTVIEW1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassExW(&wcex);
} //
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, , CW_USEDEFAULT, , nullptr, nullptr, hInstance, nullptr); if (!hWnd)
{
return FALSE;
} ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd); return TRUE;
} //
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
INITCOMMONCONTROLSEX icex; // Structure for control initialization.
icex.dwICC = ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&icex); hwndList1 = CreateWindow(WC_LISTVIEW, L"", WS_VISIBLE | WS_CHILD | LVS_REPORT | WS_BORDER | WS_VSCROLL | LVS_OWNERDRAWFIXED , , , width, height, hWnd, NULL, GetModuleHandle(NULL), );
DWORD Style = GetWindowLong(hwndList1, GWL_STYLE);
SetWindowLong(hwndList1, GWL_STYLE, Style &~WS_BORDER);
SetWindowSubclass(hwndList1, &ListProc, , NULL);
// HWND hHeader = ListView_GetHeader(hwndList1);
// SetWindowTheme(hHeader, L"", L"");
LVCOLUMN column;
column.mask = LVCF_WIDTH | LVCF_TEXT;
column.cx = ;
column.pszText = (LPWSTR)L"MASTER";
ListView_InsertColumn(hwndList1, , &column); //column for sub item 0 LVITEM lvi = {};
lvi.iItem = ListView_GetItemCount(hwndList1);
lvi.mask = LVIF_TEXT | LVIF_STATE;
lvi.pszText = (LPWSTR)L"MASTER1";
lvi.iSubItem = ;
ListView_InsertItem(hwndList1, &lvi);
lvi.pszText = (LPWSTR)L"MASTER2";
ListView_InsertItem(hwndList1, &lvi);
lvi.pszText = (LPWSTR)L"MASTER3";
ListView_InsertItem(hwndList1, &lvi);
lvi.pszText = (LPWSTR)L"MASTER4";
ListView_InsertItem(hwndList1, &lvi);
lvi.pszText = (LPWSTR)L"MASTER5";
ListView_InsertItem(hwndList1, &lvi);
lvi.pszText = (LPWSTR)L"MASTER6";
ListView_InsertItem(hwndList1, &lvi);
lvi.pszText = (LPWSTR)L"MASTER7";
ListView_InsertItem(hwndList1, &lvi);
}
break;
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_SIZE:
if (wParam == SIZE_RESTORED) {
SetWindowPos(hwndList1, , , , , , );
}
else if (wParam == SIZE_MAXIMIZED) {
SetWindowPos(hwndList1, , middle - ( + width), middleH - ( + height), width, height, );
RECT rc;
GetClientRect(hwndList1, &rc);
ListView_SetColumnWidth(hwndList1, , rc.right - rc.left);//rc.left is zero
}
break;
case WM_DRAWITEM:
{
LPDRAWITEMSTRUCT pDIS = (LPDRAWITEMSTRUCT)lParam;
HDC hDC = pDIS->hDC;
RECT rc = pDIS->rcItem;
HFONT hF;
HBRUSH bg = (HBRUSH)(::GetStockObject(DC_BRUSH));
HPEN pn = (HPEN)(::GetStockObject(NULL_PEN));
::SelectObject(hDC, bg);
::SelectObject(hDC, pn);
::SetTextColor(hDC, RGB(, , ));
int points = ;
// if (resX <= 800)
// {
hF = CreateFont(, , , , FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Tahoma");
// }
// else
// {
// points = 10;
// int fontheight = -MulDiv(points, GetDeviceCaps(hDC, LOGPIXELSY), 72);
// hF = CreateFont(fontheight, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Tahoma");
// } HFONT hOldFont = (HFONT)SelectObject(hDC, hF);
if ((pDIS->itemID % ) != )
::SetDCBrushColor(hDC, RGB(, , ));
else {
::SetDCBrushColor(hDC, RGB(, , )); }
::Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom); wchar_t buffer[] = { };
ListView_GetItemText(pDIS->hwndItem, pDIS->itemID, , (LPWSTR)buffer, ); rc.left = ; ::DrawText(hDC, (LPWSTR)buffer, -, &rc, DT_SINGLELINE | DT_VCENTER);
SelectObject(hDC, hOldFont);
DeleteObject(hF); }
break;
case WM_MEASUREITEM:
{
MEASUREITEMSTRUCT * m = (MEASUREITEMSTRUCT*)lParam;
m->itemHeight = ;
}
break; //case WM_NOTIFY:
// if (((LPNMHDR)lParam)->code == NM_CUSTOMDRAW)
// {
// LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
// switch (lplvcd->nmcd.dwDrawStage)
// {
// case CDDS_PREPAINT:
// return CDRF_NOTIFYITEMDRAW; // case CDDS_ITEMPREPAINT:
// if (((int)lplvcd->nmcd.dwItemSpec % 2) == 0) {
// lplvcd->clrText = RGB(0, 0, 0);
// // lplvcd->clrTextBk = RGB(255, 255, 255);
// lplvcd->clrTextBk = RGB(193, 228, 255);
// }
// else {
// lplvcd->clrText = RGB(0, 0, 0);
// lplvcd->clrTextBk = RGB(202, 233, 255);
// } // return CDRF_NEWFONT;
// }
// return TRUE;
// }
//
case WM_PAINT:
{
PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps);
RECT rc,clrc;
GetWindowRect(hwndList1, &rc);
ScreenToClient(hWnd, (LPPOINT)&rc.left);
ScreenToClient(hWnd, (LPPOINT)&rc.right);
auto hpen = CreatePen(PS_SOLID, , RGB(, , ));
auto oldpen = SelectObject(hdc, hpen);
SelectObject(hdc, GetStockObject(NULL_BRUSH));
if (S_1)
{
Rectangle(hdc, rc.left - , rc.top - , rc.right + , rc.bottom + );
}
else
{
Rectangle(hdc, , , , );
}
SelectObject(hdc, oldpen);
DeleteObject(oldpen);
ReleaseDC(hwndList1, hdc);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break; case WM_DESTROY:
PostQuitMessage();
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return ;
} // Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE; case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
} LRESULT CALLBACK ListProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, UINT_PTR, DWORD_PTR) {
switch (msg)
{
case WM_NOTIFY:
if (((LPNMHDR)lp)->code == NM_CUSTOMDRAW)
{
LPNMCUSTOMDRAW lpcd = (LPNMCUSTOMDRAW)lp;
switch (lpcd->dwDrawStage)
{
case CDDS_PREPAINT: return CDRF_NOTIFYITEMDRAW;
case CDDS_ITEMPREPAINT:
{ SetBkColor(lpcd->hdc, RGB(, , ));
// SetBkColor(lpcd->hdc, RGB(255, 0, 0));
SetTextColor(lpcd->hdc, RGB(, , ));
return CDRF_NEWFONT;
} }
} break; case WM_NCPAINT:
{
RECT rc;
GetWindowRect(hwnd, &rc);
OffsetRect(&rc, -rc.left, -rc.top);
auto hdc = GetWindowDC(hwnd);
auto hpen = CreatePen(PS_SOLID, , RGB(, , ));
auto oldpen = SelectObject(hdc, hpen);
SelectObject(hdc, GetStockObject(NULL_BRUSH));
// Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);
SelectObject(hdc, oldpen);
DeleteObject(oldpen);
ReleaseDC(hwnd, hdc);
S_1 = TRUE;
// return 0;
break;
} case WM_NCDESTROY:
RemoveWindowSubclass(hwnd, ListProc, );
break;
} return DefSubclassProc(hwnd, msg, wp, lp);
} //InvalidateRect(hwnd, &rc, TRUE);

里面有很多注释的代码,我没删除是因为它们是有意义的

当创建ListView控件的时候,添加自定义的样式LVS_OWNERDRAWFIXED之后, 主窗口的WM_DRAWITEM起作用,WM_NOTIFY失效; 控件的自定义的窗口处理里的WM_NOTIFY依然有效。

ListView的创建的更多相关文章

  1. Android LIstView初次创建getview方法执行多次问题

    写listview优化的时候,发现Listview初次创建的时候会多次执行getView方法. <?xml version="1.0" encoding="utf- ...

  2. Android开发之自定义的ListView(UITableViewController)

    Android开发中的ListView, 顾名方法思义,就是表视图.表示图在iOS开发中就是TableView.两者虽然名称不一样,但是其使用方法,使用场景以及该控件的功能都极为相似,都是用来展示大量 ...

  3. Android 之 ListView的学习

    ListView 是一个控件,一个在垂直滚动的列表中显示条目的一个控件,这些条目的内容来自于一个ListAdapter .EditText Button TextView ImageView Chec ...

  4. Android之listview && adapter

    今天我们讲的也是非常重要的一个控件listview-最常用也是最难的 一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView ...

  5. 【转】ListView学习笔记(二)——ViewHolder

    在android开发中Listview是一个很重要的组件,它以列表的形式根据数据的长自适应展示具体内容,用户可以自由的定义listview每一列的布局,但当listview有大量的数据需要加载的时候, ...

  6. 【转】Android之自定义Adapter的ListView

    http://www.cnblogs.com/topcoderliu/archive/2011/05/07/2039862.html 在开发中,我们经常使用到ListView这个控件.Android的 ...

  7. Android listview与adapter用法

    listview与adapter用法 博客分类: android   一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView ...

  8. ListView 和 Adapter用法

    一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView就是实现这个功能的.第二点也不难做到,在后面的学习中读者会发现,这非常 ...

  9. Android中 ListView 详解(二)

    本文版权归 csdn noTice501 所有,转载请详细标明原作者及出处,以示尊重! 作者:noTice501 原文:http://blog.csdn.net/notice520/article/d ...

随机推荐

  1. [python]自动计算1-100的平方和

    def power(x,n): s=1 while n > 0: n = n-1 s = s * x return sm=0for i in range(1,101) : n=power(i,2 ...

  2. 阶段3 2.Spring_06.Spring的新注解_1 spring的新注解-Configuration和ComponentScan

    解决测试类重复代码的问题,xml还是存在的问题,没法脱离xml文件 要想在QueryRunner上加注解,是加不了的 创建工程 复制依赖项到pom.xml 复制注解的工程里面的com文件夹 配置文件b ...

  3. Maven 安装 / 常用配置 / 阿里maven中央仓库

    Maven 官方下载地址: http://maven.apache.org/download.cgi 可以选择清华的镜像: 解压在settings.xml里面配置阿里中央仓库: <mirror& ...

  4. Linux从一个服务器拷贝文件到另一个服务器上

    ***复制文件夹到另外一个服务器scp -r tmp root@114.215.80.12:/work/temp输入密码 scp -r customer root@114.215.80.12:/hom ...

  5. WEB应用服务器都有哪些?

    大型WEB服务器在UNIX和Linux平台下使用最广泛的免费HTTP服务器是W3C.NCSA和APACHE服务器,而Windows平台NT/2000/2003使用IIS的WEB服务器.在选择使用WEB ...

  6. beego 注解路由

    场景描述:使用注解路由,不起作用. 额外描述: 路由的添加都写在 main函数中了,同时未设置 beego.BConfig.RunMode ="dev"也未引入 :routers包 ...

  7. cocos2dx[3.2](11) 新事件分发机制

    在2.x中处理事件需要用到委托代理(delegate),相信学过2.x的触摸事件的同学,都知道创建和移除的流程十分繁琐. 而在3.x中由于加入了C++11的特性,而对事件的分发机制通过事件分发器Eve ...

  8. python学习之数据类型(set)

    3.9 集合(set) 3.9.1 介绍 ​ 集合是一个无序且不重复的元素集合.元素必须是可哈希的(int,str,tuple,bool).可以把它看作是dic的key的集合.用{}表示. ​ 注意: ...

  9. LeetCode.925-长按的名字(Long Pressed Name)

    这是悦乐书的第355次更新,第380篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第217题(顺位题号是925).你的朋友正在键盘上输入他的名字. 有时,在键入字符c时, ...

  10. 暴力破解-DVWA_1.9-笔记

     穷举法  通过社工尽可能的缩小密码字典的范围   所用到的工具: Firefox浏览器及其插件Proxy Switcher, OWASP ZAP代理抓包工具. OWASP ZAP  代理抓包工具 h ...