找回密码
 加入
搜索
查看: 49502|回复: 76

[交流] 第三讲 GDI画笔、线型

 火... [复制链接]
发表于 2013-5-14 19:28:17 | 显示全部楼层 |阅读模式
本帖最后由 seniors 于 2013-6-16 08:04 编辑

3、GDI之画笔pen
本讲要用到的函数
_WinAPI_DrawLine两点间画线
_WinAPI_MoveTo移动当前位置到某点
_WinAPI_LineTo从当前位置画线到某点
_WinAPI_LineDDA;确切的说这不是画线函数,它是从某点到另一点的过程中,每经过一个点调用指定的函数

_WinAPI_Rectangle 在一个矩形区域画矩形
_WinAPI_RoundRect 在矩形区域画圆角矩形
_WinAPI_AngleArc 画弧线从角度到角度
_WinAPI_Arc 画弧线从点到点
_WinAPI_ArcTo 画弧线从点到点,改变当前位置
_WinAPI_Ellipse  在矩形区域画椭圆
上面这些函数都要用到画笔来绘制线条,有些封闭区域的,还会用画刷填充,画刷下一讲再讲。
画笔建立有二种方式
一是用系统画笔:如$hPen = _WinAPI_GetStockObject($BLACK_PEN);黑画笔,$NULL_PEN是空画笔,也就是不会绘制线条
系统定义好的画笔只有黑白空三种,要更多样的就要用第二种方式。有没有记得上次我们用_WinAPI_GetStockObject设置字体。
二是用_WinAPI_CreatePen,它可以指定类型、指定画笔宽度、指定颜色BGR格式
线宽只适用于实心纯色和空画笔(PS_NULL),其它类型即便你设置了也当做宽度是1

实心纯色有两种方式PS_SOLID和PS_INSIDEFRAME
PS_SOLID画线时,是以线宽的早间为准
PS_INSIDEFRAME画封闭图形时,线宽是全部画在图形内;如果不是封闭图形则和PS_SOLID相同

画笔使用也要_WinAPI_SelectObject选择,用好后再_WinAPI_SelectObject换回,如果不用了再_WinAPI_DeleteObject释放

本讲例子中,还讲到了一个不填充封闭矩形的方法

布置作业,画出一个简单的刻度尺,其中刻度线用_WinAPI_LineDDA函数
如果lineDDA看不懂,我隐藏了画刻度线的代码,回复看一下

#include <APIConstants.au3>
#include <WinAPIEx.au3>

GUICreate("第三讲", 300, 200)
$ctrlId = GUICtrlCreatePic("", 0, 0, 300, 200)
$hWnd = GUICtrlGetHandle($ctrlId)
GUISetState()
gditest()

While 1
        $Msg = GUIGetMsg()
        Switch $Msg
                Case -3
                        ExitLoop
        EndSwitch
WEnd
GUIDelete()
Exit

Func gditest()
        $hDC = _WinAPI_GetDC($hWnd)

        $hPen_PS_SOLID = _WinAPI_CreatePen($PS_SOLID, 10, 0x0000FF);红色实线画笔宽度为10
        $hPen_PS_INSIDEFRAME = _WinAPI_CreatePen($PS_INSIDEFRAME, 10, 0x0000FF);红色实线向内画笔宽度为10
        $hPen_PS_DASH0 = _WinAPI_CreatePen($PS_DASH, 0, 0x000000);黑色虚线宽度为0的画笔,实际宽度还是1,如果大于1,则变为实线画笔,宽度大于1只对实线画笔和空画笔
        $hPen_PS_DASH1 = _WinAPI_CreatePen($PS_DASH, 1, 0x000000);黑色虚线宽度为1的画笔
        $hBrush = _WinAPI_GetStockObject($NULL_BRUSH);空画刷
        ;一、和三去对照看
        $oldPen = _WinAPI_SelectObject($hDC, $hPen_PS_SOLID);选择红色实线画笔宽度为10
        $oldBrush = _WinAPI_SelectObject($hDC, $hBrush);选择空画刷,作用是画矩形框时中间不填充,和三、中的矩形框看
        $tRECT = _WinAPI_CreateRect(10, 10, 90, 90);
        _WinAPI_Rectangle($hDC, $tRECT);画矩形框,因为画刷是空的所以中间没有填充颜色
        _WinAPI_DrawLine($hDC, 120, 10, 220, 10);画一条直线和三、画的直线对比看看
        _WinAPI_SelectObject($hDC, $oldPen);
        _WinAPI_SelectObject($hDC, $oldBrush);
        ;二、在一、的基础上画一条宽度为1的虚线,看看一中的宽度10的画笔是怎么画的
        $oldPen = _WinAPI_SelectObject($hDC, $hPen_PS_DASH0);选择宽度为0的虚线
        $oldBrush = _WinAPI_SelectObject($hDC, $hBrush);选择空画刷
        $tRECT = _WinAPI_CreateRect(10, 10, 90, 90);
        _WinAPI_Rectangle($hDC, $tRECT);画矩形
        _WinAPI_DrawLine($hDC, 120, 10, 220, 10);画直线
        _WinAPI_SelectObject($hDC, $oldPen);
        _WinAPI_SelectObject($hDC, $oldBrush);
        ;三、和一的对照看
        $oldPen = _WinAPI_SelectObject($hDC, $hPen_PS_INSIDEFRAME);选择红色实线向内画笔宽度为10
        $tRECT = _WinAPI_CreateRect(10, 110, 90, 190);
        _WinAPI_Rectangle($hDC, $tRECT);
        _WinAPI_DrawLine($hDC, 120, 110, 220, 110);
        _WinAPI_SelectObject($hDC, $oldPen);
        ;四在三的基础上画虚线,看三中的宽度10的画笔是怎么画的
        $oldPen = _WinAPI_SelectObject($hDC, $hPen_PS_DASH1);选择宽度为1的虚线
        $oldBrush = _WinAPI_SelectObject($hDC, $hBrush);
        $tRECT = _WinAPI_CreateRect(10, 110, 90, 190);
        _WinAPI_Rectangle($hDC, $tRECT);
        _WinAPI_DrawLine($hDC, 120, 110, 220, 110);
        _WinAPI_SelectObject($hDC, $oldPen);
        _WinAPI_SelectObject($hDC, $oldBrush);
        ;释放建立的对象
        _WinAPI_DeleteObject($hPen_PS_SOLID)
        _WinAPI_DeleteObject($hPen_PS_INSIDEFRAME)
        _WinAPI_DeleteObject($hPen_PS_DASH0)
        _WinAPI_DeleteObject($hPen_PS_DASH1)
        _WinAPI_DeleteObject($hBrush)
        
        ;画讲解文字
        $hPen = _WinAPI_CreatePen($PS_SOLID, 2, 0x00FFFF)
        $oldPen = _WinAPI_SelectObject($hDC, $hPen)
        _WinAPI_MoveTo($hDC, 90, 10)
        _WinAPI_LineTo($hDC, 110, 50)
        _WinAPI_MoveTo($hDC, 120, 10)
        _WinAPI_LineTo($hDC, 110, 50)
        _WinAPI_MoveTo($hDC, 90, 110)
        _WinAPI_LineTo($hDC, 110, 150)
        _WinAPI_MoveTo($hDC, 120, 110)
        _WinAPI_LineTo($hDC, 110, 150)
        _WinAPI_SelectObject($hDC, $oldPen)
        _WinAPI_DeleteObject($hPen)
        
        $hFont = _WinAPI_GetStockObject($DEFAULT_GUI_FONT)
        _WinAPI_SelectObject($hDC, $hFont)
        _WinAPI_SetBkColor($hDC, 0x339933)
        $tRECT = _WinAPI_CreateRect(110, 50, 290, 90)
        _WinAPI_DrawText($hDC, "PS_SOLID"&@CR&"画笔以中间为准", $tRECT, $DT_EDITCONTROL)
        _WinAPI_SetBkColor($hDC, 0x3399FF)
        $tRECT = _WinAPI_CreateRect(110, 150, 290, 190)
        _WinAPI_DrawText($hDC, "PS_INSIDEFRAME画笔"&@CR&"画直线以中间为准"&@CR&"画封闭图形向内画线", $tRECT, $DT_EDITCONTROL)

        _WinAPI_ReleaseDC($hWnd, $hDC)
EndFunc   ;==>gditest

游客,如果您要查看本帖隐藏内容请回复

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 6威望 +3 金钱 +180 贡献 +40 收起 理由
lixiaolong + 8
楼上风云 + 30 教程结束的时候,你才是事实的富翁哦。
xms77 + 50 + 5 学习学习!
lpxx + 30 + 14 太好了
user3000 + 20 ++

查看全部评分

发表于 2013-5-14 20:40:37 | 显示全部楼层
坐沙发致敬~!
发表于 2013-5-14 21:12:04 | 显示全部楼层
要好好认真学习一下。
发表于 2013-5-14 21:24:03 | 显示全部楼层
继续学习,没抢到沙发~
发表于 2013-5-14 21:31:27 | 显示全部楼层
回复 1# seniors
每天一讲认真学习
发表于 2013-5-14 21:34:34 | 显示全部楼层
没有沙发  首页支持 !
发表于 2013-5-14 22:14:43 | 显示全部楼层
回复 1# seniors


    大虾,你辛苦了。
正在继续消化中....
发表于 2013-5-14 22:37:20 | 显示全部楼层
回复 1# seniors
GDI真的需要花很大的力气来学习啊!
Seniors大侠,为什么GDI画的东西在窗体最小化后会消失呢?窗体回复后要怎样重画图形?
发表于 2013-5-14 22:54:21 | 显示全部楼层
这么多函数,看过了当时能记住每个的用法,如果日后不练习的话,估计很快会忘掉。
主要是一些逻辑搞不清楚,所以标尺画的很乱,还请老师指点。
交作业:

;~ 布置作业,画出一个简单的刻度尺,其中刻度线用_WinAPI_LineDDA函数
;~ 如果lineDDA  看不懂,我隐藏了画刻度线的代码,回复看一下
#include <APIConstants.au3>
#include <WinAPIEx.au3>
#include <WindowsConstants.au3>

Global Const $STM_SETIMAGE = 0x0172
Global Const $STM_GETIMAGE = 0x0173
Global $Count = 0, $n = 0
Global $HBiaoChi = DllCallbackRegister('BiaoChi', 'none', 'int;int;lparam')
Global $BIAOCHI = DllCallbackGetPtr($HBiaoChi)

GUICreate("作业3", @DesktopWidth, 100, -1, -1, $WS_POPUP)
$ctrlId = GUICtrlCreatePic("", 0, 0, @DesktopWidth, 100)
$hWnd = GUICtrlGetHandle($ctrlId)
GUISetState()
WinSetOnTop("作业3", "", 1)
gditest()

While 1
        $Msg = GUIGetMsg()
        Switch $Msg
                Case -3
                        ExitLoop
        EndSwitch
WEnd
GUIDelete()
Exit

Func gditest()
        $hDC = _WinAPI_GetDC($hWnd)
        $hPen = _WinAPI_CreatePen($PS_SOLID, 1, 0x000000)
        _WinAPI_SelectObject($hDC, $hPen)
        $hMemDC = _WinAPI_CreateCompatibleDC($hDC)
        $hBitmap = _WinAPI_CreateCompatibleBitmapEx($hDC, @DesktopWidth, 100, _WinAPI_SwitchColor(_WinAPI_GetSysColor($COLOR_3DFACE)))
        $hMemSv = _WinAPI_SelectObject($hMemDC, $hBitmap)
        _WinAPI_LineDDA(0, 0, @DesktopWidth, 0, $BIAOCHI, $hMemDC)


        _WinAPI_DeleteObject($hPen)
        _WinAPI_ReleaseDC($hWnd, $hDC)
        _WinAPI_SelectObject($hMemDC, $hMemSv)
        _WinAPI_DeleteDC($hMemDC)
        
        _SendMessage($hWnd, $STM_SETIMAGE, 0, $hBitmap)
        $hObj = _SendMessage($hWnd, $STM_GETIMAGE)
        If $hObj <> $hBitmap Then
                _WinAPI_DeleteObject($hBitmap)
        EndIf
EndFunc   ;==>gditest

Func BiaoChi($iX, $iY, $hDC)
        Local $NUM = Mod($Count, 20)
        Switch $NUM
                Case 0
                        _WinAPI_DrawLine($hDC, $iX, $iY, $iX, $iY + 25)
                        _WinAPI_SetBkMode($hDC, $TRANSPARENT)
                        $tRECT = _WinAPI_CreateRect($iX - 8, $iY + 25, $iX + 8, $iY + 45)
                        _WinAPI_DrawText($hDC, $n, $tRECT, $DT_CENTER)
                        $n += 1
                Case 10
                        _WinAPI_DrawLine($hDC, $iX, $iY, $iX, $iY + 15)
        EndSwitch
        $Count += 1
EndFunc   ;==>BiaoChi

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 1金钱 +10 收起 理由
seniors + 10 学习最认真

查看全部评分

 楼主| 发表于 2013-5-15 06:56:09 | 显示全部楼层
回复 8# xms77
不要着急,反正先能用GDI画出来,要一直能显示,只是告诉系统我要显示这个图形,不急啊
 楼主| 发表于 2013-5-15 07:04:28 | 显示全部楼层
回复 9# haijie1223
我不知道其它人是不是能记住每个函数的用法,反正我是记不住。
我只是知道有这样一个功能,到用时再看帮助,反正我不是专业编程的,对不对。

标尺画的不错
发表于 2013-5-15 07:08:54 | 显示全部楼层
回复 11# seniors


    看着帮助,照葫芦画瓢,不过通过你的讲解和做你布置的作业,起码能知道这些函数的用法,收获良多~
 楼主| 发表于 2013-5-15 08:15:21 | 显示全部楼层
回复 12# haijie1223
少个了一个画点函数,还是用lineDDA来画个正弦函数图像

#include <APIConstants.au3>
#include <WinAPIEx.au3>

Global $HBiaoChi = DllCallbackRegister('BiaoChi', 'none', 'int;int;lparam')
Global $BIAOCHI = DllCallbackGetPtr($HBiaoChi)

GUICreate("作业3", 600, 60)
$ctrlId = GUICtrlCreatePic("", 0, 0, 600, 60)
$hWnd = GUICtrlGetHandle($ctrlId)
GUISetState()
WinSetOnTop("作业3", "", 1)
gditest()

While 1
        $Msg = GUIGetMsg()
        Switch $Msg
                Case -3
                        ExitLoop
        EndSwitch
WEnd
GUIDelete()
Exit

Func gditest()
        $hDC = _WinAPI_GetDC($hWnd)
        $hPen = _WinAPI_CreatePen($PS_SOLID, 1, 0x000000)
        _WinAPI_SelectObject($hDC, $hPen)
        _WinAPI_SetBkMode($hDC, $TRANSPARENT)
        _WinAPI_DrawLine($hDC, 0, 30, 600, 30)
        _WinAPI_DrawLine($hDC, 10, 0, 10, 60)
        _WinAPI_TextOut($hDC, 20, 5, "y = 20 * sin(x)")
        _WinAPI_LineDDA(10, 30, 590, 30, $BIAOCHI, $hDC)
        _WinAPI_DeleteObject($hPen)
        _WinAPI_ReleaseDC($hWnd, $hDC)
EndFunc   ;==>gditest

Func BiaoChi($iX, $iY, $hDC)
        _WinAPI_SetPixel($hDC, $iX, 20 * Sin(($iX - 10) / 10) + 30, 0xFF0000)
EndFunc   ;==>BiaoChi

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 1金钱 +30 贡献 +5 收起 理由
haijie1223 + 30 + 5 学习了~

查看全部评分

发表于 2013-5-15 08:36:35 | 显示全部楼层
学习一下GDI知识
发表于 2013-5-15 19:44:32 | 显示全部楼层
回复 1# seniors


    学习了
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-23 16:04 , Processed in 0.079394 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表