
Core Graphic是一套基于C的框架,用于一切绘图 *** 作,UIKit就是基于Core Graphic实现的,因此它可以实现比UIKit更底层的功能。
主要应用在自定义View时,使用 drawRect 方法时的绘画工具
在view上绘制一个图形的方式有很多种,表现形式可能不一样,但其实质步骤都是一样的:
1)获取上下文
2)绘制路径
3)添加路径到上下文
4)修改图形状态参数
5)渲染上下文
常用方法:
常用方法:
常用方法:
在Paint事件中,Paint的成员PaintEventArgs类有Graphics属性,e是PaintEventArgs类的对象。用对象e调用Graphics属性。你的DrawMe方法中没有输入参数PaintEventArgs e,应当用CreateGraphics方法创建图形对象:
Graphics g = thisCreateGraphics(); //窗体获取Graphics对象的引用
Graphics g = controlCreateGraphics(); //控件获取Graphics对象的引用
你这里大概是窗体,因此你的代码改为:
Graphics g = thisCreateGraphics();
gFillRectangle(blueBrush, x, y, width, height);
就可以了。
如果你想要在DrawMe中添加参数e为图形对象,DrawMe应当是某个事件(通常是Paint事件)的处理程序,要声明一个委托和事件,构造事件处理方法,这个事件处理方法是根据你的委托和事件名称,当你敲+=时系统提示你按Tap键,自动给你加上的,并且参数也是系统给你加上的,一般不自己做,在属性框选择合适的事件。
用窗体的backgroundImage属性设置背景,画线的代码不要放在paint事件里面,这样要擦除的话只要用thisinvalidate()就可以,如果你把画线代码放到Paint事件里面的话那是擦不掉的,背景不用担心,它会自动留着的。
在paint事件之外画线必须先获取graphics:
Graphics g=GraphicsFormHWnd(thisHandle);
gDrawLine(……); //参数你自己设置
using System;
using SystemDrawing;
using SystemCollections;
using SystemComponentModel;
using SystemWindowsForms;
using SystemData;
using SystemDrawingImaging;
using SystemRuntimeInteropServices;
namespace LiBoColorPicker
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : SystemWindowsFormsForm
{
// 桌面工作区的尺寸
Size workingArea;
// Form 的初始位置和在左下角,右下角的位置
Point formLoc, ptLeftBottom, ptRightBottom;
private SystemWindowsFormsLabel lblColor;
private SystemWindowsFormsTextBox txtArgb;
private SystemWindowsFormsTimer tmr;
private SystemWindowsFormsToolTip tip;
private SystemComponentModelIContainer components;
public Form1()
{
InitializeComponent();
thisFormBorderStyle = FormBorderStyleFixedToolWindow;
thisStartPosition = FormStartPositionCenterScreen;
Rectangle rect = SystemInformationWorkingArea;
workingArea = new Size(rectWidth, rectHeight);
ptLeftBottom = new Point(0, workingAreaHeight - thisHeight);
ptRightBottom = new Point(workingAreaWidth - thisWidth,
workingAreaHeight - thisHeight);
String tipMsg = "在窗体空白处双击鼠标左键开始取色,按ESC键确定颜色";
tipSetToolTip(this, tipMsg);
tipSetToolTip(lblColor, tipMsg);
tipSetToolTip(txtArgb, tipMsg);
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
componentsDispose();
}
}
baseDispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
thiscomponents = new SystemComponentModelContainer();
thislblColor = new SystemWindowsFormsLabel();
thistmr = new SystemWindowsFormsTimer(thiscomponents);
thistxtArgb = new SystemWindowsFormsTextBox();
thistip = new SystemWindowsFormsToolTip(thiscomponents);
thisSuspendLayout();
//
// lblColor
//
thislblColorBorderStyle = SystemWindowsFormsBorderStyleFixedSingle;
thislblColorLocation = new SystemDrawingPoint(8, 8);
thislblColorName = "lblColor";
thislblColorTabIndex = 0;
//
// tmr
//
thistmrTick += new SystemEventHandler(thistmr_Tick);
//
// txtArgb
//
thistxtArgbBorderStyle = SystemWindowsFormsBorderStyleFixedSingle;
thistxtArgbFont = new SystemDrawingFont("Arial", 105F, SystemDrawingFontStyleRegular, SystemDrawingGraphicsUnitPoint, ((SystemByte)(0)));
thistxtArgbLocation = new SystemDrawingPoint(8, 40);
thistxtArgbName = "txtArgb";
thistxtArgbTabIndex = 1;
thistxtArgbText = "";
thistxtArgbKeyPress += new SystemWindowsFormsKeyPressEventHandler(thistxtArgb_KeyPress);
//
// Form1
//
thisAutoScaleBaseSize = new SystemDrawingSize(6, 14);
thisClientSize = new SystemDrawingSize(115, 70);
thisControlsAdd(thistxtArgb);
thisControlsAdd(thislblColor);
thisName = "Form1";
thisText = "屏幕取色(upto)";
thisDoubleClick += new SystemEventHandler(thisForm1_DoubleClick);
thisMouseEnter += new SystemEventHandler(thisForm1_MouseEnter);
thisResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
ApplicationRun(new Form1());
}
[ DllImport ( "gdi32dll" ) ]
private static extern bool BitBlt (
IntPtr hdcDest, // 目标设备的句柄
int nXDest, // 目标对象的左上角的X坐标
int nYDest, // 目标对象的左上角的X坐标
int nWidth, // 目标对象的矩形的宽度
int nHeight, // 目标对象的矩形的长度
IntPtr hdcSrc, // 源设备的句柄
int nXSrc, // 源对象的左上角的X坐标
int nYSrc, // 源对象的左上角的X坐标
int dwRop // 光栅的 *** 作值
);
[ DllImport ( "gdi32dll" ) ]
private static extern IntPtr CreateDC (
string lpszDriver, // 驱动名称
string lpszDevice, // 设备名称
string lpszOutput, // 无用,可以设定位"NULL"
IntPtr lpInitData // 任意的打印机数据
);
private void Form1_DoubleClick(object sender, EventArgs e)
{
formLoc = thisLocation;
thisLocation = ptRightBottom;
thisTopMost = true;
tmrEnabled = true;
}
private void tmr_Tick(object sender, EventArgs e)
{
// 创建显示器的DC
IntPtr hdlDisplay = CreateDC("DISPLAY", null, null, IntPtrZero);
// 从指定设备的句柄创建新的 Graphics 对象
Graphics gfxDisplay = GraphicsFromHdc(hdlDisplay);
// 创建只有一个象素大小的 Bitmap 对象
Bitmap bmp = new Bitmap(1, 1, gfxDisplay);
// 从指定 Image 对象创建新的 Graphics 对象
Graphics gfxBmp = GraphicsFromImage(bmp);
// 获得屏幕的句柄
IntPtr hdlScreen = gfxDisplayGetHdc();
// 获得位图的句柄
IntPtr hdlBmp = gfxBmpGetHdc();
// 把当前屏幕中鼠标指针所在位置的一个象素拷贝到位图中
BitBlt(hdlBmp, 0, 0, 1, 1, hdlScreen, MousePositionX, MousePositionY, 13369376);
// 释放屏幕句柄
gfxDisplayReleaseHdc(hdlScreen);
// 释放位图句柄
gfxBmpReleaseHdc(hdlBmp);
lblColorBackColor = bmpGetPixel(0, 0); // 获取像素的颜色
txtArgbText = "0x" + lblColorBackColorToArgb()ToString("x")ToUpper();
gfxDisplayDispose();
gfxBmpDispose();
bmpDispose(); // 释放 bmp 所使用的资源
}
private void txtArgb_KeyPress(object sender, KeyPressEventArgs e)
{
// 当按下ESC键时,确定所取的颜色ARGB值
// 注意:只有当窗体处于激活状态时才有效
if (eKeyChar == (char)KeysEscape)
{
tmrEnabled = false;
thisLocation = formLoc;
thisTopMost = false;
txtArgbSelectAll();
}
}
private void Form1_MouseEnter(object sender, EventArgs e)
{
if (thisLocation == ptLeftBottom) //窗体在左下角
{
thisLocation = ptRightBottom;
}
else if (thisLocation == ptRightBottom) // 窗体在右下角
{
thisLocation = ptLeftBottom;
实现原理是启动一个应用程序,通过ProcessID得到窗体句柄,然后对其设定父窗体句柄为本程序某控件句柄(本例是窗体内一个Panel的句柄),这样就达成了内嵌的效果。
新建窗体,上面放置一个Panel控件,名为pnlApp,然后按下面代码编写:
unit frmTestEmbedApp;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TForm1 = class(TForm)
pnlApp: TPanel;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormResize(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hWin: HWND = 0;
implementation
{$R dfm}
type
// 存储窗体信息
PProcessWindow = ^TProcessWindow;
TProcessWindow = record
ProcessID: Cardinal;
FoundWindow: hWnd;
end;
// 窗体枚举函数
function EnumWindowsProc(Wnd: HWND; ProcWndInfo: PProcessWindow): BOOL; stdcall;
var
WndProcessID: Cardinal;
begin
GetWindowThreadProcessId(Wnd, @WndProcessID);
if WndProcessID = ProcWndInfo^ProcessID then begin
ProcWndInfo^FoundWindow := Wnd;
Result := False; // 已找到,故停止 EnumWindows
end
else
Result := True; // 继续查找
end;
// 由 ProcessID 查找窗体 Handle
function GetProcessWindow(ProcessID: Cardinal): HWND;
var
ProcWndInfo: TProcessWindow;
begin
ProcWndInfoProcessID := ProcessID;
ProcWndInfoFoundWindow := 0;
EnumWindows(@EnumWindowsProc, Integer(@ProcWndInfo)); // 查找窗体
Result := ProcWndInfoFoundWindow;
end;
// 在 Panel 上内嵌运行程序
function RunAppInPanel(const AppFileName: string; ParentHandle: HWND; var WinHandle: HWND): Boolean;
var
si: STARTUPINFO;
pi: TProcessInformation;
begin
Result := False;
// 启动进程
FillChar(si, SizeOf(si), 0);
sicb := SizeOf(si);
siwShowWindow := SW_SHOW;
if not CreateProcess(nil, PChar(AppFileName), nil, nil, true,
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, si, pi) then Exit;
// 等待进程启动
WaitForInputIdle(pihProcess, 10000);
// 取得进程的 Handle
WinHandle := GetProcessWindow(pidwProcessID);
if WinHandle > 0 then begin
// 设定父窗体
WindowsSetParent(WinHandle, ParentHandle);
// 设定窗体位置
SetWindowPos(WinHandle, 0, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOZORDER);
// 去掉标题栏
SetWindowLong(WinHandle, GWL_STYLE, GetWindowLong(WinHandle, GWL_STYLE)
and (not WS_CAPTION) and (not WS_BORDER) and (not WS_THICKFRAME));
Result := True;
end;
// 释放 Handle
CloseHandle(pihProcess);
CloseHandle(pihThread);
end;
procedure TForm1FormClose(Sender: TObject; var Action: TCloseAction);
begin
// 退出时向内嵌程序发关闭消息
if hWin > 0 then PostMessage(hWin, WM_CLOSE, 0, 0);
end;
procedure TForm1FormCreate(Sender: TObject);
const
App = 'C:\Windows\Notepadexe';
begin
pnlAppAlign := alClient;
// 启动内嵌程序
if not RunAppInPanel(App, pnlAppHandle, hWin) then ShowMessage('App not found');
end;
procedure TForm1FormResize(Sender: TObject);
begin
// 保持内嵌程序充满 pnlApp
if hWin <> 0 then MoveWindow(hWin, 0, 0, pnlAppClientWidth, pnlAppClientHeight, True);
end;
end
以上就是关于iOS 画布(Core Graphics)全部的内容,包括:iOS 画布(Core Graphics)、C#中Graphics的应用、c#绘图中关于清除自己绘制的部分图形等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)