Jump to content
  • Advertisement
Steven Ford

C# Custom Control Scaling issue in Level Designer (WinForms)

Recommended Posts


I'm currently writing a level designer for my 2D game*. To do so, I'm writing this using c# / WinForms. The editor is designed to look a bit like VS, ie. a set of tool boxes surrounding a central panel to show the various tiles and game objects. I've got this central panel as a custom control (inheriting directly from Control) using custom rendering. The development machine that I'm using is using a 27" 4k display and so I've set Windows up to use a text zoom of 150% to avoid killing my eyes.

Unfortunately, when I use my standard drawing code of (within the OnPaint override):

                    var location = new System.Drawing.Point(
                        offsetX + (tilePair.Key.X * tileLayer.TileSize.Width ),
                        offsetY + (tilePair.Key.Y * tileLayer.TileSize.Height ) );
                    var destRectangle = new Rectangle( location, tileLayer.TileSize );

                    if( pe.ClipRectangle.IntersectsWith( destRectangle ) )
                        pe.Graphics.DrawImage( tilePair.Value.Image,
                            GraphicsUnit.Pixel );

The actual rendering is being done at 150% size of the destRectangle (~96 pixels vs. 64 that's expected). 

This isn't necessarily a problem so long as I can source that scale factor in advance so I can ensure the appropriate resizing / layout code. Googling doesn't lead to much info on how to source this information and there's nothing obvious that I can find in intellisense as to give me pointers as to where to investigate further.

So... does anyone have either any ideas as to how to solve this or links to places where I can investigate this sort of issue.

Thanks in advance


*originally, I was using Tiled - which is rather good, but for various reasons I wanted to write my own. 1. so I can define custom objects with a fixed set of properties for level designers who aren't that technical to use and 2. to use it as a learning exercise around the command pattern.

Share this post

Link to post
Share on other sites

This article may be of some use to you. IIRC you can directly access the display's DPI (dots per inch) settings from a Graphics object in C#/WinForms, but I'd recommend reading the whole article as there are some subtle gotchas in DPI-awareness.

Share this post

Link to post
Share on other sites

Thanks @ApochPiQ.

It's now vaguely working - I'm now needing to get the control embedded within a centring control with scrollbars, but I think that's a slightly different question! :-)

Share this post

Link to post
Share on other sites

You should be able to just drop your control inside a Panel control and call it a day, if I understand your requirements correctly.

Share this post

Link to post
Share on other sites

Unfortunately, just dropping it within a panel, setting that to AutoScroll=true requires my control to be anchored to top, left (if anchored to bottom / right, then the scrollbars disappear). This doesn't handle centring nicely. So currently, I've done the above and said that it's a tool for me and so I can deal with the ugliness.

Given that I want to, shortly, add a zoom feature into the custom control, I guess I'll park this complexity and come back to it. Get the rest of the functionality working before worrying about the prettification

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
  • Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Armaan Gupta
      Hey There,
      I am a developer and Im working on a blockchain based infinite runner type game. Right now, I am working on releasing the beta version with a couple other game developers, but would love to expand the team and have other talented and bright people contributing. The game portion of the project isnt very complicated, and wouldnt require anyone to pull thier hair out for it.
      If you are interested in joining a project, interested in the idea, or would like some more information, please don't hesitate to ask either by commenting, discord (username: Guppy#7625), or by email (armaangupta01@gmail.com).
      Thank you!
    • By bojanzarnoski@gmx.de
      I want to get into coding again by programming a 2D platformer to get started, but i don't know if i should use Java or C# with the unity engine.
      I am pretty fit with Java, but with c# i have to start from scratch. What do you recommend and why?
    • By SickTwistGames
      Ok, firstly, Hi.
      This is my first post on this forum. I am an Indie Dev making my first game so bear with me when I say dumb stuff, I'm on a huge learning curve.
      My first question is about inventory systems for unity. I am trying to make a survival type game with crafting. I have purchased Inventory manager pro by devdog from the unity asset store and it seems like a pretty powerful assett but for an intermediate coder its a little tough to use.  I'm beginning to wonder if it was the right purchase.
      So my question is.... does anyone have any experience of inventory plugins / systems for unity and can anyone reccomend a system to me?
      It needs to have the following: Loot system, crafting system, character sheet, blueprint system,  character stats system. Ideally with as little coding as possible.
    • By ethancodes
      I've got a bug with my brick breaker style game. The bricks move down one line at a time ever 1.5 seconds. What appears to be happening is occasionally the ball will be just about to hit the brick when the brick moves down a line, and now the ball is behind it. I'm not sure how to fix this. I have two ideas but I'm not sure of implementation. 1 solution would be to check where they were and where they are going to be before rendering the frame. Then if they crossed paths, then register the brick as hit. Solution 2 would be change how the bricks move. I could maybe slide them down line by line, instead of a jump down. I'm not sure of this will fix the issue or not. Any ideas?
    • By Scouting Ninja
      Once again Unity is frustrating me to the point of insanity.
      What I am looking for is a way to find a ray intersect with the edges of the mesh, using Unity's already made collision system. I want to point out that I know how to do a line intersect, what I want to know is if Unity supports this already.

      The image above shows how I sweep a ray,intersecting the mesh. The top green image shows what I want and the red shows what Unity is giving me.
      I want to know if there is some way, to find the edges in Unity without creating a custom line intersection tool.
      Most engines I know don't use rays for this but instead use a plane like this:

      I checked the Unity "Plane intersection" but it is just a ray cast. It will still need me to find the vertices on the collision mesh to cast the ray from; if I am doing that then making my own line intersection tool is better.
      I looked online and can find anything on this. Also I don't want to cut the mesh, so I don't need a way to know what side is what.
      Does Unity even have collisions that support edge only detection?
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!