home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1999 February
/
VPR9902A.BIN
/
Vpr_data
/
Program
/
ce
/
LVtips
/
Main.c
< prev
next >
Wrap
C/C++ Source or Header
|
1998-11-25
|
16KB
|
568 lines
//
// 「LVtips」 Main.c
// Author WindowsCE Users Group
//
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <commdlg.h>
#include <shlobj.h>
#include "resource.h"
#define IDC_LIST 20001
#define IDC_DROP 20002
#define NUM_PVIEW_PROCFIELDS 6
TCHAR szAppName[] = TEXT("ListView Tips サンプル");
#define szTitle szAppName
HINSTANCE ghInstance;
HWND hwMain;
HWND hwCmdBar;
HWND hwndLV;
HWND hwCBMenu;
int iView = 0; // 表示方法: 0 = 詳細, 1 = リスト,
// 2 = 小さなアイコン, 3 = 大きなアイコン
int Viewmode[] = {IDM_DETAILS, IDM_LIST, IDM_SMALLICONS, IDM_LARGEICONS};
LRESULT CALLBACK AboutProc(HWND ,UINT, WPARAM , LPARAM);
#define CUSTCOLORS 16
DWORD dwCustColors[CUSTCOLORS];
DWORD dwColor = 0;
int SortColumn = 0;
int SortMode = 0;
int ColumnOrder = 0;
BOOL bExCHECKBOXES = 0;
BOOL bExFULLROWSELECT = 0;
BOOL bExGRIDLINES = 0;
BOOL bExHEADERDRAGDROP = 0;
BOOL bExSUBITEMIMAGES = 0;
//----------------------------------------------------------------------------
// カスタムカラーテーブルをイニシャル
BOOL InitColor()
//----------------------------------------------------------------------------
{
memset(dwCustColors, 255, sizeof(dwCustColors));
return TRUE;
}
//----------------------------------------------------------------------------
BOOL ColorProc(
HWND hwnd)
//----------------------------------------------------------------------------
{
CHOOSECOLOR cc;
memset(&cc, 0, sizeof(CHOOSECOLOR));
cc.lStructSize = sizeof(CHOOSECOLOR); // 構造体のサイズ
cc.hwndOwner = hwnd; // ダイアログボックスを所有するウィンドウハンドル
cc.rgbResult = dwColor; // 最初に選択される色
cc.lpCustColors = dwCustColors; // カスタム カラーの配列を指すポインタ
cc.Flags = CC_RGBINIT | CC_FULLOPEN; // 初期化フラグ
if(ChooseColor(&cc))
{ // [OK]を選んだ場合
dwColor = cc.rgbResult;
return TRUE;
}
return FALSE;
}
//----------------------------------------------------------------------------
LRESULT WINAPI Main_OnCommand(
HWND hwnd,
int id,
HWND hwndCtl,
UINT codeNotify)
//----------------------------------------------------------------------------
{
DWORD dwExStyle;
switch (id)
{
case IDM_DETAILS: // 「詳細」
case IDM_LIST: // 「リスト」
case IDM_SMALLICONS: // 「小さなアイコン」
case IDM_LARGEICONS: // 「大きなアイコン」
{
int i;
DWORD dwStyle;
DWORD Styles[] = {LVS_REPORT, LVS_LIST, LVS_SMALLICON, LVS_ICON};
iView = id - IDM_DETAILS;
CheckMenuRadioItem(hwCBMenu, Viewmode[0], Viewmode[3], Viewmode[iView], MF_BYCOMMAND);
for(i = 0; i < 4; i++)
SendMessage(hwCmdBar, TB_PRESSBUTTON, Viewmode[i], (iView == i)? TRUE:FALSE);
dwStyle = GetWindowLong(hwndLV, GWL_STYLE);
for(i = 0; i < 4; i++)
dwStyle &= ~(Styles[i]);
dwStyle = Styles[iView];
SetWindowLong(hwndLV, GWL_STYLE, dwStyle);
UpdateWindow(hwndLV);
}
return TRUE;
case IDM_BGCOLOR:
if(ColorProc(hwnd) == TRUE)
{
ListView_SetBkColor(hwndLV, dwColor);
ListView_SetTextBkColor(hwndLV, dwColor);
ListView_SetTextColor(hwndLV, (dwColor)? RGB(0, 0, 0):RGB(255, 255, 255));
InvalidateRect(hwndLV, NULL, TRUE);
UpdateWindow(hwndLV);
}
break;
case IDM_CHECKBOXES:
bExCHECKBOXES = (bExCHECKBOXES)? 0:1;
CheckMenuItem(GetSubMenu(hwCBMenu,1), IDM_CHECKBOXES,
MF_BYCOMMAND | (bExCHECKBOXES)? MF_CHECKED:MF_UNCHECKED);
dwExStyle = ListView_GetExtendedListViewStyle(hwndLV);
if(bExCHECKBOXES)
dwExStyle |= LVS_EX_CHECKBOXES;
else
dwExStyle &= ~LVS_EX_CHECKBOXES;
ListView_SetExtendedListViewStyle(hwndLV, dwExStyle);
UpdateWindow(hwndLV);
break;
case IDM_FULLROWSELECT:
bExFULLROWSELECT = (bExFULLROWSELECT)? 0:1;
CheckMenuItem(GetSubMenu(hwCBMenu,1), IDM_FULLROWSELECT,
MF_BYCOMMAND | (bExFULLROWSELECT)? MF_CHECKED:MF_UNCHECKED);
dwExStyle = ListView_GetExtendedListViewStyle(hwndLV);
if(bExFULLROWSELECT)
dwExStyle |= LVS_EX_FULLROWSELECT;
else
dwExStyle &= ~LVS_EX_FULLROWSELECT;
ListView_SetExtendedListViewStyle(hwndLV, dwExStyle);
UpdateWindow(hwndLV);
break;
case IDM_GRIDLINES:
bExGRIDLINES = (bExGRIDLINES)? 0:1;
CheckMenuItem(GetSubMenu(hwCBMenu,1), IDM_GRIDLINES,
MF_BYCOMMAND | (bExGRIDLINES)? MF_CHECKED:MF_UNCHECKED);
dwExStyle = ListView_GetExtendedListViewStyle(hwndLV);
if(bExGRIDLINES)
dwExStyle |= LVS_EX_GRIDLINES;
else
dwExStyle &= ~LVS_EX_GRIDLINES;
ListView_SetExtendedListViewStyle(hwndLV, dwExStyle);
UpdateWindow(hwndLV);
break;
case IDM_HEADERDRAGDROP:
bExHEADERDRAGDROP = (bExHEADERDRAGDROP)? 0:1;
CheckMenuItem(GetSubMenu(hwCBMenu,1), IDM_HEADERDRAGDROP,
MF_BYCOMMAND | (bExHEADERDRAGDROP)? MF_CHECKED:MF_UNCHECKED);
dwExStyle = ListView_GetExtendedListViewStyle(hwndLV);
if(bExHEADERDRAGDROP)
dwExStyle |= LVS_EX_HEADERDRAGDROP;
else
dwExStyle &= ~LVS_EX_HEADERDRAGDROP;
ListView_SetExtendedListViewStyle(hwndLV, dwExStyle);
UpdateWindow(hwndLV);
break;
case IDM_SUBITEMIMAGES:
bExSUBITEMIMAGES = (bExSUBITEMIMAGES)? 0:1;
CheckMenuItem(GetSubMenu(hwCBMenu,1), IDM_SUBITEMIMAGES,
MF_BYCOMMAND | (bExSUBITEMIMAGES)? MF_CHECKED:MF_UNCHECKED);
dwExStyle = ListView_GetExtendedListViewStyle(hwndLV);
if(bExSUBITEMIMAGES)
dwExStyle |= LVS_EX_SUBITEMIMAGES;
else
dwExStyle &= ~LVS_EX_SUBITEMIMAGES;
ListView_SetExtendedListViewStyle(hwndLV, dwExStyle);
{
int item = -1;
// 砂時計表示
SetCursor((HCURSOR) IDC_WAIT);
// 再描画を禁止にする
SendMessage(hwndLV, WM_SETREDRAW, FALSE, 0);
while((item = ListView_GetNextItem(hwndLV, item, LVNI_ALL)) != -1)
ListView_Update(hwndLV, item);
// 再描画を許可する
SendMessage(hwndLV, WM_SETREDRAW, TRUE, 0);
// 砂時計消去
SetCursor(NULL);
}
break;
case IDM_SetColumnOrderArray:
{
int iArray[2][NUM_PVIEW_PROCFIELDS] = {{0, 1, 2, 3, 4, 5}, {0, 2, 4, 1, 3, 5}};
ColumnOrder = (ColumnOrder + 1) & 0x1;
ListView_SetColumnOrderArray(hwndLV, NUM_PVIEW_PROCFIELDS, iArray[ColumnOrder]);
}
break;
case IDM_ADDITEM:
{
int iCol, iLine;
LVITEM lvins;
TCHAR szWork[80];
// 砂時計表示
SetCursor((HCURSOR) IDC_WAIT);
// 再描画を禁止にする
SendMessage(hwndLV, WM_SETREDRAW, FALSE, 0);
// 追加するアイテム数が分かっているので
ListView_SetItemCount(hwndLV, 10); // 10個分確保
// アイテムを10件分追加する
for (iLine = 0; iLine < 10; iLine++)
{
wsprintf(szWork, TEXT("データ %02d%02d"), iLine, 0);
memset(&lvins, 0, sizeof(LVITEM));
lvins.mask = LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE;
lvins.lParam = iLine;
lvins.iItem = iLine;
lvins.pszText = szWork;
lvins.cchTextMax = sizeof(szWork);
lvins.iImage = 3;
ListView_InsertItem(hwndLV, &lvins);
lvins.mask = LVIF_TEXT | LVIF_IMAGE;
for (iCol = 1; iCol < NUM_PVIEW_PROCFIELDS; iCol++)
{
wsprintf(szWork, TEXT("%04d"), rand() % 10000);
lvins.iSubItem = iCol;
ListView_SetItem(hwndLV, &lvins);
}
}
// 再描画を許可する
SendMessage(hwndLV, WM_SETREDRAW, TRUE, 0);
// 砂時計消去
SetCursor(NULL);
}
break;
case IDM_FILECLOSE: //「閉じる」を選択
SendMessage(hwnd, WM_CLOSE, 0, 0L);
break;
case IDM_ABOUT: //「バージョン情報」を選択
DialogBox(ghInstance, MAKEINTRESOURCE(IDD_ABOUT1), hwnd, (DLGPROC)AboutProc);
break;
default:
return FALSE;
}
return TRUE;
}
//----------------------------------------------------------------------------
BOOL WINAPI Main_OnCreate(
HWND hwnd,
LPCREATESTRUCT lpCreateStruct)
//----------------------------------------------------------------------------
{
// 小さいアイコンをタスクバーに登録
SendMessage(hwnd, WM_SETICON, FALSE,
(LPARAM) LoadImage(ghInstance, MAKEINTRESOURCE(IDI_ICON1),
IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR));
// カラーテーブルのイニシャル
InitColor();
return TRUE;
}
//----------------------------------------------------------------------------
void WINAPI Main_OnDestroy(
HWND hwnd)
//----------------------------------------------------------------------------
{
PostQuitMessage(0);
}
//----------------------------------------------------------------------------
// Function: ListView Sorting
int CALLBACK ListViewCompareFunc(
LPARAM lparam1, //
LPARAM lparam2, //
LPARAM lparamSort) //
//----------------------------------------------------------------------------
{
LV_FINDINFO fi;
INT nItem1, nItem2;
int iResult;
TCHAR szData1[40], szData2[40];
DWORD Mode, Column;
Mode = (DWORD)(lparamSort & 0xffff);
Column = (DWORD)(lparamSort >> 16);
fi.flags = LVFI_PARAM;
fi.lParam = lparam1;
nItem1 = ListView_FindItem(hwndLV, -1, &fi);
ListView_GetItemText(hwndLV, nItem1, Column, szData1, sizeof(szData1)/sizeof(TCHAR));
fi.lParam = lparam2;
nItem2 = ListView_FindItem(hwndLV, -1, &fi);
ListView_GetItemText(hwndLV, nItem2, Column, szData2, sizeof(szData2)/sizeof(TCHAR));
iResult = 0;
switch(Mode)
{
case 1: // 昇順
if(Column)
iResult = wcslen(szData1) - wcslen(szData2);
if(! iResult)
iResult = wcsicmp(szData1, szData2);
break;
case 2: // 降順
if(Column)
iResult = wcslen(szData2) - wcslen(szData1);
if(! iResult)
iResult = wcsicmp(szData2, szData1);
break;
default: // 何もしない
iResult = lparam1 - lparam2;
}
return iResult;
}
//----------------------------------------------------------------------------
LRESULT Main_OnNotify(
HWND hwnd,
int idCtl,
LPNMHDR pnmh)
//----------------------------------------------------------------------------
{
NM_LISTVIEW *pnmlv = (NM_LISTVIEW *) pnmh;
LVCOLUMN lvC;
switch (pnmh->code)
{
case TBN_DROPDOWN:
{
int i;
POINT pt;
RECT rc;
struct MENUITEM {
UINT uIDNewItem;
LPCTSTR lpNewItem;
} static MenuItem[] = {
{IDM_LARGEICONS, TEXT("大きいアイコン")},
{IDM_SMALLICONS, TEXT("小さいアイコン")},
{IDM_LIST, TEXT("リスト")},
{IDM_DETAILS, TEXT("詳細")},
{0, NULL}
};
HMENU hMenu = CreatePopupMenu();
// Popupメニュー項目作成
for(i = 0; MenuItem[i].lpNewItem; i++)
AppendMenu(hMenu, MF_STRING, MenuItem[i].uIDNewItem, MenuItem[i].lpNewItem);
// Radioマーク付加
CheckMenuRadioItem(hMenu, 0, 3, 3-iView, MF_BYPOSITION);
// Popupする場所を計算
GetClientRect(hwCmdBar, (LPRECT) &rc);
pt.x = rc.left;
pt.y = rc.bottom;
ClientToScreen(hwCmdBar, &pt);
TrackPopupMenu(hMenu, TPM_TOPALIGN | TPM_LEFTALIGN, pt.x, pt.y, 0, hwnd, NULL);
DestroyMenu(hMenu);
return TRUE;
}
}
switch (pnmh->idFrom)
{
case IDC_LIST:
switch (pnmh->code)
{
case LVN_COLUMNCLICK:
if(pnmlv->iSubItem != SortColumn)
{
lvC.mask = LVCF_IMAGE;
lvC.iImage = 0;
ListView_SetColumn(hwndLV, SortColumn, &lvC);
SortMode = 1;
SortColumn = pnmlv->iSubItem;
}
else
{
SortMode++;
SortMode %= 3;
}
lvC.mask = LVCF_IMAGE;
lvC.iImage = SortMode;
ListView_SetColumn(hwndLV, SortColumn, &lvC);
// Sort
ListView_SortItems(hwndLV, ListViewCompareFunc,
(LPARAM) MAKELONG(SortMode, SortColumn));
return TRUE;
}
}
return FALSE;
}
//----------------------------------------------------------------------------
LRESULT CALLBACK WndProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
//----------------------------------------------------------------------------
{
switch(uMsg)
{
HANDLE_MSG(hwnd, WM_CREATE, Main_OnCreate );
HANDLE_MSG(hwnd, WM_COMMAND, Main_OnCommand);
HANDLE_MSG(hwnd, WM_NOTIFY, Main_OnNotify );
HANDLE_MSG(hwnd, WM_DESTROY, Main_OnDestroy);
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return FALSE;
}
//----------------------------------------------------------------------------
BOOL InitApplication(
HINSTANCE hInstance)
//----------------------------------------------------------------------------
{
WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));
wc.style = CS_HREDRAW | CS_VREDRAW ;
wc.lpszClassName = szAppName;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
return RegisterClass(&wc);
}
//----------------------------------------------------------------------------
BOOL InitInstance(
HINSTANCE hInstance,
int CmdShow)
//----------------------------------------------------------------------------
{
RECT rc;
int iCol, i;
HIMAGELIST himLage, himSmall;
LVCOLUMN lvColumn;
TCHAR szWork[80];
static TBBUTTON tbORGButton[] = {
{0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0, 0, 0},
{VIEW_DETAILS, IDC_DROP, TBSTATE_ENABLED, TBSTYLE_DROPDOWN, 0, 0, 0L, -1},
};
WORD iIcons[] = {IDI_ICON2, IDI_ICON3, IDI_ICON4, IDI_ICON1, 0};
ghInstance = hInstance;
InitCommonControls();
// create MainWindow
if((hwMain = CreateWindow(szAppName, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, ghInstance, NULL)) == NULL)
return FALSE;
// create Commandbar(menu)
hwCmdBar = CommandBar_Create(ghInstance, hwMain, 1);
CommandBar_InsertMenubar(hwCmdBar, ghInstance, IDR_MENU1, 0);
hwCBMenu = CommandBar_GetMenu(hwCmdBar, 0);
CheckMenuRadioItem(hwCBMenu, Viewmode[0], Viewmode[3], Viewmode[iView], MF_BYCOMMAND);
CommandBar_AddBitmap(hwCmdBar, HINST_COMMCTRL, IDB_VIEW_SMALL_COLOR, 0, 0, 0);
CommandBar_AddButtons(hwCmdBar, sizeof(tbORGButton)/sizeof(TBBUTTON), tbORGButton);
CommandBar_AddAdornments(hwCmdBar, 0, 0);
GetClientRect(hwMain, &rc);
rc.top = CommandBar_Height(hwCmdBar);
rc.bottom -= rc.top;
rc.left = 0;
rc.right -= rc.left;
// List View Window
if((hwndLV = CreateWindow(WC_LISTVIEW, TEXT(""), WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP |
LVS_REPORT,
rc.left, rc.top, rc.right, rc.bottom,
hwMain, (HMENU) IDC_LIST, hInstance, NULL)) == NULL)
return FALSE;
// 大きいアイコンのイメージリスト作成
himLage = ImageList_Create(32, 32, ILC_COLOR | ILC_MASK, 0, 0);
for(i = 0; iIcons[i]; i++)
ImageList_AddIcon(himLage, LoadIcon(ghInstance, MAKEINTRESOURCE(iIcons[i])));
ListView_SetImageList(hwndLV, himLage, LVSIL_NORMAL);
// 小さいアイコンのイメージリスト作成
himSmall = ImageList_Create(16, 16, ILC_COLOR | ILC_MASK, 0, 0);
for(i = 0; iIcons[i]; i++)
ImageList_AddIcon(himSmall, LoadImage(ghInstance, MAKEINTRESOURCE(iIcons[i]),
IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR));
ListView_SetImageList(hwndLV, himSmall, LVSIL_SMALL);
memset(&lvColumn, 0, sizeof(lvColumn));
lvColumn.mask = (LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH | LVCF_IMAGE);
lvColumn.fmt = (LVCFMT_LEFT | LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE);
GetClientRect(hwndLV, &rc);
lvColumn.cx = (rc.right - rc.left - GetSystemMetrics(SM_CXVSCROLL)) / NUM_PVIEW_PROCFIELDS;
for (iCol = 0; iCol < NUM_PVIEW_PROCFIELDS; iCol++)
{
wsprintf(szWork, TEXT("カラム %d"), iCol);
lvColumn.pszText = szWork;
lvColumn.iSubItem = iCol;
lvColumn.iImage = 0;
ListView_InsertColumn(hwndLV, iCol, &lvColumn);
}
ShowWindow(hwMain, CmdShow);
UpdateWindow(hwMain);
return TRUE;
}
//----------------------------------------------------------------------------
int WINAPI WinMain(
HINSTANCE hInstance, //
HINSTANCE hPrevInstance, //
LPTSTR lpCmdLine, //
int nCmdShow) //
//----------------------------------------------------------------------------
{
MSG msg;
HANDLE hAccel;
if(! hPrevInstance)
if(! InitApplication(hInstance))
return FALSE;
if(! InitInstance(hInstance, nCmdShow))
return FALSE;
hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCELERATOR1));
while(GetMessage(&msg, NULL, 0, 0))
{ // Message Loop
if (! TranslateAccelerator(hwMain, hAccel, &msg))
TranslateMessage(&msg); // Translates virtual key codes
DispatchMessage(&msg); // Dispatches message to window
}
return msg.wParam;
}