注册 登录
编程论坛 VFP论坛

drawfocusrect 用法存疑,

sam_jiang 发布于 2022-09-20 00:01, 1654 次点击
哪位兄弟对api比较熟悉,帮我看看我的这段程序那里的逻辑有问题?我得不到正确的矩形。。。
代码如下:
程序代码:

**************************************************
*-- Form:         form1 (d:\documents\visual foxpro 项目\drawrectangle.scx)
*-- ParentClass:  form
*-- BaseClass:    form
*-- Time Stamp:   09/19/22 11:52:08 PM
*
DEFINE CLASS form1 AS form


    Top = 0
    Left = -1
    Height = 650
    Width = 1000
    DoCreate = .T.
    BorderStyle = 3
    Caption = "Form1"
    MaxButton = .F.
    Name = "Form1"


    ADD OBJECT container1 AS container WITH ;
        Top = 0, ;
        Left = 0, ;
        Width = 1000, ;
        Height = 650, ;
        SpecialEffect = 1, ;
        BackColor = RGB(255,255,255), ;
        Name = "Container1"


    ADD OBJECT form1.container1.timer1 AS timer WITH ;
        Top = 10, ;
        Left = 10, ;
        Height = 23, ;
        Width = 23, ;
        Name = "Timer1"


    PROCEDURE long2str
        PARAMETERS longval
        PRIVATE i, retstr
        retstr = ""
        FOR i = 24 TO 0 Step -8
            retstr = CHR(int(longval/(2^i))) + retstr
            longval = mod(longval, (2^i))
        NEXT
        Return retstr
    ENDPROC


    PROCEDURE Resize
        this.container1.Width=this.Width-1
        this.container1.Height=this.Height-1
    ENDPROC


    PROCEDURE container1.MouseMove
        LPARAMETERS nButton, nShift, nXCoord, nYCoord

        this.tox=nxcoord
        this.toy=nycoord


    ENDPROC


    PROCEDURE container1.MouseDown
        LPARAMETERS nButton, nShift, nXCoord, nYCoord

        IF nbutton=1 &&mouse left
            this.x=nxcoord
            this.y=nycoord
        ENDIF
        this.timer1.Interval=100
    ENDPROC


    PROCEDURE container1.Init
        this.AddProperty("X",0)
        this.AddProperty("Y",0)
        this.AddProperty("toX",0)
        this.AddProperty("toY",0)
        this.AddProperty("oldX",0)
        this.AddProperty("oldY",0)
        this.AddProperty("iscleared",.t.)
        STORE 1 TO this.Top,this.left
        this.Height=thisform.Height-1
        this.Width=thisform.Width-1
    ENDPROC


    PROCEDURE container1.MouseUp
        LPARAMETERS nButton, nShift, nXCoord, nYCoord
        this.timer1.Interval=0
        IF this.iscleared=.f.
            lcrect=thisform.long2str(this.x)+thisform.long2str(this.y)+thisform.long2str(this.oldx)+thisform.long2str(this.oldy)
            DrawFocusRect(getdc(thisform.hwnd),lcrect)
            this.iscleared=.t.
        ENDIF
    ENDPROC


    PROCEDURE timer1.Init
        DECLARE Integer DrawFocusRect IN WIN32API ;
        Integer hdc , ;
        String lpRect
        DECLARE Integer GetDC IN WIN32API ;
        Integer hwnd
        DECLARE integer ReleaseDC IN WIN32API integer hwnd,integer hdc
    ENDPROC


    PROCEDURE timer1.Timer
        IF this.parent.tox>this.parent.x and this.parent.toy>this.parent.y
            IF this.Parent.tox#this.Parent.oldx or this.Parent.toy#this.Parent.oldy
                lcrect=thisform.long2str(this.parent.x)+thisform.long2str(this.parent.y)+thisform.long2str(this.parent.tox)+thisform.long2str(this.parent.toy)
                DrawFocusRect(getdc(thisform.hwnd),lcrect)

                *************************************************************************************************
                ACTIVATE SCREEN
                ?this.parent.iscleared &&我观察程序运行过程用
                ??this.parent.x,this.Parent.y,this.parent.oldx,this.parent.oldy,this.parent.tox,this.parent.toy
                *************************************************************************************************
                this.parent.oldx=this.Parent.tox
                this.parent.oldy=this.Parent.toy
                IF this.Parent.iscleared=.f.
                    this.Parent.iscleared=.t.
                ELSE
                    this.Parent.iscleared=.f.
                ENDIF
            ENDIF
        ENDIF
    ENDPROC


ENDDEFINE
*
*-- EndDefine: form1
**************************************************


我的企图: 按下鼠标键,移动鼠标,实时画出一个矩形,鼠标松开后,矩形清除。
14 回复
#2
sam_jiang2022-09-20 00:05
程序运行结果,都不是矩形了。。。
只有本站会员才能查看附件,请 登录
#3
sam_jiang2022-09-20 00:13
DECLARE Integer DrawFocusRect IN WIN32API Integer hdc , String lpRect

这个函数使用相同参数时,第一次画,第二次是擦除。我用计时器来记住上次画的参数,一遍在画新的矩形时,及时擦去旧的矩形。问题可能就出在这里timer1.timer
#4
sych2022-09-20 08:50
form1=CREATEOBJECT("form1")
form1.show()
read even
retu
**************************************************
*-- Form:         form1 (d:\documents\visual foxpro 项目\drawrectangle.scx)
*-- ParentClass:  form
*-- BaseClass:    form
*-- Time Stamp:   09/19/22 11:52:08 PM
*
DEFINE CLASS form1 AS form


    Top = 0
    Left = -1
    Height = 650
    Width = 1000
    DoCreate = .T.
    BorderStyle = 3
    Caption = "Form1"
    MaxButton = .F.
    Name = "Form1"


    ADD OBJECT container1 AS container WITH ;
        Top = 0, ;
        Left = 0, ;
        Width = 1000, ;
        Height = 650, ;
        SpecialEffect = 1, ;
        BackColor = RGB(255,255,255), ;
        Name = "Container1"


    ADD OBJECT timer1 AS timer WITH ;
        Top = 10, ;
        Left = 10, ;
        Height = 23, ;
        Width = 23, ;
        Name = "Timer1"


    PROCEDURE long2str
    PARAMETERS longval
    PRIVATE i, retstr
    retstr = ""
    FOR i = 24 TO 0 Step -8
        retstr = CHR(int(longval/(2^i))) + retstr
        longval = mod(longval, (2^i))
    NEXT
    Return retstr
    ENDPROC


    PROCEDURE Resize
    this.container1.Width=this.Width-1
    this.container1.Height=this.Height-1
    ENDPROC


    PROCEDURE container1.MouseMove
    LPARAMETERS nButton, nShift, nXCoord, nYCoord

    this.tox=nxcoord
    this.toy=nycoord


    ENDPROC


    PROCEDURE container1.MouseDown
    LPARAMETERS nButton, nShift, nXCoord, nYCoord

    IF nbutton=1 &&mouse left
        WITH this
            IF .x#.oldx or .y#.oldy
                lcrect=thisform.long2str(.x)+thisform.long2str(.y)+thisform.long2str(.oldx)+thisform.long2str(.oldy)
                DrawFocusRect(getdc(thisform.hwnd),lcrect)
            endif
            .x=nxcoord
            .y=nycoord
            .oldx=nxcoord
            .oldy=nycoord
        endwith
    ENDIF
    thisform.timer1.Interval=100
    ENDPROC


    PROCEDURE container1.Init
    this.AddProperty("X",0)
    this.AddProperty("Y",0)
    this.AddProperty("toX",0)
    this.AddProperty("toY",0)
    this.AddProperty("oldX",0)
    this.AddProperty("oldY",0)
    this.AddProperty("iscleared",.t.)
    STORE 1 TO this.Top,this.left
    this.Height=thisform.Height-1
    this.Width=thisform.Width-1
    ENDPROC


    PROCEDURE container1.MouseUp
    LPARAMETERS nButton, nShift, nXCoord, nYCoord
    thisform.timer1.Interval=0
*        IF this.iscleared=.f.
*            lcrect=thisform.long2str(this.x)+thisform.long2str(this.y)+thisform.long2str(this.oldx)+thisform.long2str(this.oldy)
*            DrawFocusRect(getdc(thisform.hwnd),lcrect)
*            this.iscleared=.t.
*        ENDIF
    ENDPROC


    PROCEDURE timer1.Init
    DECLARE Integer DrawFocusRect IN WIN32API ;
        Integer hdc , ;
        String lpRect
    DECLARE Integer GetDC IN WIN32API ;
        Integer hwnd
    DECLARE integer ReleaseDC IN WIN32API integer hwnd,integer hdc
    ENDPROC


    PROCEDURE timer1.Timer
    WITH thisform.container1
        IF .tox>.x and .toy>.y
            IF .tox#.oldx or .toy#.oldy
                lcrect=thisform.long2str(.x)+thisform.long2str(.y)+thisform.long2str(.oldx)+thisform.long2str(.oldy)
                DrawFocusRect(getdc(thisform.hwnd),lcrect)
                .oldx=.tox
                .oldy=.toy
                lcrect=thisform.long2str(.x)+thisform.long2str(.y)+thisform.long2str(.oldx)+thisform.long2str(.oldy)
                DrawFocusRect(getdc(thisform.hwnd),lcrect)

*************************************************************************************************
                ACTIVATE SCREEN
*                ?.iscleared &&我观察程序运行过程用
                ??.x,.y,.oldx,.oldy,.tox,.toy
*************************************************************************************************
*                IF .iscleared=.f.
*                    .iscleared=.t.
*                ELSE
*                    .iscleared=.f.
*                ENDIF
            ENDIF
        ENDIF
    endwith
    ENDPROC

    PROCEDURE unload
    CLEAR EVENTS
    ENDPROC
ENDDEFINE
#5
sych2022-09-20 08:55
long2str函数可以用bintoc代替
#6
gs25367856782022-09-20 08:57
弄这个API的,都是高手
#7
吹水佬2022-09-20 10:21
先不用timer,手动按一下按一下试试
#8
sam_jiang2022-09-20 11:47
回复 4楼 sych


非常感谢,看来是我画蛇添足了。
#9
sam_jiang2022-09-20 11:48
回复 7楼 吹水佬
这个方法好,之前我在调试窗口都没法跟踪!
#10
sam_jiang2022-09-20 13:07
回复 5楼 sych
bintoc怎么用?
#11
sych2022-09-20 15:47
clea
form1=CREATEOBJECT("form1")
form1.show()
read even
retu

DEFINE CLASS form1 AS form
    Top = 0
    Left = -1
    Height = 650
    Width = 1000
    DoCreate = .T.
    BorderStyle = 3
    Caption = "Form1"
    MaxButton = .F.
    Name = "Form1"

    ADD OBJECT container1 AS container WITH ;
        Top = 0, ;
        Left = 0, ;
        Width = 1000, ;
        Height = 650, ;
        SpecialEffect = 1, ;
        BackColor = RGB(255,255,255), ;
        Name = "Container1"

    ADD OBJECT timer1 AS timer WITH ;
        Top = 10, ;
        Left = 10, ;
        Height = 23, ;
        Width = 23, ;
        Name = "Timer1"

    PROCEDURE Resize
    this.container1.Width=this.Width-1
    this.container1.Height=this.Height-1
    ENDPROC

    PROCEDURE container1.MouseMove
    LPARAMETERS nButton, nShift, nXCoord, nYCoord
    this.tox=nxcoord
    this.toy=nycoord
    ENDPROC

    PROCEDURE container1.MouseDown
    LPARAMETERS nButton, nShift, nXCoord, nYCoord
    IF nbutton=1 &&mouse left
        WITH this
            .draw()
            .x=nxcoord
            .y=nycoord
            .oldx=nxcoord
            .oldy=nycoord
        endwith
        thisform.timer1.Interval=100
    ENDIF
    ENDPROC

    PROCEDURE container1.Init
    this.AddProperty("X",0)
    this.AddProperty("Y",0)
    this.AddProperty("toX",0)
    this.AddProperty("toY",0)
    this.AddProperty("oldX",0)
    this.AddProperty("oldY",0)
    STORE 1 TO this.Top,this.left
    this.Height=thisform.Height-1
    this.Width=thisform.Width-1
    ENDPROC

    PROCEDURE container1.draw
    WITH this
        IF .x#.oldx or .y#.oldy
            lcrect=bintoc(MIN(.x,.oldx),"4rs")+bintoc(MIN(.y,.oldy),"4rs")+bintoc(MAX(.x,.oldx),"4rs")+bintoc(MAX(.y,.oldy),"4rs")
            DrawFocusRect(getdc(thisform.hwnd),lcrect)
        endif
    ENDWITH
    ENDPROC
   
    PROCEDURE container1.MouseUp
    LPARAMETERS nButton, nShift, nXCoord, nYCoord
    thisform.timer1.Interval=0
    ENDPROC

    PROCEDURE timer1.Init
    DECLARE Integer DrawFocusRect IN WIN32API ;
        Integer hdc , ;
        String lpRect
    DECLARE Integer GetDC IN WIN32API ;
        Integer hwnd
    DECLARE integer ReleaseDC IN WIN32API integer hwnd,integer hdc
    ENDPROC

    PROCEDURE timer1.Timer
    WITH thisform.container1
        IF .tox#.oldx or .toy#.oldy
            .draw()
            .oldx=.tox
            .oldy=.toy
            .Draw()
        ENDIF
    endwith
    ENDPROC

    PROCEDURE unload
    CLEAR EVENTS
    ENDPROC
ENDDEFINE
#12
schtg2022-09-20 18:41
学习啦,谢谢!
#13
sych2022-09-20 20:37
修改后,可以上下左右拖动画框
#14
sam_jiang2022-09-21 18:53
回复 13楼 sych
哈哈,厉害,还出升级版的了。。。
#15
sam_jiang2022-09-30 17:47
回复 11楼 sych
学习你的代码,好像可以不用timer,直接用mousemove也可以实现啊。
1