Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SetMapMode (gdi32)
 
.
Summary

C# Signature:

[DllImport("gdi32.dll")]
static extern int SetMapMode(IntPtr hdc, int fnMapMode);

Constants:

//Mapping Modes
static int MM_TEXT = 1;
static int MM_LOMETRIC = 2;
static int MM_HIMETRIC = 3;
static int MM_LOENGLISH = 4;
static int MM_HIENGLISH = 5;
static int MM_TWIPS = 6;
static int MM_ISOTROPIC = 7;
static int MM_ANISOTROPIC = 8;

//Minimum and Maximum Mapping Mode values
static int MM_MIN = MM_TEXT;
static int MM_MAX = MM_ANISOTROPIC;
static int MM_MAX_FIXEDSCALE = MM_TWIPS;

User-Defined Types:

MapModes

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

using System;

using System.Runtime.InteropServices; // For calling Windows API functions.

partial class Form1

{

    [DllImport("gdi32.dll")]
    static extern int SetMapMode(IntPtr hDC, int nMapMode);

    [DllImport("gdi32.dll")]
    static extern bool SetViewportOrgEx(IntPtr hDC, int x, int y, Point[] prevPoint);

    [DllImport("gdi32.dll")]
    static extern bool SetWindowOrgEx(IntPtr hDC, int x, int y, Point[] prevPoint);

    [DllImport("gdi32.dll")]
    static extern bool SetViewportExtEx(IntPtr hDC, int nExtentX, int nExtentY, Size[] prevSize);

    [DllImport("gdi32.dll")]
    static extern bool SetWindowExtEx(IntPtr hDC, int nExtentX, int nExtentY, Size[] prevSize);

    [DllImport("Gdi32.dll")]
    public static extern int CreatePen(int nPenStyle, int nWidth, int nColor);

    [DllImport("Gdi32.dll")]
    public static extern int GetStockObject(int nStockBrush);

    [DllImport("Gdi32.dll")]
    public static extern int SelectObject(IntPtr hDC, int hGdiObject);

    [DllImport("Gdi32.dll")]
    public static extern int DeleteObject(int hBitmap);

    [DllImport("Gdi32.dll")]
    public static extern int MoveToEx(IntPtr hDC, int x, int y, int nPreviousPoint);

    [DllImport("Gdi32.dll")]
    public static extern int LineTo(IntPtr hDC, int x, int y);

    [DllImport("Gdi32.dll")]
    public static extern int Rectangle(IntPtr hDC, int nLeft, int nTop, int nRight, int nBottom);

    [DllImport("Gdi32.dll")]
    public static extern bool DPtoLP(IntPtr hdc, [In, Out] Point[] lpPoints, int nCount);

    // Mapping modes.
    const int MM_TEXT = 1;
    const int MM_LOMETRIC = 2;
    const int MM_HIMETRIC = 3;
    const int MM_LOENGLISH = 4;
    const int MM_HIENGLISH = 5;
    const int MM_TWIPS = 6;
    const int MM_ISOTROPIC = 7;
    const int MM_ANISOTROPIC = 8;

    // Gdi stock objects.
    const int WHITE_BRUSH = 0;
    const int LTGRAY_BRUSH = 1;
    const int GRAY_BRUSH = 2;
    const int DKGRAY_BRUSH = 3;
    const int BLACK_BRUSH = 4;
    const int NULL_BRUSH = 5;
    const int HOLLOW_BRUSH = NULL_BRUSH;
    const int WHITE_PEN = 6;
    const int BLACK_PEN = 7;
    const int NULL_PEN = 8;
    const int OEM_FIXED_FONT = 10;
    const int ANSI_FIXED_FONT = 11;
    const int ANSI_VAR_FONT = 12;
    const int SYSTEM_FONT = 13;
    const int DEVICE_DEFAULT_FONT = 14;
    const int DEFAULT_PALETTE = 15;
    const int SYSTEM_FIXED_FONT = 16;

    // Pen styles.
    const int PS_SOLID = 0;
    const int PS_DASH = 1;
    const int PS_DOT = 2;
    const int PS_DASHDOT = 3;
    const int PS_DASHDOTDOT = 4;
    const int PS_NULL = 5;
    const int PS_INSIDEFRAME = 6;
    const int PS_USERSTYLE = 7;
    const int PS_ALTERNATE = 8;
    const int PS_STYLE_MASK = 0x0000000F;
    const int PS_ENDCAP_ROUND = 0x00000000;
    const int PS_ENDCAP_SQUARE = 0x00000100;
    const int PS_ENDCAP_FLAT = 0x00000200;
    const int PS_ENDCAP_MASK = 0x00000F00;
    const int PS_JOIN_ROUND = 0x00000000;
    const int PS_JOIN_BEVEL = 0x00001000;
    const int PS_JOIN_MITER = 0x00002000;
    const int PS_JOIN_MASK = 0x0000F000;
    const int PS_COSMETIC = 0x00000000;
    const int PS_GEOMETRIC = 0x00010000;
    const int PS_TYPE_MASK = 0x000F0000;

     ...
     ...
     ...

}

// ____________ Place the following code inside an event handler: ____________________________

Graphics dc = this.CreateGraphics();

Debug.Assert(dc != null);

GraphicsContainer graphicsContainer = dc.BeginContainer();

IntPtr hDC = dc.GetHdc();

Debug.Assert(hDC != null);

Point[] point = new Point[1];

Size[] size = new Size[1];

double fAspectRatio = (double)(this.ClientRectangle.Bottom) / (double)(this.ClientRectangle.Right);

SetMapMode(hDC, MM_ANISOTROPIC);

SetViewportOrgEx(hDC,

         0,
         this.ClientRectangle.Bottom,    // So that (0,0) is at the bottom left.
         point);

SetWindowOrgEx(hDC,

           0,
           0,
           point);

SetViewportExtEx(hDC,

         this.ClientRectangle.Right,
         -this.ClientRectangle.Bottom, // Negative so that y gets positive as you go up.
         size);

SetWindowExtEx(hDC,

1000,

           (int)(1000 * fAspectRatio),
           size);

// Create a red pen and select it into the device context.

int hPen = CreatePen(PS_SOLID, 1, 0x000000FF);

int hOldPen = SelectObject(hDC, hPen);

// Draw a line.

MoveToEx(hDC, 10, 25, 0);

LineTo(hDC, 990, (int)(990 * fAspectRatio));

// Create a null brush and select it into the device context.

int hBrush = GetStockObject(NULL_BRUSH);

int hOldBrush = SelectObject(hDC, hBrush);

// Draw a rectangle.

Rectangle(hDC, 10, 25, 990, (int)(990 * fAspectRatio));

// Cleanup.

SelectObject(hDC, hOldPen);

SelectObject(hDC, hOldBrush);

DeleteObject(hPen);

dc.ReleaseHdc();

dc.EndContainer(graphicsContainer);

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
SetMapMode on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions