새 이미지를 만들어 원래 이미지를 오프셋으로 붙여 넣기 만하면됩니다. 그런 다음 Button
의 Image
으로 설정하십시오.
는
예 :
private void button1_MouseDown(object sender, MouseEventArgs e)
{
// replace "button_image.png" with the filename of the image you are using
Image normalImage = Image.FromFile("button_image.png");
Image mouseDownImage = new Bitmap(normalImage.Width + 1, normalImage.Height + 1);
Graphics g = Graphics.FromImage(mouseDownImage);
// this will draw the normal image at an offset on mouseDownImage
g.DrawImage(normalImage, 1, 1); // offset is one pixel each for x and y
// clean up
g.Dispose();
button1.Image = mouseDownImage;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
// reset image to the normal one
button1.Image = Image.FromFile("button_image.png");
}
편집 : 이미지는 '팝'백업 마우스 버튼을 계속 누르고있는 동안 커서가 버튼 영역을 벗어날 때 (참조하지 않을 경우 다음 함수는 노동의 문제를 해결 아래 코멘트) :
private void button1_MouseMove(object sender, MouseEventArgs e)
{
Point relMousePos = e.Location;
bool mouseOverButton = true;
mouseOverButton &= relMousePos.X > 0;
mouseOverButton &= relMousePos.X < button1.Width;
mouseOverButton &= relMousePos.Y > 0;
mouseOverButton &= relMousePos.Y < button1.Height;
if (mouseOverButton != MouseButtons.None)
{
button1_MouseDown(sender, e);
}
else
{
button1_MouseUp(sender, e);
}
}
버튼을 누르고있는 동안 커서를 버튼 바깥으로 움직이면 끝내기 전까지는 효과가 있습니다. 텍스트는 물론 이미지가 아닌 버튼이 다시 팝업됩니다. 또한 일부 그래픽 레이아웃에는 텍스트가 아래/오른쪽으로 이동하지 않으므로 텍스트의 위치를 결정할 수있는 방법이 있어야 좋으므로 이미지의 텍스트와 항상 같은 오프셋이 적용됩니다. – Labbed
@Labbed : 텍스트 오프셋을 얻는 것에 대해 잘 모르겠다면, 아마도 버튼을 비트 맵에 그릴 필요가있을 것입니다 (그 기능은'button1.DrawToBitmap'입니다). 그리고 검은 픽셀을 찾으십시오. 커서를 버튼 영역을 떠날 때 문제를 해결하기 위해 내 대답을 편집합니다. – Timo
고마워,하지만 이제 버튼을 가리키 자마자 MouseDown이 실행됩니다. 검은 색 픽셀을 찾는 것도 좋은 방법이 아닙니다. 텍스트의 색을 바꿀 수 있고 오늘날 대부분의 시스템에서는 컨트롤 텍스트에 ClearType을 사용하므로 시스템 색을 찾을 수 없기 때문입니다. 실제로 버튼에 텍스트를 사용하지 않고 단추로 이미지가있는 다른 프로그램처럼 이미지를 움직 이기만하면됩니다. 나는 .NET이 왜 이상한 행동을하는지 이해하지 못한다. – Labbed