|
What about simply adding up the RGB values and toggling between black and white foreground color depending on a certain threshold?
Something like this:
if (bgColor.R + bgColor.G + bgColor.B < 384)
fgColor = Color.White;
else
fgColor = Color.Black;
You can play with the threshold value or the weights of r,g and b (because the human eye is more sensitive to yellow-green than it is to blue, for example), but even with the very simple approach above I got good results.
mav
|
|
|
|
|
Thanks everyone, great ideas, it turned out to be simpler than my coding addled brain thought at the time.
We experimented with your ideas and this is the final algo.:
static public Color InvertColor(Color col)<br />
{ <br />
int nSourceColor=col.R + col.G + col.B;<br />
<br />
int r = 255 - col.R; <br />
int g = 255 - col.G; <br />
int b = 255 - col.B; <br />
<br />
int nInvertColor=r+g+b;<br />
<br />
Color invert;<br />
if(nSourceColor-nInvertColor< 28)<br />
invert=Color.White;<br />
else<br />
invert = Color.FromArgb(r,g,b); <br />
return invert;<br />
}
28 seemed to be a good threshold to ensure that grays didn't cancel each other out.
|
|
|
|
|
Could you just make PadButton.ImageProgperty reference Pic.Image?
|
|
|
|
|
I am a student developing a small application to make my job of maintening a few hundred staff computers a little easier. Part of the program opens an Internet Explorer window at a specific URL.
The problem I am running into is figuring out how to send a Javascript Command to this particular Internet Explorer Window (the equivalent of clicking a button for this page). I have searched this site and many others with no luck.
Any suggestions?
|
|
|
|
|
That's really vague.
What's sending javascript to a window? What kind of "program" are you talking about? How does your question at all relate to C# - which is what this forum is about.
Please be more verbose if you would like any help.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Sorry for the vagueness, I am writing the program in C#, which is why I'm asking for help in this forum. I have C# create a new instance of Internet Explorer and then want to interact with this instance, by sending it a javascript command.
<br />
public static void openBrowser(string url)<br />
{<br />
object o = null;<br />
<br />
SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorerClass();<br />
IWebBrowserApp wb = (IWebBrowserApp) ie;<br />
wb.Visible = true;<br />
<br />
wb.Navigate(url, ref o, ref o, ref o, ref o);<br />
}<br />
So, in this example, I could open up a page like the Office Updates Page and have it check for updates:
http://office.microsoft.com/search/redir.aspx?AssetID=ES790020331033&Origin=HH011710291033&CTT=5
Now, that page automatically scans, but then requires the user to press the "Agree and Start Installation" Button (equivalent of executing the javascript method StartInstallation()). Is there any way to execute this javascript command inside my program? Something like wb.JavaScript(string command)?
Hopefully that makes more sense
Chris
|
|
|
|
|
No you cannot send JScript (Microsoft's implementation, which is what IE uses) to the document, but you can walk the DOM (document object model) yourself. Reference the Microsoft.mshtml.dll assembly in your project (should've been installed with VS.NET) and cast the AxWebBrowser2 to IHTMLDocument2 , which is defined in the mshtml namespace.
From there it's a matter of casting objects to the interfaces that implement the methods, properties, or events you need. These members are named the same thing as in scripting because they are the objects that are scripted (script is merely an engine for using an object model, both in IE and Mozilla and many other applications).
For documentation on the HTML DOM, see the HTML and DHTML Reference[^] in the MSDN Library. There will be some differences since you'll be using an interop assembly (which proxies calls to the unmanaged COM server that is MSHTML, which the WebBrowser control (Internet Explorer) hosts for HTML rendering, etc.), but you shouldn't have any problems if you're familiar with the HTML DOM.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thanks for the help Heath, however, I did find a method easier than what you suggested (for my purposes anyway). After loading the current site, I can "send" a javascript command to the IE instance by "navigating" to a javascript command. I've attached a modification of my earlier example.
<br />
public static void openBrowser(string url)<br />
{<br />
object o = null;<br />
<br />
SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorerClass();<br />
IWebBrowserApp wb = (IWebBrowserApp) ie;<br />
wb.Visible = true; <br />
wb.Navigate(url, ref o, ref o,ref o, ref o);<br />
<br />
while(wb.Busy) {}
Thread.Sleep(6000);
<br />
wb.Navigate("javascript:StartInstallation()", ref o, ref o, ref o, ref o);<br />
}<br />
Where, in this instance, my "url" is:
http://office.microsoft.com/search/redir.aspx?AssetID=ES790020331033&Origin=HH011710291033&CTT=5
Thanks again for the help, hope someone else finds this useful in the future.
Chris
|
|
|
|
|
I regularly have questions about what/which way is best, most efficient or ethical.
I want to develop applications that look, feel and work well from both a user and coding perspective.
If as an example I wanted to check if a value had changed, I would consider using something like:
<br />
<br />
int _CurrentValue;<br />
<br />
int CurrentValue<br />
{<br />
set{_CurrentValue = value;}<br />
get{return _CurrentValue;}<br />
}<br />
<br />
bool ValueChanged(int NewValue)<br />
{<br />
if (CurrentValue != NewValue)<br />
{<br />
return true;<br />
} else {<br />
return false;<br />
}<br />
}<br />
I was taught (many years ago) that a method should have one entry and one exit point wherever possible so to abide to that standard, the above could be written as:
<br />
bool ValueChanged(int NewValue)<br />
{<br />
bool result;<br />
if (CurrentValue != NewValue)<br />
{<br />
result = true;<br />
} else {<br />
result = false;<br />
}<br />
return result;<br />
}<br />
I was also taught however that you shouldn't use a variable where one was not needed and as the previous code demonstrated, only the programming style was different the result would be the same.
I was just hoping for comments as which method is best/more efficient.
Regards
Wayne Phipps
____________
Time is the greatest teacher... unfortunately, it kills all of its students
LearnVisualStudio.Net
|
|
|
|
|
his is the strangest thing I've ever seen. Why does 'ValueChanged' change the value ? How would someone looking at the API work this out ?
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
I was testing the values returned from a method and left that in by accident.
I agree, a method that checks if a property has changed should not change the property it was checking.
I have ammended the example as not to cause confusion.
Regards
Wayne Phipps
____________
Time is the greatest teacher... unfortunately, it kills all of its students
LearnVisualStudio.Net
|
|
|
|
|
In that case I have several comments:
1. If I were to write this, I'd just do something like return(CurrentValue == theValue);, there's no need for a bool variable
2. If I did have to use a variable, I'd make sure it had a default value. In cases like this, I'd start with something like bool retVal = false;, and just set it to true if the test passed later
3. You seem to be asking if the current value != a specific value. I'd do this in the calling code: if (myClass.CurrentVale != theValue) instead of adding a method needlessly.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Thanks for your comments, they all make sense.
However I wrote a simple method to demonstrate multiple and single exit points from the method not the simple evaluation of an integer.
Some methods become much more complicated when using nested if or switch statements and it appears very easy to end up with multiple exit points which make the method difficult to follow and debug.
I try to avoid this wherever possible but just wanted other opinions on best practices.
Regards
Wayne Phipps
____________
Time is the greatest teacher... unfortunately, it kills all of its students
LearnVisualStudio.Net
|
|
|
|
|
OK, in that case, I'd always create the return value first, default it to the worst case scenario ( and never create a variable that I don't assign to ), and then I'd prefer switch to if statements where-ever possible, and if my switch statements are getting too big, think about if I can use OO or at least extra levels of call stack depth to factor some of that out.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
HI.Can someone tell me where can I find the code of program which calculates angle between 2 vectors in 2D. Thanx in forward
|
|
|
|
|
Who can tell you where to find stuff? Yahoo can[^] and Google can[^].
How hard is it to type "calculate angle between 2 vectors" in Yahoo or Google?
Long live !
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I have a utility that I wrote that performs a repair and compact on all the access databases in a particular directory. The problem I have is that I have 97 and 2003 databases in the same folder. If I use "Jet OLEDB:Engine Type=5" on the older access 97 files they are unreadable afterword. But if I use "Jet OLEDB:Engine Type=4" to work with the 97 files it blows up on the 2003 files. I would assume that someting like determining if a database is access 97 or 2003 would be simple but I'm finding it increasingly dificult to track down. I can do a "select version" against access 2003 and it gives me the version but the same command dosent exist in 97. I dont want to assume a database is 97 because the command fails. Any assistance on this would be greatly appriceated. Thanks.
|
|
|
|
|
From a very old VB6 utility of the same nature:
I would expect that you could do the same with the OleDbConnection object in .Net, but haven't tried it.
Dim Con As New ADODB.Connection<br />
Dim p As Variant<br />
Con.Open "Provider= Microsoft.Jet.OLEDB.4.0;Data Source=" & src & ";User Id = admin; Password =;"<br />
p = Con.Properties("Jet OLEDB:Engine Type")<br />
Con.Close<br />
Set Con = Nothing<br />
If p = 4 Then<br />
DbEngine.CompactDatabase "Jet OLEDB:Engine Type = 4;Data Source=" & src, "Jet OLEDB:Engine Type = 4;Data Source=" & dest<br />
Else<br />
DbEngine.CompactDatabase "Data Source=" & src, "Data Source=" & dest<br />
End If<br />
'
Anger is the most impotent of passions. It effects nothing it goes about, and hurts the one who is possessed by it more than the one against whom it is directed.
Carl Sandburg
|
|
|
|
|
Hi!
I have some problems reading an XML file with the following code:
--------------
XPathDocument oDoc = new XPathDocument(sXmlFilePath);
XPathNavigator oNavigator = oDoc.CreateNavigator();
XPathNodeIterator oIterator = oNavigator.Select("/pages/page");
if(oIterator.Count == 0)
return;
while(oIterator.MoveNext()) {
//read through the pages
}
-------------
The code works with the following XML:
<?xml version="1.0" encoding="utf-8" ?>
<pages>
<page name-fr="Niveaux" name-en="Levels">
...
</page>
</pages>
But fails with:
<?xml version="1.0" encoding="utf-8" ?>
<pages xmlns="http://tempuri.org/ConfigStructure2.xsd">
<page name-fr="Niveaux" name-en="Levels">
...
</page>
</pages>
Because of the xmlns.
What is the syntax in my "Select" to make sure I don't care about what the xmlns is and go directly to iterate though the pages? Is there another way to skip directly to the "page" nodes?
Thanks!
|
|
|
|
|
IIRC it doesnt work on the default namespace. The trick was to add the namespace with an alias to an XmlNamespaceManager object and pass that to the SelectXXX or XPath methods. Note, you need to prepend the namespace alias in your query.
HTH
top secret Download xacc-ide 0.0.3 now! See some screenshots
|
|
|
|
|
Hi everyone:
I wanted to know what the best way to do this is. I basically have a program that I have made in C#. Requests will be made of this program by sending messages to an MSMQ queue.
Triggers have two things they can invoke, and programs can monitor queues so, I see three ways that this can be done:
1) Invoke an exe as a trigger action that I have programmed (this didn't seem like a good idea)
2) Monitor the queue with a windows service and asynchronously process it (I didn't see anything wrong with this)
3) Invoke a COM object on access.
The third option seemed like the best one for me, but I didn't know how to go about creating a COM object from C# or if it was even possible! Does anyone know if such a thing can be done or whether I should or shouldn't do it?
Thanks a lot!
|
|
|
|
|
I am relatively new to the C# language and need to extract data from a text file. I would like to be able to search the file which is tab delimmited and extract various fields. I have been using StreamReader to read the file but I am not sure of the method that actually searches for relevant fields and then extracts them.
|
|
|
|
|
Are you looking specifically on ways to parse the data once you have read it from the file? Specifically you could look into using regular expressions within .NET as a parsing mechanism. Check out the documentation .NET Framework Regular Expressions[^] from .NET Framework Developer's Guide.
|
|
|
|
|
Thankyou, I will have a read of the documentation. I basically have a text file full of results in rows and columns. The idea is that the user selects the results that need to be viewed and they are extracted from the file.
|
|
|
|
|
You could always use ADO.
Heres an example in VB.Net that I found usefull:
http://www.dotnetspider.com/technology/KBPages/890.aspx[^]
It allows sql queries to be performed against the csv file. You can even perform joins to other csv files in the same directory.
I believe you will have to create a Schema.ini file if you file is tab delimited.
Hope this helps.
Regards
Wayne Phipps
____________
Time is the greatest teacher... unfortunately, it kills all of its students
LearnVisualStudio.Net
|
|
|
|