rodrigocfd / winsafe

Windows API and GUI in safe, idiomatic Rust.
https://crates.io/crates/winsafe
MIT License
518 stars 30 forks source link

When the new_dlg function is used to create a window, the wm_create event does not work #115

Closed MDZZDYXCMDZZDYXC closed 10 months ago

MDZZDYXCMDZZDYXC commented 10 months ago

winsafe = "0.0.18"

use winsafe::gui;
use winsafe::prelude::*;
fn main() {
    let window = gui::WindowMain::new_dlg(108, Some(128), None);
    let notresize = (gui::Horz::None,gui::Vert::None);
    let dsd = gui::Edit::new_dlg(&window, 1000, notresize);
    let btn = gui::Button::new_dlg(&window, 1002, (gui::Horz::Resize,gui::Vert::Resize));
    let tab = gui::ListView::new_dlg(&window, 1003, (gui::Horz::Repos,gui::Vert::Repos), Some(winsafe::HMENU::CreatePopupMenu().unwrap()));
    window.on().wm_create(|_|{
        println!("message");
        Ok(1)
    });
    window.run_main(None);

}
rodrigocfd commented 10 months ago

It never will:

MDZZDYXCMDZZDYXC commented 10 months ago

I understand what you mean, the wm_create function is called differently in these two cases of creating forms. wm_init_dialog will be called repeatedly when the form is enlarged or reduced, etc. I have the need to load the configuration file when the form is launched. Without using the designer.res file, wm_create function is sufficient. Of course, I can implement the requirement in wm_init_dialog if I set a flag. What I want is to have an event or a function that only executes once when the program starts. One of the following codes triggers wm create and the other does not.

fn main() {
    let window = gui::WindowMain::new(
        gui::WindowMainOpts{
            title:"test1".into(),
            ..Default::default()
        }
    );
    window.on().wm_create(|_|{
        println!("message");
        Ok(0)
    });
    window.run_main(None);
}
fn main() {
    let window = gui::WindowMain::new_dlg(108, Some(128), None);

    window.on().wm_create(|_|{
        println!("message");
        Ok(0)
    });
    window.run_main(None);
}
MDZZDYXCMDZZDYXC commented 10 months ago

Maybe I didn't describe it very well. I would like to have a more uniform event where the configuration is loaded onto the form after it is created rather than being called repeatedly.

rodrigocfd commented 10 months ago

wm_init_dialog will be called repeatedly when the form is enlarged or reduced, etc.

Not at all... WM_INITDIALOG is called only once, immediately before the dialog is shown. There is no other situation where it's called.

Please post your code, so we can try to spot the problem.

MDZZDYXCMDZZDYXC commented 10 months ago

Strange, I tried to reproduce the phenomenon again, but the code worked fine. I guarantee that I encountered this situation, maybe it was affected by some system Settings, and if I encounter this situation again, I will describe it in as much detail as possible. In both cases, I just upgraded rust to 1.74. But now there is a new problem, init_dialog does not work, the code is as follows:

use winsafe::gui;
use winsafe::prelude::*;
fn main() {
    let window = gui::WindowMain::new(
        gui::WindowMainOpts{
            title:"test1".into(),
            ..Default::default()
        }
    );
    window.on().wm_init_dialog(|_|{
        println!("hello");
        Ok(true)
    });
    window.run_main(None);
}

bug233

rodrigocfd commented 10 months ago

But now there is a new problem, init_dialog does not work, the code is as follows

There are 2 types of window, normal and dialog.

You're creating a normal window, and trying to handle wm_init_dialog.