Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than gdi32, prefix the name with the module name and a period.
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,
SetViewportOrgEx(hDC,
0,
this.ClientRectangle.Bottom, // So that (0,0) is at the bottom left.
point);
SetWindowOrgEx(
hDC,
SetWindowOrgEx(hDC,
0,
0,
point);
SetViewportExtEx(
hDC,
SetViewportExtEx(hDC,
this.ClientRectangle.Right,
-this.ClientRectangle.Bottom, // Negative so that y gets positive as you go up.
size);
SetWindowExtEx(
hDC,
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
The SetMapMode API
6/6/2012 1:10:10 PM - -96.57.19.34
TODO - a short description
5/21/2012 11:11:00 PM - anonymous
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
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).