|
Hi,
I'm very new in c# code. I would like to have a list a windows that open in current desktop in window z order.I have go through the internet and found this code
public partial class Form1 : Form
{
public const int GW_HWNDNEXT = 2;
public const int GW_HWNDPREV = 3;
[DllImport("user32.dll")]
static extern IntPtr GetTopWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "GetWindow", SetLastError = true)]
public static extern IntPtr GetNextWindow(IntPtr hwnd, [MarshalAs(UnmanagedType.U4)] int wFlag);
public Form1()
{
InitializeComponent();
}
public static Form GetTopMostWindow(IntPtr hWnd_mainFrm)
{
Form frm = null;
IntPtr hwnd = GetTopWindow((IntPtr)null);
if (hwnd != IntPtr.Zero)
{
while ((!IsWindowVisible(hwnd) || frm == null) && hwnd != hWnd_mainFrm)
{
hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
try
{
frm = (Form)Form.FromHandle(hwnd);
}
catch
{
}
}
}
return frm;
}
}
My question how to get the GetTopMostWindow() return value from form1() class?
Thank you 
|
|
|
|
|
bunge-bunge wrote: My question how to get the GetTopMostWindow() return value from form1() class?
Form1.GetTopMostWindow(Application.MainForm.Handle);
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I'm at a loss for what this is supposed to be? Ever seen this pattern before?
[Serializable]
public class MyCollection : ArrayList {
public enum MyUpdateFields {
MyGroupName
}
}
So, that's basically an ArrayList that defines a useless enum? Why even create this in the first place? I would not even know this exists if Code Analysis hadn't complained about it.
|
|
|
|
|
Jasmine2501 wrote: So, that's basically an ArrayList Basically, yes. The author should be complimented for writing a specific collection-class, as opposed to using generics throughout the app for each collection. It makes changes easier, as one can easily change the collection-class, without having to check each method that uses the collection.
Jasmine2501 wrote: that defines a useless enum? I'm not sure whether it's "useless"; it's merely defined in the collection-class, implying that it's used there, or in conjunction with that class. If it's not used (place the cursor there an hit F12 to find all references) than it'd be better to remove it.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I don't see the point in having an Enum that only defines one value. What possible purpose could that have? I can make it private and the app still builds - nobody is using it, and I posted all the code for the class, it's not using it. My concern is not about the unused code though, it's about and Enum with only one value. How do you think that might be used? It can only ever have one value, and it can't be null. I don't see the point. I was kind of wondering if it's done for some reason I've never heard of - a lot of this codebase is copied straight from the MSDN.
I agree that sometimes a named class that doesn't extend the "system" class makes sense. However, I don't have any issues with using ArrayList, if that's what you need. It is a waste of code and a useless increase in complexity - I don't need my own special ArrayList class, the system one is fine. I agree with what you're saying, but I don't think it's enough of an advantage in most cases. "Don't use the generic class" is often used to increase complexity for no reason at all. When the code is not very well documented, it puts me in the position of "OK, I see this class doesn't do anything extra from ArrayList, so why is it here?" and I can go and look at places where it's used and it's doing nothing special, and then I think "certainly the guy before me didn't write extra code just because someone told him not to use the generic classes" - sends me down a rabbit hole of trying to understand reasoning I didn't participate in.
In this question, I'm looking for someone to say, "oh yeah, that's the Lipshitz design pattern, here's a link"
|
|
|
|
|
Jasmine2501 wrote: What possible purpose could that have? Was it in one of their examples? Isn't it just there as a proxy for "you could hypothetically have a enum here.."?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I don't know where they got it from. I only had access to the previous developer for 'not long enough' and he showed me only the things he thought were important. He would be happy to admit that he has zero experience with ASP.Net - the site was converted from PHP, so it has some weird patterns and an overall odd style.
|
|
|
|
|
It's not a useless enum! The enum is simply scoped to the class MyCollection (a terrible name, if that's the actual name). So you can do stuff like this (for example). It's assumed that Foo has a member of type MyCollection.MyUpdateFields .
MyCollection coll = new MyCollection();
Foo f = new Foo();
foo.UpdateField = MyCollection.MyGroupName;
coll.Add (foo);
It's too bad the author hasn't commented their code. (This wouldn't even be accepted for a code review where I work).
/ravi
|
|
|
|
|
I can see in your sample code that you have created a variable of the enum type, and set the value to be the only possible value for that enum - that is all fine and good, but what are you going to use that value for? Remember, setting values that don't get used is a waste of memory - the compiler even warns you about it sometimes.
And yeah, I've changed the names to protect the innocent.
|
|
|
|
|
It's an example to demonstrate enum scoping.
/ravi
|
|
|
|
|
Could you explain what you mean by that? Or point me to a link, Google isn't finding anything with that term.
|
|
|
|
|
By scoped enum, I simply mean an enum that's scoped to a class. See the answer to this[^] SO question.
/ravi
|
|
|
|
|
I'd rather think the code stayed incomplete for some reason.
When overriding the Add() method of the collection (which he hasn't done yet), he could e.g. check that the item to be added has the correct value in its MyUpdateFields property and otherwise throw an exception.
Of course, Generics are a better way to solve it, but they were not available in e.g. .Net 1.1. And since the application was originally written in PHP, some odd ways of doing things might have their origin there.
|
|
|
|
|
Jasmine2501 wrote: Why even create this in the first place? Why even bother to try and figure out what this little-code-monster is, when you can get busy, and write better code than this dinosaur coprolite ?
Probably, the author intended, at some point, to extend the enumeration, for some purpose: who cares ?
'ArrayList is deprecated, for good reasons.
Suggest you review the section titled "Performance Considerations" here: [^].
yours, Bill
~
“This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal
|
|
|
|
|
BillWoodruff wrote: Why even bother to try and figure out what this little-code-monster is, when you can get busy, and write better code
Because I'm in a testing phase right now and can't write additional code until that's done. The best I can do at the moment is try to understand what's here already. That is pretty important when taking over maintenance of undocumented apps.
|
|
|
|
|
Hello All,
I have an application that frequently uses the general List object with an bitmap type". I have two questions concerning these.
1: What is the best way to copy the bitmaps from one list to another while ensuring the Bitmaps in the original list are unchanged if changes are made to the new list. Currently i'm doing it in a forloop such as:
List<Bitmap> aviList1 = new List<Bitmap>(); aviList1.Clear();
for (int i=0; I originalList.Count; i++)
aviList1.Add(new Bitmap(originalList[i]));
2: Is calling originalList.Clear sufficient to free up the memory or would it be necessary to loop through the list and dispose of the Bitmaps individually?
Thanks in advance for the always great support here at CodeProject.
Bryan
|
|
|
|
|
Bitmaps are a scarce resource: they implement IDisposable, so you should clean up behind you, and Dispose all bitmaps you create. If you don't you may well get "Out of Memory" problems long before the actual RAM is exhausted. Additionally, depending on how you initially constructed the bitmaps, you may leave files or streams in use until the bitmap.Dispose method is called by your code, or by the garbage collector.
You need to construct a new image for each that you add to the other list, if you want the original unmodified when the aviList1[index] bitmap is changed.
However, it would look tidier with a foreach loop:
foreach (Bitmap b in originalList)
aviList1.Add(new Bitmap(b)); Or you could do it with Linq:
aviList.AddRange(originalList.Select(bm => new Bitmap(bm))); (But that just hides the loop)
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
Hi OG,
I am inferring from your answer (upvoted), that, in this case, it would be best for the OP to loop through his List<bitmap> and specifically dispose of them: that he should not rely on just clearing the List<bitmap>: is that correct ?
thanks, Bill
~
“This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal
|
|
|
|
|
Yes - clearing the list does not dispose of any of the items on it (thank goodness!) so the bitmaps will be left hanging until the garbage collector gets round to disposing of them for you.
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
If I may be so bold as to humbly point you to a little article[^] that I wrote a while back on memory managing images, you might find that it helps a lot. I use this a lot.
|
|
|
|
|
Sorry chaps, I couldn't find an appropriate forum so I'll take a chance I won't get flamed for using this one. I have a regex question. Given an input string like this:
Some text containing % complete and not much else
If I use a regex of \bcomplete\b I can find the whole word complete just fine. What I want to do is find % complete. I've tried \b% complete\b and \b\% complete\b and \b\x25 complete\b and other variations I can think of but I can never get it to select % complete. Does anyone know if it's possible to do it and how? I can find nothing anywhere that says % cannot participate in an expression.
I've tried two different apps, such as RegexBuilder and RegexBuddy but I can't get it working in either. Does anyone have any ideas? Thanks.
If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.
|
|
|
|
|
Leslie Nielsen 151576 wrote: I couldn't find an appropriate forum
What, like this[^] one?
|
|
|
|
|
Point taken. I based my search on the menu you get when clicking discussions at the top of the page. I didn't notice the All Message Boards...
I'll post there.
If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.
|
|
|
|
|
Hi,
I have a problem with treeview in windows application
for example i have a treeview as follows with checkboxes:-
Root0
Node0
Node1
Root1
Node2
Node3
now if i checked node0 nothing will happn with root0 but if node0 and node1 both are checked thn root0 can be shows as checked
and if i check on root0 thn both nodes with be checked automatically
i m trying soo hard but m found nothing still, can anyone help me with it
|
|
|
|
|
Firstly I would suggest that you explain what you have done more, possibly show some code as we can then potentially see where the problem is rather than guess.
Secondly I wouldn't post your problem multiple times as this is condsidered rude / bad form.
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|