View Full Version : There has got to be a faster way
A_LOTA_NOTA
2009-01-09, 09:29 PM
I have pasted a partial list of the if statements I'm using (didn't see the need to past them all). I would think something like a switch case would be faster but using "IndexOf" I can't figure out how to get it to work. Wanted to see if anyone here had any ideas.
if (strWindowName.IndexOf("AutoCAD") > -1)
{
strNewAppName = "ACAD";
}// ACAD
if (strWindowName.IndexOf("Internet Explorer") > -1)
{
strNewAppName = "Explorer";
}// Internet Explorer
if (strWindowName.IndexOf("Microsoft Excel") > -1)
{
strNewAppName = "Excel";
}// Excel
if (strWindowName.IndexOf("Microsoft Outlook") > -1)
{
strNewAppName = "Outlook";
}// Outlook
Kerry Brown
2009-01-10, 06:14 AM
Pretty difficult to form an idea from that code.
Do you realize that the strNewAppName variable will hold the value of whichever test is the last to return true.
are you sure you didn't intend to use the following testing ?
if
else if
else
Switch-case will test an integral or string type expression, so are you able to assign either to the expression ?
schrodingerscat
2009-01-12, 02:58 AM
Try this (though I didn't really get what you were doing so this is based on the assumption that strWindowName is an output of a current window and I'm ignoring the indexof part):
string ProgName = strWindowName.ToString();
switch(ProgName)
{
case "AutoCAD":
strNewAppName = "ACAD";
case etc
Not too sure what you're doing though so this may be wrong.
A_LOTA_NOTA
2009-01-12, 02:01 PM
Sorry, I will try to give a some more info.
First I'm getting the active window name.
private void GetActiveWindow()
{
const int nChars = 256;
int handle = 0;
StringBuilder Buff = new StringBuilder(nChars);
handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
strWindowName = Buff.ToString();
}
}// end GetActiveWindow
Then I'm using that info to compare to a list of Apps that I use & that is where all the if statements are used.
schrodingerscat
2009-01-12, 09:39 PM
So why do you need the IndexOf? Couldn't you just compare the strings by slightly modifying what I put above?
Or is the IndexOf test there to first check whether or not the application is actually open?
Going under the assumption that I'm right about that, you may need to look at assigning not the window title to a variable but instead assigning the executable name. This will output either ACAD, ACAD.exe, or c:\Program Files\AutoCAD 2008\ACAD.exe.
I haven't ever actually tried this, so I'm guessing a lot of this, but I'd say give this a try:
reference System.Diagnostic;
Assign window name to a variable, use a function of that variable to get the executable details and then convert that to a string.
A_LOTA_NOTA
2009-01-13, 02:06 PM
Randy,
I am very much a beginner & much of what I know I have learned from examples I have found on the net. I originally started out my search trying to find an example on how to get the exe of the active window. All I could find was how to get the title of the foreground window. Not knowing if what I was looking for was even possible I went with what I could find. I'll see what I can find & try doing what you have suggested.
Thanks for your help!
A_LOTA_NOTA
2009-01-14, 04:01 AM
I'm not having any luck getting the executable of the active window. I’m sure it can be done, I just haven’t been able to do it yet.
Powered by vBulletin® Version 4.1.11 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.