[07-02] 在.NET中使用API函数画弹性线(C#语言)
使用API函数,做的一个.NET装配件(*.dll),调试时,MoveToEx函数会出现警告:调试堆栈不对称,查了很多资料也找不到解答的方法,画弹性线的效果还可以,与前一篇的实例结构差不多,类的构造函数的初始化参数也差不多,但本质上发生了改变,
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
namespace NewQueryGDI_
{
public class RubberLine
{
[DllImport("gdi32.DLL")]
private static extern Int32 SetROP2(IntPtr hDC, int nDrawMode);
[DllImport("gdi32.dll")]
private static extern bool MoveToEx(IntPtr hDC, int x, int y, Point lpPoint);
[DllImport("gdi32.dll")]
private static extern bool LineTo(IntPtr hDC, int x, int y);
[DllImport("gdi32.dll")]
private static extern IntPtr CreatePen(int nPenStyle, int nWidth, int crColor);
[DllImport("gdi32.dll")]
private static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
[DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);
/////////////////////////////////////////////////////////////////
// //#define R2_BLACK 1 /* 0 */ 画图模式
// //#define R2_MASKNOTPEN 3 /* DPna */
// //#define R2_NOTCOPYPEN 4 /* PN */
// //#define R2_MASKPENNOT 5 /* PDna */
// //#define R2_NOT 6 /* Dn */
// //#define R2_XORPEN 7 /* DPx */
// //#define R2_NOTMASKPEN 8 /* DPan */
// //#define R2_MASKPEN 9 /* DPa */
// //#define R2_NOTXORPEN 10 /* DPxn */
// //#define R2_NOP 11 /* D */
// //#define R2_MERGENOTPEN 12 /* DPno */
// //#define R2_COPYPEN 13 /* P */
// //#define R2_MERGEPENNOT 14 /* PDno */
// //#define R2_MERGEPEN 15 /* DPo */
// //#define R2_WHITE 16 /* 1 */
// //#define R2_LAST 16
/////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
// //* Pen Styles */ 笔形
// //#define PS_SOLID 0 PS_SOLID 画笔画出的是实线
// //#define PS_DASH 1 /* ------- */ PS_DASH 画笔画出的是虚线(nWidth必须是1)
// //#define PS_DOT 2 /* ....... */ PS_DOT 画笔画出的是点线(nWidth必须是1)
// //#define PS_DASHDOT 3 /* _._._._ */ PS_DASHDOT 画笔画出的是点划线(nWidth必须是1)
// //#define PS_DASHDOTDOT 4 /* _.._.._ */ PS_DASHDOTDOT 画笔画出的是点-点-划线(nWidth必须是1)
// //#define PS_NULL 5 PS_NULL 画笔不能画图
// //#define PS_INSIDEFRAME 6 PS_INSIDEFRAME 画笔在由椭圆、矩形、圆角矩形、饼图以及弦等生成的封闭对象框中画图。如指定的准确RGB颜色不存在,就进行抖动处理
// //#define PS_USERSTYLE 7
// //#define PS_ALTERNATE 8
// //#define PS_STYLE_MASK 0x0000000F
//
// //#define PS_ENDCAP_ROUND 0x00000000
// //#define PS_ENDCAP_SQUARE 0x00000100
// //#define PS_ENDCAP_FLAT 0x00000200
// //#define PS_ENDCAP_MASK 0x00000F00
//
// //#define PS_JOIN_ROUND 0x00000000
// //#define PS_JOIN_BEVEL 0x00001000
// //#define PS_JOIN_MITER 0x00002000
// //#define PS_JOIN_MASK 0x0000F000
//
// //#define PS_COSMETIC 0x00000000
// //#define PS_GEOMETRIC 0x00010000
// //#define PS_TYPE_MASK 0x000F0000
////////////////////////////////////////////////////////////////////////
private const int R2_NOT = 6;
private const int R2_XORPEN = 7;
private Point p1 = Point.Empty;
private Point p2 = Point.Empty;
protected Control parent;
private int nPenStyle;
private IntPtr hDC;
private int nWidth;
private int crColor;
private IntPtr hObject;
public RubberLine(Control parent, Point prepoint, Graphics currgra, int nPenStyle)
{
this.p1 = prepoint;
this.parent = parent;
hDC = currgra.GetHdc();
parent.Capture = true;
Cursor.Clip = parent.RectangleToScreen(parent.ClientRectangle);
p2 = p1;
this.nPenStyle = nPenStyle;
this.nWidth = 1;
this.crColor = 255;
}
public RubberLine(Control parent, Point prepoint, Graphics currgra, int nPenStyle, int nWidth, int crColor)
{
this.p1 = prepoint;
this.parent = parent;
hDC = currgra.GetHdc();
parent.Capture = true;
Cursor.Clip = parent.RectangleToScreen(parent.ClientRectangle);
p2 = p1;
this.nPenStyle = nPenStyle;
this.nWidth = nWidth;
this.crColor = crColor;
}
public void End()
{
Draw();
Cursor.Clip = Rectangle.Empty;
parent.Capture = false;
p1 = Point.Empty;
p2 = Point.Empty;
}
public void ResizeTo(Point point)
{
Int32 Mix;
Mix = SetROP2(hDC, R2_NOT);
hObject = CreatePen(nPenStyle, nWidth, crColor);
hObject = SelectObject(hDC, hObject);
Draw();
p2 = point;
Draw();
}
protected void Draw()
{
Point ptsOld = new Point();
bool Success;
Success = MoveToEx(hDC, p1.X, p1.Y, ptsOld);
Success = LineTo(hDC, p2.X, p2.Y);
}
public void Delet_HDC(Graphics currgra)
{
currgra.ReleaseHdc(hDC);
bool Success = DeleteObject(hObject);
}
}
}