コード:
#include <windows.h>
#include <commctrl.h>
#include "resource.h"
#include <shlwapi.h>
#include <windowsx.h>
#pragma comment(lib, "comctl32.lib") // ツリービューの作成に必要
#pragma comment(lib, "shlwapi.lib")
HWND hList; //ファイル列挙リストハンドル。不可視。
char *lpDirectoryName = "Projects";
// このコード モジュールに含まれる関数の宣言を転送します:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
char szModulePath[MAX_PATH];
char szAddBuf[MAX_PATH];
HTREEITEM InsertTreeItem(HWND hwndTreeView, HTREEITEM hitemParent, LPTSTR lpszName, BOOL bChild);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
// アプリケーションの初期化を実行します:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// メイン メッセージ ループ:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
//
// 関数: MyRegisterClass()
//
// 目的: ウィンドウ クラスを登録します。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL , IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_HP);
wcex.lpszClassName = TEXT("HP");
wcex.hIconSm = LoadIcon(NULL , IDI_APPLICATION);
return RegisterClassEx(&wcex);
}
//
// 関数: InitInstance(HINSTANCE, int)
//
// 目的: メイン ウィンドウを作成します。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hWnd = CreateWindow(TEXT("HP"), TEXT("HP"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 関数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: メイン ウィンドウのメッセージを処理します。
//
// WM_CREATE - ウインドウ作成時の処理
// WM_SIZE - ウインドウサイズ変更時の処理
// WM_COMMAND - アプリケーション メニューの処理
// WM_DESTROY - 中止メッセージを表示して戻る
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
static HWND hTree; // ツリービューのハンドル
HTREEITEM hTop, hChild1, hChild2;
TV_INSERTSTRUCT tv;
char szListBuf[MAX_PATH];
int n,c,nList;
switch (message)
{
case WM_CREATE:
CreateDirectory(lpDirectoryName, NULL);
GetModuleFileNameA(NULL,
szModulePath,
sizeof(szModulePath)/sizeof(szModulePath[0]));
for ( c=0; c<2; c++ ){
PathRemoveFileSpec(szModulePath);
}
PathAppend(szModulePath,lpDirectoryName);
SetCurrentDirectory(szModulePath);
MessageBox(NULL,szModulePath,"テスト",MB_OK);
hList = CreateWindow(
TEXT("LISTBOX") , NULL ,
WS_CHILD | LBS_STANDARD /*| WS_VISIBLE*/ ,
0 , 0 , 500 , 500 , hWnd , (HMENU)1 ,
((LPCREATESTRUCT)(lParam))->hInstance , NULL
);
SendMessage(
hList , LB_DIR ,
DDL_READWRITE | DDL_DIRECTORY , (LPARAM)TEXT("*.*")
);
// コモンコントロールの初期化
InitCommonControls();
hTree = CreateWindowEx(0, WC_TREEVIEW, TEXT(""),
WS_CHILD | WS_VISIBLE
| TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT | TVS_INFOTIP,
0, 0, 0, 0,
hWnd, (HMENU)ID_TREE1,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
memset(LPTSTR(&tv), '\0', sizeof(tv));
/*InsertTreeItem(hTree, TVI_ROOT, TEXT("Projects"), TRUE);*/
tv.hInsertAfter = TVI_LAST;
tv.item.mask = TVIF_TEXT;
tv.hParent = TVI_ROOT;
tv.item.pszText = TEXT("Projects");
hTop = TreeView_InsertItem(hTree, &tv);
nList = ListBox_GetCount(hList);
for (n = 0 ;n < nList; n++ ){
ListBox_GetText(hList,n,szAddBuf);
tv.hParent = hTop;
tv.item.pszText = szAddBuf;
TreeView_InsertItem(hTree, &tv);
}
//tv.hInsertAfter = TVI_LAST;
//tv.item.mask = TVIF_TEXT;
//tv.hParent = TVI_ROOT;
//tv.item.pszText = TEXT("Projects");
//hTop = TreeView_InsertItem(hTree, &tv);
//tv.item.pszText = TEXT("親2");
//hTop = TreeView_InsertItem(hTree, &tv);
//tv.item.pszText = TEXT("親3");
//hTop = TreeView_InsertItem(hTree, &tv);
//tv.hParent = hTop;
//tv.item.pszText = TEXT("子1");
//hChild1 = TreeView_InsertItem(hTree, &tv);
//tv.item.pszText = TEXT("子2");
//hChild2 = TreeView_InsertItem(hTree, &tv);
//tv.hParent = hChild1;
//tv.item.pszText = TEXT("孫1");
//TreeView_InsertItem(hTree, &tv);
//tv.hParent = hChild1;
//tv.item.pszText = TEXT("孫2");
//TreeView_InsertItem(hTree, &tv);
//tv.hParent = hTop;
//tv.item.pszText = TEXT("子3");
//TreeView_InsertItem(hTree, &tv);
//tv.hParent = hTop;
//tv.item.pszText = TEXT("子4");
//TreeView_InsertItem(hTree, &tv);
break;
case WM_SIZE:
MoveWindow(hTree, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 選択されたメニューの解析:
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_NOTIFY:
if (((LPNMHDR)lParam)->code == TVN_ITEMEXPANDING) {
LPTVITEM lpItem = &((LPNMTREEVIEW)lParam)->itemNew;
if (!(lpItem->state & TVIS_EXPANDEDONCE)) {
//InsertTreeItem(hTree, lpItem->hItem, TEXT("B"), FALSE);
//InsertTreeItem(hTree, lpItem->hItem, TEXT("C"), FALSE);
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
HTREEITEM InsertTreeItem(HWND hwndTreeView, HTREEITEM hitemParent, LPTSTR lpszName, BOOL bChild)
{
TVINSERTSTRUCT is;
is.hParent = hitemParent;
is.item.mask = TVIF_TEXT;
is.item.pszText = lpszName;
if (bChild) {
is.item.mask |= TVIF_CHILDREN;
is.item.cChildren = 1;
}
return TreeView_InsertItem(hwndTreeView, &is);
}