Skip to content

Instantly share code, notes, and snippets.

@serid
Created September 23, 2020 23:46
Show Gist options
  • Save serid/0c77884599c85ed27157a6dafa280952 to your computer and use it in GitHub Desktop.
Save serid/0c77884599c85ed27157a6dafa280952 to your computer and use it in GitHub Desktop.
struct Size {
x: i32,
y: i32,
}
impl Size {
fn new(x: i32, y: i32) -> Self {
Size { x, y }
}
}
struct Rectangle {
left: i32,
top: i32,
size: Size,
}
impl Rectangle {
fn new(left: i32, top: i32, size: Size) -> Self {
Rectangle { left, top, size }
}
}
struct Point {
x: i32,
y: i32,
}
impl Point {
fn new(x: i32, y: i32) -> Self {
Point { x, y }
}
fn empty() -> Self {
Point { x: 0, y: 0 }
}
}
struct Graphics {
}
impl Graphics {
fn new() -> Self {
Graphics { }
}
fn copy_from_screen(&mut self, _p1: Point, p2: Point, size: Size) {
unsafe {
use winapi::shared::windef::HDC;
use winapi::shared::windef::HBITMAP;
use winapi::um::winuser::GetDC;
use winapi::um::wingdi::CreateCompatibleDC;
use winapi::um::wingdi::CreateCompatibleBitmap;
use winapi::um::wingdi::SelectObject;
use winapi::um::wingdi::BitBlt;
use winapi::um::wingdi::DeleteDC;
use winapi::um::winuser::OpenClipboard;
use winapi::um::winuser::EmptyClipboard;
use winapi::um::winuser::SetClipboardData;
use winapi::um::winuser::CloseClipboard;
use winapi::um::wingdi::SRCCOPY;
use winapi::um::winuser::CF_BITMAP;
use winapi::ctypes::c_int;
use winapi::shared::ntdef::NULL;
let h_screen_dc: HDC = GetDC(NULL as _);
let h_memory_dc: HDC = CreateCompatibleDC(h_screen_dc);
let width: c_int = size.x;
let height: c_int = size.y;
println!["width: {}", width];
println!["height: {}", height];
let mut h_bitmap: HBITMAP = CreateCompatibleBitmap(h_screen_dc, width, height);
// get a new bitmap
let h_old_bitmap: HBITMAP = SelectObject(h_memory_dc, h_bitmap as _) as _;
BitBlt(h_memory_dc, 0, 0, width, height, h_screen_dc, p2.x, p2.y, SRCCOPY);
h_bitmap = SelectObject(h_memory_dc, h_old_bitmap as _) as _;
DeleteDC(h_memory_dc);
DeleteDC(h_screen_dc);
OpenClipboard(NULL as _);
EmptyClipboard();
SetClipboardData(CF_BITMAP, h_bitmap as _);
CloseClipboard();
}
}
}
fn init() {
unsafe {
use winapi::um::winuser::SetProcessDPIAware;
SetProcessDPIAware();
}
}
fn get_screen_rectangle() -> Rectangle {
unsafe {
use winapi::um::winuser::GetSystemMetrics;
use winapi::um::winuser::SM_XVIRTUALSCREEN;
use winapi::um::winuser::SM_CXVIRTUALSCREEN;
use winapi::um::winuser::SM_YVIRTUALSCREEN;
use winapi::um::winuser::SM_CYVIRTUALSCREEN;
use winapi::ctypes::c_int;
let x1: c_int = GetSystemMetrics(SM_XVIRTUALSCREEN);
let x2: c_int = GetSystemMetrics(SM_CXVIRTUALSCREEN);
let y1: c_int = GetSystemMetrics(SM_YVIRTUALSCREEN);
let y2: c_int = GetSystemMetrics(SM_CYVIRTUALSCREEN);
Rectangle::new(x1, y1, Size::new(x2 - x1, y2 - y1))
}
}
fn main() {
println!("Hello, world!");
init();
let bounds = Rectangle::new(0, 0, Size::new(100, 200));
// let bounds = get_screen_rectangle();
let mut g = Graphics::new();
g.copy_from_screen(Point::new(bounds.left, bounds.top), Point::empty(), bounds.size);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment