Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to invoke a windows app from fox app 4

Status
Not open for further replies.

johncook

Programmer
Joined
Nov 24, 2002
Messages
154
Location
US
I have a form that requires user typed input.

My app is touchscreen.

Win XP has an accesory "On screen keyboard".

I want to invoke this accessory in the load or init of this form and release it in the form's release.

I may need to "tell" the accessory where I want it placed on the screen, etc.

So basically I guess I am asking how do you "run" a windows "program" from my VFP form program.

I extensively looked in MS's knowledgebase but found nothing.

Thanks,
John
 
* here is some code that should work
* Assumes you have a file myfile.txt
* it will launch notepad

appLaunch("c:\myfile.txt")

***********************************************************************
FUNCTION appLaunch
PARAMETER tcOutFile,tcCmd

IF PCOUNT() < 1
MESSAGEBOX(&quot;You did not provide a filename.&quot;,&quot;AppLauch Method&quot;)
RETURN
ENDIF

IF PCOUNT() < 2
tcCmd = &quot;Open&quot;
ENDIF

tcCmd = PROPER(m.tcCmd)

* This code was originally located in Ken Levy's DBF2XML program so blame
* Ken for the bugs :)

* API Call to communicate with an application based on the registered
* file-type.
* For example:
* On my computer txt is notepad; DOC is word

DECLARE INTEGER ShellExecute ;
IN SHELL32.DLL ;
INTEGER nWinHandle,;
STRING cOperation,;
STRING cFileName,;
STRING cParameters,;
STRING cDirectory,;
INTEGER nShowWindow

* tip: to determine commands available for each filetype
* Launch explorer
* go to View / Folder Options
* Select the File Type Tag
* go to the item you wish to look up (example Acrobat)
* Press Edit
* You will see acrobat has Open, Print, and Printto option types.

IF !FILE(tcOutFile)
MESSAGEBOX(tcOutFile + &quot; does not exist!&quot;,&quot;CAPP -- AppLauch Method&quot;)
ELSE

* play with this.
* by adding the ,0 instead of ,1 I was able to get it to
* print directly without opening the document on my sceen.


* this is handy if you wish to print a pdf, word document,
* spreadsheet, text file, etc. from within your applicaiton.
IF m.tcCmd = &quot;Print&quot;
lnFileStatus = ShellExecute(0,&quot;&tcCmd&quot;,tcOutFile,&quot;&quot;,&quot;&quot;,0)
ELSE
lnFileStatus = ShellExecute(0,&quot;&tcCmd&quot;,tcOutFile,&quot;&quot;,&quot;&quot;,1)
ENDIF
DO CASE
CASE m.lnFileStatus > 32
* successful open
CASE m.lnFileStatus = 2
MESSAGEBOX(&quot;Bad Association (for example, invalid URL)&quot;,;
&quot;CAPP -- AppLauch Method&quot;)
CASE m.lnFileStatus = 29
MESSAGEBOX(&quot;Failure to load application&quot;,&quot;CAPP -- AppLauch Method&quot;)
CASE m.lnFileStatus = 30
MESSAGEBOX(&quot;Application is busy&quot;,&quot;CAPP -- AppLauch Method&quot;)
CASE m.lnFileStatus = 31
MESSAGEBOX(&quot;No application association with specified &quot;+;
&quot;command: &quot; + m.tcCmd,&quot;CAPP -- AppLauch Method&quot;)
OTHERWISE
MESSAGEBOX(&quot;Unknown Error&quot;,&quot;CAPP -- AppLauch Method&quot;)
RETURN .F.
ENDCASE
ENDIF



Jim Osieczonek
Delta Business Group, LLC
 
Thanks Jim,
I have been looking at other postings as recommended by Dave. I believe I &quot;get it&quot; that I will use a shellexecute command. I have never done so before. I have posted a question on MSoft foxpro developers net to: How do I determine what parameters to pass to this keyboard app and are there parameters I can pass that tell it where to be placed on the screen, etc.

I am a little lost here but will figure it out eventually.
Thanks,
John
 
OK, I am stuck.

Here is the text from VFP Help on shellexecute:

This class enables you to launch an application or document with its associated application from the current application.

To use, drop the class on a project or form or, from the Component Gallery Item Shortcut Menu, select Add to Project or Add to Form. When you add the class to a form, Visual FoxPro places the class on the form. You can then specify the appropriate property values and provide any necessary input and output objects. When you drop the class on a project, you can choose between adding the class or creating a subclass.


Question 1: &quot;from the Component Gallery Item Shortcut Menu&quot;
I see nothing in the component gallery when I launch it from the Tools menu. What is a &quot;Component Gallery Item Shortcut Menu&quot;?

Thanks,
John
 
johncook,

All you need is this:

RUN /N7 osk

...now the above code assumes that you are looking for the Windows XP on screen keyboard and not some third-party on screen keyboard.


Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
Hi

In your main.prg, at the beginning before the event processing starts, put the code..

DECLARE INTEGER ShellExecute IN shell32.dll ;
INTEGER hndWin, STRING cAction, STRING cFileName, ;
STRING cParams, STRING cDir, INTEGER nShowWin

Now where you want to run the application..

=ShellExecute(0,&quot;open&quot;,&quot;NotePad.exe&quot;,&quot;myText.txt&quot;,&quot;c:\&quot;,1)

OR

cExe = &quot;NotePad.EXE&quot;
cParam = &quot;Test.Txt&quot;
cDir = &quot;C:\&quot;
=ShellExecute(0,&quot;open&quot;,cExe,cParam,cDir,1)

:-)

ramani :-)
(Subramanian.G)
 
Slighthaze/Ramani,
I must be DENSE.

Thanks,
John
 
Well there is that little item that you want where you could position the &quot;on screen keyboard&quot; at the position you wanted. I didn't help you with that. however, there should be a pretty straight-forward way to do it with API calls using the window handle, which you could get using FindWindow API call. I don't have time to write it for you right now, but maybe another member or maybe you could figure it out and post it back here so other members in the future will know how.

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
slighthaze,
I would love to do exactly what you are telling me but I do not understand what you are telling me.

To give you an idea of how lost I am, I tried typing: &quot;FindWindow API call&quot; in the VFP command window.

Can you point me in the right direction?

Thanks,
John
 
johncook,

Ok here's the code. It will open the On-Screen Keyboard application, get a reference to its window handle and then use that to move it to the bottom-center of your screen. I am also showing you how to use ShellExecute rather than the RUN command just for informational purposes. Cut-N-Paste the code below into a prg file and run it from within VFP.


#DEFINE SM_CXSCREEN 0
#DEFINE SM_CYSCREEN 1

LOCAL lnScrWidth, lnScrHeight, lnWinWidth, lnWinHeight

DO DeclareAPIs &&Need these functions

ShellExecute (0, &quot;open&quot;, &quot;osk&quot;, &quot;&quot;, &quot;&quot;, 0)
*!* RUN /N7 osk && This works too

=INKEY(2) &&Give windows a moment to open it

lnhwnd = FindWindow (0, &quot;On-Screen Keyboard&quot;)

GetWidthHeight(lnhwnd, @lnWinWidth, @lnWinHeight)

lnScrWidth = GetSystemMetrics(SM_CXSCREEN)
lnScrHeight = GetSystemMetrics(SM_CYSCREEN)

*!* Bottom Centered
MoveWindow (lnhwnd,;
(lnScrWidth-lnWinWidth)/2, lnScrHeight-lnWinHeight,;
lnWinWidth, lnWinHeight, 1)

*!* *!* Centered
*!* MoveWindow (lnhwnd,;
*!* (lnScrWidth-lnWinWidth)/2, (lnScrHeight-lnWinHeight)/2,;
*!* lnWinWidth, lnWinHeight, 1)

*!* *!* Upper Left
*!* MoveWindow (lnhwnd,;
*!* 0, 0,;
*!* lnWinWidth, lnWinHeight, 1)

***********************

PROCEDURE DeclareAPIs
***********************
DECLARE INTEGER ShellExecute IN shell32;
INTEGER hwnd, STRING lpOperation,;
STRING lpFile, STRING lpParameters,;
STRING lpDirectory, INTEGER nShowCmd
DECLARE INTEGER FindWindow IN user32;
STRING lpClassName, STRING lpWindowName
DECLARE INTEGER GetSystemMetrics IN user32 INTEGER nIndex
DECLARE INTEGER MoveWindow IN user32;
INTEGER HWND, INTEGER X, INTEGER Y,;
INTEGER nWidth, INTEGER nHeight, INTEGER bRepaint
DECLARE INTEGER GetWindowRect IN user32 INTEGER HWND, STRING @lpRect
ENDPROC

***********************
FUNCTION buf2int(cBuffer)
***********************
RETURN ASC(SUBSTR(cBuffer, 1,1)) + ;
BITLSHIFT(ASC(SUBSTR(cBuffer, 2,1)), 8) +;
BITLSHIFT(ASC(SUBSTR(cBuffer, 3,1)), 16) +;
BITLSHIFT(ASC(SUBSTR(cBuffer, 4,1)), 24)
ENDFUNC

***********************
PROCEDURE GetWidthHeight(phwnd, pWidth, pHeight)
***********************
LOCAL lpRect, lnWinLeft, lnWinTop, lnWinRight, lnWinBottom
lpRect = REPLI (CHR(0), 16)
GetWindowRect (phwnd, @lpRect)
lnWinLeft = buf2int(SUBSTR(lpRect, 1,4))
lnWinTop = buf2int(SUBSTR(lpRect, 5,4))
lnWinRight = buf2int(SUBSTR(lpRect, 9,4))
lnWinBottom = buf2int(SUBSTR(lpRect, 13,4))
pWidth = lnWinRight - lnWinLeft + 1
pHeight = lnWinBottom - lnWinTop + 1
ENDPROC


Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
Thanks all,
Slighthaze,
I ran the code. I understand a little of the code but I need to do some background work on the big picture such as exactly what is an API call, etc. It appears you may be &quot;asking&quot; the on-screen keyboard what it's parameters are or something like that. This will take some time but I sure appreciate the head start.

Thanks again,
John
 
johncook,

Go to and check out the information available there... There is alot of stuff that you can look at for free there (even more stuff if you are a paid member of the site). As far as API calls from VFP that site will really help explain the whole process. My code above, while not a direct copy from the news2news site, relies heavily on the API declarations and stuff that I learned from that site. So anyways, go check it out and all of this may start to make more sense.

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
Thanks Slighthaze. I will definitely do that. I am one of those people that just has to understand WHY it works. HAHA
John
 
johncook,

The windows API, (Application Program Interface), Function calls are &quot;Hooks&quot; into Microsoft's .DLL (Dynamic Link Libraries) files. The files that windos OS uses to do everything.

They actually call the &quot;C++&quot; language code functions exposed to programmers like everyone here.

This allows use to do things in the MS-Windows platforms or OS (Operating Systems)that our regular &quot;High-level&quot; programming languages, such as Visual-FoxPro, can not do.

&quot;High-level&quot; languages, like VFP, can usally accomplish things in less lines of code and easier-to-understand code.
 
justamistere
Thanks for the explanation. It does help.
John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top