デスクトップでアイコンをクリックすると、アイコンは現在使用されているWindowsテーマに基づいて暗くなります。コントロールをクリックして日陰にする
イメージを表示するカスタムコントロールがあります。私はWindowsアイコンのクリックと同じ機能を持っていたいと思います。カスタムコントロールを選択してWinFormsで同じ結果を得るにはどうすればよいですか?
デスクトップでアイコンをクリックすると、アイコンは現在使用されているWindowsテーマに基づいて暗くなります。コントロールをクリックして日陰にする
イメージを表示するカスタムコントロールがあります。私はWindowsアイコンのクリックと同じ機能を持っていたいと思います。カスタムコントロールを選択してWinFormsで同じ結果を得るにはどうすればよいですか?
今、私は次のコードを使用しています...もし誰かがもっと良いものを持っていれば、私はそれを変更してうれしく思います!
private void drawAndShadeTheImage(Graphics g)
{
//if the image is null then there is nothing to do.
if (Image != null)
{
Bitmap bitMap = new Bitmap(Image);
//if this control is selected, shade the image to allow the user to
//visual identify what is selected.
if (ContainsFocus)
{
//The delta is the percentage of change in color shading of
//the image.
int delta = 70;
//zero is the lowest value (0 - 255) that can be represented by
//a color component.
int zero = 0;
//Get each pixel in the image and shade it.
for (int y = 0; y < bitMap.Height; y++)
{
for (int x = 0; x < bitMap.Width; x++)
{
Color oColor = bitMap.GetPixel(x, y);
//Lime is the background color on the image and should
//always be transparent, if this check is removed the
//background will be displayed.
if (oColor.ToArgb() != Color.Lime.ToArgb())
{
int oR = oColor.R - delta < zero ? zero :
oColor.R - delta;
int oG = oColor.G - delta < zero ? zero :
oColor.G - delta;
int oB = oColor.B - delta < zero ? zero :
oColor.B - delta;
int oA = oColor.A - delta < zero ? zero :
oColor.A - delta;
Color nColor = Color.FromArgb(oA, oR, oG, oB);
bitMap.SetPixel(x, y, nColor);
}
}
}
}
//Make the background of the image transparent.
bitMap.MakeTransparent();
g.DrawImage(bitMap, this.ClientRectangle);
}
}
System.Drawing.KnownColorを使用すると、ユーザーのテーマに適した色を得ることができます。
Windowsは、Windows XP以降、選択されたアイコンのアルファブレンディングを実装しています。あなたが似た外観を実現したい場合には、アルファブレンドを使用して画像を描画する必要があります。
public static void DrawBlendImage(Graphics canvas, Image source, Color blendColor, float blendLevel, int x, int y)
{
Rectangle SourceBounds = new Rectangle(x, y, source.Width, source.Height);
ColorMatrix MaskMatrix = new ColorMatrix();
MaskMatrix.Matrix00 = 0f;
MaskMatrix.Matrix11 = 0f;
MaskMatrix.Matrix22 = 0f;
MaskMatrix.Matrix40 = (float)blendColor.R/byte.MaxValue;
MaskMatrix.Matrix41 = (float)blendColor.G/byte.MaxValue;
MaskMatrix.Matrix42 = (float)blendColor.B/byte.MaxValue;
ImageAttributes MaskAttributes = new ImageAttributes();
MaskAttributes.SetColorMatrix(MaskMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
ColorMatrix TransparentMatrix = new ColorMatrix();
TransparentMatrix.Matrix33 = blendLevel;
ImageAttributes TransparentAttributes = new ImageAttributes();
TransparentAttributes.SetColorMatrix(TransparentMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
canvas.DrawImage(source, SourceBounds, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, MaskAttributes);
canvas.DrawImage(source, SourceBounds, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, TransparentAttributes);
}
をあなたのケースでは、あなたがblendColorとしてSystemColors.Highlightを使用することができます。
これを試してみる機会はありませんでした(現在はコードの別の領域で作業しています)。私はそれをテストした後、答えを受け入れるでしょう。お手伝いありがとう! – Billy
私はこのコードでいくつか演奏しました...私はKnownColor.Highlightがこの例でどのように使われているか分かりません。 – Billy
@Billy、小さな間違い、私はSystemColors.HighlightがKnownColor.Hightlightでないことを意味します。 – arbiter