본문 바로가기
프로그램

[Rust] 윈도우 코드

by 오디세이99 2022. 8. 14.
728x90
반응형

다음과 같이 Rust로 윈도우를 만드는 코드를 볼 수 있습니다.

 

 

GitHub - microsoft/windows-rs: Rust for Windows

 

GitHub - microsoft/windows-rs: Rust for Windows

Rust for Windows. Contribute to microsoft/windows-rs development by creating an account on GitHub.

github.com

 

GitHub - microsoft/windows-rs: Rust for Windows

 

GitHub - microsoft/windows-rs: Rust for Windows

Rust for Windows. Contribute to microsoft/windows-rs development by creating an account on GitHub.

github.com

 

 

windows-samples-rs/main.rs at master · microsoft/windows-samples-rs · GitHub

 

GitHub - microsoft/windows-samples-rs

Contribute to microsoft/windows-samples-rs development by creating an account on GitHub.

github.com

use windows::{
    core::*, Win32::Foundation::*, Win32::Graphics::Gdi::ValidateRect,
    Win32::System::LibraryLoader::GetModuleHandleA, Win32::UI::WindowsAndMessaging::*,
};

fn main() -> Result<()> {
    unsafe {
        let instance = GetModuleHandleA(None);
        debug_assert!(instance.0 != 0);

        let window_class = "window";

        let wc = WNDCLASSA {
            hCursor: LoadCursorW(None, IDC_ARROW),
            hInstance: instance,
            lpszClassName: PSTR(b"window\0".as_ptr() as _),

            style: CS_HREDRAW | CS_VREDRAW,
            lpfnWndProc: Some(wndproc),
            ..Default::default()
        };

        let atom = RegisterClassA(&wc);
        debug_assert!(atom != 0);

        CreateWindowExA(
            Default::default(),
            window_class,
            "This is a sample window",
            WS_OVERLAPPEDWINDOW | WS_VISIBLE,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            None,
            None,
            instance,
            std::ptr::null_mut(),
        );

        let mut message = MSG::default();

        while GetMessageA(&mut message, HWND(0), 0, 0).into() {
            DispatchMessageA(&mut message);
        }

        Ok(())
    }
}

extern "system" fn wndproc(window: HWND, message: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
    unsafe {
        match message as u32 {
            WM_PAINT => {
                println!("WM_PAINT");
                ValidateRect(window, std::ptr::null());
                LRESULT(0)
            }
            WM_DESTROY => {
                println!("WM_DESTROY");
                PostQuitMessage(0);
                LRESULT(0)
            }
            _ => DefWindowProcA(window, message, wparam, lparam),
        }
    }
}
728x90
반응형

댓글