A lightweight, zoomable, and pannable custom control for WinForms applications.
- 🖱️ Zoom and pan support
- 🔁 Coordinate transformation:
ScreenToWorld
andWorldToScreen
- 🎨 Custom drawing via
Render
event - 🪶 Lightweight and fast rendering
- 🧩 Easy to integrate into existing WinForms applications
- 🧪 Demo app included to showcase real-world usage
ViewportControl/
├── assets/
├── demos/
│ └── ViewportControl.Demo/ # WinForms demo application
├── src/
│ └── ViewportControl/ # The control implementation
├── CODE_OF_CONDUCT.md
├── .gitignore
├── LICENSE
├── README.md
└── ViewportControl.sln
To get started using ViewportControl
in your WinForms application:
Open ViewportControl.sln
in Visual Studio.
Build and run the ViewportControl.Demo project located in the demos
folder to see the control in action.
You can use ViewportControl
just like any other WinForms control. Here's how:
- Add a reference to the
ViewportControl
project in your WinForms application. - Add the
Viewport
control manually in code, or compile the control into a DLL and add it to your toolbox.
var viewport = new Viewport();
viewport.Dock = DockStyle.Fill;
this.Controls.Add(viewport);
The control provides easy methods to convert between screen and world coordinates:
PointF worldPoint = viewport.ScreenToWorld(screenPoint);
PointF screenPoint = viewport.WorldToScreen(worldPoint);
Can reset zoom and pan to the default state:
viewport.ResetView();
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. |
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.
Contributions are welcome! Whether it's bug fixes, improvements, or feature requests - your input makes ViewportControl better for everyone.
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Make your changes and test them thoroughly.
- 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.
- 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!