msgbox.prg
上传用户:skx808
上传日期:2020-09-21
资源大小:4k
文件大小:10k
源码类别:

其他数据库

开发平台:

VFP

  1. *---------------------------------------------------------------------
  2. procedure MSGBOX
  3.           parameters Msg, Buttons, Wtitle, Dummy1, Dummy2
  4.    * VFP version
  5. * Parameters:  Msg      Multi-line display string
  6. *              Buttons    0 = Ok           (default if omitted)
  7. *                         1 = Ok, Cancel
  8. *                         2 = Abort, Retry, Ignore
  9. *                         3 = Yes, No, Cancel
  10. *                         4 = Yes, No
  11. *                         5 = Retry, Cancel
  12. *                       optional: string with custom buttons
  13. *              Wtitle   optional window title
  14. *              Dummy1 & Dummy2 - obsolete, no longer used
  15. * Returns action number
  16. * Example usage:
  17. * Action = MsgBox("Location is not on file.+chr(13)+"Do you wish to add it?", 4)
  18. * Action = MsgBox("Are you sure you want to quit?", "!<Yes;?<Maybe", 5)
  19. * 10/18/01 - Get rid of DOS window, use a screen form for custom buttons. DC
  20. *        NB: Custom buttons aren't used often, so the auto-sizing has
  21. *             not been finely tuned - not worth the expense.  It works!
  22. *            Delete color scheme and default action parameters.
  23. * 02/15/01 - "Local all" doesn't work, fix local declarations
  24. * 01/10/01 - De-select current dbf to avoid conflicts with field names
  25. * 12/13/00 - Add CR to force Windows wrapping at a reasonable width.
  26. *            Delete prevision for centered lines delimited by ';'.
  27. *            Multiple lines now must be separated by chr(13).
  28. * 12/23/99 - VFP: use system MessageBox if no special buttons.
  29. * 11/08/95 - provide for automatic wordwrap if no ';' in Msg
  30. * 11/95 - Windows-compatible button sets copied from FoxTools by D.Covill
  31. *         Reference FoxPro Developers Journal, November 1995, Page 9
  32. * Original "ALERT" by Steve Ramsower
  33. *----------------------------------------
  34. set talk off
  35. local OldSel
  36. OldSel = select()            && 01/10/01
  37. select 0 && so we don't pick up data fields by mistake
  38. *-- Msg is the only required parameter
  39. if parameters() < 3
  40.    Wtitle = ''
  41. endif
  42. if parameters() < 2
  43.    Buttons = 0
  44. endif
  45. if empty(Wtitle)
  46.    Wtitle = 'Note:'
  47. endif
  48. if vartype(Buttons) = 'N'
  49.    *--We can use the Windows messagebox
  50.    local iButtons, iResult, cTitle
  51. *-- if just an Ok, then use Info icon, otherwise use Question
  52. iButtons = iif(Buttons = 0, 64, Buttons + 32)
  53. cTitle = Wtitle
  54.    *-- Fix the Msg so it doesn't come out as one long line!
  55.    *-- (Windows doesn't wrap it until it gets about 100 chars long)
  56.    iResult = MessageBox(MsgBox2(Msg), iButtons, cTitle)
  57.    *-- now map the result codes back to our (positional) system
  58.    do case
  59.    case iResult = 0          && Escape
  60.       *-- the same
  61.    case Buttons = 1         && Ok=1, Cancel=2
  62.       *-- leave it alone
  63.    case Buttons = 2         && Abort=3, Retry=4, Ignore=5
  64.       iResult = iResult - 2
  65.    case Buttons = 3         && Yes=6, No=7, Cancel=2
  66.       do case
  67.       case iResult = 6
  68.          iResult = 1
  69.       case iResult = 7
  70.          iResult = 2
  71.       case iResult = 2
  72.          iResult = 3
  73.       endcase
  74.    case Buttons = 4         && Yes=6, No=7
  75.       iResult = iResult - 5
  76.    case Buttons = 5         && Retry=4, Cancel=2
  77.       iResult = iif(iResult=4, 1, 2)
  78.    endcase
  79.    select (OldSel) && 01/10/01
  80.    return m.iResult
  81. endif
  82.    *-- Custom buttons, have to create our own messagebox
  83. *-- Break message into lines
  84. local MaxLen, NumLines, MsgArray, Center
  85. m.MaxLen = 0                 && maximum line length
  86. OldMemo = set("memowidth")
  87.    *-- calculate a reasonable breakdown of lines
  88.    set memowidth to 30
  89.    NumLines = memlines(Msg)
  90.    if NumLines > 5
  91.      set memowidth to 40
  92.      NumLines = memlines(Msg)
  93.    endif
  94.    if NumLines > 10
  95.       set memowidth to 50
  96.       NumLines = memlines(Msg)
  97.    endif
  98.    if NumLines > 20
  99.       set memowidth to 80
  100.       NumLines = memlines(Msg)
  101.    endif
  102.    for I = 1 to NumLines
  103.       MaxLen = max(MaxLen, len(mline(Msg, I)))
  104.    endfor
  105. *-- now stuff CR on end of each line
  106. T2 = ''
  107. for L = 1 to memlines(Msg)
  108.    T2 = T2 + mline(Msg, L) + chr(13)
  109. endfor
  110. Msg = left(T2, len(T2)-1)   && strip off last chr(13)
  111. *-- and reset the memowidth
  112. set memowidth to (OldMemo)
  113. Center = iif(NumLines = 1, .T., .F.)
  114. *-- Create the form and set the editbox size
  115. oBox = createobject("frmMessageBox")
  116. with oBox
  117.    *-- Size the form and the edit box
  118.    .edtMessage.Width = m.MaxLen * 8
  119.    .edtMessage.Height = m.NumLines * 18
  120.    .Width = .edtMessage.Width + 24
  121.    .Height = .edtMessage.Height + 50
  122.    .Caption = Wtitle
  123.    .cMessage = Msg               && the message text
  124. endwith
  125. *-- Parse buttons and calculate longest
  126. if empty(Buttons)
  127.    NumBtns = 1
  128.    Buttons = "OK"
  129. else
  130.    NumBtns  = occurs(';',Buttons) + 1    && number of buttons
  131. endif
  132. dimension aButtons[m.NumBtns]
  133. Remain = Buttons
  134. MaxWidth = 0
  135. for I = 1 to m.NumBtns
  136.    Break = at(';', m.Remain)
  137.    if m.Break > 0
  138.       aButtons[I] = left(m.Remain, m.Break - 1)
  139.       m.Remain = substr(m.Remain, m.Break + 1)
  140.    else
  141.       aButtons[I] = m.Remain
  142.    endif
  143.    *-- get length of buttons without escape chars
  144.    Temp = strtran(aButtons[I], '', '')
  145.    Temp = strtran(Temp, '<', '')
  146.    MaxWidth = max(MaxWidth, len(Temp))
  147. endfor
  148. BtnWidth = MaxWidth * 9  + 10     && Pixels
  149. BtnWidth = max(BtnWidth, 40)      && Don't make 'em too small
  150. Gap = 10
  151. BtnTotal = BtnWidth * NumBtns + Gap * (NumBtns-1)
  152. if BtnTotal > oBox.Width
  153.    with oBox
  154.       .Width = BtnTotal
  155.       .edtMessage.Width = BtnTotal - 24
  156.    endwith
  157. endif
  158. * do MsgBox in VLIB with ;
  159.   + "NumLines = "+str(NumLines,3)+chr(13);
  160.   + "Longest = "+str(MaxLen,3)+chr(13);
  161.   + "NumBtns = "+str(NumBtns,2)+chr(13);
  162.   + "BtnWidth = "+str(BtnWidth,3)+chr(13);
  163.   + ""
  164. *-- Now put the buttons on the form
  165. *-- And delete the ones not used
  166. with oBox
  167.    *-- Calculate the gap between buttons
  168.    Bleft = (.Width - BtnTotal) / 2
  169.    for I = 1 to NumBtns
  170.       CmdName = "Command"+str(I,1)
  171.       *-- Pass the button number as a parameter
  172.       *-- On click, the button will set Action to this number
  173.       with oBox.&CmdName
  174.          .Top = oBox.Height - 28
  175.          .Left = Bleft
  176.          .Width = BtnWidth
  177.          .Caption = aButtons[I]
  178.          .Visible = .T.
  179.       endwith
  180.       Bleft = Bleft + BtnWidth + Gap
  181.    endfor
  182.    *-- delete the ones we didn't use
  183.    for J = NumBtns+1 to 5
  184.       CmdName = "Command"+str(J,1)
  185.       .RemoveObject(CmdName)
  186.    endfor
  187. endwith
  188. * do ERRTRAP in VLIB with lineno(), "MSGBOX:"
  189. * set step on
  190. *-- Display the window
  191. Action = 0                   && default
  192. oBox.Show()                  && Sets Action on exit
  193. select (m.OldSel)
  194. return m.Action
  195. *---------------------------------------------------------------------
  196. DEFINE CLASS frmMessageBox AS form
  197. * 10/18/01 - New form by D.Covill
  198.    AutoCenter = .T.
  199. Top = 0
  200. Left = 0
  201. Height = 242
  202. Width = 375
  203. DoCreate = .T.
  204. Caption = "Note:"
  205. WindowType = 1            && modal
  206.    MaxButton = .F.
  207.    MinButton = .F.
  208.    cMessage = ' '            && the message itself
  209. Name = "frmMessageBox"
  210. ADD OBJECT edtmessage AS editbox WITH ;
  211. BackStyle = 0, ;       && transparent
  212. BorderStyle = 0, ;     && none
  213.       ControlSource = "thisform.cMessage", ;
  214. Height = 192, ;
  215. Left = 24, ;           && leave margin at left
  216. FontSize = 11, ;
  217. ReadOnly = .T., ;
  218. ScrollBars = 0, ;      && none
  219. TabIndex = 6, ;
  220. Top = 12, ;
  221. Width = 348, ;
  222. Name = "edtMessage"
  223.    *-- Up to 5 command buttons, we'll remove the ones not used
  224.    *-- (Code turns out to be easier than adding new ones.)
  225. ADD OBJECT command1 AS commandbutton WITH ;
  226. Top = 216, ;
  227. Left = 12, ;
  228. Height = 28, ;
  229. Width = 60, ;
  230. FontSize = 10, ;
  231. Caption = "Cmd1", ;    && will be set by caller
  232. TabIndex = 1, ;
  233. Name = "Command1"
  234. ADD OBJECT command2 AS commandbutton WITH ;
  235. Top = 216, ;
  236. Left = 84, ;
  237. Height = 28, ;
  238. Width = 60, ;
  239. FontSize = 10, ;
  240. Caption = "cmd1", ;
  241. TabIndex = 2, ;
  242. Name = "Command2"
  243. ADD OBJECT command3 AS commandbutton WITH ;
  244. Top = 216, ;
  245. Left = 156, ;
  246. Height = 28, ;
  247. Width = 60, ;
  248. FontSize = 10, ;
  249. Caption = "cmd3", ;
  250. TabIndex = 3, ;
  251. Name = "Command3"
  252. ADD OBJECT command4 AS commandbutton WITH ;
  253. Top = 216, ;
  254. Left = 228, ;
  255. Height = 28, ;
  256. Width = 60, ;
  257. FontSize = 10, ;
  258. Caption = "cmd4", ;
  259. TabIndex = 4, ;
  260. Name = "Command4"
  261. ADD OBJECT command5 AS commandbutton WITH ;
  262. Top = 216, ;
  263. Left = 300, ;
  264. Height = 25, ;
  265. Width = 60, ;
  266. Caption = "cmd5", ;
  267. TabIndex = 5, ;
  268. Name = "Command5"
  269.    *-- Buttons set the Action memvar, then exit
  270. PROCEDURE command1.Click
  271. m.Action = 1
  272. thisform.Release()
  273. ENDPROC
  274. PROCEDURE command2.Click
  275. m.Action = 2
  276. thisform.Release()
  277. ENDPROC
  278. PROCEDURE command3.Click
  279. m.Action = 3
  280. thisform.Release()
  281. ENDPROC
  282. PROCEDURE command4.Click
  283. m.Action = 4
  284. thisform.Release()
  285. ENDPROC
  286. PROCEDURE command5.Click
  287. m.Action = 5
  288. thisform.Release()
  289. ENDPROC
  290. ENDDEFINE
  291. *---------------------------------------------------------------------
  292. procedure MsgBox2
  293.           lparameters Tx      && Text of message
  294.    * Break message into multiple lines so it looks more reasonable
  295. * Windows MessageBox() doesn't wrap the lines until they get nearly
  296. * the entire width of the screen.  We'll insert CR characters at
  297. * reasonable points so the box is more of a rectangle.
  298. * 01/10/01 - Add "m.", remove potential conflict with field names
  299. local OldMemo, T2, L
  300. OldMemo = set("memowidth")
  301. *-- Keep the width small until the number of lines gets too large
  302. set memowidth to 40
  303. if memlines(Tx) > 5
  304.    set memowidth to 50
  305. endif
  306. if memlines(Tx) > 12
  307.    set memowidth to 80
  308. endif
  309. *-- now stuff CR on end of each line
  310. T2 = ''
  311. for L = 1 to memlines(Tx)
  312.    T2 = T2 + mline(Tx, L) + chr(13)
  313. endfor
  314. T2 = left(T2, len(T2)-1)   && strip off last chr(13)
  315. *-- and reset the memowidth
  316. set memowidth to (OldMemo)
  317. return T2
  318. *---------------------------------------------------------------------