rodrigocfd / windigo

Windows API and GUI in idiomatic Go.
https://pkg.go.dev/github.com/rodrigocfd/windigo
MIT License
405 stars 22 forks source link

How to add List Column to ListView? #26

Closed shaoerkuai closed 1 year ago

shaoerkuai commented 1 year ago

Demo:

func NewMyWindow() *MyWindow {
    wnd := ui.NewWindowMain(
        ui.WindowMainOpts().
            Title("Title").
            ClientArea(win.SIZE{Cx: 340, Cy: 500}),
    )
    icx := win.INITCOMMONCONTROLSEX{
        DwICC: co.ICC_LISTVIEW_CLASSES,
    }
    icx.SetDwSize()
    win.InitCommonControlsEx(&icx)
    me := &MyWindow{
        wnd: wnd,
        lblName: ui.NewStatic(wnd,
            ui.StaticOpts().
                Text("YourName").
                Position(win.POINT{X: 10, Y: 22}),
        ),
        txtName: ui.NewEdit(wnd,
            ui.EditOpts().
                Position(win.POINT{X: 80, Y: 20}).
                Size(win.SIZE{Cx: 150}),
        ),
        btnShow: ui.NewButton(wnd,
            ui.ButtonOpts().
                Text("&Show").
                Position(win.POINT{X: 240, Y: 19}),
        ),
        **table: ui.NewListView(wnd, ui.ListViewOpts().
            Position(win.POINT{
                X: 10,
                Y: 50,
            }).Size(win.SIZE{Cx: 400, Cy: 200}).
            CtrlStyles(co.LVS_REPORT).WndStyles(co.WS_CHILD|co.WS_VISIBLE))**,
    }

    me.btnShow.On().BnClicked(func() {
        msg := fmt.Sprintf("Hello, %s!", me.txtName.Text())
        print(me.wnd.Hwnd().MessageBox(msg, "Saying hello", co.MB_OKCANCEL|co.MB_ICONINFORMATION))
    })
    **dwStyle := me.table.ExtendedStyle()
    dwStyle |= co.LVS_EX_FULLROWSELECT
    dwStyle |= co.LVS_EX_GRIDLINES
    me.table.SetExtendedStyle(true, dwStyle)
    me.table.Columns().Add([]int{20, 5}, "1", "2")
    me.table.Columns().Add([]int{1}, "aaa")**
    return me

image

I'm using ListView.Add().Columns() but nothing shown:(

And if call me.table.Items().Add("value1", "value2")

A panic occured :panic: LVM_SETITEMTEXT col 1, "value2" failed.

rodrigocfd commented 1 year ago

It panics because the listview doesn't exist yet. Windows will tell you when the controls are ready by sending you a WM_INITDIALOG message, which is when you have your controls.

Since this is a very basic mistake, I suggest you to learn the basics of Win32 API first.

shaoerkuai commented 1 year ago

I just learned the Window Procedure and has made list view worked without panic.

Here is a workaround that might help other users who don't know much about the Win32 API:

  1. Register a Notify handler after Window created

notify := myWindow.wnd.On() make sure that DO PUT IT before RunAsMain(), since RunAsMain will block.

  1. Accpet WM_CREATE
    notify.WmCreate(func(p wm.Create) int {
// DO WHAT YOU WANT
        dwStyle := myWindow.table.ExtendedStyle()
        dwStyle |= co.LVS_EX_FULLROWSELECT
        dwStyle |= co.LVS_EX_GRIDLINES
        myWindow.table.SetExtendedStyle(true, dwStyle)
        myWindow.table.Columns().Add([]int{80, 70, 120}, "Col 1", "Col 2", "Col 3")
        return 0
    })

I think I will contribute some basic docs pr to the project once I figure out some things, this is a promising project if someone sticked to windows classic gui programming (I am working on embbed devices testing tools development, and I don't need Qt or Fyne, they areunnecessary).