為啥我視窗背景顏色都一樣,但是不同點獲得的argb值都不同啊???
下面有圖,不同的倆點,獲取的像素顏色argb值都不一樣。。救救孩子



代碼如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace 視窗測驗
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
[DllImport("gdi32.dll")]
public static extern System.UInt32
GetPixel(IntPtr hdc, int xPos, int yPos);
[DllImport("user32.dll")]
public static extern IntPtr
GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
private static extern IntPtr
ReleaseDC(IntPtr hc, IntPtr hDest);
public Color GetPixelColor(int x, int y)//函式
{
IntPtr hdc = GetDC(IntPtr.Zero);
uint pixel = GetPixel(hdc, x, y);
ReleaseDC(IntPtr.Zero, hdc);
Color color = Color.FromArgb((int)(pixel & 0x000000FF), (int)(pixel & 0x0000FF00) >> 8, (int)(pixel & 0x00FF0000) >> 16);
color = Color.FromArgb(color.ToArgb());
return color;
}
List<Point> lp = new List<Point>();
private void Form1_MouseDown_1(object sender, MouseEventArgs e)
{
Graphics g = this.CreateGraphics();
g.FillRectangle(Brushes.Red, e.X, e.Y, 5, 5);
lp.Add(e.Location);
label1.Text = GetPixelColor(e.X, e.Y).ToString();
g.Dispose();
}
}
}

uj5u.com熱心網友回復:
g.FillRectangle(Brushes.Red, e.X, e.Y, 5, 5);lp.Add(e.Location);
label1.Text = GetPixelColor(e.X, e.Y).ToString();
Form1_MouseDown_1得到的坐標(紅色部分)是相對于Form1的(具體說Form1的客戶區)。
GetPixel用的HDC用IntPtr.Zero(Windows桌面)的設備場境。它的坐標是相對于螢屏的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/94773.html
標籤:C#
上一篇:VS19連接Sql17時雖然連接成功,但在選擇資料庫物件的時候發生錯誤!
下一篇:c#表單
