关于颜色
设置form颜色:
public Form1()
{
InitializeComponent();
this.BackColor = System.Drawing.Color.White;//白色
}
然后:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("gdi32.dll")]
public static extern uint GetPixel(IntPtr hDC, int XPos, int YPos);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
IntPtr _DC = GetDC(this.Handle);
System.Drawing.Graphics formGraphics = e.Graphics;
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red, 1);//画笔颜色为红色
formGraphics.DrawLine(myPen, 8, 8 , 14 , 8 );//画一条红线
uint _ColorLong1 = GetPixel(_DC, 9, 9); //提取线上一点的颜色
Color _Color1 = Color.FromArgb((int)_ColorLong1 & 0xFF, (int)(_ColorLong1 & 0xFF00) / 256, (int)(_ColorLong1 & 0xFF0000) / 65536);
if (_Color1.ToArgb().Equals(Color.Black.ToArgb()))//怎么这时_Color1的ARGB的值为(255,255,255,255)而不是红色的值(255,255,0,0)
请问是怎么回事啊,期待大牛们指导,急啊
------解决方案--------------------
private void Form1_Paint(object sender, PaintEventArgs e)
{
IntPtr _DC = Zgke.SysInfo.WindowsAPI.Win32API.GetDC(this.Handle);
System.Drawing.Graphics formGraphics = e.Graphics;
System.Drawing.Pen myPen = new System.Drawing.Pen(Color.FromArgb(255, 255, 0, 0), 1);//画笔颜色为红色
formGraphics.DrawLine(myPen, 8, 8, 14, 8);//画一条红线
// uint _ColorLong1 = GetPixel(_DC, 9, 9); //提取线上一点的颜色
uint _ColorLong1 = Zgke.SysInfo.WindowsAPI.Win32API.GetPixel(_DC, 9, 8); //提取线上一点的颜色
Color _Color1 = Color.FromArgb((int)_ColorLong1 & 0xFF, (int)(_ColorLong1 & 0xFF00) / 256, (int)(_ColorLong1 & 0xFF0000) / 65536);
textBox1.Text = _Color1.ToString();
}
我获取到的是 A255 R255 G0 B0 是没问题的.