获取程序实例:hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;

lParam是操作系统调用你的窗口过程传给你的参数 他根据处理消息的不同含义也会不同

当在处理WM_CREATE消息时 lParam里面保存的是一指针,指向LPCREATESTRUCT结构的地址。

((LPCREATESTRUCT) lParam)->hInstance 就是通过保存在lParam里面的地址取出LPCREATESTRUCT结构里面的hInstance 即程序的实例句柄 

以下是一个简单的treeview实现,支持当前选中行字体变成蓝色的效果: ```cpp #include <windows.h> #include <commctrl.h> #pragma comment(lib, "comctl32.lib") LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); HWND hTreeView; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT("TreeViewDemo"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppName, TEXT("TreeView Demo"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static HTREEITEM hSelectedItem = NULL; switch (message) { case WM_CREATE: { hTreeView = CreateWindowEx(0, WC_TREEVIEW, NULL, WS_CHILD | WS_VISIBLE | TVS_HASBUTTONS | TVS_LINESATROOT | TVS_HASLINES, 0, 0, 0, 0, hwnd, (HMENU)1, ((LPCREATESTRUCT)lParam)->hInstance, NULL); TVINSERTSTRUCT tvInsert; tvInsert.hParent = TVI_ROOT; tvInsert.hInsertAfter = TVI_LAST; tvInsert.item.mask = TVIF_TEXT; tvInsert.item.pszText = TEXT("Item 1"); TreeView_InsertItem(hTreeView, &tvInsert); tvInsert.item.pszText = TEXT("Item 2"); TreeView_InsertItem(hTreeView, &tvInsert); tvInsert.item.pszText = TEXT("Item 3"); TreeView_InsertItem(hTreeView, &tvInsert); } return 0; case WM_NOTIFY: if (((LPNMHDR)lParam)->code == TVN_SELCHANGED) { if (hSelectedItem) { TVITEM tvItem; tvItem.hItem = hSelectedItem; tvItem.mask = TVIF_STATE; tvItem.stateMask = TVIS_SELECTED; tvItem.state = 0; TreeView_SetItem(hTreeView, &tvItem); } hSelectedItem = ((LPNMTREEVIEW)lParam)->itemNew.hItem; TVITEM tvItem; tvItem.hItem = hSelectedItem; tvItem.mask = TVIF_STATE; tvItem.stateMask = TVIS_SELECTED; tvItem.state = TVIS_SELECTED; TreeView_SetItem(hTreeView, &tvItem); return 0; } break; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); } ``` 以上代码实现了一个简单的树形控件,当用户选中某一行时,选中行的字体颜色会变成蓝色。具体实现是,在`WM_NOTIFY`消息中捕获`TVN_SELCHANGED`消息,然后根据选中的行设置字体颜色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值