LPTSTRはプロジェクトの設定がワイド文字かマルチバイト文字かで型が変わりますね。
デフォルトはワイド文字でwchar_t型になったと思います。
紛らわしいぞ!LPCTSTR、LPTSTR、LPSTR、LPCSTRは全部意味が違う!
猫でもわかるのコードはVC++ 5.0位のかなり古いコンパイラが対象で修正しながらやるのは結構大変そうなので、文字セットをマルチ バイト文字セットを仕様する
にしてコマンドラインに/Zc:strictStrings-
で、
文字リテラルをconstにしないといけないのを回避してしまうのもありではないかと思います。
cpp
1// listvw01.cpp
2#pragma comment(lib, "comctl32.lib")
3
4#define STRICT
5#define ID_LISTVIEW 100
6#include <windows.h>
7#include <commctrl.h>
8#include <tchar.h>
9
10LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
11BOOL InitApp(HINSTANCE);
12BOOL InitInstance(HINSTANCE, int);
13
14TCHAR szClassName[] = _T("listvw01"); //ウィンドウクラス
15HINSTANCE hInst;
16
17int WINAPI _tWinMain(HINSTANCE hCurInst, HINSTANCE hPrevInst,
18 LPTSTR lpsCmdLine, int nCmdShow)
19{
20 MSG msg;
21
22 if (!InitApp(hCurInst))
23 return FALSE;
24 if (!InitInstance(hCurInst, nCmdShow))
25 return FALSE;
26 while (GetMessage(&msg, NULL, 0, 0)) {
27 TranslateMessage(&msg);
28 DispatchMessage(&msg);
29 }
30 return msg.wParam;
31}
32
33//ウィンドウ・クラスの登録
34
35BOOL InitApp(HINSTANCE hInst)
36{
37 WNDCLASSEX wc;
38 wc.cbSize = sizeof(WNDCLASSEX);
39 wc.style = CS_HREDRAW | CS_VREDRAW;
40 wc.lpfnWndProc = WndProc; //プロシージャ名
41 wc.cbClsExtra = 0;
42 wc.cbWndExtra = 0;
43 wc.hInstance = hInst; //インスタンス
44 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
45 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
46 wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
47 wc.lpszMenuName = NULL; //メニュー名
48 wc.lpszClassName = (LPCTSTR)szClassName;
49 wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
50 return (RegisterClassEx(&wc));
51}
52
53//ウィンドウの生成
54
55BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
56{
57 HWND hWnd;
58 hInst = hInstance;
59 hWnd = CreateWindow(szClassName,
60 TEXT("猫でもわかるリストビュー"),//タイトルバーにこの名前が表示されます
61 WS_OVERLAPPEDWINDOW, //ウィンドウの種類
62 CW_USEDEFAULT, //X座標
63 CW_USEDEFAULT, //Y座標
64 CW_USEDEFAULT, //幅
65 CW_USEDEFAULT, //高さ
66 NULL,//親ウィンドウのハンドル、親を作るときはNULL
67 NULL,//メニューハンドル、クラスメニューを使うときはNULL
68 hInst,//インスタンスハンドル
69 NULL);
70 if (!hWnd)
71 return FALSE;
72 ShowWindow(hWnd, nCmdShow);
73 UpdateWindow(hWnd);
74 return TRUE;
75}
76
77//ウィンドウプロシージャ
78
79LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
80{
81 int id;
82 static HWND hList;
83 LV_COLUMN lvcol;
84 LV_ITEM item;
85
86 switch (msg) {
87 case WM_CREATE:
88 InitCommonControls();
89 hList = CreateWindowEx(0,
90 WC_LISTVIEW, const_cast<LPTSTR>(_T("")),
91 WS_CHILD | WS_VISIBLE | LVS_REPORT,
92 0, 0, 0, 0,
93 hWnd,
94 (HMENU)ID_LISTVIEW,
95 hInst,
96 NULL);
97
98 lvcol.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
99 lvcol.fmt = LVCFMT_LEFT;
100 lvcol.cx = 100;
101 lvcol.pszText = const_cast<LPTSTR>(_T("名前"));
102 lvcol.iSubItem = 0;
103 ListView_InsertColumn(hList, 0, &lvcol);
104
105 lvcol.cx = 200;
106 lvcol.pszText = const_cast<LPTSTR>(_T("住所"));
107 lvcol.iSubItem = 1;
108 ListView_InsertColumn(hList, 1, &lvcol);
109
110 item.mask = LVIF_TEXT;
111 item.pszText = const_cast<LPTSTR>(_T("粂井康孝"));
112 item.iItem = 0;
113 item.iSubItem = 0;
114 ListView_InsertItem(hList, &item);
115
116 item.pszText = const_cast<LPTSTR>(_T("北海道旭川市"));
117 item.iItem = 0;
118 item.iSubItem = 1;
119 ListView_SetItem(hList, &item);
120
121 item.pszText = const_cast<LPTSTR>(_T("粂井ひとみ"));
122 item.iItem = 1;
123 item.iSubItem = 0;
124 ListView_InsertItem(hList, &item);
125
126 item.pszText = const_cast<LPTSTR>(_T("東京都千代田区"));
127 item.iItem = 1;
128 item.iSubItem = 1;
129 ListView_SetItem(hList, &item);
130
131 item.pszText = const_cast<LPTSTR>(_T("粂井志麻"));
132 item.iItem = 2;
133 item.iSubItem = 0;
134 ListView_InsertItem(hList, &item);
135
136 item.pszText = const_cast<LPTSTR>(_T("北海道中富良野町"));
137 item.iItem = 2;
138 item.iSubItem = 1;
139 ListView_SetItem(hList, &item);
140
141 break;
142 case WM_SIZE:
143 MoveWindow(hList, 0, 0, LOWORD(lp), HIWORD(lp), TRUE);
144 break;
145 case WM_CLOSE:
146 id = MessageBox(hWnd,
147 _T("終了してもよいですか"),
148 _T("終了確認"),
149 MB_YESNO | MB_ICONQUESTION);
150 if (id == IDYES) {
151 DestroyWindow(hWnd);
152 }
153 break;
154 case WM_DESTROY:
155 PostQuitMessage(0);
156 break;
157 default:
158 return (DefWindowProc(hWnd, msg, wp, lp));
159 }
160 return 0L;
161}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/07 10:37