GithubHelp home page GithubHelp logo

Comments (1)

rodrigocfd avatar rodrigocfd commented on June 2, 2024

The problem is that you adding a node to the treeview control, but the control doesn't physically exist yet. That is: the treeview object has been created, but the owner window didn't render it yet.

In order to perform this operation, you must do it in the WM_CREATE event (if an ordinary window), or in the WM_INITDIALOG event (if a dialog window).

The example below shows you code, corrected:

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use winsafe::{prelude::*, gui, co, AnyResult, HWND};

#[derive(Clone)]
pub struct MyWindow {
	wnd:       gui::WindowMain,
	tv: gui::TreeView,
}

impl MyWindow {
	pub fn new() -> Self {
		let wnd = gui::WindowMain::new(
			gui::WindowMainOpts {
				title: "My window title".to_owned(),
				size: (300, 150),
				..Default::default()
			},
		);

		let tv = gui::TreeView::new(
			&wnd,
			Default::default(),
		);
		// tv.items().add_root("text", None); THIS LINE GOES TO WM_CREATE

		let new_self = Self { wnd, tv };
		new_self.events(); // add the window event handlers
		new_self
	}

	pub fn run(&self) -> AnyResult<i32> {
		self.wnd.run_main(None)
	}

	fn events(&self) {
		let self2 = self.clone();
		self.wnd.on().wm_create(move |_| { // called right after the window is created
			self2.tv.items().add_root("text", None);
			Ok(0)
		});
	}
}


fn main() {
	if let Err(e) = run_app() {
		HWND::NULL.MessageBox(
			&e.to_string(), "Uncaught error", co::MB::ICONERROR).unwrap();
	}
}

fn run_app() -> AnyResult<i32> {
	MyWindow::new()
		.run()
		.map_err(|err| err.into())
}

The message error doesn't help, though. I'll implement a better error handling for such case.

from winsafe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.