之前两篇关于win32 htmlayout博文,记载了一个实现了简单的点击按钮弹出新窗口的demo,之后实践中发现,图形界面开发,最重要的还是要实现响应用户操作,改变原有界面的功能。比如说,界面上有一个用户列表,选择其中一个名字,点击删除按键,则可以将这个名字删掉。这种类似的操作是很常见的。使界面有了动态性。
对于win32 htmlayout就是操作html的dom节点,虽然没做过C++ win32,但做java web对dom操作也算很熟悉了。
dom操作就是这样:
//************************************ // 作 者: sonne // 函 数 名: OnButtonClick // 功 能: 按钮响应事件 // 完 整 名: OnButtonClick // 访 问 权: public // 返回值类型: VOID // 方法限定符: //************************************ void OnButtonClick(HELEMENT button) { htmlayout::dom::element cBut = button; if (!wcscmp(cBut.get_attribute("id"),L"ACT")) { htmlayout::dom::element root; htmlayout::dom::element name; root = htmlayout::dom::element::root_element(hwnd); name = root.get_element_by_id("NAME"); name.insert(htmlayout::dom::element::create("option", L"Whoami"), 0); } if (!wcscmp(cBut.get_attribute("id"),L"DEL")) { htmlayout::dom::element root; htmlayout::dom::element nameList; root = htmlayout::dom::element::root_element(hwnd); nameList = root.get_element_by_id("NAME"); htmlayout::dom::element selName; selName = nameList.find_first("option:checked"); selName.destroy(); SetWindowText(hwnd,"NAME LIST"); } if (!wcscmp(cBut.get_attribute("id"),L"EDI")) { htmlayout::dom::element root; htmlayout::dom::element nameList; root = htmlayout::dom::element::root_element(hwnd); nameList = root.get_element_by_id("NAME"); htmlayout::dom::element selName; selName = nameList.find_first("option:checked"); selName.set_value("Iamwho"); SetWindowText(hwnd,"NAME LIST"); } }
实现的效果:
初始化显示为Bob,Alice,Peter三个名字。点击ACT按钮则添加一个名为Whoami的名字,选中后点击EDI按钮则将名字改为Iamwho,选中后点击DEL按钮则删除。
这就是基于dom操作动态效果实现增删改查的小demo。
github代码地址:
https://github.com/SonnAdolf/sonne_desktop_graphical_development