Skip to content

Instantly share code, notes, and snippets.

@iancormac84
Created September 26, 2015 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iancormac84/b0caaae9407bc277db9e to your computer and use it in GitHub Desktop.
Save iancormac84/b0caaae9407bc277db9e to your computer and use it in GitHub Desktop.
Example of creating a window with a PaintDc context and drawing to it using a brush. Probably incorrect.
// Copyright 2015 The Rust-Windows Project Developers. See the
// COPYRIGHT file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(libc)]
extern crate libc;
extern crate winapi;
#[macro_use]
extern crate rust_windows as windows;
extern crate gdi32;
extern crate user32;
use libc::c_int;
use std::{mem, ptr};
use winapi::{HWND, HRGN, HBRUSH, CREATESTRUCTW, UINT};
use windows::main_window_loop;
use windows::instance::Instance;
use windows::resource::*;
use windows::window::{WindowImpl, Window, WndClass, WindowParams};
use windows::window::{OnCreate, OnDestroy, OnPaint};
use windows::window;
use windows::gdi::PaintDc;
struct MainFrame {
pub win: Window,
title: String,
}
pub const CW_USEDEFAULT: c_int = 0x80000000u32 as c_int;
pub const GA_PARENT: UINT = 1;
pub const GA_ROOT: UINT = 2;
pub const GA_ROOTOWNER: UINT = 3;
wnd_proc!(MainFrame, win, WM_CREATE, WM_DESTROY, WM_PAINT);
impl OnCreate for MainFrame {
fn on_create(&self, _cs: &CREATESTRUCTW) -> bool {
let params = WindowParams {
window_name: "Learn to Program Windows".to_string(),
style: window::WS_OVERLAPPEDWINDOW,
x: CW_USEDEFAULT as isize,
y: CW_USEDEFAULT as isize,
width: CW_USEDEFAULT as isize,
height: CW_USEDEFAULT as isize,
parent: self.win,
menu: ptr::null_mut(),
ex_style: 0,
};
let sample_window = Window::new(Instance::main_instance(), None, "Sample Window Class", &params);
match sample_window {
None => false,
Some(e) => true,
}
}
}
impl OnDestroy for MainFrame {}
impl OnPaint for MainFrame {
fn on_paint(&self) {
let pdc = PaintDc::new(self).expect("Paint DC");
let client_rect = self.win.client_rect().unwrap();
let hrgn: HRGN = unsafe {
gdi32::CreateRectRgnIndirect(&client_rect)
};
let hbrush: HBRUSH = unsafe {
gdi32::CreateSolidBrush(0x008f1e35)
};
unsafe {
gdi32::FillRgn(pdc.dc.raw(), hrgn, hbrush);
}
}
}
impl MainFrame {
fn new(instance: Instance, title: String) -> Option<Window> {
let wnd_class = WndClass {
classname: "Sample Window Class".to_string(),
style: 0x0001 | 0x0002, // CS_HREDRAW | CS_VREDRAW
icon: None,
icon_small: None,
cursor: Image::load_cursor_resource(32512), // hourglass
background: (5 + 1) as HBRUSH,
menu: MenuResource::MenuId(0),
cls_extra: 0,
wnd_extra: 0,
};
let res = wnd_class.register(instance);
if !res {
return None;
}
let wproc = Box::new(MainFrame {
win: Window::null(),
title: title.clone(),
});
let win_params = WindowParams {
window_name: title,
style: window::WS_OVERLAPPEDWINDOW,
x: CW_USEDEFAULT as isize,
y: CW_USEDEFAULT as isize,
width: CW_USEDEFAULT as isize,
height: CW_USEDEFAULT as isize,
parent: Window::null(),
menu: ptr::null_mut(),
ex_style: 0,
};
Window::new(instance, Some(wproc as Box<WindowImpl + 'static>),
&wnd_class.classname[..], &win_params)
}
}
fn main() {
let instance = Instance::main_instance();
let main = MainFrame::new(instance, "Learn to Program Windows".to_string());
let main = main.unwrap();
main.show(1);
main.update();
let exit_code = main_window_loop();
std::process::exit(exit_code as i32);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment