Managed C++ and .NET Development: Visual Studio .NET 2003 Edition
GDI+ Coordinate Systems
When you rendered the strings earlier, you placed them where they were supposed to be on the screen by specifying pixel distances from the top-left corner, increasing the X-axis when moving to the right and increasing the Y-axis when moving down to the bottom (see Figure 11-5).
A key aspect of GDI+ is that it is supposed to be device independent. How can that be, if everything is rendered based on a pixel standard? Pixels are only one of several coordinate systems supported by GDI+ (see Table 11-4). For example, instead of coordinate (100, 100), meaning 100 pixels to the right and 100 pixels down, the meaning could be 100 millimeters to the right and 100 millimeters down. To change the coordinate system to be based on a different unit of measure, you need to change the PageUnit property of the Graphics class to a different GraphicsUnit.
SYSTEM | DESCRIPTION |
---|---|
Display | Specifies 1/75 of an inch as a unit of measure |
Document | Specifies 1/300 of an inch as a unit of measure |
Inch | Specifies 1 inch as a unit of measure |
Millimeter | Specifies 1 millimeter as a unit of measure |
Pixel | Specifies 1 pixel as a unit of measure |
Point | Specifies a printer's point or 1/72 of an inch as a unit of measure |
It is also possible to move the origin (0, 0) away from the top-left corner to somewhere else the drawing surface. This requires you to translate the origin (0, 0) to where you want it located using the Graphics class's TranslateTransform() method.
The example in Listing 11-6 changes the unit of measure to millimeter and shifts the origin to (20, 20).
Listing 11-6: Changing the Unit of Measure and the Origin
namespace NewUnitsOrigin { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public __gc class Form1 : public System::Windows::Forms::Form { public: Form1(void) //... protected: void Dispose(Boolean disposing) //... private: System::ComponentModel::Container * components; void InitializeComponent(void) { this->AutoScaleBaseSize = System::Drawing::Size(6, 15); this->ClientSize = System::Drawing::Size(442, 265); this->Name = S"Form1"; this->Text = S"Millimeter Unit of measure Origin (20,20)"; this->Paint += new System::Windows::Forms::PaintEventHandler(this, Form1_Paint); } private: System::Void Form1_Paint(System::Object * sender, System::Windows::Forms::PaintEventArgs * e) { Graphics *g = e->Graphics; // Draw a rectangle before unit of measure and origin change g->DrawRectangle(Pens::Black, 5, 5, 50, 20); // Draw same rectangle after change g->PageUnit = GraphicsUnit::Millimeter; g->TranslateTransform(20, 20); g->DrawRectangle(Pens::Black, 5, 5, 50, 20); } }; }
As you can see in NewUnitsOrigin.exe, it is possible to use multiple types of units of measure and origins within the same Paint event handler. Figure 11-6 displays a small rectangle, which was generated by the default pixel unit of measure and origin. The larger and thicker lined rectangle is what was generated when the unit of measure was changed to millimeter and origin was moved to (20, 20).
You should notice a couple of things in this example. First, the client size still uses pixel width and height. There is no PageUnit property for a form. Second, when you change the PageUnit of the Graphics class, all rendering from that point is changed to the new unit of measure. This is true even for the width of lines. Pens::Black creates lines 1 unit thick. When the unit is millimeters, Pens::Black will end up creating a line 1 millimeter thick.
Категории