I'm working on a base for a small simple game-style program and I'm using Form1_Resize to control the scale and location of the premade controls i've placed. Currently they're just a picturebox and two blank buttons for testing however they scale and work perfect when the screen is being resized in this method.
I added a custom control to place images with transparency, and it functions fine as well however when I resize the screen the image from the custom control will disappear. I know it's to do with it being painted over and not being repainted when the screen is being resized. By telling it to refresh in the form1_Resize method it does reappear after resizing is done but still is gone while being resized. I've tried everything I could find really but this is still mostly new to me and i'm having no luck.
Here are my classes below.
What I have tried:
Form1.cs
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;
namespace List_Game
{
public partial class Form1 : Form
{
int baseFormWidth = 1280;
int baseFormHeight = 720;
int baseStartButtonWidth = 250;
int baseStartButtonHeight = 100;
int baseSettingsButtonWidth = 250;
int baseSettingsButtonHeight = 60;
int windowWidthOffset;
int windowHeightOffset;
int startButtonXOffset;
int startButtonYOffset;
int settingsButtonXOffset;
int settingsButtonYOffset;
public Form1()
{
InitializeComponent();
windowWidthOffset = this.Width - GameWindow.Width;
windowHeightOffset = this.Height - GameWindow.Height;
}
float WidthScale()
{
return (float)this.Width / (float)baseFormWidth;
}
float HeightScale()
{
return (float)this.Height / (float)baseFormHeight;
}
private void Form1_Resize(object sender, EventArgs e)
{
GameWindow.Width = this.Width - windowWidthOffset;
GameWindow.Height = this.Height - windowHeightOffset;
float scaledStartButtonWidth = baseStartButtonWidth * WidthScale();
float scaledStartButtonHeight = baseStartButtonHeight * HeightScale();
StartButton.Width = (int)scaledStartButtonWidth;
StartButton.Height = (int)scaledStartButtonHeight;
float scaledSettingsButtonWidth = baseSettingsButtonWidth * WidthScale();
float scaledSettingsButtonHeight = baseSettingsButtonHeight * HeightScale();
SettingsButton.Width = (int)scaledSettingsButtonWidth;
SettingsButton.Height = (int)scaledSettingsButtonHeight;
startButtonXOffset = GameWindow.Width / 2 - StartButton.Width / 2;
startButtonYOffset = (int)(GameWindow.Height / 1.44) - StartButton.Height / 2;
settingsButtonXOffset = GameWindow.Width / 2 - (SettingsButton.Width / 2);
settingsButtonYOffset = GameWindow.Height - (int)(SettingsButton.Height * 1.25);
Point startButtonLocation = new Point(startButtonXOffset, startButtonYOffset);
Point settingsButtonLocation = new Point(settingsButtonXOffset, settingsButtonYOffset);
StartButton.Location = startButtonLocation;
SettingsButton.Location = settingsButtonLocation;
}
private void Settings_Click(object sender, EventArgs e)
{
}
private void Start_Click(object sender, EventArgs e)
{
}
}
}
ScreenDrawing.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace List_Game
{
abstract public class ScreenDrawing : Panel
{
public ScreenDrawing()
{
this.ResizeRedraw = true;
}
protected Graphics graphics;
abstract protected void OnDraw();
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000020;
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
this.graphics = e.Graphics;
this.graphics.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.AntiAlias;
this.graphics.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
this.graphics.PixelOffsetMode =
System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
this.graphics.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighQuality;
OnDraw();
}
}
}
DrawControl.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace List_Game
{
class DrawControl : ScreenDrawing
{
public DrawControl()
{
this.ResizeRedraw = true;
}
Image displayedImage = null;
int width;
int height;
Rectangle big;
public void SetImage()
{
if (this.BackgroundImage == null)
{
displayedImage = global::List_Game.Properties.Resources.StartButton2;
}
else displayedImage = this.BackgroundImage;
}
protected override void OnDraw()
{
SetImage();
width = displayedImage.Size.Width;
height = displayedImage.Size.Height;
this.Width = width;
this.Height = height;
big = new Rectangle(0, 0, width, height);
this.graphics.DrawImage(displayedImage, big);
}
}
}