我有这个代码,它使表单始终在顶部,透明并点击。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace HyperBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.TopMost = true; // make the form always on top
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; // hidden border
this.WindowState = FormWindowState.Maximized; // maximized
this.MinimizeBox = this.MaximizeBox = false; // not allowed to be minimized
this.MinimumSize = this.MaximumSize = this.Size; // not allowed to be resized
this.TransparencyKey = this.BackColor = Color.Red; // the color key to transparent, choose a color that you don't use
// Set the form click-through
int initialStyle = GetWindowLong(this.Handle, -20);
SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);
}
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int SetParent(int hWndChild, int hWndNewParent);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(
[MarshalAs(UnmanagedType.LPTStr)] string lpClassName,
[MarshalAs(UnmanagedType.LPTStr)] string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr SetParent(
IntPtr hWndChild, // handle to window
IntPtr hWndNewParent // new parent window
);
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// draw what you want
e.Graphics.FillRectangle(Brushes.Red, new Rectangle((SystemInformation.WorkingArea.Width / 2) - 4, (SystemInformation.WorkingArea.Height / 2) - 20, 8, 40));
e.Graphics.FillRectangle(Brushes.Red, new Rectangle((SystemInformation.WorkingArea.Width / 2) - 20, (SystemInformation.WorkingArea.Height / 2) - 4, 40, 8));
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
IntPtr hwndf = this.Handle;
IntPtr hwndParent = FindWindow("chrome.exe", null);
SetParent(hwndf, hwndParent);
}
}
}
问题是当我绘制图形时,它什么都没画。当坐标在100左右时就可以了。但是当它完成上述方法时,没有任何反应。完全没有像素。有人可以解释为什么会发生这种情况并重新发布一个固定的片段,谢谢。莱恩
答案 0 :(得分:1)
OnPaint
为您的表单提供图形对象,而不是屏幕。您正在根据系统的工作区域填充矩形,而不是表单。您需要调整矩形坐标并将表单放在您希望图形显示的位置。位置为(0, 0)
的矩形是表单客户区的左上角。您还应该能够通过调用基本Form类上公开的ClientRectangle
来访问该矩形。
请查看此问题,以便在表单外绘图:Draw / Paint Outside Form 如果您不想在表单上绘画,那么这应该让您从正确的方向开始,但根据需要重新定位和调整表单可能会更容易。
编辑在调试问题时至少添加某种边框可能是明智之举。这将帮助您查看表单的位置以及表单所在的监视器。然后,您可以在OnPaint中断点检查您的数字,以确保正确创建矩形,但是,确保您在表单的客户区域内绘画应解决您的问题。