如何在PB中利用API绘图
作者:csdn 发布人:admin 发布时间:2004-10-10
浏览次数:788
放入我的网络收藏夹
--------------------------------------------------------------------------------
浏览字体设置: - +
如何在PB中利用API绘图
A :
========================================================================
一、绘制自然线--类似画板中的画笔
========================================================================
//定义global external function
Function ulong GetDC(ulong hwnd) library "user32.dll"
FUNCTION boolean MoveToEx(ulong hwnd,long wx, long wy,ref str_pos prepos2) LIBRARY "Gdi32.dll"
FUNCTION boolean LineTo(ulong hwnd,long wx, long wy) LIBRARY "Gdi32.dll"
FUNCTION ulong SetCapture(ulong hwnd) LIBRARY "user32.dll"
FUNCTION ulong ReleaseCapture() LIBRARY "user32.dll"
FUNCTION boolean DeleteObject(ulong nObjectHandle ) LIBRARY "Gdi32.dll"
//创建结构str_pos
xpos long
ypos long
//新建一个窗体
//窗口中定义实例变量
//起始坐标
ulong il_xstart
ulong il_ystart
//鼠标按下标志
boolean ib_flags
//窗口的mousedown事件
ib_flags = True
SetCapture(handle(This))
//起始坐标
il_xstart = unitstopixels(pointerx(),XUnitsToPixels!)
il_ystart = unitstopixels(pointery(),yUnitsToPixels!)
//窗口的mousemove事件
ulong ul_device
uLong ul_xend
uLong ul_yend
ulong ul_handle
ulong newPen
//取得终止坐标
ul_xend = unitstopixels(pointerx(),XUnitsToPixels!)
ul_yend = unitstopixels(pointery(),YUnitsToPixels!)
//声明数组
str_pos strvar
If ul_xend < This.width And ul_yend < this.height Then
If ib_flags Then
//取得窗口句柄
ul_handle = handle(This)
//获得设备场景
ul_device = GetDC(ul_handle)
//用指定颜色创建一个新画笔
newPen = CreatePen(1,2, rgb(255,0,0))
//为设备场景中描绘的线段指定颜色及样式
SelectObject(ul_device, newPen)
//定位起始坐标
MoveToEx(ul_device, il_xstart, il_ystart, strvar)
//从起始坐标点定位到终止坐标画线
LineTo (ul_device, ul_xend, ul_yend)
//删除画笔
DeleteObject(newPen)
//为下次画线设置起始坐标为当前线的终止坐标
il_xstart = ul_xend
il_ystart = ul_yend
End If
End If
//窗口mouseup事件
ReleaseCapture()
ib_flags = False
//窗口colsequery事件
ulong ul_device
ulong ul_handle
ul_handle = handle(This)
ul_device = GetDC(ul_handle)
//删除场景
DeleteDc(ul_device)
//备注:如果把mousemove事件的代码放到mouseup事件中,则可以绘制一条直线
==================================================================================
二、绘制矩形
//定义global external function
Function ulong GetDC(ulong hwnd) library "user32.dll"
FUNCTION ulong SetCapture(ulong hwnd) LIBRARY "user32.dll"
FUNCTION ulong ReleaseCapture() LIBRARY "user32.dll"
FUNCTION ulong CreatePen(int nPenStyle, int nPenWidth, ulong nColor) LIBRARY "Gdi32.dll"
FUNCTION ulong SelectObject(ulong hdc, ulong nNewObjectHandle) LIBRARY "Gdi32.dll"
FUNCTION boolean DeleteObject(ulong nObjectHandle ) LIBRARY "Gdi32.dll"
FUNCTION ulong Rectangle(ulong hdc,ulong X1,ulong Y1,ulong X2,ulong Y2) LIBRARY "gdi32.dll"
FUNCTION ulong CreateSolidBrush(ulong crColor) LIBRARY "gdi32.dll"
FUNCTION ulong FillRect(ulong hdc,ref str_RECT lpRect,ulong hBrush) LIBRARY "user32.dll"
//删除专用设备场景或信息场景,释放所有相关窗口资源
FUNCTION ulong DeleteDC(ulong hdc) LIBRARY "gdi32.dll"
//创建结构str_rect
left long
top long
right long
buttom long
//创建一窗体
//窗口的实例变量
ulong il_x1
ulong il_y1
boolean ib_flags
//窗口的mousedown事件
ib_flags = True
//设置光标
SetCapture(handle(This))
//记录鼠标起始位置,取得矩形的第一坐标
il_x1 = unitstopixels(pointerx(),XUnitsToPixels!)
il_y1 = unitstopixels(pointery(),yUnitsToPixels!)
//窗口的mousemove事件
ulong l_device
uLong ll_x2
uLong ll_y2
ulong l_handle
ulong hbrush
//取得矩形的第二坐标
ll_x2 = unitstopixels(pointerx(),XUnitsToPixels!)
ll_y2 = unitstopixels(pointery(),YUnitsToPixels!)
If ib_flags Then
//设置光标
SetPointer(Cross!)
l_handle = handle(This)
//指定窗口的设备场景句柄
l_device = GetDC(l_handle)
ulong newPen
//为画笔填充颜色
newPen = CreatePen(1,2, rgb(192,192,192))
newPen = SelectObject(l_device, newPen)
//建立一个虚拟矩形框
Rectangle(l_device,il_x1,il_y1,ll_x2,ll_y2)
//删除画笔
DeleteObject(newpen)
End If
//窗口的mouseup事件
ulong l_device
uLong ll_x2
uLong ll_y2
ulong l_handle
ulong hBrush
ulong newPen
ll_x2 = unitstopixels(pointerx(),XUnitsToPixels!)
ll_y2 = unitstopixels(pointery(),YUnitsToPixels!)
str_rect lstr_rect
lstr_rect.left = il_x1
lstr_rect.top = il_y1
lstr_rect.right = ll_x2
lstr_rect.buttom = ll_y2
If ll_x2 < This.width And ll_y2 < this.height Then
If ib_flags Then
l_handle = handle(This)
l_device = GetDC(l_handle)
//获得设置颜色的刷子句柄
hBrush = CreateSolidBrush(rgb(0,0,255))
//用指定刷子填充指定区域
FillRect(l_device,lstr_rect,hBrush)
//删除刷子句柄
DeleteObject(hBrush)
DeleteDC(l_device)
End If
End If
SetPointer(Arrow!)
ReleaseCapture()
ib_flags = False
//窗口的closequery事件
ulong l_device
ulong l_handle
ulong newpen
l_handle = handle(This)
l_device = GetDC(l_handle)
//删除场景
DeleteDC(l_device)
==================================================================================
三、绘制圆/椭圆
//定义global external function
Function ulong GetDC(ulong hwnd) library "user32.dll"
FUNCTION ulong SetCapture(ulong hwnd) LIBRARY "user32.dll"
FUNCTION ulong ReleaseCapture() LIBRARY "user32.dll"
function ulong CreatePen(int nPenStyle, int nPenWidth, ulong nColor) LIBRARY "Gdi32.dll"
function ulong SelectObject(ulong hdc, ulong nNewObjectHandle) LIBRARY "Gdi32.dll"
function boolean DeleteObject(ulong nObjectHandle ) LIBRARY "Gdi32.dll" LIBRARY "gdi32.dll"
FUNCTION ulong CreateSolidBrush(ulong crColor) LIBRARY "gdi32.dll"
FUNCTION ulong CreateEllipticRgn(ulong X1,ulong Y1,ulong X2,ulong Y2) LIBRARY "gdi32.dll"
FUNCTION ulong FillRgn(ulong hdc,ulong hRgn,ulong hBrush) LIBRARY "gdi32.dll"
FUNCTION ulong Ellipse(ulong hdc,ulong X1,ulong Y1,ulong X2,ulong Y2) LIBRARY "gdi32.dll"
//创建一窗体
//创建窗口实例变量
ulong il_x1
ulong il_y1
boolean ib_flags
//窗口的mousedown事件
ib_flags = True
SetCapture(handle(This))
//取得椭圆第一坐标
il_x1 = unitstopixels(pointerx(),XUnitsToPixels!)
il_y1 = unitstopixels(pointery(),yUnitsToPixels!)
//窗口的mousemove事件
IF ib_flags Then
SetPointer(Cross!)
End If
//窗口的mouseup事件
ulong l_device
uLong ll_x2
uLong ll_y2
ulong l_handle
ulong hBrush
ulong ul_handle
//取得椭圆第二坐标
ll_x2 = unitstopixels(pointerx(),XUnitsToPixels!)
ll_y2 = unitstopixels(pointery(),YUnitsToPixels!)
If ll_x2 < This.width And ll_y2 < this.height Then
If ib_flags Then
l_handle = handle(This)
l_devi