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. TestControl is basically a combination of the ScreenDrawing and DrawControl classes with a few tweaks as a test however it didn't seem to make any difference at all.
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;
// When resized, difference of form size and window size
int windowWidthOffset;
int windowHeightOffset;
//Position of button box to center it when scaling screen
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
{
protected Graphics graphics;
abstract protected void OnDraw();
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Update the private member so we can use it in the OnDraw method
this.graphics = e.Graphics;
// Set the best settings possible (quality-wise)
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;
// Calls the OnDraw subclass method
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
{
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();
// Sets the images' sizes and positions
width = displayedImage.Size.Width;
height = displayedImage.Size.Height;
this.Width = width;
this.Height = height;
big = new Rectangle(0, 0, width, height);
// Draws the two images
this.graphics.DrawImage(displayedImage, big);
}
}
}
TestControl.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
{
public class TransparentControl : Control
{
private readonly Timer refresher;
private Image _image = global::List_Game.Properties.Resources.StartButton2;
public TransparentControl()
{
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
BackColor = Color.Transparent;
refresher = new Timer();
refresher.Tick += TimerOnTick;
refresher.Interval = 50;
refresher.Enabled = true;
refresher.Start();
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnMove(EventArgs e)
{
RecreateHandle();
}
protected override void OnPaint(PaintEventArgs e)
{
if (_image != null)
{
e.Graphics.DrawImage(_image, (Width / 2) - (_image.Width / 2), (Height / 2) - (_image.Height / 2));
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
//Do not paint background
if (_image != null)
{
e.Graphics.DrawImage(_image, (Width / 2) - (_image.Width / 2), (Height / 2) - (_image.Height / 2));
}
}
//Hack
public void Redraw()
{
RecreateHandle();
}
private void TimerOnTick(object source, EventArgs e)
{
RecreateHandle();
refresher.Stop();
}
public Image Image
{
get
{
return _image;
}
set
{
_image = global::List_Game.Properties.Resources.StartButton2;
RecreateHandle();
}
}
}
}
User contributions licensed under CC BY-SA 3.0