게임 오버레이 기본 개념
게임 실행 화면 상단에 반투명 레이어를 표시하는 기술로, 본 예제에서는 다음 두 가지 핵심 기능을 구현합니다:
- 실시간 FPS 측정: 게임 실행 속도 모니터링
- 단축키 스크린샷: 키 입력 시 화면 캡처 저장
구현 단계
1. 투명 창 생성
public class GameOverlay : Form
{
public GameOverlay()
{
// 창 설정
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = Color.LightGray; // 투명도 적용을 위한 배경색
this.Opacity = 0.7; // 반투명 효과
this.TopMost = true; // 항상 최상위 표시
this.ShowInTaskbar = false;
// 기본 크기 설정
this.Size = new Size(200, 80);
}
}
2. FPS 계산 로직
private System.Diagnostics.Stopwatch frameTimer = new System.Diagnostics.Stopwatch();
private int frameCount;
private float currentFPS;
void UpdateFrameRate()
{
if (!frameTimer.IsRunning)
{
frameTimer.Start();
return;
}
frameCount++;
// 500ms마다 FPS 갱신
if (frameTimer.ElapsedMilliseconds > 500)
{
currentFPS = (float)frameCount / frameTimer.Elapsed.TotalSeconds;
frameCount = 0;
frameTimer.Restart();
// 화면 갱신
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// FPS 값 화면 출력
e.Graphics.DrawString($"FPS: {currentFPS:0.0}",
new Font("Arial", 12),
Brushes.White,
new Point(10, 10));
}
3. 스크린샷 기능
private void CaptureScreen()
{
try
{
Rectangle screenArea = Screen.PrimaryScreen.Bounds;
using (Bitmap screenshot = new Bitmap(screenArea.Width, screenArea.Height))
{
using (Graphics graphics = Graphics.FromImage(screenshot))
{
graphics.CopyFromScreen(Point.Empty, Point.Empty, screenArea.Size);
}
// 문서 폴더에 저장
string savePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
$"screenshot_{DateTime.Now:yyyyMMddHHmmss}.png"
);
screenshot.Save(savePath, System.Drawing.Imaging.ImageFormat.Png);
}
}
catch (Exception ex)
{
Debug.WriteLine($"캡처 실패: {ex.Message}");
}
}
// 키 이벤트 핸들러
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.F12) // F12 키 입력 시
{
CaptureScreen();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
주요 문제 해결 방법
- 창 투명도 문제: BackColor를 LightGray로 설정하고 Opacity 0.5~0.8 권장
- FPS 값 불안정: 0.5초 간격 평균값 계산으로 안정화
- 스크린샷 저장 오류: Environment.SpecialFolder로 안전한 저장 경로 사용
기능 확장 아이디어
- FPS 임계값별 색상 변경(30fps 미만: 빨강)
- 스크린샷 파일명에 자동 타임스탬프 추가
- OpenHardwareMonitor 라이브러리 연동으로 CPU/GPU 온도 모니터링