| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1516 人关注过本帖
标题:那个高手有时间帮我完善一下一个类似 messagebox 的函数的??
只看楼主 加入收藏
qingfameng
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:35
帖 子:964
专家分:3019
注 册:2010-2-6
收藏
得分:10 
**根据楼主的思路简化了一下(仅供参考)
******************* 测试参数,测试时打开  
*clear
 * msg1="人类的认知极限,远距离超过 10的23次方_米,就很难达到了。在170亿年前,"+0h0A
 * msg2="空间和时间都不存在,没有太阳、月亮和星星。突然,一个神秘的奇点爆发了!"+0h0A
 * msg3="天地万物开始产生。随后,我们也来了。神秘的巨大爆发,直到今天还在进行,"+0h0a
 * msg4="对外速度越来越快,我们却正在生活在其中 ..."
 * msg=msg1+msg2+msg3+msg4
 * cr=2
 * msgt='提示:'
 * msgc='chek11'
 * col1=RGB(240,240,240)
 * col2=RGB(0,0,250)
 * col3=RGB(255,255,255)
 * col4=RGB(0,128,0)
 * dfmsg(msg,cr,msgt,msgc,col1,col2,col3,col4)
 *?tmp_5
 *return
 ****************

 PROCEDURE dfmsg && 检查函数的输入数据,并定义默认值   
 LPARAMETERS msg,cr,msgt,msgc,col1,col2,col3,col4
 LOCAL ARRAY tmp_1(1)
 LOCAL tmp_2,tmp_3,tmp_4
 PUBLIC tmp_5

    msg=ICASE(Type('msg')$'N',ALLTRIM(STR(msg)),Type('msg')$'L','',Type('msg')$'D' or Type('msg')$'T',ttoc(msg),ALLTRIM(msg))
    cr=ICASE(varType(cr)#'N',0,cr)
    msgt=ICASE(TYPE('msgt')$'N',ALLTRIM(str(msgt)),TYPE('msgt')$'L','',Type('msgt')$'D' or Type('msgt')$'T',ttoc(msgt),ALLTRIM(msgt))
    msgc=ICASE(TYPE('msgc')$'N',ALLTRIM(str(msgc)),TYPE('msgc')$'L','',Type('msgc')$'D' or Type('msgc')$'T',ttoc(msgc),ALLTRIM(msgc))
    col1=IIF(TYPE("col1")#'N',ICASE(TYPE("f_color_01")#'N',f_color_01,14215660),col1)
    col2=IIF(TYPE("col2")#'N',ICASE(TYPE("f_color_02")#'N',f_color_02,0),col2)
    col3=IIF(TYPE("col3")#'N',ICASE(TYPE("f_color_01")#'N',f_color_01,14215660),col3)
    col4=IIF(TYPE("col4")#'N',ICASE(TYPE("f_color_02")#'N',f_color_02,0),col4)

    store 0 to tmp_1,tmp_2 && 定义行数,最大宽度
    tmp_2=alines(tmp_1,msg)
    tmp_3=0
       FOR EACH tmp_4 IN tmp_1
           tmp_3 = MAX(tmp_3,LEN(tmp_4))
       ENDFOR
       tmp_3=ICASE(LEN(msgc)<>0,MAX(tmp_3,LEN(msgc)),tmp_3) && 最大长度
       tmp_4=ICASE(LEN(msgc)=0,0,1)
       tmp_3=ICASE(tmp_3>10,tmp_3-10,0)
       tmp_5=0

       oForm = CREATEOBJECT("cForm",tmp_2,tmp_3)
       tmp_5=tmp_2
       oForm.Show
      * READ EVENTS && 测试时要关闭,编译时不能关
 ENDFUNC

  
*****************************************  
     DEFINE CLASS cForm AS Form && 定义表单
       Closable = .F.
       Height = 80+(tmp_2+tmp_4)*FONTMETRIC(1)
       Width = 100 +tmp_3*FONTMETRIC(6)
       backcolor=col1
       AutoCenter = .T.
       BorderStyle = 2
       Caption = msgt
       FontCondense = .T.
       MaxButton = .F.
       MinButton = .F.
       WindowType = 1
       AlwaysOnTop = .T.

      
       ADD object lb1 as label with wordrap=.t.,BackStyle=0,forecolor=col2,Caption=msg,Left=30,;
           Top = 25,Height=(tmp_2+tmp_4)*FONTMETRIC(1),Width = LEN(msg)*FONTMETRIC(6)  
       ADD object chk as checkbox with AutoSize=.T.,BackStyle=0,forecolor=col2,Caption=msgc,;
           Top=35+tmp_2*FONTMETRIC(1),Left=30,Height=16,Width=20+LEN(msgc)*FONTMETRIC(6)
       ADD object cmd1 as commandbutton with backcolor=col3,forecolor=col4,Top=40+(tmp_2+tmp_4)*FONTMETRIC(1),;
           Left=(40+tmp_3*FONTMETRIC(6))/3,Height = 20,Width=30,Caption = "是"
       ADD object cmd2 as commandbutton with backcolor=col3,forecolor=col4,Top=40+(tmp_2+tmp_4)*FONTMETRIC(1),;
           Left=(40+tmp_3*FONTMETRIC(6))/3*2+30,Height=20,Width=30,caption="否"

       procedure init
            lparameters tmp_2,tmp_3
            IF LEN(msgc)>0
               this.chk.visible=.t.
               this.cmd1.enabled=.f.
            ENDIF
            cm1=ICASE(cr=0,'this.cmd1.left=(70+tmp_3*FONTMETRIC(6))/2',cr=2,'this.cmd1.caption="确定"','')
            &cm1
            cm2=ICASE(cr=0,'this.cmd1.visible=.t.',cr=2,'this.cmd2.caption="取消"','')
            &cm2  
       endproc
   
       procedure chk.click
          this.parent.cmd1.enabled=this.Value
       endpro   
       procedure cmd1.click
              tmp_5=1
              thisform.Release()
              clear events
       endproc
       procedure cmd2.click
              thisform.Release()
              clear events
       endproc

  ENDDEFINE
      
2014-09-14 18:17
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10568
专家分:43011
注 册:2014-5-20
收藏(1)
得分:0 
以下是引用liuxingang28在2014-9-13 19:41:24的发言:

平心而论,系统函数 MessageBox()已十分完善,且功能强大。唯一的不足是,对于初学者来说,记住那些代表图标、按钮类型、默认按钮和返回值的数值需花点心思,但这些数值均有规律可循,记住也不难。对话框中的字体、颜色有那么重要吗?个人认为,在一个严肃的商用环境中,要遵循“约定俗成”的惯例,少搞花架子,应将主要精力放在程序的功能、易用性和运行效率方面。从实用性方面来说,设计一个代替 Wait Window 命令的函数可能更实用一些。


MessageBox()确是经典,好多编程语言也有类似函数。
Wait 命令在DOS平台时少不了,到Windows平台后几乎忘掉了,今因MessageBox而提起,参考上面MessageBox的例子,再试写个模拟Wait的WaitBox(),加多个进度条功能。
程序代码:
CLEAR
nVar = 0
oWait = WaitBox("按任一键继续......", @nVar)
? nVar
oWait = WaitBox("暂停 3 秒继续......", @nVar, .F., 3000)
oWait = WaitBox("正在下载数据"+0h0D+"请稍候......", @nVar, .T., 0)
IF !ISNULL(oWait)
    FOR i = 1 TO 100
        oWait.nProgress = i / 100
        INKEY(0.1, "H")
    ENDFOR
    oWait.Release
ENDIF
RETURN

FUNCTION WaitBox(cMsg, pVar, bNoWait, nTimeOut)
    PRIVATE oForm, wRet
    wRet = 0
    oForm = CREATEOBJECT("WaitForm", cMsg, bNoWait, nTimeOut)
    oForm.Show(2)
    IF !bNoWait
        READ EVENTS
    ENDIF
    pVar = wRet
    RETURN oForm
ENDFUNC

DEFINE CLASS WaitForm AS Form
    Desktop = .T.
    WindowType = 1 
    AutoCenter = .T.
    TitleBar = 0
    *BorderStyle = 1
    KeyPreview = .T.
    
    nTimeOut = 0
    bNoWait = .F.
    nProgress = 0

    ADD OBJECT Label1 AS Label WITH Width=SYSMETRIC(1),AutoSize=.T.,WordWrap=.T.,FontSize=24
    ADD OBJECT Shape1 AS Shape WITH Top=this.Height-35,Height=25,Anchor=4
    ADD OBJECT Shape2 AS Shape WITH Top=this.Height-35,Height=25,Anchor=4,;
        Width=0,BackColor=RGB(50,150,250),BorderStyle=0
    ADD OBJECT Timer1 AS Timer
        
    PROCEDURE Init
        LPARAMETERS cMsg, bNoWait, nTimeOut
        this.Label1.Caption = IIF(!EMPTY(cMsg), cMsg, "")
        this.nTimeOut = IIF(!EMPTY(nTimeOut), nTimeOut, 0)
        this.bNoWait = !EMPTY(bNoWait)
        this.Timer1.Interval = IIF(!this.bNoWait, this.nTimeOut, 0)
    ENDPROC

    PROCEDURE Activate
        INKEY(0.05)
        this.Height = this.Label1.Height + 65
        this.Width = this.Label1.Width + 40
        this.AutoCenter = .T.
        this.Label1.Top = INT((this.ViewPortHeight - this.Label1.Height - 45)/2)
        this.Label1.Left = INT((this.ViewPortWidth - this.Label1.Width)/2)
        this.Shape1.Left = this.Label1.Left
        this.Shape1.Width = this.Label1.Width
        this.Shape2.Left = this.Label1.Left
    ENDPROC
    
    PROCEDURE Destroy
        IF !this.bNoWait
            CLEAR EVENTS
        ENDIF
    ENDPROC

    PROCEDURE KeyPress
        LPARAMETERS nKeyCode, nShiftAltCtrl
        IF this.Timer1.Interval == 0
            wRet = nKeyCode
            thisform.Release
        ENDIF
    ENDPROC

    PROCEDURE Timer1.Timer
        this.Enabled = .F.
        INKEY(0.001)
        thisform.Release
    ENDPROC
    
    PROCEDURE nProgress_Assign
        LPARAMETERS vNewVal
        IF BETWEEN(vNewVal, 0, 1)
            this.Shape2.Width = this.Shape1.Width * vNewVal
        ENDIF
    ENDPROC
ENDDEFINE


[ 本帖最后由 吹水佬 于 2014-9-14 22:33 编辑 ]
2014-09-14 22:30
antony521
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:170
专家分:175
注 册:2009-8-20
收藏
得分:0 
以下是引用吹水佬在2014-9-14 22:30:03的发言:



MessageBox()确是经典,好多编程语言也有类似函数。
Wait 命令在DOS平台时少不了,到Windows平台后几乎忘掉了,今因MessageBox而提起,参考上面MessageBox的例子,再试写个模拟Wait的WaitBox(),加多个进度条功能。

CLEAR
nVar = 0
oWait = WaitBox("按任一键继续......", @nVar)
? nVar
oWait = WaitBox("暂停 3 秒继续......", @nVar, .F., 3000)
oWait = WaitBox("正在下载数据"+0h0D+"请稍候......", @nVar, .T., 0)
IF !ISNULL(oWait)
    FOR i = 1 TO 100
        oWait.nProgress = i / 100
        INKEY(0.1, "H")
    ENDFOR
    oWait.Release
ENDIF
RETURN

FUNCTION WaitBox(cMsg, pVar, bNoWait, nTimeOut)
    PRIVATE oForm, wRet
    wRet = 0
    oForm = CREATEOBJECT("WaitForm", cMsg, bNoWait, nTimeOut)
    oForm.Show(2)
    IF !bNoWait
        READ EVENTS
    ENDIF
    pVar = wRet
    RETURN oForm
ENDFUNC

DEFINE CLASS WaitForm AS Form
    Desktop = .T.
    WindowType = 1
    AutoCenter = .T.
    TitleBar = 0
    *BorderStyle = 1
    KeyPreview = .T.
   
    nTimeOut = 0
    bNoWait = .F.
    nProgress = 0

    ADD OBJECT Label1 AS Label WITH Width=SYSMETRIC(1),AutoSize=.T.,WordWrap=.T.,FontSize=24
    ADD OBJECT Shape1 AS Shape WITH Top=this.Height-35,Height=25,Anchor=4
    ADD OBJECT Shape2 AS Shape WITH Top=this.Height-35,Height=25,Anchor=4,;
        Width=0,BackColor=RGB(50,150,250),BorderStyle=0
    ADD OBJECT Timer1 AS Timer
        
    PROCEDURE Init
        LPARAMETERS cMsg, bNoWait, nTimeOut
        this.Label1.Caption = IIF(!EMPTY(cMsg), cMsg, "")
        this.nTimeOut = IIF(!EMPTY(nTimeOut), nTimeOut, 0)
        this.bNoWait = !EMPTY(bNoWait)
        this.Timer1.Interval = IIF(!this.bNoWait, this.nTimeOut, 0)
    ENDPROC

    PROCEDURE Activate
        INKEY(0.05)
        this.Height = this.Label1.Height + 65
        this.Width = this.Label1.Width + 40
        this.AutoCenter = .T.
        this.Label1.Top = INT((this.ViewPortHeight - this.Label1.Height - 45)/2)
        this.Label1.Left = INT((this.ViewPortWidth - this.Label1.Width)/2)
        this.Shape1.Left = this.Label1.Left
        this.Shape1.Width = this.Label1.Width
        this.Shape2.Left = this.Label1.Left
    ENDPROC
   
    PROCEDURE Destroy
        IF !this.bNoWait
            CLEAR EVENTS
        ENDIF
    ENDPROC

    PROCEDURE KeyPress
        LPARAMETERS nKeyCode, nShiftAltCtrl
        IF this.Timer1.Interval == 0
            wRet = nKeyCode
            thisform.Release
        ENDIF
    ENDPROC

    PROCEDURE Timer1.Timer
        this.Enabled = .F.
        INKEY(0.001)
        thisform.Release
    ENDPROC
   
    PROCEDURE nProgress_Assign
        LPARAMETERS vNewVal
        IF BETWEEN(vNewVal, 0, 1)
            this.Shape2.Width = this.Shape1.Width * vNewVal
        ENDIF
    ENDPROC
ENDDEFINE

这个好!不过也有点小问题.
1.点鼠标不动.
2.只有提示信息时不应该有进度条框
2014-09-15 08:59
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10568
专家分:43011
注 册:2014-5-20
收藏
得分:0 
回复 13 楼 antony521
只是一个简单示例,实际应用可加修饰。
2014-09-15 09:12
qingfameng
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:35
帖 子:964
专家分:3019
注 册:2010-2-6
收藏
得分:0 
对12楼吹水佬赞一个
2014-09-15 16:16
zjj1974
Rank: 2
等 级:论坛游民
威 望:1
帖 子:58
专家分:38
注 册:2014-8-10
收藏
得分:0 
第十二楼的帖子中;如果在 KeyPress 过程后,再加上如下语句,可能会更好一些


    PROCEDURE MouseUp
        LPARAMETERS nButton, nShift, nXCoord, nYCoord
        IF 0 = this.Timer1.Interval and .f. = this.bNoWait
           thisform.Release
        ENDIF
    ENDPROC
   
    PROCEDURE Label1.MouseUp
       LPARAMETERS nButton, nShift, nXCoord, nYCoord
       thisform.MouseUp
    ENDPROC     
   
    PROCEDURE Shape1.MouseUp
       LPARAMETERS nButton, nShift, nXCoord, nYCoord
       thisform.MouseUp
    ENDPROC     
2014-09-18 10:06
快速回复:那个高手有时间帮我完善一下一个类似 messagebox 的函数的??
数据加载中...
 
   



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

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