Skip to content

A lightweight, zoomable, and pannable custom control for WinForms applications.

License

Notifications You must be signed in to change notification settings

kehan-zhou/viewport-control

Repository files navigation

ViewportControl

License: MIT Platform: WinForms

A lightweight, zoomable, and pannable custom control for WinForms applications.

🎬 Demo Preview

Demo

✨ Features

  • 🖱️ Zoom and pan support
  • 🔁 Coordinate transformation: ScreenToWorld and WorldToScreen
  • 🎨 Custom drawing via Render event
  • 🪶 Lightweight and fast rendering
  • 🧩 Easy to integrate into existing WinForms applications
  • 🧪 Demo app included to showcase real-world usage

📦 Project Structure

ViewportControl/ 
├── assets/ 
├── demos/ 
│  └── ViewportControl.Demo/   # WinForms demo application 
├── src/ 
│  └── ViewportControl/   # The control implementation
├── CODE_OF_CONDUCT.md 
├── .gitignore 
├── LICENSE 
├── README.md
└── ViewportControl.sln

🚀 Getting Started

To get started using ViewportControl in your WinForms application:

Step 1: Open the Solution

Open ViewportControl.sln in Visual Studio.

Step 2: Run the Demo

Build and run the ViewportControl.Demo project located in the demos folder to see the control in action.

⚙️ Usage

You can use ViewportControl just like any other WinForms control. Here's how:

  1. Add a reference to the ViewportControl project in your WinForms application.
  2. Add the Viewport control manually in code, or compile the control into a DLL and add it to your toolbox.

Example

var viewport = new Viewport();
viewport.Dock = DockStyle.Fill;
this.Controls.Add(viewport);

Coordinate Conversion

The control provides easy methods to convert between screen and world coordinates:

PointF worldPoint = viewport.ScreenToWorld(screenPoint);
PointF screenPoint = viewport.WorldToScreen(worldPoint);

Reset View

Can reset zoom and pan to the default state:

viewport.ResetView();

Handling Custom Drawing

Use the Render event for custom drawing:

viewport.Render += (s, e) =>
{
    e.Graphics.DrawRectangle(Pens.Red, new Rectangle(10, 10, 100, 100))
};

💡 Paint Event vs Render Event

Aspect Paint Render
When it occurs Before any coordinate transforms. After coordinate transforms.
Usage For UI elements that don't require world-space coordinates (e.g., static overlays, grid lines, toolbars). For custom drawings that depend on the world space (e.g., zoomable or pannable content).
Transformations Does not take zoom/pan into account. Takes zoom and pan into account.

📄 License

This project is licensed under the MIT License.

You're free to use, modify, and distribute this control in personal and commercial projects.

See the LICENSE file for full details.

🤝 Contributing

Contributions are welcome! Whether it's bug fixes, improvements, or feature requests - your input makes ViewportControl better for everyone.

How to Contribute

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Make your changes and test them thoroughly.
  4. Submit a pull request describing your changes.

Please follow the existing coding style and include appropriate comments and documentation. For major changes, open an issue first to discuss what you'd like to do.

🙏 Acknowledgements

  • Inspired by various custom graphics and CAD-style WinForms applications.
  • Thanks to the .NET community for tools, libraries, and documentation.
  • Special thanks to contributors and users who provide feedback and ideas.

If you've found this control useful, consider giving it a ⭐ on GitHub and sharing it with others!

About

A lightweight, zoomable, and pannable custom control for WinForms applications.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages