标题:gridhittest函数的用法
只看楼主
sam_jiang
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:542
专家分:781
注 册:2021-10-13
结帖率:97.44%
已结贴  问题点数:20 回复次数:4 
gridhittest函数的用法
grid有个重要的函数,gridhittest,烦恼的是,每次运用总会碰到无法得到正确的输出参数。。。

有没有比较了解这个函数的兄弟,帮忙解释一下?

1,点击header的时候,nwhere_out参数为0,按理应该返回1
2,点击Cell的时候,nRow_out可以得到正确的参数,nCol_out=0(按理赢显示第几列),nwhere_out=0(按理应该为3)
3,有数据时,这个函数在数据区不输出nRow_out,nCol_out,nwhere_out
4,鼠标移动到header之间是,nwhere_out应该为2,结果也是0

贴上测试代码
程序代码:
**************************************************
*-- Form:         form1 (d:\documents\visual foxpro 项目\gridhittest.scx)
*-- ParentClass:  form
*-- BaseClass:    form
*-- Time Stamp:   09/14/22 09:17:00 PM
*
DEFINE CLASS form1 AS form


    Top = 1
    Left = 0
    Height = 504
    Width = 681
    DoCreate = .T.
    Caption = "Form1"
    Name = "Form1"


    ADD OBJECT grid1 AS grid WITH ;
        Height = 288, ;
        Left = 12, ;
        Top = 12, ;
        Width = 516, ;
        Name = "Grid1"


    ADD OBJECT label1 AS label WITH ;
        Caption = "点击位置", ;
        Height = 16, ;
        Left = 12, ;
        Top = 312, ;
        Width = 54, ;
        Name = "Label1"


    ADD OBJECT label2 AS label WITH ;
        Caption = "行号", ;
        Height = 16, ;
        Left = 12, ;
        Top = 336, ;
        Width = 38, ;
        Name = "Label2"


    ADD OBJECT label3 AS label WITH ;
        Caption = "列号", ;
        Height = 16, ;
        Left = 12, ;
        Top = 360, ;
        Width = 38, ;
        Name = "Label3"


    ADD OBJECT label4 AS label WITH ;
        Caption = "所在窗格", ;
        Height = 16, ;
        Left = 12, ;
        Top = 384, ;
        Width = 54, ;
        Name = "Label4"


    ADD OBJECT text1 AS textbox WITH ;
        Height = 20, ;
        Left = 72, ;
        Top = 312, ;
        Width = 100, ;
        Name = "Text1"


    ADD OBJECT text2 AS textbox WITH ;
        Height = 20, ;
        Left = 72, ;
        Top = 333, ;
        Width = 100, ;
        Name = "Text2"


    ADD OBJECT text3 AS textbox WITH ;
        Height = 20, ;
        Left = 72, ;
        Top = 354, ;
        Width = 100, ;
        Name = "Text3"


    ADD OBJECT text4 AS textbox WITH ;
        Height = 20, ;
        Left = 72, ;
        Top = 375, ;
        Width = 100, ;
        Name = "Text4"


    ADD OBJECT label5 AS label WITH ;
        Caption = "当前X:", ;
        Height = 16, ;
        Left = 240, ;
        Top = 310, ;
        Width = 38, ;
        Name = "Label5"


    ADD OBJECT label6 AS label WITH ;
        Caption = "当前Y:", ;
        Height = 16, ;
        Left = 240, ;
        Top = 340, ;
        Width = 38, ;
        Name = "Label6"


    ADD OBJECT text5 AS textbox WITH ;
        Height = 20, ;
        Left = 290, ;
        Top = 310, ;
        Width = 100, ;
        Name = "Text5"


    ADD OBJECT text6 AS textbox WITH ;
        Height = 20, ;
        Left = 290, ;
        Top = 340, ;
        Width = 100, ;
        Name = "Text6"


    ADD OBJECT text7 AS textbox WITH ;
        Height = 31, ;
        Left = 290, ;
        Top = 370, ;
        Width = 151, ;
        Name = "Text7"


    ADD OBJECT text8 AS textbox WITH ;
        Height = 20, ;
        Left = 490, ;
        Top = 340, ;
        Width = 100, ;
        Name = "Text8"


    ADD OBJECT text9 AS textbox WITH ;
        Height = 20, ;
        Left = 490, ;
        Top = 370, ;
        Width = 100, ;
        Name = "Text9"


    PROCEDURE MouseMove
        LPARAMETERS nButton, nShift, nXCoord, nYCoord
        thisform.text8.Value=nxcoord
        thisform.text9.Value=nycoord
    ENDPROC


    PROCEDURE grid1.MouseMove
        LPARAMETERS nButton, nShift, nXCoord, nYCoord
        LOCAL nXCoord, nYCoord, nWhere_Out, nRow_Out, nCol_Out, nView_Out
        nwhere_out=0
        nrow_out=0
        ncol_out=0
        nview_out=0
        tf=this.GridHitTest(nXCoord, nYCoord, @nWhere_Out, @nRow_Out, @nCol_Out, @nView_Out)
        IF tf=.t.
        thisform.text1.Value=nwhere_out
        thisform.text2.Value=nrow_out
        thisform.text3.Value=ncol_out
        thisform.text4.Value=nview_out
        ENDIF

        thisform.text5.Value=nxcoord
        thisform.text6.Value=nycoord
        thisform.CurrentX=nxcoord
        thisform.CurrentY=nycoord
    ENDPROC


    PROCEDURE grid1.Init
        temptable=SYS(2015)
        CREATE TABLE &temptable FREE (颜色 c(30), XS N(5),S N(5),M N(5),L N(5),XL N(5),XXL N(5),TOTAL n(6))
        this.RecordMark=.t.
        this.DeleteMark=.f.
        this.SetAll('alignment',2,'header')
        this.headerheight=35
        this.RowHeight=25
        this.recordsource=temptable
        this.AllowAddNew=.t.

        APPEND BLANK
    ENDPROC


    PROCEDURE grid1.Click
        nxcoord=thisform.CurrentX
        nycoord=thisform.CurrentY

        STORE 0 TO nwhere_out,nrow_out,ncol_out,nview_out
        isclicked=this.GridHitTest(nXCoord, nYCoord, @nWhere_Out, @nRow_Out, @nCol_Out, @nView_Out)
        IF isclicked
            thisform.text1.Value=nwhere_out
        ENDIF

    ENDPROC


    PROCEDURE text7.MouseMove
        LPARAMETERS nButton, nShift, nXCoord, nYCoord
        *NODEFAULT

        thisform.text8.Value=nxcoord
        thisform.text9.Value=nycoord
    ENDPROC


ENDDEFINE
*
*-- EndDefine: form1
**************************************************
搜索更多相关主题的帖子: WITH Left Name ADD Top 
2022-09-14 21:47
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:5 
先观察一下调用GridHitTest的事件有无如常触发
GRID的状态不同,有些GRID的事件未必会触发
2022-09-15 12:13
csyx
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:29
帖 子:484
专家分:1827
注 册:2018-3-13
得分:15 
因为题头和单元格上面都有的其他对象,它们的事件覆盖了表格的同名事件
例如:在移动到题头上时, 触发的是 header.mousemove,不会触发 grid.mousemove,同理 ...

在 grid.init 最后加上这几句试试
For each oo in this.columns
    BindEvent(oo, 'MouseMove', this, 'MouseMove')
    BindEvent(oo.Header1, 'MouseMove', this, 'MouseMove')
EndFor

2022-09-15 14:10
sam_jiang
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:542
专家分:781
注 册:2021-10-13
得分:0 
以下是引用csyx在2022-9-15 14:10:58的发言:

因为题头和单元格上面都有的其他对象,它们的事件覆盖了表格的同名事件
例如:在移动到题头上时, 触发的是 header.mousemove,不会触发 grid.mousemove,同理 ...

在 grid.init 最后加上这几句试试
For each oo in this.columns
    BindEvent(oo, 'MouseMove', this, 'MouseMove')
    BindEvent(oo.Header1, 'MouseMove', this, 'MouseMove')
EndFor


确实如你所说,当鼠标移动或点击header和Cell时,触发的是header或cell的鼠标事件,而不是grid的鼠标事件,怪不得gridhittest函数没法得到正确的结果,grid压根就没有收到鼠标事件。

在grid的init事件加入以下代码就完美解决了我的疑惑,非常感谢!
For each ocolumn in this.columns
    BindEvent(ocolumn, 'MouseMove', this, 'MouseMove')
    FOR i=1 TO 2
        BINDEVENT(ocolumn.controls(i),"MouseMove",ocolumn,"MouseMove")
    ENDFOR
EndFor
2022-09-15 23:15
sam_jiang
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:542
专家分:781
注 册:2021-10-13
得分:0 
回复 2楼 吹水佬
鼠标事件首先被传递给最里层的控件了,grid根本就没有收到鼠标事件。

form有个keypreview可以截获控件的keypress事件,但没有截获控件的鼠标事件,估计也算是个bug吧。
2022-09-15 23:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-510039-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.058682 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved