March 29, 2024, 07:44:46 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


CreateWindowW types

Started by Andy, August 23, 2018, 05:44:28 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

August 23, 2018, 05:44:28 AM Last Edit: August 23, 2018, 06:18:45 AM by Andy
Sorry, another one for you all.

I know I can use:

CreateWindowW

To create a static, button, edit control, but can I use it to create:

a combo box. and a listview?

If so, how do I populate them with data?

ADDSTRING surely won't work?

Sorry was just wondering.

Thanks,
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

fasecero

You will have to use winapi for this. I can make an example of a combo. The listview is rather more complicated, do you need the icon view with text and image or the detail view with several columns?

Andy

Fasecero,

Thanks for looking at this, I would just need the list view with several columns.

Any example you can provide would be great.

Thanks,
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

fasecero

August 23, 2018, 11:53:52 AM #3 Last Edit: August 23, 2018, 12:05:19 PM by fasecero
Ok, here we go. I'm going to put everything together, maybe it's a bit too much at first but I think it's better that way. First an example of a combo


$INCLUDE "windowssdk.inc"
$INCLUDE "Commctrl.inc"

CONST COMBO_1 = 1
INT subclassID = 12345
INT hwnd_combo = 0
WINDOW w1
OPENWINDOW w1,0,0,600,400,@MINBOX|@MAXBOX|@SIZE,NULL,"Simple Window",&w1_handler

' main loop
WAITUNTIL w1 = 0
END

' window procedure
SUB w1_handler
SELECT @MESSAGE
CASE @IDCREATE
CENTERWINDOW w1
OnInit()

CASE @IDCLOSEWINDOW
CLOSEWINDOW w1
OnFinish()
ENDSELECT
ENDSUB

SUB subclassProc(hWnd:INT,uMsg:INT,wParam:INT,lParam:INT,uIdSubclass:UINT_PTR,dwRefData:DWORD_PTR),INT
SELECT uMsg
CASE WM_COMMAND
SELECT LOWORD(wParam)
CASE COMBO_1
SELECT HIWORD(wParam)
CASE CBN_SELCHANGE ' the user has selected another index
OnIndexChange(lParam)
ENDSELECT
ENDSELECT

DEFAULT
RETURN DefSubclassProc(hWnd, uMsg, wParam, lParam)
ENDSELECT

RETURN 0
ENDSUB

SUB OnIndexChange(int handle) ' aux func
INT index = SendMessageW(handle, CB_GETCURSEL, 0, 0)
SETCAPTION w1, "Selected position: " + STR$(index + 1)
ENDSUB

SUB OnInit()
' subclass the window
SetWindowSubclass(w1.hwnd, &subclassProc, subclassID, 0)

' init common controls
_InitCommonControls()

' create the combo
INT dwStyles = CBS_DROPDOWNLIST | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE
hwnd_combo = CreateCombo(w1.hwnd, 10, 10, 200, 100, COMBO_1, dwStyles)

' add some items
ComboAddString(hwnd_combo, L"First")
ComboAddString(hwnd_combo, L"Second")
ComboAddString(hwnd_combo, L"Third")

' select the second item
ComboSelectItem(hwnd_combo, 1) ' zero based index

' the first time you have to manually update the position yourself
OnIndexChange(hwnd_combo)
ENDSUB

SUB OnFinish()
RemoveWindowSubclass(w1.hwnd, &subclassProc, subclassID)
ENDSUB

' ------------------------------------------------------------------------------------------------------------
' UNICODE FUNCTIONS
' ------------------------------------------------------------------------------------------------------------

SUB _InitCommonControls()
' Ensures that the common control DLL (Comctl32.dll) is loaded, and registers specific common control classes from the DLL.
' An application must call this function before creating a common control.
INITCOMMONCONTROLSEX icc
icc.dwSize=LEN(icc)
icc.dwICC= ICC_BAR_CLASSES | ICC_LINK_CLASS | ICC_LISTVIEW_CLASSES | ICC_PROGRESS_CLASS | ICC_TAB_CLASSES | ICC_TREEVIEW_CLASSES | ICC_UPDOWN_CLASS
InitCommonControlsEx (&icc)
ENDSUB

SUB CreateCombo(INT parent, int x, int y, int dimx, int dimy, int ctrl, int style), INT
RETURN CreateWindowExW(0, WC_COMBOBOXW, L"", _
        style, _
        x, y, dimx, dimy, _
        parent, ctrl, GetModuleHandle(0), 0)
ENDSUB

SUB ComboAddString(INT handle, pointer text)
SendMessageW(handle, CB_ADDSTRING, 0, text)
ENDSUB

SUB ComboSelectItem(INT handle, int index)
SendMessageW(handle, CB_SETCURSEL, index, 0)
ENDSUB



Combobox creation styles
https://docs.microsoft.com/en-us/windows/desktop/controls/combo-box-styles

All actions you can do with SendMessageW
https://docs.microsoft.com/en-us/windows/desktop/controls/bumper-combobox-control-reference-messages

Combobox notification list
https://docs.microsoft.com/en-us/windows/desktop/controls/bumper-combobox-control-reference-notifications

fasecero

August 23, 2018, 12:00:10 PM #4 Last Edit: August 23, 2018, 12:03:13 PM by fasecero
And here's a minimal unicode listview, if there is anything you don't understand just ask.


$INCLUDE "windowssdk.inc"
$INCLUDE "Commctrl.inc"

CONST LISTVIEW_1 = 1
INT subclassID = 12345
INT hwnd_listview = 0
WINDOW w1
OPENWINDOW w1,0,0,600,400,@MINBOX|@MAXBOX|@SIZE,NULL,"Simple Window",&w1_handler

' main loop
WAITUNTIL w1 = 0
END

' window procedure
SUB w1_handler
SELECT @MESSAGE
CASE @IDCREATE
CENTERWINDOW w1
OnInit()

CASE @IDCLOSEWINDOW
CLOSEWINDOW w1
OnFinish()
ENDSELECT
ENDSUB

SUB subclassProc(hWnd:INT,uMsg:INT,wParam:INT,lParam:INT,uIdSubclass:UINT_PTR,dwRefData:DWORD_PTR),INT
SELECT uMsg
CASE WM_NOTIFY
SELECT *<NMHDR>lparam.idFrom
CASE LISTVIEW_1
SELECT *<NMHDR>lparam.code
CASE NM_CLICK ' click in a listview item
int index = *<NMITEMACTIVATE>lparam.iItem
MessageBoxW(hWnd, WSTR$(index + 1), L"item click", 0)
ENDSELECT
ENDSELECT

DEFAULT
RETURN DefSubclassProc(hWnd, uMsg, wParam, lParam)
ENDSELECT

RETURN 0
ENDSUB

SUB OnInit()
' subclass the window
SetWindowSubclass(w1.hwnd, &subclassProc, subclassID, 0)

' init common controls
_InitCommonControls()

' create the listview
INT dwStyles = WS_VISIBLE | WS_CHILD | LVS_REPORT | LVS_SHOWSELALWAYS
hwnd_listview = CreateListview(w1.hwnd, 40, 30, 500, 300, LISTVIEW_1, dwStyles)

' enable full row selection
ListviewSetExtStyles(hwnd_listview, LVS_EX_FULLROWSELECT)

' add colums
ListviewAddColumn(hwnd_listview, 0, 150, L"First")
ListviewAddColumn(hwnd_listview, 1, 150, L"Second")
ListviewAddColumn(hwnd_listview, 2, 150, L"Third")

' add some items
ListviewAddItem(hwnd_listview, L"Item #1", 0)
ListviewAddItem(hwnd_listview, L"Item #2", 0)
ListviewAddItem(hwnd_listview, L"Item #3", 0)

' add some subitems
ListviewModifyItem(hwnd_listview, 0, 1, L"Subitem #12", 0)
ListviewModifyItem(hwnd_listview, 0, 2, L"Subitem #13", 0)
ListviewModifyItem(hwnd_listview, 1, 1, L"Subitem #22", 0)

' select the second item
ListviewSelectItem(hwnd_listview, 1, 1)
ENDSUB

SUB OnFinish()
RemoveWindowSubclass(w1.hwnd, &subclassProc, subclassID)
ENDSUB

' ------------------------------------------------------------------------------------------------------------
' UNICODE FUNCTIONS
' ------------------------------------------------------------------------------------------------------------

SUB _InitCommonControls()
' Ensures that the common control DLL (Comctl32.dll) is loaded, and registers specific common control classes from the DLL.
' An application must call this function before creating a common control.
INITCOMMONCONTROLSEX icc
icc.dwSize=LEN(icc)
icc.dwICC= ICC_BAR_CLASSES | ICC_LINK_CLASS | ICC_LISTVIEW_CLASSES | ICC_PROGRESS_CLASS | ICC_TAB_CLASSES | ICC_TREEVIEW_CLASSES | ICC_UPDOWN_CLASS
InitCommonControlsEx (&icc)
ENDSUB

' -------

SUB CreateListview(INT parent, int x, int y, int dimx, int dimy, int ctrl, int style), INT
    RETURN CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, L"", _
        style, _
        x, y, dimx, dimy, _
        parent, ctrl, GetModuleHandle(0), 0)
ENDSUB

SUB ListviewSetExtStyles(INT handle, int dw_extStyles)
SendMessageW(handle, LVM_SETEXTENDEDLISTVIEWSTYLE, dw_extStyles, dw_extStyles)
ENDSUB

SUB ListviewAddColumn(INT handle, int index, int width, pointer title), INT
LVCOLUMN lvc
ZeroMemory(&lvc, LEN(lvc))

    lvc.mask = LVCF_TEXT | LVCF_FMT | LVCF_WIDTH | LVCF_SUBITEM
lvc.iSubItem=index
    lvc.fmt = LVCFMT_LEFT
    lvc.cx = width
lvc.pszText = title
RETURN SendMessageW(handle, LVM_INSERTCOLUMNW, index, &lvc)
ENDSUB

SUB ListviewAddItem(INT handle, pointer text, int imageNro), INT
LVITEM pitem
ZeroMemory(&pitem, LEN(pitem))

pitem.mask=LVIF_IMAGE | LVIF_TEXT
pitem.iItem=1000
pitem.iSubItem=0
pitem.pszText=text
pitem.iImage=imageNro

RETURN SendMessageW(handle, LVM_INSERTITEMW,  0, &pitem)
ENDSUB

SUB ListviewModifyItem(INT handle, int index, int sub_index, pointer text, int imageNro)
LVITEM pitem
ZeroMemory(&pitem, LEN(pitem))

pitem.mask= LVIF_TEXT | LVIF_IMAGE
pitem.iSubItem = sub_index
pitem.iItem=index
pitem.pszText=text
    pitem.iImage=imageNro
SendMessageW(handle, LVM_SETITEMW,  0, &pitem)
SendMessageW(handle, LVM_UPDATE, index, 0)
ENDSUB

SUB ListviewSelectItem(INT handle, int index, INT _select), INT
LVITEM pitem
ZeroMemory(&pitem, LEN(pitem))

pitem.mask=LVIF_STATE
pitem.iItem=index
pitem.iSubItem = 0

IF _select THEN
pitem.state=LVIS_SELECTED
pitem.stateMask =LVIS_SELECTED
ELSE
pitem.state=0
pitem.stateMask =LVIS_SELECTED
ENDIF

RETURN SendMessageW(handle, LVM_SETITEM,  0, &pitem)
ENDSUB


Listview creation styles
https://docs.microsoft.com/es-es/windows/desktop/Controls/list-view-window-styles

Listview extended styles
https://docs.microsoft.com/es-es/windows/desktop/Controls/extended-list-view-styles

All actions you can do with SendMessageW
https://docs.microsoft.com/es-es/windows/desktop/Controls/bumper-list-view-control-reference-messages

Listview notification list
https://docs.microsoft.com/es-es/windows/desktop/Controls/bumper-list-view-control-reference-notifications

Andy

Thanks Fasecero,

You've put a lot into this post for me - appreciated!

Having a look at it now.

Thanks,
Andy.
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.