472,951 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 software developers and data experts.

Sendmessage using dot net

Rob
Hi all,

I am having trouble converting the code below (found on
http://vbnet.mvps.org/index.html?cod...endmessage.htm) into a
format that will work using vb .NET.

Can anyone have a look at it and let me know what I need to change. I
have tried changing the "hwnd" type into intptr's but there seem to be
other problems too, like it won't allow "lParam As Any" to be
declared.

Many thanks,

Rob
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Copyright ©1996-2004 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Const WM_SETTEXT = &HC

Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal wCmd As Long) As Long

Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, _
lpdwProcessId As Long) As Long

Private Declare Function BringWindowToTop Lib "user32" _
(ByVal hwnd As Long) As Long

Private Declare Function SetWindowText Lib "user32" _
Alias "SetWindowTextA" _
(ByVal hwnd As Long, ByVal _
lpString As String) As Long

Private Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

Private Sub Command1_Click()

Dim hWndApp As Long

'call the wrapper function to start shell,
'and return the handle to the shelled app.
hWndApp = StartShell("notepad.exe")

'if found, prove it!
If hWndApp Then

'change its caption
Call SetWindowText(hWndApp, "The handle to Notepad is " &
CStr(hWndApp))

'bring it to the top
Call BringWindowToTop(hWndApp)

'and add some text to its edit window
Call PutTextIntoNotepad(hWndApp)

End If

End Sub
Private Function GethWndFromProcessID(hProcessIDToFind As Long) As
Long

Dim hWndDesktop As Long
Dim hWndChild As Long
Dim hWndChildProcessID As Long

On Local Error GoTo GethWndFromProcessID_Error

'get the handle to the desktop
hWndDesktop = GetDesktopWindow()

'get the first child under the desktop
hWndChild = GetWindow(hWndDesktop, GW_CHILD)

'hwndchild will = 0 when no more child windows are found
Do While hWndChild <> 0

'get the ThreadProcessID of the window
Call GetWindowThreadProcessId(hWndChild, hWndChildProcessID)

'if it matches the target, exit returning that value
If hWndChildProcessID = hProcessIDToFind Then
GethWndFromProcessID = hWndChild
Exit Do
End If

'not found, so get the next hwnd
hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)

Loop

Exit Function

GethWndFromProcessID_Error:
GethWndFromProcessID = 0
Exit Function

End Function
Private Function StartShell(sApplication As String) As Long

Dim hProcessID As Long

'start using shell, and get the process ID
hProcessID = Shell(sApplication, vbNormalFocus)

'return the hwnd to the shelled process
StartShell = GethWndFromProcessID(hProcessID)

End Function
Private Sub PutTextIntoNotepad(hWndApp As Long)

'This adds text to notepad, by locating its
''Edit' class window. This is similar to the
'method used to locate the hwnd itself, but
'here we know the parent (hWndApp) so only
'have to search its child windows.

Dim hWndChild As Long
Dim sMsg As String
Dim sBuffer As String * 32
Dim nSize As Long

'this string is split only to fit the browser window
sMsg = "This method demonstrates using Shell() "
sMsg = sMsg & "to launch notepad, obtain its hwnd using "
sMsg = sMsg & "GetWindowThreadProcessId, and then use that"
sMsg = sMsg & "hwnd to change its caption and place text into "
sMsg = sMsg & "its Edit window using SetWindowText and
SendMessage."

'get the first child window in Notepad
hWndChild = GetWindow(hWndApp, GW_CHILD)

'hwndchild will = 0 when no more child windows are found
Do While hWndChild <> 0

'get the Class Name of the window
nSize = GetClassName(hWndChild, sBuffer, 32)

'if nSize > 0, it contains the length
'of the class name retrieved
If nSize Then

'if the class name is "Edit",
'set some text and exit
If Left$(sBuffer, nSize) = "Edit" Then

Call SendMessage(hWndChild, WM_SETTEXT, 0&, ByVal sMsg)
Exit Sub

End If

End If

'not found, so get the next hwnd
hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)

Loop

End Sub
Nov 21 '05 #1
3 5255
Hi,

Declare Function SendMessage Lib "user32" Alias "SendMessageA" _

(ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, _

ByVal lParam As String) As Integer

Declare Function GetWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As
IntPtr, _

ByVal wCmd As Integer) As IntPtr

Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _

(ByVal hwnd As IntPtr, ByVal lpClassName As String, _

ByVal nMaxCount As Integer) As Integer

Const GW_CHILD As Integer = 5

Const GW_HWNDNEXT As Integer = 2

Const WM_SETTEXT As Integer = &HC

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim p As Process = Process.Start("notepad.exe")

Dim nSize As Integer

Dim hwndChild As IntPtr

Dim sBuffer As String = Space(32)

hwndChild = GetWindow(p.MainWindowHandle, GW_CHILD)

'hwndchild will = 0 when no more child windows are found

Do While Not hwndChild.Equals(IntPtr.Zero)

'get the Class Name of the window

nSize = GetClassName(hwndChild, sBuffer, 32)

'if nSize > 0, it contains the length

'of the class name retrieved

If nSize >= 0 Then

'if the class name is "Edit",

'set some text and exit

If sBuffer.IndexOf("Edit") >= 0 Then

SendMessage(hwndChild, WM_SETTEXT, 0, "Test")

Exit Sub

End If

End If

Loop

End Sub
Ken
-------------------
"Rob" <rw*********@rtt.uk.com> wrote in message
news:38**************************@posting.google.c om...
Hi all,

I am having trouble converting the code below (found on
http://vbnet.mvps.org/index.html?cod...endmessage.htm) into a
format that will work using vb .NET.

Can anyone have a look at it and let me know what I need to change. I
have tried changing the "hwnd" type into intptr's but there seem to be
other problems too, like it won't allow "lParam As Any" to be
declared.

Many thanks,

Rob
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Copyright ©1996-2004 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Const WM_SETTEXT = &HC

Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal wCmd As Long) As Long

Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, _
lpdwProcessId As Long) As Long

Private Declare Function BringWindowToTop Lib "user32" _
(ByVal hwnd As Long) As Long

Private Declare Function SetWindowText Lib "user32" _
Alias "SetWindowTextA" _
(ByVal hwnd As Long, ByVal _
lpString As String) As Long

Private Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

Private Sub Command1_Click()

Dim hWndApp As Long

'call the wrapper function to start shell,
'and return the handle to the shelled app.
hWndApp = StartShell("notepad.exe")

'if found, prove it!
If hWndApp Then

'change its caption
Call SetWindowText(hWndApp, "The handle to Notepad is " &
CStr(hWndApp))

'bring it to the top
Call BringWindowToTop(hWndApp)

'and add some text to its edit window
Call PutTextIntoNotepad(hWndApp)

End If

End Sub
Private Function GethWndFromProcessID(hProcessIDToFind As Long) As
Long

Dim hWndDesktop As Long
Dim hWndChild As Long
Dim hWndChildProcessID As Long

On Local Error GoTo GethWndFromProcessID_Error

'get the handle to the desktop
hWndDesktop = GetDesktopWindow()

'get the first child under the desktop
hWndChild = GetWindow(hWndDesktop, GW_CHILD)

'hwndchild will = 0 when no more child windows are found
Do While hWndChild <> 0

'get the ThreadProcessID of the window
Call GetWindowThreadProcessId(hWndChild, hWndChildProcessID)

'if it matches the target, exit returning that value
If hWndChildProcessID = hProcessIDToFind Then
GethWndFromProcessID = hWndChild
Exit Do
End If

'not found, so get the next hwnd
hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)

Loop

Exit Function

GethWndFromProcessID_Error:
GethWndFromProcessID = 0
Exit Function

End Function
Private Function StartShell(sApplication As String) As Long

Dim hProcessID As Long

'start using shell, and get the process ID
hProcessID = Shell(sApplication, vbNormalFocus)

'return the hwnd to the shelled process
StartShell = GethWndFromProcessID(hProcessID)

End Function
Private Sub PutTextIntoNotepad(hWndApp As Long)

'This adds text to notepad, by locating its
''Edit' class window. This is similar to the
'method used to locate the hwnd itself, but
'here we know the parent (hWndApp) so only
'have to search its child windows.

Dim hWndChild As Long
Dim sMsg As String
Dim sBuffer As String * 32
Dim nSize As Long

'this string is split only to fit the browser window
sMsg = "This method demonstrates using Shell() "
sMsg = sMsg & "to launch notepad, obtain its hwnd using "
sMsg = sMsg & "GetWindowThreadProcessId, and then use that"
sMsg = sMsg & "hwnd to change its caption and place text into "
sMsg = sMsg & "its Edit window using SetWindowText and
SendMessage."

'get the first child window in Notepad
hWndChild = GetWindow(hWndApp, GW_CHILD)

'hwndchild will = 0 when no more child windows are found
Do While hWndChild <> 0

'get the Class Name of the window
nSize = GetClassName(hWndChild, sBuffer, 32)

'if nSize > 0, it contains the length
'of the class name retrieved
If nSize Then

'if the class name is "Edit",
'set some text and exit
If Left$(sBuffer, nSize) = "Edit" Then

Call SendMessage(hWndChild, WM_SETTEXT, 0&, ByVal sMsg)
Exit Sub

End If

End If

'not found, so get the next hwnd
hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)

Loop

End Sub
Nov 21 '05 #2
Use this, it works. Its a whole form, create a new form and replace its code
contents with this.

Option Explicit On

Option Strict On

Option Compare Binary

Imports System

Imports System.Text

Imports System.Web.Mail

Imports System.Windows.Forms

' <remarks>

' Hauptformular der Anwendung.

' </remarks>

Public Class MainForm

Inherits System.Windows.Forms.Form

#Region " Designer Code"

Public Sub New()

MyBase.New()

InitializeComponent()

' Eigener Initialisierungscode.

Me.txtFrom.Text = "fr**@anonymous.com"

Me.txtTo.Text = "to@anonymous.com"

Me.txtSubject.Text = "Enter title here..."

Me.txtBody.Text = "Write your message here..."

Me.cboPriority.SelectedIndex = 1

End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

Private components As System.ComponentModel.IContainer

Friend WithEvents btnSend As System.Windows.Forms.Button

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents Label2 As System.Windows.Forms.Label

Friend WithEvents Label3 As System.Windows.Forms.Label

Friend WithEvents Label4 As System.Windows.Forms.Label

Friend WithEvents txtFrom As System.Windows.Forms.TextBox

Friend WithEvents txtTo As System.Windows.Forms.TextBox

Friend WithEvents txtCC As System.Windows.Forms.TextBox

Friend WithEvents txtBCC As System.Windows.Forms.TextBox

Friend WithEvents Label5 As System.Windows.Forms.Label

Friend WithEvents txtSubject As System.Windows.Forms.TextBox

Friend WithEvents txtBody As System.Windows.Forms.TextBox

Friend WithEvents lstAttachments As System.Windows.Forms.ListBox

Friend WithEvents Label6 As System.Windows.Forms.Label

Friend WithEvents cboPriority As System.Windows.Forms.ComboBox

Friend WithEvents btnRemoveAttachment As System.Windows.Forms.Button

Friend WithEvents btnAddAttachment As System.Windows.Forms.Button

Friend WithEvents Label7 As System.Windows.Forms.Label

Friend WithEvents Label8 As System.Windows.Forms.Label

<System.Diagnostics.DebuggerStepThrough()> _

Private Sub InitializeComponent()

Me.btnSend = New System.Windows.Forms.Button

Me.Label1 = New System.Windows.Forms.Label

Me.txtFrom = New System.Windows.Forms.TextBox

Me.Label2 = New System.Windows.Forms.Label

Me.txtTo = New System.Windows.Forms.TextBox

Me.Label3 = New System.Windows.Forms.Label

Me.txtCC = New System.Windows.Forms.TextBox

Me.Label4 = New System.Windows.Forms.Label

Me.txtBCC = New System.Windows.Forms.TextBox

Me.Label5 = New System.Windows.Forms.Label

Me.txtSubject = New System.Windows.Forms.TextBox

Me.txtBody = New System.Windows.Forms.TextBox

Me.lstAttachments = New System.Windows.Forms.ListBox

Me.Label6 = New System.Windows.Forms.Label

Me.cboPriority = New System.Windows.Forms.ComboBox

Me.btnRemoveAttachment = New System.Windows.Forms.Button

Me.btnAddAttachment = New System.Windows.Forms.Button

Me.Label7 = New System.Windows.Forms.Label

Me.Label8 = New System.Windows.Forms.Label

Me.SuspendLayout()

'

'btnSend

'

Me.btnSend.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.btnSend.Location = New System.Drawing.Point(187, 320)

Me.btnSend.Name = "btnSend"

Me.btnSend.TabIndex = 18

Me.btnSend.Text = "&Send"

'

'Label1

'

Me.Label1.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.Label1.Location = New System.Drawing.Point(16, 18)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(48, 16)

Me.Label1.TabIndex = 0

Me.Label1.Text = "&From:"

'

'txtFrom

'

Me.txtFrom.Location = New System.Drawing.Point(64, 16)

Me.txtFrom.Name = "txtFrom"

Me.txtFrom.Size = New System.Drawing.Size(192, 20)

Me.txtFrom.TabIndex = 1

Me.txtFrom.Text = ""

'

'Label2

'

Me.Label2.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.Label2.Location = New System.Drawing.Point(16, 42)

Me.Label2.Name = "Label2"

Me.Label2.Size = New System.Drawing.Size(48, 16)

Me.Label2.TabIndex = 2

Me.Label2.Text = "&To:"

'

'txtTo

'

Me.txtTo.Location = New System.Drawing.Point(64, 40)

Me.txtTo.Name = "txtTo"

Me.txtTo.Size = New System.Drawing.Size(192, 20)

Me.txtTo.TabIndex = 3

Me.txtTo.Text = ""

'

'Label3

'

Me.Label3.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.Label3.Location = New System.Drawing.Point(16, 66)

Me.Label3.Name = "Label3"

Me.Label3.Size = New System.Drawing.Size(48, 16)

Me.Label3.TabIndex = 4

Me.Label3.Text = "&CC:"

'

'txtCC

'

Me.txtCC.Location = New System.Drawing.Point(64, 64)

Me.txtCC.Name = "txtCC"

Me.txtCC.Size = New System.Drawing.Size(192, 20)

Me.txtCC.TabIndex = 5

Me.txtCC.Text = ""

'

'Label4

'

Me.Label4.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.Label4.Location = New System.Drawing.Point(16, 90)

Me.Label4.Name = "Label4"

Me.Label4.Size = New System.Drawing.Size(48, 16)

Me.Label4.TabIndex = 6

Me.Label4.Text = "&BCC:"

'

'txtBCC

'

Me.txtBCC.Location = New System.Drawing.Point(64, 88)

Me.txtBCC.Name = "txtBCC"

Me.txtBCC.Size = New System.Drawing.Size(192, 20)

Me.txtBCC.TabIndex = 7

Me.txtBCC.Text = ""

'

'Label5

'

Me.Label5.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.Label5.Location = New System.Drawing.Point(16, 114)

Me.Label5.Name = "Label5"

Me.Label5.Size = New System.Drawing.Size(48, 16)

Me.Label5.TabIndex = 8

Me.Label5.Text = "S&ubject:"

'

'txtSubject

'

Me.txtSubject.Location = New System.Drawing.Point(64, 112)

Me.txtSubject.Name = "txtSubject"

Me.txtSubject.Size = New System.Drawing.Size(192, 20)

Me.txtSubject.TabIndex = 9

Me.txtSubject.Text = ""

'

'txtBody

'

Me.txtBody.Location = New System.Drawing.Point(16, 168)

Me.txtBody.Multiline = True

Me.txtBody.Name = "txtBody"

Me.txtBody.ScrollBars = System.Windows.Forms.ScrollBars.Both

Me.txtBody.Size = New System.Drawing.Size(416, 144)

Me.txtBody.TabIndex = 13

Me.txtBody.Text = ""

'

'lstAttachments

'

Me.lstAttachments.Location = New System.Drawing.Point(264, 32)

Me.lstAttachments.Name = "lstAttachments"

Me.lstAttachments.Size = New System.Drawing.Size(168, 95)

Me.lstAttachments.TabIndex = 15

'

'Label6

'

Me.Label6.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.Label6.Location = New System.Drawing.Point(264, 16)

Me.Label6.Name = "Label6"

Me.Label6.Size = New System.Drawing.Size(64, 16)

Me.Label6.TabIndex = 14

Me.Label6.Text = "&Attachments:"

'

'cboPriority

'

Me.cboPriority.DropDownStyle =
System.Windows.Forms.ComboBoxStyle.DropDownList

Me.cboPriority.Items.AddRange(New Object() {"Low", "Normal", "High"})

Me.cboPriority.Location = New System.Drawing.Point(168, 136)

Me.cboPriority.Name = "cboPriority"

Me.cboPriority.Size = New System.Drawing.Size(88, 21)

Me.cboPriority.TabIndex = 11

'

'btnRemoveAttachment

'

Me.btnRemoveAttachment.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.btnRemoveAttachment.Location = New System.Drawing.Point(352, 134)

Me.btnRemoveAttachment.Name = "btnRemoveAttachment"

Me.btnRemoveAttachment.Size = New System.Drawing.Size(56, 24)

Me.btnRemoveAttachment.TabIndex = 17

Me.btnRemoveAttachment.Text = "&Remove"

'

'btnAddAttachment

'

Me.btnAddAttachment.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.btnAddAttachment.Location = New System.Drawing.Point(288, 134)

Me.btnAddAttachment.Name = "btnAddAttachment"

Me.btnAddAttachment.Size = New System.Drawing.Size(56, 24)

Me.btnAddAttachment.TabIndex = 16

Me.btnAddAttachment.Text = "A&dd..."

'

'Label7

'

Me.Label7.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.Label7.Location = New System.Drawing.Point(120, 138)

Me.Label7.Name = "Label7"

Me.Label7.Size = New System.Drawing.Size(48, 16)

Me.Label7.TabIndex = 10

Me.Label7.Text = "&Priotity:"

'

'Label8

'

Me.Label8.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.Label8.Location = New System.Drawing.Point(16, 152)

Me.Label8.Name = "Label8"

Me.Label8.Size = New System.Drawing.Size(48, 16)

Me.Label8.TabIndex = 12

Me.Label8.Text = "&Message:"

'

'MainForm

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(448, 360)

Me.Controls.Add(Me.Label8)

Me.Controls.Add(Me.Label7)

Me.Controls.Add(Me.btnAddAttachment)

Me.Controls.Add(Me.btnRemoveAttachment)

Me.Controls.Add(Me.cboPriority)

Me.Controls.Add(Me.Label6)

Me.Controls.Add(Me.lstAttachments)

Me.Controls.Add(Me.txtBody)

Me.Controls.Add(Me.Label5)

Me.Controls.Add(Me.txtSubject)

Me.Controls.Add(Me.Label4)

Me.Controls.Add(Me.txtBCC)

Me.Controls.Add(Me.Label3)

Me.Controls.Add(Me.txtCC)

Me.Controls.Add(Me.Label2)

Me.Controls.Add(Me.txtTo)

Me.Controls.Add(Me.Label1)

Me.Controls.Add(Me.txtFrom)

Me.Controls.Add(Me.btnSend)

Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle

Me.MaximizeBox = False

Me.Name = "MainForm"

Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScree n

Me.Text = "SendMail"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSend.Click

Dim mailmsg As New MailMessage

With mailmsg

..From = Me.txtFrom.Text.Trim()

..To = Me.txtTo.Text.Trim()

..Cc = Me.txtCC.Text.Trim()

..Bcc = Me.txtBCC.Text.Trim()

..Subject = Me.txtSubject.Text.Trim()

..Body = Me.txtBody.Text.Trim()

Select Case Me.cboPriority.SelectedIndex

Case 0

..Priority = MailPriority.Low

Case 1

..Priority = MailPriority.Normal

Case 2

..Priority = MailPriority.High

End Select

Dim s As String

For Each s In Me.lstAttachments.Items

..Attachments.Add(New MailAttachment(s))

Next s

End With

Try

SmtpMail.Send(mailmsg)

MessageBox.Show("Your mail has been successfully sent!")

Catch ex As Exception

MessageBox.Show("The following problem occurred when attempting to send your
mail: " & ex.Message)

End Try

End Sub

Private Sub btnAddAttachment_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAddAttachment.Click

Dim ofdlg As New OpenFileDialog

ofdlg.CheckFileExists = True

ofdlg.CheckPathExists = True

If ofdlg.ShowDialog(Me) = DialogResult.OK Then

Me.lstAttachments.Items.Add(ofdlg.FileName)

End If

End Sub

Private Sub btnRemoveAttachment_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnRemoveAttachment.Click

If Me.lstAttachments.SelectedIndex >= 0 Then

Me.lstAttachments.Items.Remove(Me.lstAttachments.S electedItem)

End If

End Sub

Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub

End Class
--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Rob" <rw*********@rtt.uk.com> wrote in message
news:38**************************@posting.google.c om...
Hi all,

I am having trouble converting the code below (found on
http://vbnet.mvps.org/index.html?cod...endmessage.htm) into a
format that will work using vb .NET.

Can anyone have a look at it and let me know what I need to change. I
have tried changing the "hwnd" type into intptr's but there seem to be
other problems too, like it won't allow "lParam As Any" to be
declared.

Many thanks,

Rob
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Copyright ©1996-2004 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Const WM_SETTEXT = &HC

Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal wCmd As Long) As Long

Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, _
lpdwProcessId As Long) As Long

Private Declare Function BringWindowToTop Lib "user32" _
(ByVal hwnd As Long) As Long

Private Declare Function SetWindowText Lib "user32" _
Alias "SetWindowTextA" _
(ByVal hwnd As Long, ByVal _
lpString As String) As Long

Private Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

Private Sub Command1_Click()

Dim hWndApp As Long

'call the wrapper function to start shell,
'and return the handle to the shelled app.
hWndApp = StartShell("notepad.exe")

'if found, prove it!
If hWndApp Then

'change its caption
Call SetWindowText(hWndApp, "The handle to Notepad is " &
CStr(hWndApp))

'bring it to the top
Call BringWindowToTop(hWndApp)

'and add some text to its edit window
Call PutTextIntoNotepad(hWndApp)

End If

End Sub
Private Function GethWndFromProcessID(hProcessIDToFind As Long) As
Long

Dim hWndDesktop As Long
Dim hWndChild As Long
Dim hWndChildProcessID As Long

On Local Error GoTo GethWndFromProcessID_Error

'get the handle to the desktop
hWndDesktop = GetDesktopWindow()

'get the first child under the desktop
hWndChild = GetWindow(hWndDesktop, GW_CHILD)

'hwndchild will = 0 when no more child windows are found
Do While hWndChild <> 0

'get the ThreadProcessID of the window
Call GetWindowThreadProcessId(hWndChild, hWndChildProcessID)

'if it matches the target, exit returning that value
If hWndChildProcessID = hProcessIDToFind Then
GethWndFromProcessID = hWndChild
Exit Do
End If

'not found, so get the next hwnd
hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)

Loop

Exit Function

GethWndFromProcessID_Error:
GethWndFromProcessID = 0
Exit Function

End Function
Private Function StartShell(sApplication As String) As Long

Dim hProcessID As Long

'start using shell, and get the process ID
hProcessID = Shell(sApplication, vbNormalFocus)

'return the hwnd to the shelled process
StartShell = GethWndFromProcessID(hProcessID)

End Function
Private Sub PutTextIntoNotepad(hWndApp As Long)

'This adds text to notepad, by locating its
''Edit' class window. This is similar to the
'method used to locate the hwnd itself, but
'here we know the parent (hWndApp) so only
'have to search its child windows.

Dim hWndChild As Long
Dim sMsg As String
Dim sBuffer As String * 32
Dim nSize As Long

'this string is split only to fit the browser window
sMsg = "This method demonstrates using Shell() "
sMsg = sMsg & "to launch notepad, obtain its hwnd using "
sMsg = sMsg & "GetWindowThreadProcessId, and then use that"
sMsg = sMsg & "hwnd to change its caption and place text into "
sMsg = sMsg & "its Edit window using SetWindowText and
SendMessage."

'get the first child window in Notepad
hWndChild = GetWindow(hWndApp, GW_CHILD)

'hwndchild will = 0 when no more child windows are found
Do While hWndChild <> 0

'get the Class Name of the window
nSize = GetClassName(hWndChild, sBuffer, 32)

'if nSize > 0, it contains the length
'of the class name retrieved
If nSize Then

'if the class name is "Edit",
'set some text and exit
If Left$(sBuffer, nSize) = "Edit" Then

Call SendMessage(hWndChild, WM_SETTEXT, 0&, ByVal sMsg)
Exit Sub

End If

End If

'not found, so get the next hwnd
hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)

Loop

End Sub

Nov 21 '05 #3
Rob
Thanks Ken it works nicely, also One handed man cheers that code I'll
need to have a closer look at when I get the time, it looks very
interesting!!

Thanks again guys!

Rob
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Fred Heida | last post by:
Hi All, maybe this is a sill question..but if i have TextBox and use the SendMessage(..) to send a character the control.. the KeyDown event is not triggered.... in would need to check in the...
8
by: g.franzkowiak | last post by:
Hello everybody, I've tryed to use an interprocess communication via SendMessage on Windows. Unfortunately, nothing goes on ...
5
by: Mark Overstreet | last post by:
I am trying to click a button in another window and I have it's hWnd value so I was trying to use Send message. Here is my code but it doesn't work as expected... response =...
3
by: JSK | last post by:
Hi, As any one worked in VB.NET and made use of Sendmessage API? The Issue I am running into is how to pass pointers of Data Structures (UDT) to the SendMessage. I starting looking at "IntPrt"...
18
by: Lars Netzel | last post by:
Hello! Thanx to this newgroup I have finally, with the help of you guys, gotten this to work halfway.. but the final action is still not working, clicking the "Button2" thru SendMessage(). ...
22
by: SQACSharp | last post by:
I'm trying to get the control name of an editbox in another window. The following code set the value "MyPassword" in the password EditBox but it fail to return the control name of the EditBox. ...
1
by: tommyk | last post by:
Hi I am using the following code to search for a given string in either a listbox or a combo box, but the function always returns the index as "-1". The function fires from the change event of a...
5
by: michelqa | last post by:
Hi, I need to call a lot of different native SendMessage to retreive informations from non managed application. Some win32 messages use struct pointer for lparam....how to create and...
3
by: michelqa | last post by:
Hi, I already post a similar question last week without success. Ok I want to get the current text selection in a RICHEDIT control.. This can be easily done in C++ with EM_EXGETSEL message. I...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.