里面涉及到很多知识
包括3D样式的去除,重绘ListView控件,以及处理控件的边框颜色
// Test_listview_1.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "Test_listview_1.h"
#include
#include
#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依然有效。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章