/// Use a closure as window procedure.
///
/// The closure will be boxed and stored in a global variable. It will be
/// released upon WM_DESTROY. (It doesn't get to handle WM_DESTROY.)
Use a closure as window procedure.
The closure will be boxed and stored in a global variable. It will be
released upon WM_DESTROY. (It doesn't get to handle WM_DESTROY.)
Use a closure as window procedure . The closure will be boxed and stored in a global variable . It will be released upon WM_DESTROY . ( It doesn ' t get to handle WM_DESTROY . )
pub unsafe fn as_global_wnd_proc<F: Fn(HWND, UINT, WPARAM, LPARAM) -> isize + 'static>(f: F,) 
        -> unsafe extern "system" fn(hwnd: HWND, msg: UINT, w_param: WPARAM, l_param: LPARAM) -> isize
    {
        let f_ptr = Box::into_raw(Box::new(f));
        GLOBAL_F.set(f_ptr as usize);
        unsafe extern "system" fn wnd_proc<F: Fn(HWND, UINT, WPARAM, LPARAM) -> isize + 'static>(
            hwnd: HWND,
            msg: UINT,
            w_param: WPARAM,
            l_param: LPARAM,
        ) -> isize {
            let f_ptr = GLOBAL_F.get() as *mut F;
            if msg == WM_DESTROY {
                Box::from_raw(f_ptr);
                GLOBAL_F.set(0);
                PostQuitMessage(0);
                return 0;
            }
            if !f_ptr.is_null() {
                let f = &*f_ptr;
                f(hwnd, msg, w_param, l_param)
            } else {
                DefWindowProcW(hwnd, msg, w_param, l_param)
            }
        }
        wnd_proc::<F>
    }
pub unsafe fn as_global_wnd_proc < F : Fn ( HWND , UINT , WPARAM , LPARAM ) -> isize + ' static > ( f : F , ) -> unsafe extern " " fn ( hwnd : HWND , msg : UINT , w_param : WPARAM , l_param : LPARAM ) -> isize { let f_ptr = Box :: into_raw ( Box :: new ( f ) ) ; GLOBAL_F . set ( f_ptr as usize ) ; unsafe extern " " fn wnd_proc < F : Fn ( HWND , UINT , WPARAM , LPARAM ) -> isize + ' static > ( hwnd : HWND , msg : UINT , w_param : WPARAM , l_param : LPARAM , ) -> isize { let f_ptr = GLOBAL_F . get ( ) as * mut F ; if msg == WM_DESTROY { Box :: from_raw ( f_ptr ) ; GLOBAL_F . set ( 0 ) ; PostQuitMessage ( 0 ) ; return 0 ; } if ! f_ptr . is_null ( ) { let f = & * f_ptr ; f ( hwnd , msg , w_param , l_param ) } else { DefWindowProcW ( hwnd , msg , w_param , l_param ) } } wnd_proc :: < F > }
Use a closure as window procedure.
Use a closure as window procedure .
f
F