home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 1999 July / APC47-1.ISO / workshop / win95 / pws / msmqocm.cab / disdraw.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-10-06  |  11.6 KB  |  343 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Form1"
  4.    ClientHeight    =   5820
  5.    ClientLeft      =   3345
  6.    ClientTop       =   2295
  7.    ClientWidth     =   6330
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   5820
  10.    ScaleWidth      =   6330
  11.    Begin VB.Frame Frame1 
  12.       Caption         =   "Sent Messages"
  13.       Height          =   975
  14.       Left            =   3120
  15.       TabIndex        =   6
  16.       Top             =   4680
  17.       Width           =   2535
  18.       Begin VB.OptionButton Option1 
  19.          Caption         =   "&Express"
  20.          Height          =   252
  21.          Index           =   0
  22.          Left            =   240
  23.          TabIndex        =   4
  24.          Top             =   240
  25.          Value           =   -1  'True
  26.          Width           =   2052
  27.       End
  28.       Begin VB.OptionButton Option1 
  29.          Caption         =   "&Recoverable"
  30.          Height          =   252
  31.          Index           =   1
  32.          Left            =   240
  33.          TabIndex        =   5
  34.          Top             =   600
  35.          Width           =   2052
  36.       End
  37.    End
  38.    Begin VB.PictureBox Picture1 
  39.       Height          =   3855
  40.       Left            =   240
  41.       MousePointer    =   1  'Arrow
  42.       ScaleHeight     =   253
  43.       ScaleMode       =   3  'Pixel
  44.       ScaleWidth      =   389
  45.       TabIndex        =   3
  46.       Top             =   120
  47.       Width           =   5895
  48.    End
  49.    Begin VB.CommandButton Attach 
  50.       Caption         =   "&Attach"
  51.       Default         =   -1  'True
  52.       BeginProperty Font 
  53.          Name            =   "MS Sans Serif"
  54.          Size            =   8.25
  55.          Charset         =   0
  56.          Weight          =   700
  57.          Underline       =   0   'False
  58.          Italic          =   0   'False
  59.          Strikethrough   =   0   'False
  60.       EndProperty
  61.       Height          =   375
  62.       Left            =   4800
  63.       TabIndex        =   2
  64.       Top             =   4200
  65.       Width           =   1335
  66.    End
  67.    Begin VB.TextBox FriendName 
  68.       Height          =   285
  69.       Left            =   1440
  70.       TabIndex        =   1
  71.       Top             =   4200
  72.       Width           =   2055
  73.    End
  74.    Begin VB.Label Label1 
  75.       Caption         =   "Remote &Friend:"
  76.       Height          =   255
  77.       Left            =   240
  78.       TabIndex        =   0
  79.       Top             =   4200
  80.       Width           =   1215
  81.    End
  82. Attribute VB_Name = "Form1"
  83. Attribute VB_GlobalNameSpace = False
  84. Attribute VB_Creatable = False
  85. Attribute VB_PredeclaredId = True
  86. Attribute VB_Exposed = False
  87. ' ------------------------------------------------------------------------
  88. '               Copyright (C) 1995 Microsoft Corporation
  89. ' You have a royalty-free right to use, modify, reproduce and distribute
  90. ' the Sample Application Files (and/or any modified version) in any way
  91. ' you find useful, provided that you agree that Microsoft has no warranty,
  92. ' obligations or liability for any Sample Application Files.
  93. ' ------------------------------------------------------------------------
  94. ' Type Guid
  95. Const guidDraw = "{151ceac0-acb5-11cf-8b51-0020af929546}"
  96. Option Explicit
  97. Const MaxNumLen = 7
  98. Private Type Line
  99.     X1 As Long
  100.     Y1 As Long
  101.     X2 As Long
  102.     Y2 As Long
  103. End Type
  104. Dim lLastX As Long
  105. Dim lLastY As Long
  106. Dim Lines() As Line
  107. Dim cLines As Integer
  108. Dim lArraySize As Integer
  109. Dim strScreenText As String
  110. Dim fWasText As Integer
  111. Dim strLogin As String
  112. Dim q As MSMQQueue
  113. Attribute q.VB_VarHelpID = -1
  114. Dim WithEvents qevent As MSMQEvent
  115. Attribute qevent.VB_VarHelpID = -1
  116. Dim qFriend As MSMQQueue
  117. Dim msgOut As MSMQMessage
  118. 'Locate a remote queue
  119. Private Sub Attach_Click()
  120.     Dim queryFriend As New MSMQQuery
  121.     Dim qinfoFriend As MSMQQueueInfo
  122.     Dim qinfos As MSMQQueueInfos
  123.     FriendName = UCase(FriendName)
  124.     Set qinfos = queryFriend.LookupQueue( _
  125.         Label:=(FriendName), _
  126.         ServiceTypeGuid:=guidDraw)
  127.     qinfos.Reset
  128.     Set qinfoFriend = qinfos.Next
  129.     If qinfoFriend Is Nothing Then                   'And locate it
  130.         MsgBox "No Such friend, Sorry..."   'No queue defined
  131.     Else
  132.         If Not qFriend Is Nothing Then
  133.           If qFriend.IsOpen Then qFriend.Close
  134.         End If
  135.         Set qFriend = qinfoFriend.Open(MQ_SEND_ACCESS, 0)
  136.         Caption = strLogin + " - Connected to " + FriendName
  137.         Attach.Enabled = False
  138.     End If
  139. End Sub
  140. ' Application Initialization
  141. Private Sub Form_Load()
  142.     Dim strDefaultQueueName As String
  143.     Dim lTempPointer As Long
  144.     Dim query As New MSMQQuery
  145.     Dim qinfo As MSMQQueueInfo
  146.     Dim qinfos As MSMQQueueInfos
  147.     Dim strComputerName As String
  148.     Set msgOut = New MSMQMessage
  149.     strDefaultQueueName = Environ("USERNAME")
  150.     strLogin = InputBox("Your Name Please", "Login", strDefaultQueueName)
  151.     If strLogin = "" Then End
  152.     strLogin = UCase(strLogin)
  153.     Caption = strLogin
  154.     Set qinfos = query.LookupQueue( _
  155.         Label:=strLogin, _
  156.         ServiceTypeGuid:=guidDraw)
  157.     qinfos.Reset             'And locate this queue
  158.     Set qinfo = qinfos.Next
  159.     If qinfo Is Nothing Then
  160.         Set qinfo = New MSMQQueueInfo
  161.         strComputerName = "."
  162.         qinfo.PathName = strComputerName + "\" + strLogin
  163.         qinfo.Label = strLogin
  164.         qinfo.ServiceTypeGuid = guidDraw
  165.         qinfo.Create                 'If there is no such create one.
  166.     End If
  167.     On Error GoTo retry_on_error
  168.         lTempPointer = Screen.MousePointer
  169.         Screen.MousePointer = 11 'ccArrowHourglass
  170.         Set q = qinfo.Open(MQ_RECEIVE_ACCESS, 0)
  171.         On Error GoTo 0
  172.         Screen.MousePointer = lTempPointer
  173.         GoTo all_ok
  174. retry_on_error:
  175.     '
  176.     'We may still not see the queue until the next replication
  177.     ' In this case, we get MQ_ERROR_QUEUE_NOT_FOUND and retry.
  178.     '
  179.     If Err.Number = MQ_ERROR_QUEUE_NOT_FOUND Then
  180.         Err.Clear
  181.         DoEvents
  182.         Resume
  183.     Else
  184.         MsgBox Err.Description, , "Error in Open"
  185.         End
  186.     End If
  187. all_ok:
  188.     'All messages will be received asynchronously
  189.     ' So need an event handler
  190.     Set qevent = New MSMQEvent
  191.     q.EnableNotification qevent
  192. End Sub
  193. 'Gets points and returns a line
  194. Private Function PointsToLine(ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Line
  195.     Dim lineNew As Line
  196.     lineNew.X1 = X1
  197.     lineNew.Y1 = Y1
  198.     lineNew.X2 = X2
  199.     lineNew.Y2 = Y2
  200.     PointsToLine = lineNew
  201. End Function
  202. 'Draw a line in the picture control
  203. Private Sub DrawLine(lineDraw As Line)
  204.     Picture1.Line (lineDraw.X1, lineDraw.Y1)-(lineDraw.X2, lineDraw.Y2)
  205.     fWasText = False
  206. End Sub
  207. 'Display a line
  208. Private Sub AddLine(lineNew As Line)
  209.     DrawLine lineNew
  210.     cLines = cLines + 1
  211.     If (cLines > lArraySize) Then
  212.         lArraySize = cLines * 2
  213.         ReDim Preserve Lines(lArraySize)
  214.     End If
  215.     Lines(cLines - 1) = lineNew
  216. End Sub
  217. 'Clear the display
  218. Private Sub ClearDraw()
  219.     cLines = 0
  220.     strScreenText = ""
  221.     Picture1.Refresh
  222. End Sub
  223. 'Decode a string into a line
  224. Private Function LineToString(lineIn As Line) As String
  225.     Dim strFormat As String
  226.     strFormat = String(MaxNumLen, "0")
  227.     LineToString = Format$(lineIn.X1, strFormat) + Format$(lineIn.Y1, strFormat) + Format$(lineIn.X2, strFormat) + Format$(lineIn.Y2, strFormat)
  228. End Function
  229. 'Encode a line into a string
  230. Private Function StringToLine(strIn As String) As Line
  231.     Dim lineOut As Line
  232.     lineOut.X1 = Val(Mid$(strIn, 1, MaxNumLen))
  233.     lineOut.Y1 = Val(Mid$(strIn, MaxNumLen + 1, MaxNumLen))
  234.     lineOut.X2 = Val(Mid$(strIn, MaxNumLen * 2 + 1, MaxNumLen))
  235.     lineOut.Y2 = Val(Mid$(strIn, MaxNumLen * 3 + 1, MaxNumLen))
  236.     StringToLine = lineOut
  237. End Function
  238. Private Sub Form_Unload(Cancel As Integer)
  239.     If Not q Is Nothing Then
  240.         q.Close
  241.     End If
  242.     If Not qFriend Is Nothing Then qFriend.Close
  243. End Sub
  244. Private Sub FriendName_Change()
  245.         Attach.Enabled = True
  246. End Sub
  247. 'Message Receive event
  248. Private Sub qevent_Arrived(ByVal q As Object, ByVal lCursor As Long)
  249.     Dim msgIn As MSMQMessage
  250.     Dim lineNew As Line
  251.     Dim strTextIn As String
  252.     On Error GoTo ErrorHandler
  253.     Set msgIn = q.Receive(ReceiveTimeout:=100)
  254.     If Not msgIn Is Nothing Then
  255.         strTextIn = msgIn.Body          'Read the body of the message
  256.         If Len(strTextIn) = 1 Then         'If 1 byte long
  257.             TypeChar strTextIn             'it is a character - so display it
  258.         Else
  259.             lineNew = StringToLine(msgIn.Body)   'Otherwise it is a line
  260.             AddLine lineNew                         'so draw it
  261.         End If
  262.     End If
  263. ErrorHandler:
  264.     ' reenable event firing
  265.     q.EnableNotification qevent
  266. End Sub
  267. Private Sub qevent_ArrivedError(ByVal pdispQueue As Object, ByVal lErrorCode As Long, ByVal lCursor As Long)
  268.     MsgBox Hex$(lErrorCode), , "Receive Error!"
  269.     q.EnableNotification qevent
  270. End Sub
  271. Private Sub Option1_Click(Index As Integer)
  272.     msgOut.Delivery = Index
  273. End Sub
  274. 'Key press event
  275. Private Sub Picture1_KeyPress(KeyAscii As Integer)
  276.     TypeChar (Chr(KeyAscii))                'Display the character
  277.     If Not qFriend Is Nothing Then
  278.         If qFriend.IsOpen Then
  279.             msgOut.Priority = 4                 'Set the priority to 4 (high)
  280.             msgOut.Body = Chr(KeyAscii)         'Fill the body with the character
  281.             msgOut.Label = "Key: " + msgOut.Body
  282.             msgOut.Send qFriend                 'And send the message
  283.         End If
  284.     End If
  285. End Sub
  286. 'Display a character
  287. '(Handles backspace)
  288. Private Sub TypeChar(Key As String)
  289.     If Asc(Key) = 8 Then 'BackSpace
  290.         If strScreenText <> "" Then
  291.             strScreenText = Left$(strScreenText, Len(strScreenText) - 1)
  292.             Picture1.Refresh
  293.         End If
  294.     Else
  295.         strScreenText = strScreenText + Key
  296.         If fWasText Then
  297.             Picture1.Print Key;
  298.         Else
  299.             Picture1.Refresh
  300.         End If
  301.     End If
  302. End Sub
  303. 'Mouse Down Event
  304. Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  305.     If Button = 1 Then      'Remember the
  306.         lLastX = X           'Mouse location
  307.         lLastY = Y
  308.     End If
  309. End Sub
  310. 'Mouse Move Event
  311. Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  312.     If Button = 1 And X > 0 And Y > 0 Then          'Something to draw?
  313.         Dim lineNew As Line
  314.         lineNew = PointsToLine(lLastX, lLastY, X, Y)  'Get a new line
  315.         AddLine lineNew                             'And display it
  316.         If Not qFriend Is Nothing Then
  317.             If qFriend.IsOpen Then
  318.                 msgOut.Priority = 3                     'Set the priority to 3 (low)
  319.                 msgOut.Body = LineToString(lineNew)     'Fill the body with the line
  320.                 msgOut.Label = Str(lLastX) + "," + Str(lLastY) + " To " + Str(X) + "," + Str(Y)
  321.                 msgOut.Send qFriend                     'And send the message
  322.             End If
  323.         End If
  324.         lLastX = X
  325.         lLastY = Y
  326.     End If
  327. End Sub
  328. '2nd button click == Clear the display
  329. Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  330.     If Button = 2 Then ClearDraw
  331. End Sub
  332. 'Repaint the display event
  333. Private Sub Picture1_Paint()
  334.     Dim I As Integer
  335.     For I = 0 To cLines - 1
  336.         DrawLine Lines(I)
  337.     Next
  338.     Picture1.CurrentX = 0
  339.     Picture1.CurrentY = 0
  340.     Picture1.Print strScreenText;
  341.     fWasText = True
  342. End Sub
  343.