c#

位置:IT落伍者 >> c# >> 浏览文章

C#中利用GDI作图解决异或问题


发布日期:2022年06月03日
 
C#中利用GDI作图解决异或问题

using System;

using SystemDrawing;

using SystemCollections;

using SystemComponentModel;

using SystemWindowsForms;

using SystemData;

using SystemRuntimeInteropServices;

namespace RubberBands

{

/// <summary>

/// Summary description for Form

/// </summary>

public class RubberBandForm : SystemWindowsFormsForm

{

/// API Declarations

private struct POINTAPI {

public int x;

public int y;

}

[DllImport(gdiDLL)]

private static extern Int SetROP(IntPtr hDC

int nDrawMode);

[DllImport(gdidll)]

private static extern bool MoveToEx(IntPtr hDC

int x

int y

POINTAPI lpPoint);

[DllImport(gdidll)]

private static extern bool LineTo(IntPtr hDC

int x

int y);

[DllImport(gdidll)]

private static extern IntPtr CreatePen(int nPenStyle

int nWidth

int crColor);

[DllImport(gdidll)]

private static extern IntPtr SelectObject(IntPtr hDC

IntPtr hObject);

[DllImport(gdidll)]

private static extern bool DeleteObject(IntPtr hObject);

private const int R_NOT = ;

private const int R_XORPEN = ;

private const int PS_DOT = ;

/// <summary>

/// Required designer variable

/// </summary>

private SystemComponentModelContainer components = null;

private SystemDrawingGraphics m_oDrawingSurface;

private POINTAPI m_ptsStart = new POINTAPI();

private POINTAPI m_ptsPrevious;

public RubberBandForm()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//Add any initialization after the InitializeComponent() call

m_oDrawingSurface = thisCreateGraphics();

}

/// <summary>

/// Clean up any resources being used

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

componentsDispose();

}

}

baseDispose( disposing );

}

protected override void OnMouseDown(MouseEventArgs e) {

// Check whether the left mouse button

// has generated the associated event

if (eButton == MouseButtonsLeft) {

// Store the starting point for your rubberband line

m_ptsStartx = eX;

m_ptsStarty = eY;

// Store the previous end point for your rubberband line

m_ptsPrevious = m_ptsStart;

}

baseOnMouseDown (e);

}

protected override void OnMouseMove(MouseEventArgs e) {

// Check if the left mouse button is pressed

if( eButton == MouseButtonsLeft) {

// Declare local variables

// handle to an appropriate device context obtained

// from m_oDrawingSurface

IntPtr hDC;

// previous mix mode; returned from SetROP

Int Mix ;

// temporary variables used for rubberbanding logic

POINTAPI ptsCurrent;

POINTAPI ptsOld = new POINTAPI();

// determines whether MoveToEx and LineTo calls are successful

Boolean Success;

// Store the current end point of your rubberband line

ptsCurrentx = eX;

ptsCurrenty = eY;

// Perform the requisite graphical operations

// Obtain a handle to an appropriate device context

hDC = m_oDrawingSurfaceGetHdc();

// Set the raster mode to invert the screen color

Mix = SetROP(hDC R_NOT);

// Move to the starting point of the rubberband line

Success = MoveToEx(hDC m_ptsStartx m_ptsStarty ptsOld);

// Erase the previous line

Success = LineTo(hDC m_ptsPreviousx m_ptsPreviousy);

// Move to the starting point of the rubberband line

Success = MoveToEx(hDC m_ptsStartx m_ptsStarty ptsOld);

// Draw the new line

Success = LineTo(hDC ptsCurrentx ptsCurrenty);

// Release the obtained handle to a device context

m_oDrawingSurfaceReleaseHdc(hDC);

// Store the current end point for the next erase operation

m_ptsPrevious = ptsCurrent;

}

baseOnMouseMove (e);

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support do not modify

/// the contents of this method with the code editor

/// </summary>

private void InitializeComponent()

{

//

// RubberBandForm

//

thisAutoScaleBaseSize = new SystemDrawingSize( );

thisBackColor = SystemDrawingSystemColorsWindow;

thisClientSize = new SystemDrawingSize( );

thisName = RubberBandForm;

thisText = Using Unmanaged APIs;

}

#endregion

/// <summary>

/// The main entry point for the application

/// </summary>

[STAThread]

static void Main()

{

ApplicationRun(new RubberBandForm());

}

}

}

               

上一篇:C#实现选择排序算法

下一篇:C#枚举中的位运算