[Win API] Owner-Draw ListBox - Adding Wrong File

Hello,

i've did this little application (gonna finish it but I need to fix this problem) by one tutorial, however, after adding file to listbox it will add "C:\windows\syswow64\shell32.dll" to my list instead of selected file, here's sourcecode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <Windows.h>
#include "resource.h"

#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

HINSTANCE g_hInstance;

BOOL CALLBACK MainDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

void AddFileToList(HWND hWnd), RemoveFileFromList(HWND hWnd), DrawItem(LPDRAWITEMSTRUCT lpDIS);

#define MY_WM_INITDIALOG (WM_USER + 1)

INT WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR lpCmdLine, INT nShowCmd)
{
	UNREFERENCED_PARAMETER(hPrevInst);
	UNREFERENCED_PARAMETER(lpCmdLine);

	g_hInstance = hInst;

	WNDCLASSEX wClass;
	ZeroMemory(&wClass,sizeof(WNDCLASSEX));
	wClass.cbClsExtra=0;
	wClass.cbSize=sizeof(WNDCLASSEX);
	wClass.cbWndExtra=DLGWINDOWEXTRA;
	wClass.hbrBackground=(HBRUSH)(COLOR_BTNFACE + 1);
	wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wClass.hIcon=NULL;
	wClass.hIconSm=NULL;
	wClass.hInstance=hInst;
	wClass.lpfnWndProc=(WNDPROC)MainDlgProc;
	wClass.lpszClassName="Browser Class";
	wClass.lpszMenuName=NULL;
	wClass.style=CS_HREDRAW | CS_VREDRAW;

	if(!RegisterClassEx(&wClass))
	{
		int nResult = GetLastError();
		MessageBox(NULL, "Window Class Creation Failed!", NULL, MB_ICONERROR | MB_OK);
		return 0;
	}

	HWND hWnd = CreateDialog(hInst, MAKEINTRESOURCE(IDD_MAIN1), NULL, NULL);

	if(!hWnd)
	{
		int nResult = GetLastError();
		MessageBox(NULL, "Dialog Creation Failed!", NULL, MB_ICONERROR | MB_OK);
		return 0;
	}

	MSG msg;
	ZeroMemory(&msg,sizeof(MSG));

	while(GetMessage(&msg, NULL, 0, 0) == TRUE)
	{
		if(!IsDialogMessage(hWnd, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	return msg.wParam;
}

BOOL CALLBACK MainDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	LPMEASUREITEMSTRUCT lpMIS;
	switch(msg)
	{
	case MY_WM_INITDIALOG:
		{
			
		}
		break;
	case WM_CREATE:
		{
			PostMessage(hWnd, MY_WM_INITDIALOG, 0, 0);
		}
		break;
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		}
		break;
	case WM_DRAWITEM:
		{
			DrawItem((LPDRAWITEMSTRUCT)lParam);
			break;
		}
	case WM_MEASUREITEM:
		{
			lpMIS = (LPMEASUREITEMSTRUCT)lParam;
			lpMIS->itemHeight = GetSystemMetrics(SM_CYICON)+4;
			break;
		}
	case WM_COMMAND:
		{
			switch(LOWORD(wParam))
			{
			case IDC_ADD1:
				{
					AddFileToList(hWnd);
					break;
				}
			case IDC_REMOVE1:
				{
					RemoveFileFromList(hWnd);
					break;
				}
			}
			break;
		}
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}

void AddFileToList(HWND hWnd)
{
	TCHAR szFile[102400];
	lstrcpy(szFile, "");
	OPENFILENAME ofn;
	ZeroMemory(&ofn, sizeof(OPENFILENAME));
	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hwndOwner = hWnd;
	ofn.lpstrFile = szFile;
	ofn.nMaxFile = sizeof(szFile);
	ofn.lpstrFilter = "All Files\0*.*\0";
	ofn.lpstrFileTitle = NULL;
	ofn.nMaxFileTitle = 0;
	ofn.lpstrInitialDir = NULL;
	ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
	if(!GetOpenFileName(&ofn))
		return;
	LRESULT lrFile = SendDlgItemMessage(hWnd, IDC_LIST1, LB_ADDSTRING, 0, (LPARAM)szFile);
	WORD index = 0;
	HICON hIcon = ExtractAssociatedIcon(g_hInstance, (LPTSTR)szFile, &index);
	SendDlgItemMessage(hWnd, IDC_LIST1, LB_SETITEMDATA, lrFile, (LPARAM)hIcon);
}

void RemoveFileFromList(HWND hWnd)
{
	int SelectedItem = SendDlgItemMessage(hWnd, IDC_LIST1, LB_GETCURSEL, 0, 0);
	SendDlgItemMessage(hWnd, IDC_LIST1, LB_DELETESTRING, SelectedItem, 0);
}

void DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
	TCHAR szFile[102400];
	HBRUSH hbBckgrnd;
	SendMessage(lpDIS->hwndItem, LB_GETTEXT, lpDIS->itemID, (LPARAM)szFile);
	WORD index = 0;
	HICON hIcon = ExtractAssociatedIcon(g_hInstance, szFile, &index);
	if(lpDIS->itemState & ODS_SELECTED)
	{
		hbBckgrnd = GetSysColorBrush(COLOR_HIGHLIGHT);
		SetTextColor(lpDIS->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
	}
	else
	{
		hbBckgrnd = GetSysColorBrush(COLOR_WINDOW);
		SetTextColor(lpDIS->hDC, GetSysColor(COLOR_WINDOWTEXT));
	}

	SetBkMode(lpDIS->hDC, TRANSPARENT);
	FillRect(lpDIS->hDC, &lpDIS->rcItem, hbBckgrnd);

	DrawIcon(lpDIS->hDC, lpDIS->rcItem.left+1, lpDIS->rcItem.top + 2, hIcon);
	RECT rect;
	CopyMemory(&rect, &lpDIS->rcItem, sizeof(RECT));
	rect.left += 34;
	DrawText(lpDIS->hDC, szFile, -1, &rect, DT_WORDBREAK | DT_VCENTER);
}
Last edited on
Topic archived. No new replies allowed.