Hello!
I am new to API calls that take use of a device context. I just read up a little on what a device context is, and have been working on a certain "test" procedure that ultimately will be used at work.
The procedure that I am trying to get to work will actually be used for two circumstances:
1) A little while back, I originally attempted to pull the text from another application's window. The reason was to monitor the transfer of data through the application's proprietary transfer process. I can no longer remember, but I believe I used SendMessage with a WM_GETTEXT parameter. Anyhow, I was able to retrieve nearly all the text from the window except for the actual text I was focused on retrieving. I believe the text that I wanted and couldn't get would not work because it was hitting a proprierty control labeled as a "Static" type. So, I thought I could take a screenshot of the window and run OCR to get the text.
2) I need to monitor a command shell window to determine when it is ready for input. Normally, I could simply pipe the text to a file and monitor the file, however the text I am looking for will not pipe to a file. I believe due to security purposes, piping was deliberatly blocked (When using a RunAs command, it prompts for a password. I would like to use a better method than simply "sleeping" for a few seconds). Once again, I considered using OCR on an image of the window.
So, I came up with the following code (please excuse it's messiness as I was simply trying to throw something together to see if I could even get it working):
Private Sub testSub()
Dim Hwndwindow As Long
Dim Form1DC As Long
Dim lMemoryDC As Long
Dim lBmp, lOldBmp As Long
Dim modiDoc As New MODI.Document
Dim modiImg As New MODI.Image
Dim rgxStringSearch As New RegExp
Dim strRegexResults() As String
Do
Hwndwindow = 5704966 'I just grabbed the hWnd from something similar to Spy++
Form1DC = GetDC(0)
lMemoryDC = CreateCompatibleDC(Form1DC)
'lBmp = CreateCompatibleBitmap(Form1DC, 347, 275)
lBmp = CreateCompatibleBitmap(Form1DC, 300, 300)
lOldBmp = SelectObject(lMemoryDC, lBmp)
DeleteObject lOldBmp
DeleteObject lBmp
PrintWindow Hwndwindow, lMemoryDC, 0
Dim testDC, testBmp, testOldBmp As Long
testDC = CreateCompatibleDC(Form1DC)
'lBmp = CreateCompatibleBitmap(Form1DC, 347, 275)
testBmp = CreateCompatibleBitmap(Form1DC, 300, 300)
testOldBmp = SelectObject(testDC, testBmp)
DeleteObject testOldBmp
DeleteObject testBmp
MsgBox StretchBlt(testDC, 0, 0, 300, 300, lMemoryDC, 100, 100, 100, 100, SRCCOPY)
If Dir("C:\Documents and Settings\myID\Desktop\testPic.bmp") <> "" Then
Kill "C:\Documents and Settings\myID\Desktop\testPic.bmp"
End If
stdole.SavePicture GetBitmapPic(lBmp), "C:\Documents and Settings\myID\Desktop\testPic.bmp"
If Dir("C:\Documents and Settings\myID\Desktop\testPic2.bmp") <> "" Then
Kill "C:\Documents and Settings\myID\Desktop\testPic2.bmp"
End If
stdole.SavePicture GetBitmapPic(testBmp), "C:\Documents and Settings\myID\Desktop\testPic2.bmp"
modiDoc.Create "C:\Documents and Settings\myID\Desktop\testPic.bmp"
'can I skip the file?
modiDoc.OCR miLANG_ENGLISH, False, False
Set modiImg = modiDoc.Images(0)
rgxStringSearch.Search modiImg.Layout.Text, "password", False, strRegexResults
If strRegexResults(0) = "password" Then
MsgBox "yes"
Else
MsgBox "no"
End If
DeleteDC Form1DC
DeleteDC lMemoryDC
Loop While 1
End Sub
When I view the file "testPic.bmp", the image is as I had expected. However, "testPic2.bmp" always gives a pure-black image, no matter what parameters I use in StretchBlt. I would like to pin-point the image retrieved to the specific section I would like to look at, rather than the upper-left portion of the window.
Could anyone be of any assistance? Please be patient as I am new to this type of thing. Also, if you know of a way I can simply avoid creating a file for the OCR to grab the image, that would be helpful as well, as I assume writing a new file over and over would be inefficient.
Thank you in advance for all of your help and patience! If you know of any better method for these two examples, feel free to offer your suggestions! Perhaps there is something I am not aware of with SendMessage to where I could skip the OCR process altogether. Or, perhaps the password-prompting text CAN be piped an I did something wrong (I could get it to pipe manually, but not through code). Thanks again for your help!
I am new to API calls that take use of a device context. I just read up a little on what a device context is, and have been working on a certain "test" procedure that ultimately will be used at work.
The procedure that I am trying to get to work will actually be used for two circumstances:
1) A little while back, I originally attempted to pull the text from another application's window. The reason was to monitor the transfer of data through the application's proprietary transfer process. I can no longer remember, but I believe I used SendMessage with a WM_GETTEXT parameter. Anyhow, I was able to retrieve nearly all the text from the window except for the actual text I was focused on retrieving. I believe the text that I wanted and couldn't get would not work because it was hitting a proprierty control labeled as a "Static" type. So, I thought I could take a screenshot of the window and run OCR to get the text.
2) I need to monitor a command shell window to determine when it is ready for input. Normally, I could simply pipe the text to a file and monitor the file, however the text I am looking for will not pipe to a file. I believe due to security purposes, piping was deliberatly blocked (When using a RunAs command, it prompts for a password. I would like to use a better method than simply "sleeping" for a few seconds). Once again, I considered using OCR on an image of the window.
So, I came up with the following code (please excuse it's messiness as I was simply trying to throw something together to see if I could even get it working):
Private Sub testSub()
Dim Hwndwindow As Long
Dim Form1DC As Long
Dim lMemoryDC As Long
Dim lBmp, lOldBmp As Long
Dim modiDoc As New MODI.Document
Dim modiImg As New MODI.Image
Dim rgxStringSearch As New RegExp
Dim strRegexResults() As String
Do
Hwndwindow = 5704966 'I just grabbed the hWnd from something similar to Spy++
Form1DC = GetDC(0)
lMemoryDC = CreateCompatibleDC(Form1DC)
'lBmp = CreateCompatibleBitmap(Form1DC, 347, 275)
lBmp = CreateCompatibleBitmap(Form1DC, 300, 300)
lOldBmp = SelectObject(lMemoryDC, lBmp)
DeleteObject lOldBmp
DeleteObject lBmp
PrintWindow Hwndwindow, lMemoryDC, 0
Dim testDC, testBmp, testOldBmp As Long
testDC = CreateCompatibleDC(Form1DC)
'lBmp = CreateCompatibleBitmap(Form1DC, 347, 275)
testBmp = CreateCompatibleBitmap(Form1DC, 300, 300)
testOldBmp = SelectObject(testDC, testBmp)
DeleteObject testOldBmp
DeleteObject testBmp
MsgBox StretchBlt(testDC, 0, 0, 300, 300, lMemoryDC, 100, 100, 100, 100, SRCCOPY)
If Dir("C:\Documents and Settings\myID\Desktop\testPic.bmp") <> "" Then
Kill "C:\Documents and Settings\myID\Desktop\testPic.bmp"
End If
stdole.SavePicture GetBitmapPic(lBmp), "C:\Documents and Settings\myID\Desktop\testPic.bmp"
If Dir("C:\Documents and Settings\myID\Desktop\testPic2.bmp") <> "" Then
Kill "C:\Documents and Settings\myID\Desktop\testPic2.bmp"
End If
stdole.SavePicture GetBitmapPic(testBmp), "C:\Documents and Settings\myID\Desktop\testPic2.bmp"
modiDoc.Create "C:\Documents and Settings\myID\Desktop\testPic.bmp"
'can I skip the file?
modiDoc.OCR miLANG_ENGLISH, False, False
Set modiImg = modiDoc.Images(0)
rgxStringSearch.Search modiImg.Layout.Text, "password", False, strRegexResults
If strRegexResults(0) = "password" Then
MsgBox "yes"
Else
MsgBox "no"
End If
DeleteDC Form1DC
DeleteDC lMemoryDC
Loop While 1
End Sub
When I view the file "testPic.bmp", the image is as I had expected. However, "testPic2.bmp" always gives a pure-black image, no matter what parameters I use in StretchBlt. I would like to pin-point the image retrieved to the specific section I would like to look at, rather than the upper-left portion of the window.
Could anyone be of any assistance? Please be patient as I am new to this type of thing. Also, if you know of a way I can simply avoid creating a file for the OCR to grab the image, that would be helpful as well, as I assume writing a new file over and over would be inefficient.
Thank you in advance for all of your help and patience! If you know of any better method for these two examples, feel free to offer your suggestions! Perhaps there is something I am not aware of with SendMessage to where I could skip the OCR process altogether. Or, perhaps the password-prompting text CAN be piped an I did something wrong (I could get it to pipe manually, but not through code). Thanks again for your help!