473,721 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 GetDesktopWindo w Lib "user32" () As Long

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

Private Declare Function BringWindowToTo p 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 "GetClassNa meA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessag eA" _
(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("not epad.exe")

'if found, prove it!
If hWndApp Then

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

'bring it to the top
Call BringWindowToTo p(hWndApp)

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

End If

End Sub
Private Function GethWndFromProc essID(hProcessI DToFind As Long) As
Long

Dim hWndDesktop As Long
Dim hWndChild As Long
Dim hWndChildProces sID As Long

On Local Error GoTo GethWndFromProc essID_Error

'get the handle to the desktop
hWndDesktop = GetDesktopWindo w()

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

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

'get the ThreadProcessID of the window
Call GetWindowThread ProcessId(hWndC hild, hWndChildProces sID)

'if it matches the target, exit returning that value
If hWndChildProces sID = hProcessIDToFin d Then
GethWndFromProc essID = hWndChild
Exit Do
End If

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

Loop

Exit Function

GethWndFromProc essID_Error:
GethWndFromProc essID = 0
Exit Function

End Function
Private Function StartShell(sApp lication As String) As Long

Dim hProcessID As Long

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

'return the hwnd to the shelled process
StartShell = GethWndFromProc essID(hProcessI D)

End Function
Private Sub PutTextIntoNote pad(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 & "GetWindowThrea dProcessId, 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(hWndA pp, 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(hW ndChild, 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(hWn dChild, WM_SETTEXT, 0&, ByVal sMsg)
Exit Sub

End If

End If

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

Loop

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

Declare Function SendMessage Lib "user32" Alias "SendMessag eA" _

(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 "GetClassNa meA" _

(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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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.Mai nWindowHandle, GW_CHILD)

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

Do While Not hwndChild.Equal s(IntPtr.Zero)

'get the Class Name of the window

nSize = GetClassName(hw ndChild, 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(hwn dChild, WM_SETTEXT, 0, "Test")

Exit Sub

End If

End If

Loop

End Sub
Ken
-------------------
"Rob" <rw*********@rt t.uk.com> wrote in message
news:38******** *************** ***@posting.goo gle.com...
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 GetDesktopWindo w Lib "user32" () As Long

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

Private Declare Function BringWindowToTo p 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 "GetClassNa meA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessag eA" _
(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("not epad.exe")

'if found, prove it!
If hWndApp Then

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

'bring it to the top
Call BringWindowToTo p(hWndApp)

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

End If

End Sub
Private Function GethWndFromProc essID(hProcessI DToFind As Long) As
Long

Dim hWndDesktop As Long
Dim hWndChild As Long
Dim hWndChildProces sID As Long

On Local Error GoTo GethWndFromProc essID_Error

'get the handle to the desktop
hWndDesktop = GetDesktopWindo w()

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

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

'get the ThreadProcessID of the window
Call GetWindowThread ProcessId(hWndC hild, hWndChildProces sID)

'if it matches the target, exit returning that value
If hWndChildProces sID = hProcessIDToFin d Then
GethWndFromProc essID = hWndChild
Exit Do
End If

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

Loop

Exit Function

GethWndFromProc essID_Error:
GethWndFromProc essID = 0
Exit Function

End Function
Private Function StartShell(sApp lication As String) As Long

Dim hProcessID As Long

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

'return the hwnd to the shelled process
StartShell = GethWndFromProc essID(hProcessI D)

End Function
Private Sub PutTextIntoNote pad(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 & "GetWindowThrea dProcessId, 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(hWndA pp, 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(hW ndChild, 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(hWn dChild, WM_SETTEXT, 0&, ByVal sMsg)
Exit Sub

End If

End If

'not found, so get the next hwnd
hWndChild = GetWindow(hWndC hild, 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()

InitializeCompo nent()

' Eigener Initialisierung scode.

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

Me.txtTo.Text = "to@anonymous.c om"

Me.txtSubject.T ext = "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.Disp ose()

End If

End If

MyBase.Dispose( disposing)

End Sub

Private components As System.Componen tModel.IContain er

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 btnRemoveAttach ment As System.Windows. Forms.Button

Friend WithEvents btnAddAttachmen t As System.Windows. Forms.Button

Friend WithEvents Label7 As System.Windows. Forms.Label

Friend WithEvents Label8 As System.Windows. Forms.Label

<System.Diagnos tics.DebuggerSt epThrough()> _

Private Sub InitializeCompo nent()

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.lstAttachmen ts = New System.Windows. Forms.ListBox

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

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

Me.btnRemoveAtt achment = New System.Windows. Forms.Button

Me.btnAddAttach ment = New System.Windows. Forms.Button

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

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

Me.SuspendLayou t()

'

'btnSend

'

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

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

Me.btnSend.Name = "btnSend"

Me.btnSend.TabI ndex = 18

Me.btnSend.Text = "&Send"

'

'Label1

'

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

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

Me.Label1.Name = "Label1"

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

Me.Label1.TabIn dex = 0

Me.Label1.Text = "&From:"

'

'txtFrom

'

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

Me.txtFrom.Name = "txtFrom"

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

Me.txtFrom.TabI ndex = 1

Me.txtFrom.Text = ""

'

'Label2

'

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

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

Me.Label2.Name = "Label2"

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

Me.Label2.TabIn dex = 2

Me.Label2.Text = "&To:"

'

'txtTo

'

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

Me.txtTo.Name = "txtTo"

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

Me.txtTo.TabInd ex = 3

Me.txtTo.Text = ""

'

'Label3

'

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

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

Me.Label3.Name = "Label3"

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

Me.Label3.TabIn dex = 4

Me.Label3.Text = "&CC:"

'

'txtCC

'

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

Me.txtCC.Name = "txtCC"

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

Me.txtCC.TabInd ex = 5

Me.txtCC.Text = ""

'

'Label4

'

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

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

Me.Label4.Name = "Label4"

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

Me.Label4.TabIn dex = 6

Me.Label4.Text = "&BCC:"

'

'txtBCC

'

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

Me.txtBCC.Name = "txtBCC"

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

Me.txtBCC.TabIn dex = 7

Me.txtBCC.Text = ""

'

'Label5

'

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

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

Me.Label5.Name = "Label5"

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

Me.Label5.TabIn dex = 8

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

'

'txtSubject

'

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

Me.txtSubject.N ame = "txtSubject "

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

Me.txtSubject.T abIndex = 9

Me.txtSubject.T ext = ""

'

'txtBody

'

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

Me.txtBody.Mult iline = True

Me.txtBody.Name = "txtBody"

Me.txtBody.Scro llBars = System.Windows. Forms.ScrollBar s.Both

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

Me.txtBody.TabI ndex = 13

Me.txtBody.Text = ""

'

'lstAttachments

'

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

Me.lstAttachmen ts.Name = "lstAttachments "

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

Me.lstAttachmen ts.TabIndex = 15

'

'Label6

'

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

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

Me.Label6.Name = "Label6"

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

Me.Label6.TabIn dex = 14

Me.Label6.Text = "&Attachmen ts:"

'

'cboPriority

'

Me.cboPriority. DropDownStyle =
System.Windows. Forms.ComboBoxS tyle.DropDownLi st

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

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

Me.cboPriority. Name = "cboPriorit y"

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

Me.cboPriority. TabIndex = 11

'

'btnRemoveAttac hment

'

Me.btnRemoveAtt achment.FlatSty le = System.Windows. Forms.FlatStyle .System

Me.btnRemoveAtt achment.Locatio n = New System.Drawing. Point(352, 134)

Me.btnRemoveAtt achment.Name = "btnRemoveAttac hment"

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

Me.btnRemoveAtt achment.TabInde x = 17

Me.btnRemoveAtt achment.Text = "&Remove"

'

'btnAddAttachme nt

'

Me.btnAddAttach ment.FlatStyle = System.Windows. Forms.FlatStyle .System

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

Me.btnAddAttach ment.Name = "btnAddAttachme nt"

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

Me.btnAddAttach ment.TabIndex = 16

Me.btnAddAttach ment.Text = "A&dd..."

'

'Label7

'

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

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

Me.Label7.Name = "Label7"

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

Me.Label7.TabIn dex = 10

Me.Label7.Text = "&Priotity: "

'

'Label8

'

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

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

Me.Label8.Name = "Label8"

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

Me.Label8.TabIn dex = 12

Me.Label8.Text = "&Message:"

'

'MainForm

'

Me.AutoScaleBas eSize = 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.btnAddAttac hment)

Me.Controls.Add (Me.btnRemoveAt tachment)

Me.Controls.Add (Me.cboPriority )

Me.Controls.Add (Me.Label6)

Me.Controls.Add (Me.lstAttachme nts)

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.FormBorderSt yle = System.Windows. Forms.FormBorde rStyle.FixedSin gle

Me.MaximizeBox = False

Me.Name = "MainForm"

Me.StartPositio n = System.Windows. Forms.FormStart Position.Center Screen

Me.Text = "SendMail"

Me.ResumeLayout (False)

End Sub

#End Region

Private Sub btnSend_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnSend.Click

Dim mailmsg As New MailMessage

With mailmsg

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

..To = Me.txtTo.Text.T rim()

..Cc = Me.txtCC.Text.T rim()

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

..Subject = Me.txtSubject.T ext.Trim()

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

Select Case Me.cboPriority. SelectedIndex

Case 0

..Priority = MailPriority.Lo w

Case 1

..Priority = MailPriority.No rmal

Case 2

..Priority = MailPriority.Hi gh

End Select

Dim s As String

For Each s In Me.lstAttachmen ts.Items

..Attachments.A dd(New MailAttachment( s))

Next s

End With

Try

SmtpMail.Send(m ailmsg)

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 btnAddAttachmen t_Click(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnAddAttachmen t.Click

Dim ofdlg As New OpenFileDialog

ofdlg.CheckFile Exists = True

ofdlg.CheckPath Exists = True

If ofdlg.ShowDialo g(Me) = DialogResult.OK Then

Me.lstAttachmen ts.Items.Add(of dlg.FileName)

End If

End Sub

Private Sub btnRemoveAttach ment_Click(ByVa l sender As System.Object, ByVal e
As System.EventArg s) Handles btnRemoveAttach ment.Click

If Me.lstAttachmen ts.SelectedInde x >= 0 Then

Me.lstAttachmen ts.Items.Remove (Me.lstAttachme nts.SelectedIte m)

End If

End Sub

Private Sub MainForm_Load(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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*********@rt t.uk.com> wrote in message
news:38******** *************** ***@posting.goo gle.com...
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 GetDesktopWindo w Lib "user32" () As Long

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

Private Declare Function BringWindowToTo p 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 "GetClassNa meA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessag eA" _
(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("not epad.exe")

'if found, prove it!
If hWndApp Then

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

'bring it to the top
Call BringWindowToTo p(hWndApp)

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

End If

End Sub
Private Function GethWndFromProc essID(hProcessI DToFind As Long) As
Long

Dim hWndDesktop As Long
Dim hWndChild As Long
Dim hWndChildProces sID As Long

On Local Error GoTo GethWndFromProc essID_Error

'get the handle to the desktop
hWndDesktop = GetDesktopWindo w()

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

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

'get the ThreadProcessID of the window
Call GetWindowThread ProcessId(hWndC hild, hWndChildProces sID)

'if it matches the target, exit returning that value
If hWndChildProces sID = hProcessIDToFin d Then
GethWndFromProc essID = hWndChild
Exit Do
End If

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

Loop

Exit Function

GethWndFromProc essID_Error:
GethWndFromProc essID = 0
Exit Function

End Function
Private Function StartShell(sApp lication As String) As Long

Dim hProcessID As Long

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

'return the hwnd to the shelled process
StartShell = GethWndFromProc essID(hProcessI D)

End Function
Private Sub PutTextIntoNote pad(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 & "GetWindowThrea dProcessId, 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(hWndA pp, 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(hW ndChild, 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(hWn dChild, WM_SETTEXT, 0&, ByVal sMsg)
Exit Sub

End If

End If

'not found, so get the next hwnd
hWndChild = GetWindow(hWndC hild, 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
35500
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 WndProc(...) of the WM_CHAR event... Is there another way to send characters to the TextBox, when I'm only having the control's handle which does simulated the user typing in characters so the KeyDown event gets
8
6698
by: g.franzkowiak | last post by:
Hello everybody, I've tryed to use an interprocess communication via SendMessage on Windows. Unfortunately, nothing goes on ######################################################################### #! /usr/bin/env python import win32api, win32ui, win32con
5
19405
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 = Win32API.SendMessage(hWndYesButton,Win32API.BM_SETSTATE,0,null); response = Win32API.SendMessage(hWndYesButton,Win32API.WM_LBUTTONDOWN,1,"11"); response = Win32API.SendMessage(hWndYesButton,Win32API.WM_LBUTTONDOWN,1,null); response =...
3
10163
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" as a data type but it appears to fall short of using pointer types. Any help or insight would be greatly appreciated. Thank You
18
6560
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(). Private Declare Auto Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer Private Declare Auto Function FindWindowEx Lib "user32" (ByVal hWnd1 As
22
9249
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. I'm sure the problem is the way i'm using the sendmessage API, the return string and the lParam return 0....is anybody have a clue? any sendmessage api expert here? public static extern Int32 FindWindow(String lpClassName,String
1
2662
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 textbox. This is a program that I am converting from vb6.0, so I think that there may have been a problem in translation. There were two upgrade warnings, which I have left in in code, as they may be the root of the problem! SendMessage...
5
5234
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 marshaling the struct to be able to use it in sendmessage... Here is an example LM_GETITEM: http://msdn.microsoft.com/en-us/library/bb760720(VS.85).aspx
3
6150
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 really need to do the same thing in C#. How can I put the structure in memory to be able to call SendMessage and get the expected results in the structure.
0
8840
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9131
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5981
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3189
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.