diff --git a/MakeAnnotations.h b/MakeAnnotations.h index e319a82..1681106 100644 --- a/MakeAnnotations.h +++ b/MakeAnnotations.h @@ -309,7 +309,7 @@ HBITMAP WINAPI penIcon( Gdiplus::Pen* pen ) { } -int makeAnnotations( HBITMAP snippet, RECT bounds, int lang ) { +int makeAnnotations( HMONITOR monitor, HBITMAP snippet, RECT bounds, int lang ) { // Register window class WNDCLASSW wc = { CS_OWNDC | CS_HREDRAW | CS_VREDRAW, // style @@ -324,11 +324,22 @@ int makeAnnotations( HBITMAP snippet, RECT bounds, int lang ) { WINDOW_CLASS_NAME // lpszClassName }; RegisterClassW( &wc ); - + + + // Calculate window size from snippet bounds. Add some extra space and a min size to fit buttons + int width = max(600, bounds.right - bounds.left) + 50; + int height = bounds.bottom - bounds.top + 150; + + // Determine position on requested monitor + MONITORINFO info; + info.cbSize = sizeof( info ); + GetMonitorInfo( monitor, &info ); + int x = info.rcWork.left + max( 0, ( ( info.rcWork.right - info.rcWork.left ) - width ) / 2 ); + int y = info.rcWork.top + max( 0, ( ( info.rcWork.bottom - info.rcWork.top ) - height ) / 2 ); + // Create window HWND hwnd = CreateWindowExW( WS_EX_TOPMOST, wc.lpszClassName, - localization[ lang ].title, WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, - max( 600, bounds.right - bounds.left ) + 50 , bounds.bottom - bounds.top + 150, // Add some extra space and a min size + localization[ lang ].title, WS_VISIBLE | WS_OVERLAPPEDWINDOW, x, y, width, height, NULL, NULL, GetModuleHandleW( NULL ), 0 ); // GDI+ instances of all available pens diff --git a/ScreenSnippet.cpp b/ScreenSnippet.cpp index 4f726d3..3a59784 100644 --- a/ScreenSnippet.cpp +++ b/ScreenSnippet.cpp @@ -85,11 +85,18 @@ static BOOL CALLBACK closeExistingInstance( HWND hwnd, LPARAM lparam ) { int main( int argc, char* argv[] ) { SetProcessDPIAware(); // Avoid DPI scaling affecting the resolution of the grabbed snippet + HWND foregroundWindow = GetForegroundWindow(); + + HMONITOR monitor = MonitorFromWindow( foregroundWindow, MONITOR_DEFAULTTOPRIMARY ); + // Cancel screen snippet in progress EnumWindows( closeExistingInstance, 0 ); // If no command line parameters, this was a request to cancel in-progress snippet tool if( argc < 2 ) { + if( foregroundWindow ) { + SetForegroundWindow( foregroundWindow ); + } return EXIT_SUCCESS; } @@ -129,7 +136,7 @@ int main( int argc, char* argv[] ) { // Let the user annotate the screen snippet with drawings RECT bounds = { 0, 0, bottomRight.x - topLeft.x, bottomRight.y - topLeft.y }; - int result = makeAnnotations( snippet, bounds, lang ); + int result = makeAnnotations( monitor, snippet, bounds, lang ); if( result == EXIT_SUCCESS ) { // Save annotated bitmap Gdiplus::Bitmap bmp( snippet, (HPALETTE)0 ); @@ -147,6 +154,9 @@ int main( int argc, char* argv[] ) { } Gdiplus::GdiplusShutdown( gdiplusToken ); + if( foregroundWindow ) { + SetForegroundWindow( foregroundWindow ); + } return EXIT_SUCCESS; } diff --git a/package.json b/package.json index 52836d9..3f9b712 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "screen-snippet", - "version": "1.0.0", + "version": "1.0.1", "description": "screen snippet util for windows, new version", "scripts": { "test": "echo \"Error: no test specified\" && exit 1",