C
C#2y ago
xynx

❔ WinForms, Drawing Box on screen that stays

got some errors and looks like the box doesnt update:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WHITEBOX
{
public partial class Form1 : Form
{
[DllImport("User32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("User32.dll")]
public static extern void ReleaseDC(IntPtr hwnd, IntPtr dc);
public Form1()
{
InitializeComponent();
}

private void tmrRefresh_Tick(object sender, EventArgs e)
{
this.Hide();
tmrRefresh.Interval = 1;
IntPtr desktopPtr = GetDC(IntPtr.Zero);
Graphics g = Graphics.FromHdc(desktopPtr);
int iXScreen = 1920;
int iYScreen = 1080;
int iXSize = 240;
int iYSize = 240;
int iX = (iXScreen / 2) - (iXSize / 2);
int iY = (iYScreen / 2) - (iYSize / 2);
Pen P = new Pen(Color.White, 1);
Rectangle rect = new Rectangle(iX, iY, iXSize, iYSize);
g.DrawRectangle(P, rect);
g.Dispose();
ReleaseDC(IntPtr.Zero, desktopPtr);
}

private void Form1_Load(object sender, EventArgs e)
{
this.Hide();
tmrRefresh.Enabled = true;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WHITEBOX
{
public partial class Form1 : Form
{
[DllImport("User32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("User32.dll")]
public static extern void ReleaseDC(IntPtr hwnd, IntPtr dc);
public Form1()
{
InitializeComponent();
}

private void tmrRefresh_Tick(object sender, EventArgs e)
{
this.Hide();
tmrRefresh.Interval = 1;
IntPtr desktopPtr = GetDC(IntPtr.Zero);
Graphics g = Graphics.FromHdc(desktopPtr);
int iXScreen = 1920;
int iYScreen = 1080;
int iXSize = 240;
int iYSize = 240;
int iX = (iXScreen / 2) - (iXSize / 2);
int iY = (iYScreen / 2) - (iYSize / 2);
Pen P = new Pen(Color.White, 1);
Rectangle rect = new Rectangle(iX, iY, iXSize, iYSize);
g.DrawRectangle(P, rect);
g.Dispose();
ReleaseDC(IntPtr.Zero, desktopPtr);
}

private void Form1_Load(object sender, EventArgs e)
{
this.Hide();
tmrRefresh.Enabled = true;
}
}
}
2 Replies
Anchy
Anchy2y ago
my guess is the desktop is being redrawn what is the purpose of this, most people just draw on a form with a transparent background
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.