在调整表单大小时,未绘制C#Winform-Custom Control

时间:2017-12-28 21:37:28

标签: c# winforms resize

我正在为一个简单的小型游戏风格程序工作,我正在使用Form1_Resize来控制我放置的预制控件的比例和位置。目前,它们只是一个图片框和两个用于测试的空白按钮,但是当在此方法中调整屏幕大小时,它们可以扩展并完美地工作。

我添加了一个自定义控件来放置具有透明度的图像,并且它的功能也很好但是当我调整屏幕大小时,自定义控件中的图像将会消失。我知道它是被绘制的,而不是在重新调整屏幕时重新绘制。通过告诉它在form1_Resize方法中刷新它会在调整大小后重新出现但在调整大小时仍然消失。我已经尝试了所有我能找到的东西,但这对我来说仍然是新的,我没有运气。

以下是我的课程。 TestControl基本上是ScreenDrawing和DrawControl类的组合,有一些调整作为测试,但它似乎没有任何区别。

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();
        }
    }
}

}

0 个答案:

没有答案