-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUser32__WC_LISTVIEW.py
159 lines (139 loc) · 6.24 KB
/
User32__WC_LISTVIEW.py
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
import win32gui
import win32con
import win32api
import win32gui_struct
import commctrl
import math
from ctypes import windll, wintypes, Structure, POINTER, create_string_buffer, cast
# data class generated by AI
# Define the GetDeviceCaps function signature
GetDeviceCaps = windll.gdi32.GetDeviceCaps
GetDeviceCaps.argtypes = [wintypes.HDC, wintypes.INT]
GetDeviceCaps.restype = wintypes.INT
class LVCOLUMN(Structure):
_fields_ = [
("mask", wintypes.UINT),
("fmt", wintypes.INT),
("cx", wintypes.INT),
("pszText", wintypes.LPCWSTR),
("cchTextMax", wintypes.INT),
("iSubItem", wintypes.INT),
("iImage", wintypes.INT),
("iOrder", wintypes.INT),
("cxMin", wintypes.INT),
("cxDefault", wintypes.INT),
("iGroupId", wintypes.INT)
]
class LVITEM(Structure):
_fields_ = [
("mask", wintypes.UINT),
("iItem", wintypes.INT),
("iSubItem", wintypes.INT),
("state", wintypes.UINT),
("stateMask", wintypes.UINT),
("pszText", wintypes.LPCWSTR),
("cchTextMax", wintypes.INT),
("iImage", wintypes.INT),
("lParam", wintypes.LPARAM),
("iIndent", wintypes.INT),
("iGroupId", wintypes.INT),
("cColumns", wintypes.UINT), # Added for LVIF_COLUMNS
("puColumns", POINTER(wintypes.UINT)), # Added for LVIF_COLUMNS
("piColFmt", POINTER(wintypes.INT)), # Added for LVIF_COLUMNS
("iGroup", wintypes.INT)
]
LVM_GETNEXTITEM = (commctrl.LVM_FIRST + 12)
lv_hwnd = int() # Store the listview handle
def WndProc(hwnd, msg, wParam, lParam):
global lv_hwnd
if msg == win32con.WM_COMMAND:
pass
elif msg == win32con.WM_NOTIFY:
# https://github.com/eavatar/eavatar-me/blob/master/src/avashell/win32/console.py#L168
info = win32gui_struct.UnpackNMITEMACTIVATE(lParam)
if info.code == commctrl.NM_CLICK:
# Get selected item index from the listview
idx = win32gui.SendMessage(lv_hwnd, LVM_GETNEXTITEM, -1, commctrl.LVNI_SELECTED)
print(f"index of selected item: {idx}")
elif msg == win32con.WM_DESTROY:
win32gui.PostQuitMessage(0)
return win32gui.DefWindowProc(hwnd, msg, wParam, lParam)
def main():
dpiX = 0
dpiY = 0
hdc = win32gui.GetDC(0) # 0 represents the entire screen
if hdc:
dpiX = GetDeviceCaps(hdc, win32con.LOGPIXELSX)
dpiY = GetDeviceCaps(hdc, win32con.LOGPIXELSY)
win32gui.ReleaseDC(0, hdc)
width = int(math.ceil(640.0 * dpiX / 96.0))
height = int(math.ceil(480.0 * dpiY / 96.0))
wc = win32gui.WNDCLASS()
wc.lpszClassName = "MyWindowClass"
wc.lpfnWndProc = WndProc
class_atom = win32gui.RegisterClass(wc)
hwnd = win32gui.CreateWindow(class_atom, "My Window", win32con.WS_OVERLAPPEDWINDOW,
win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, width, height, 0, 0, 0, None)
# Example listview creation (adjust xpos, ypos, nwidth, nheight, hwndParent)
xpos, ypos = 10, 10 # Example position
nwidth, nheight = 430, 200 # Example size
hwndParent = hwnd # Set parent to the main window
# Create a listbox control
global lv_hwnd
lv_hwnd = win32gui.CreateWindow(commctrl.WC_LISTVIEW, "",
win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_BORDER | commctrl.LVS_ALIGNLEFT
| commctrl.LVS_REPORT | commctrl.LVS_SHOWSELALWAYS , # Style
xpos, ypos, nwidth, nheight, hwndParent, 101, 0, None)
# Set default font for List View
lf = win32gui.LOGFONT()
lf.lfFaceName = "Calibri"
lf.lfHeight = 16
designFont = win32gui.CreateFontIndirect(lf)
win32gui.SendMessage(lv_hwnd, win32con.WM_SETFONT, designFont, True)
# Set extended styles for List View
# https://stackoverflow.com/questions/3458914/does-lvs-ex-fullrowselect-have-any-compatibility-issues-with-other-styles
ex_lv_style = win32gui.SendMessage(lv_hwnd, commctrl.LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0)
win32gui.SendMessage(lv_hwnd, commctrl.LVM_SETEXTENDEDLISTVIEWSTYLE,
ex_lv_style,
commctrl.LVS_EX_FULLROWSELECT | commctrl.LVS_EX_GRIDLINES)
# Insert Column to List View
szString = ["Column 1", "Column 2", "Column 3"]
c_cols = 3
lvc = LVCOLUMN()
lvc.mask = commctrl.LVCF_FMT | commctrl.LVCF_WIDTH | commctrl.LVCF_TEXT | commctrl.LVCF_SUBITEM
for iCol in range(c_cols):
lvc.iSubItem = iCol
lvc.cx = int(nwidth/c_cols) # Width of column in pixels.
lvc.fmt = commctrl.LVCFMT_LEFT # Left-aligned column.
buf = create_string_buffer(szString[iCol].encode('utf-8'))
lvc.pszText = cast(buf, wintypes.LPCWSTR)
win32gui.SendMessage(lv_hwnd, commctrl.LVM_INSERTCOLUMN, iCol, lvc)
# Inserts items into a list view
c_rows = 8
lvI = LVITEM()
# Initialize LVITEM members that are common to all items.
buf = create_string_buffer("item".encode('utf-8'))
lvI.pszText = cast(buf, wintypes.LPCWSTR)
lvI.mask = commctrl.LVIF_TEXT | commctrl.LVIF_IMAGE | commctrl.LVIF_STATE
lvI.stateMask = 0
lvI.state = 0
# Initialize LVITEM members that are different for each item.
for i in range(c_rows):
# https://stackoverflow.com/questions/29990028/add-a-new-row-into-listview
# First column
lvI.iItem = win32gui.SendMessage(lv_hwnd, commctrl.LVM_GETITEMCOUNT, 0, 0)
lvI.iSubItem = 0
index = win32gui.SendMessage(lv_hwnd, commctrl.LVM_INSERTITEM, 0, lvI)
# Remaining columns
for j in range(c_cols):
if index != -1:
lvI.iItem = index
lvI.iImage = j
# Insert items into the list.
lvI.iSubItem = j
win32gui.SendMessage(lv_hwnd, commctrl.LVM_SETITEM, 0, lvI)
win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL)
win32gui.UpdateWindow(hwnd)
win32gui.PumpMessages()
if __name__ == "__main__":
main()