Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
NSTechBytes authored Nov 12, 2024
1 parent 8ff7e79 commit a32c30e
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 0 deletions.
66 changes: 66 additions & 0 deletions Finalshot/Main.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[Rainmeter]
Update=1000

[Metadata]
Name=Screenshot Skin
Author=NS Tech Bytes
Version=1.0
Description=This Rainmeter skin allows you to take full screen or custom region screenshots. Use the green button for full-screen capture and the blue button for custom region capture.
License=Appache 2.0

[mFinalShot]
Measure=Plugin
Plugin=Finalshot.dll
SavePath="D:\screenshot.png"
ScreenshotFinishAction=[!Log "Picture Saved"]


[BackGroundShape]
Meter=Shape
Shape=Rectangle 0,0,400,200,8 | StrokeWidth 0 | FillColor 255,255,255,100
DyamicVariables=1

; --- [Full Screen Screenshot Button] ---
[FullScreenButton]
Meter=Shape
Shape=Rectangle 0,0,150,50,8 | StrokeWidth 0 | FillColor 10,10,10,150
X=20
Y=16r
LeftMouseUpAction=[!RainmeterPluginBang "mFinalShot ExecuteBatch 1"]
[Full_Ins]
Meter=String
X=((150)/2)r
Y=(50/2)r
stringAlign = CenterCenter
Text=FullScreen
FontColor=10,10,10
FontSize=14
Antialias=1
; --- [Custom Region Screenshot Button] ---
[CustomRegionButton]
Meter=Shape
Shape=Rectangle 0,0,150,50,8 | StrokeWidth 0 | FillColor 10,10,10,150
X=30R
Y=-25r
LeftMouseUpAction=[!RainmeterPluginBang "mFinalShot ExecuteBatch 2"]
[Custom_Ins]
Meter=String
X=((150)/2)r
Y=(50/2)r
stringAlign = CenterCenter
Text=Select Region
FontColor=10,10,10
FontSize=14
Antialias=1
; --- [Instructions Text] ---
[Instructions]
Meter=String
X=200
Y=100
stringAlign = Center
Text="Click on 'Full Screen' or 'Select Region' to capture a screenshot."
FontColor=10,10,10
FontSize=14
W=380
clipString = 2
Antialias=1
224 changes: 224 additions & 0 deletions SourceCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Rainmeter;

namespace PluginScreenshot
{
internal class Measure
{
private string savePath; // Store the save path from the .ini file
private string finishAction = ""; // Action to execute after screenshot is finished
private Rainmeter.API api; // Store reference to API

public Measure(Rainmeter.API api)
{
this.api = api; // Store API reference
}

public void Reload(Rainmeter.API api, ref double maxValue)
{
// Read the configuration from the .ini file
savePath = api.ReadString("SavePath", ""); // Do not set default path here
finishAction = api.ReadString("ScreenshotFinishAction", "");
}

public void ExecuteBatch(int actionCode)
{
if (actionCode == 1) // Full screenshot
{
TakeScreenshot();
}
else if (actionCode == 2) // Custom screenshot
{
TakeCustomScreenshot();
}
}

private void TakeScreenshot()
{
if (string.IsNullOrEmpty(savePath))
{
api.Log(API.LogType.Error, "Error: No valid SavePath specified in the .ini file.");
return; // If save path is not specified, do nothing
}

// Take a screenshot of the entire screen
Rectangle bounds = Screen.GetBounds(Point.Empty);
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}

SaveImage(bitmap);
}

// Execute the finish action after the screenshot is taken
ExecuteFinishAction();
}

private void TakeCustomScreenshot()
{
if (string.IsNullOrEmpty(savePath))
{
api.Log(API.LogType.Error, "Error: No valid SavePath specified in the .ini file.");
return; // If save path is not specified, do nothing
}

Application.Run(new CustomScreenshotForm(savePath, ExecuteFinishAction));
}

private void SaveImage(Bitmap bitmap)
{
// Always save as PNG format
bitmap.Save(savePath, ImageFormat.Png);
}

private void ExecuteFinishAction()
{
if (!string.IsNullOrEmpty(finishAction))
{
// Trigger the finish action (such as !ActivateConfig)
api.Execute(finishAction); // Use the API instance here
}
}
}

public class CustomScreenshotForm : Form
{
private Point startPoint;
private Rectangle selection;
private string savePath;
private bool isSelecting;
private Action finishAction;

public CustomScreenshotForm(string savePath, Action finishAction)
{
this.savePath = savePath;
this.finishAction = finishAction;
this.DoubleBuffered = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.BackColor = Color.Black;
this.Opacity = 0.25;
this.TopMost = true;
this.Cursor = Cursors.Cross; // Set cursor to crosshair for selection mode
this.MouseDown += new MouseEventHandler(OnMouseDown);
this.MouseMove += new MouseEventHandler(OnMouseMove);
this.MouseUp += new MouseEventHandler(OnMouseUp);
this.Paint += new PaintEventHandler(OnPaint);
}

private void OnMouseDown(object sender, MouseEventArgs e)
{
startPoint = e.Location;
isSelecting = true;
}

private void OnMouseMove(object sender, MouseEventArgs e)
{
if (isSelecting)
{
selection = new Rectangle(
Math.Min(startPoint.X, e.X),
Math.Min(startPoint.Y, e.Y),
Math.Abs(startPoint.X - e.X),
Math.Abs(startPoint.Y - e.Y));
Invalidate();
}
}

private void OnMouseUp(object sender, MouseEventArgs e)
{
isSelecting = false;

// Hide the form before capturing the screenshot to avoid capturing the overlay
this.Hide();
TakeScreenshot(selection);
this.Close(); // Close the form after capture
}

private void OnPaint(object sender, PaintEventArgs e)
{
if (isSelecting)
{
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, selection);
}
}
}

private void TakeScreenshot(Rectangle area)
{
using (Bitmap bitmap = new Bitmap(area.Width, area.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(area.Location, Point.Empty, area.Size);
}

SaveImage(bitmap);
}

// Execute the finish action after the screenshot is taken
finishAction();
}

private void SaveImage(Bitmap bitmap)
{
// Always save as PNG format
bitmap.Save(savePath, ImageFormat.Png);
}
}

public static class Plugin
{
[DllExport]
public static void Initialize(ref IntPtr data, IntPtr rm)
{
Rainmeter.API api = new Rainmeter.API(rm); // Create API instance
data = GCHandle.ToIntPtr(GCHandle.Alloc(new Measure(api))); // Pass API to Measure
}

[DllExport]
public static void Finalize(IntPtr data)
{
GCHandle.FromIntPtr(data).Free();
}

[DllExport]
public static void Reload(IntPtr data, IntPtr rm, ref double maxValue)
{
Measure measure = (Measure)GCHandle.FromIntPtr(data).Target;
measure.Reload(new Rainmeter.API(rm), ref maxValue);
}

[DllExport]
public static double Update(IntPtr data)
{
return 0.0; // No numeric output
}

[DllExport]
public static void ExecuteBang(IntPtr data, IntPtr args)
{
string arguments = Marshal.PtrToStringUni(args);

Measure measure = (Measure)GCHandle.FromIntPtr(data).Target;
if (arguments.StartsWith("ExecuteBatch"))
{
// Parse the action code
string[] parts = arguments.Split(' ');
if (parts.Length == 2 && int.TryParse(parts[1], out int actionCode))
{
measure.ExecuteBatch(actionCode);
}
}
}
}
}
Binary file added x32/Finalshot.dll
Binary file not shown.
Binary file added x64/Finalshot.dll
Binary file not shown.

0 comments on commit a32c30e

Please sign in to comment.