尝试使用rust调用xcgui.dll

示例代码:

//lib.rs
pub mod xcgui;
//xcgui.rs
use anyhow::{Result};
use libloading::{Library, Symbol};
use std::sync::OnceLock;
use widestring::{U16CStr, U16CString};
use bitflags::bitflags;
bitflags! {
    /// 炫彩窗口样式 (对应 C++ 的 window_style_ 枚举)
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct WindowStyle: u32 {
        /// 什么也没有
        const NOTHING = 0x0000;
        /// 标题栏
        const CAPTION = 0x0001;
        /// 边框
        const BORDER = 0x0002;
        /// 居中
        const CENTER = 0x0004;
        /// 拖动边框
        const DRAG_BORDER = 0x0008;
        /// 拖动窗口
        const DRAG_WINDOW = 0x0010;
        /// 允许最大化
        const ALLOW_MAX_WINDOW = 0x0020;
        /// 图标
        const ICON = 0x0040;
        /// 标题
        const TITLE = 0x0080;
        /// 控制按钮-最小化
        const BTN_MIN = 0x0100;
        /// 控制按钮-最大化
        const BTN_MAX = 0x0200;
        /// 控制按钮-关闭
        const BTN_CLOSE = 0x0400;
        //— 组合样式直接定义在宏内部 —
        /// 默认样式:CAPTION | BORDER | CENTER | DRAG_BORDER | ALLOW_MAX_WINDOW | ICON | TITLE | BTN_MIN | BTN_MAX | BTN_CLOSE
        const DEFAULT = Self::CAPTION.bits()
            | Self::BORDER.bits()
            | Self::CENTER.bits()
            | Self::DRAG_BORDER.bits()
            | Self::ALLOW_MAX_WINDOW.bits()
            | Self::ICON.bits()
            | Self::TITLE.bits()
            | Self::BTN_MIN.bits()
            | Self::BTN_MAX.bits()
            | Self::BTN_CLOSE.bits();
        /// 简单样式:CAPTION | BORDER | CENTER | DRAG_BORDER | ALLOW_MAX_WINDOW
        const SIMPLE = Self::CAPTION.bits()
            | Self::BORDER.bits()
            | Self::CENTER.bits()
            | Self::DRAG_BORDER.bits()
            | Self::ALLOW_MAX_WINDOW.bits();
        /// 弹出窗口样式:CAPTION | BORDER | CENTER | DRAG_BORDER | ALLOW_MAX_WINDOW | ICON | TITLE | BTN_CLOSE
        const POP = Self::CAPTION.bits()
            | Self::BORDER.bits()
            | Self::CENTER.bits()
            | Self::DRAG_BORDER.bits()
            | Self::ALLOW_MAX_WINDOW.bits()
            | Self::ICON.bits()
            | Self::TITLE.bits()
            | Self::BTN_CLOSE.bits();
        /// 模态窗口样式:CAPTION | BORDER | CENTER | ICON | TITLE | BTN_CLOSE
        const MODAL = Self::CAPTION.bits()
            | Self::BORDER.bits()
            | Self::CENTER.bits()
            | Self::ICON.bits()
            | Self::TITLE.bits()
            | Self::BTN_CLOSE.bits();
        /// 简单模态样式:CAPTION | BORDER | CENTER
        const MODAL_SIMPLE = Self::CAPTION.bits()
            | Self::BORDER.bits()
            | Self::CENTER.bits();
    }
}
static INSTANCE: OnceLock<XCGUI> = OnceLock::new();
pub struct XCGUI {
    xclib: Library,
}
impl XCGUI {
    pub fn init() ->  &’static Self {
        INSTANCE.get_or_init(|| XCGUI {
            xclib: unsafe {
                libloading::Library::new(“xcgui.dll”)
                    .expect(“Load xcgui.dll faild! Please check xcgui.dll file path.”)
            },
        })
    }
    pub fn XC_itow(&self, nValue: i32) -> Result<String, Box<dyn std::error::Error>> {
        unsafe {
            let xc_itow: Symbol<unsafe extern “C” fn(i32) -> *const u16> =
                self.xclib.get(b”XC_itow”)?;
            let ptr = xc_itow(nValue);
            let wstr = U16CStr::from_ptr_str(ptr);
            let s = wstr.to_string_lossy();
            println!(“Result: {}”, s);
            Ok(s)
        }
    }
    pub fn XInitXCGUI(&self , bD2D:bool)->Result<bool, Box<dyn std::error::Error>>{
        type XInitXCGUIFunc = unsafe extern “system” fn(
            bD2D: std::os::raw::c_int,    // BOOL 类型(对应 C 的 int)
        ) -> std::os::raw::c_int;
        let _XInitXCGUI: Symbol<XInitXCGUIFunc> = unsafe { self.xclib.get(b”XInitXCGUI”) }?;
    let r = unsafe {
        _XInitXCGUI(bD2D as i32)
    };
    Ok(r != 0)
    }
    pub fn XWnd_Create(&self,x:i32,y:i32,cx:i32,cy:i32,title:&str)-> Result<isize, Box<dyn std::error::Error>>{
        unsafe {
            type XWndCreateFunc = unsafe extern “system” fn(
                x: std::os::raw::c_int,
                y: std::os::raw::c_int,
                cx: std::os::raw::c_int,
                cy: std::os::raw::c_int,
                pTitle: *const u16,  // 对应 const wchar_t*
                hWndParent: isize,   // HWND 类型
                XCStyle: std::os::raw::c_int,
            ) -> isize;
            let title = U16CString::from_str(title)?;
            let _XWnd_Create: Symbol<XWndCreateFunc> =
                self.xclib.get(b”XWnd_Create”)?;
            let ptr = _XWnd_Create(x,y,cx,cy,title.as_ptr(),0, (WindowStyle::DEFAULT | WindowStyle::DRAG_WINDOW).bits() as i32 );
            println!(“XWnd_Create Result: {}”, ptr);
            Ok(ptr)
        }
    }
    pub fn XWnd_Show(&self , hWindow: isize,bShow:bool)->Result<(), Box<dyn std::error::Error>>{
        type XWndShowFunc = unsafe extern “system” fn(
            hWindow: isize,  // HWINDOW 类型(句柄)
            bShow: std::os::raw::c_int,    // BOOL 类型(对应 C 的 int)
        ) -> ();
        let xwnd_show: Symbol<XWndShowFunc> = unsafe { self.xclib.get(b”XWnd_Show”) }?;
    unsafe {
        xwnd_show(
            hWindow,
            bShow as i32,
        );
    }
    Ok(())
    }
    pub fn XRunXCGUI(&self )->Result<(), Box<dyn std::error::Error>>{
        type XRunXCGUIFunc = unsafe extern “system” fn() -> ();
        let xRunXCGUI: Symbol<XRunXCGUIFunc> = unsafe { self.xclib.get(b”XRunXCGUI”) }?;
    unsafe {
        xRunXCGUI();
    }
    Ok(())
    }
}

// main.rs

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let xcgui = xcgui_rs::xcgui::XCGUI::init();
    let vs = xcgui.XC_itow(12321312)?;
    println!(“itow: {}”,vs);
    let r = xcgui.XInitXCGUI(false)?;
    println!(“xinit: {}”,r);
    let hwnd = xcgui.XWnd_Create(0, 0, 300, 300, “炫彩GUI\0”)?;
    xcgui.XWnd_Show(hwnd, true)?;
    xcgui.XRunXCGUI()?;
    Ok(())
}
分页阅读:上一页 1 2
1. 本站鼓励用户发布原创内容,但并不保证每个人都遵守这一点,若内容侵犯了你的权益可联系管理员删除!
2. 本站资源,除文章特别指明外,均限定付费者本人使用,禁止二次 转载 传播 分发!
3. 本站资源多为第三方用户投稿 定价由资源提供者设定 收益人为资源提供者大家下载资源前仔细甄别需求与其描述是否可达预期 除非较明显的与说明不符资源外的纠纷尽量与作者点对点直接解决
4. 资源提供者发布作品请提供作品详细说明 与 截图 源码作品若引用了 其它模块或依赖请诚实说明 明细与版本!以及依赖是否开源。尽量做到资源下载后 可以直接使用与运行
5. 本站资源会员享受折扣开一个吧只有这个才是站长能拿到手的

炫彩资源网 » 尝试使用rust调用xcgui.dll

发表评论