Welcome to MSDN Blogs Sign in | Join | Help

Programming means that sometimes you have to snap two blocks together

Part of the challenge of programming (and for some people, the reason why programming is fun in the first place) is looking at the building blocks provided to you and deciding how to assemble them to build something new. After all, if everything you wanted a program to do already existed ready-made, it wouldn't be called programming any more. It would be called shopping.

Is there an API or a quick way to find out which window the mouse is in?

I replied, "The LEGO Group does not make a piece for every possible object. Sometimes you just have to take two LEGO blocks and click them together. Here are some interesting blocks: GetCursorPos, WindowFromPoint."

Thanks for your reply. But WindowFromPoint gives me the window of the object at the location of the cursor. But I'm looking for the top level window containing the cursor.

Fine, then use a different block.

I wonder how it is these people manage to write programs at all. I get the impression they write code by asking a million tiny questions and cutting and pasting together all the replies.

No wait, pasting together the replies counts as snapping blocks together. Maybe they just ask for completed programs.

Published Tuesday, August 04, 2009 7:00 AM by oldnewthing
Filed under:

Comments

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 10:39 AM by DWalker59

Shopping.  Heh heh, that's good!

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 10:50 AM by Pierre B.

The problem is that they have stubby fingers and you give them tiny blocks. They wanted to know in which ribbon in Word the command was.

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 10:59 AM by Joseph Koss

I've watched for the past decade or so as programming forums fielded homework questions. There is always someone who will answer.

So now we've got quite a few "graduates" that just got someone else to do their homework for them.

Now, what you describe is the only way they CAN program.

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 11:05 AM by John

I think the problem is that the API documentation is difficult to work with; if you don't know exactly what you're looking for, it can be a real chore to find it.  Most likely this person simply didn't know of the existence of these functions.  I didn't know about them (other than GetCursorPos), but then again I don't do a lot of UI work.

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 11:06 AM by Dan

I wrote a screenshot app that used an overlay and mouse pointing to allow the user to click a window (they were actually clicking the overlay).

Of course when I used WindowFromPoint I ran into my first problem: The overlay was the topmost window!

I ended up having to manually enumerate all the desktop windows using EnumDesktopWindows from front to back and get their bounds and check to see if the mouse cursor was in each of them.  In the latest version I cache the window handles and bounds so that this doesn't happen every time you move the mouse, which was quite slow.

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 11:33 AM by Neil (SM)

Ahh yes. The "Plz send me the codes" people.

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 12:18 PM by someone else

“The problem is that they have stubby fingers and you give them tiny blocks.”

Isn't that what they made DUPLO for? And Visual Basic?

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 2:01 PM by Joe Dietz

I've been working recently with a few (ahem) 'colleagues'.  Thus far my experience has been a series of 'this looks hard, can you show me the instructions on how to do this?' with the 'this' being whatever task was assigned to them.  They seem rather put out that I'm being obnoxious by not sending them the answers that I obviously already have.  I guess I'm just waiting for them to post private source code to a public forum asking for help...

# A more important conundrum

Tuesday, August 04, 2009 2:06 PM by JohnFx htt

My mouse is all the way to the left side of my mouse pad, but I want to move my pointer further left.

Do I need to buy another mouse-pad?

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 4:23 PM by Mike

normally these sorts of questions are followed up by "my program keeps getting closed, how do i stop this 'illegal operation' window closing my program?"

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 4:26 PM by Kevin Provance

>> After all, if everything you wanted a program to do already existed ready-made, it wouldn't be called programming any more. It would be called shopping. <<

Enter the .Nxt Framework.

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 4:33 PM by dave

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 4:54 PM by Rob

@Dan: You might have been able to keep things simple if you set WS_EX_TRANSPARENT on your overlay window in its LBUTTONUP handler - then when you call WindowFromPoint it would "fall through" your overlay.

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 5:26 PM by kip

I imagine Raymond would not have any tolerance for answering the sorts of bad questions that come in on stackoverflow.com

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 5:37 PM by someone

However, WindowFromPoint isn't really reliable. Sometimes there are multiple windows at the same position and with the same parent. Then you can get a window which isn't visible (perhaps the first or latest created).

I forgot how to solve it, but IMHO one of the oldest solution for this was to choose the parent of the window returned by WindowFromPoint

and then enumerate all child windows  and choose the largest from the possible set, because this probably contains the other found windows

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 5:57 PM by tb

A simple no would probably suffice, as your responses don't seem to provide what the asker is looking for: a quick way to find the top level window at the location of the cursor.

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 6:26 PM by David Heffernan

Raymond,

Is anyone at Microsoft aware that there is a bug in GetCursorPos when running LARGEADDRESSAWARE 32 bit processes on 64 bit Windows (Vista in my test case).  If the address of lpPoint (the single parameter to GetCursorPos) has an address >2GB then the call to GetCursorPos fails.

I run my dev machine with top down memory allocation and so trip over this all the time.  When I eventually worked out what was going on I worked around the problem by calling GetCursorInfo instead.

That's fine for my app but I think I'm still being hit by the bug in the html help viewer.  The help file for my app has a few popup topics (<a href="some-URL" target="_popup"> in the html source).  The popups appear in a bizarre place (underneath the TOC pane) when I run in top-down mode but appear right next to the hypertext link when I run in bottom-up mode.  My psychic debugging powers (which I freely admit are puny compared to yours!) detect the scent of a GetCursorPos problem.

Now, I don't know how to submit an official bug report (it's probably very easy I know).  Anyway, please don't flame me, I know that you aren't responsible for the entire Windows API.  I just thought that since you'd brought up GetCursorPos then you and your loyal readers may be interested in this.

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 6:51 PM by K

Yeah, people who don't know as much as those working for the company whose software they're targeting sure are dumb.

Those really weren't bad questions. Documentation can be way more annoying than the actual problem at hand, and last I checked, the Win32 API isn't the friendliest thing in the world.

Of course, I was going to write a far snarkier reply until I realized that ChildWindowFromPoint was linked in the WindowFromPoint documentation twice, soooo there's that...

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 7:15 PM by Warren Porter

There are thousands of API calls. Do you really expect every programmer to know exactly which one to call for any possible problem that may come up? Instead of smarmy comments about programmers who don't know the API as well as you do, why don't you provide some useful advice on how to find something in the documentation?

[Since this is a window manager issue, you don't have to look at all of Win32, just the window manager functions, which is a much more manageable set. And there are only a handful of functions whose names include the words "window" and "point". -Raymond]

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 7:19 PM by Alexandre Grigoriev

@David Heffernan,

Someone already added user comment about this problem on GetCursorPos MSDN page http://msdn.microsoft.com/en-us/library/ms648390(VS.85).aspx on June 22 2009. I think MS is now aware of it.

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 8:41 PM by Bob

I find the names of the people asking these sort of questions give clue to their origin, and it's mostly the same one country, isn't it?

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 8:44 PM by David Heffernan

@Alexandre Grigoriev,

Thanks a lot, I hadn't spotted that before - it's quite recent and I did my debugging of this perhaps 6 months ago.

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 11:41 PM by DK

There are two ways to find functions that you need to do something.

1. Learn to read the reference documentations.

2. Learn to ask experts.

# re: Programming means that sometimes you have to snap two blocks together

Tuesday, August 04, 2009 11:48 PM by Ulric

That second reply to the user really isn't clear to me...  is the idea to call ChildWindowFromPoint with NULL (or GetDesktopWindow()) as the parent? If so, ... probably this user doesn't have the skill to guess this.

In our application... I believe we use WindowFromPoint, then we search through the parents to get to the top level window.

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 12:54 AM by Me

JohnFX: No, don't buy another mouse pad, just shove your desk in the opposite direction.

PS you stole that from an old Dilbert.  ;-)

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 1:06 AM by Bob

The hard way: "I have a problem, ok, now I have to search the documentation for an answer".

The easy way: "I've read the documentation and I've been doing this for a while, I'm sure I remember something in there that will help."

And the funny thing is that it doesn't matter whether you're talking about software or embroidery - the people who put effort into educating themselves will always be better than the people who only resort to learning when they desperately need to solve a problem.

You don't have to work for Microsoft. But if you choose to write software for Windows, then you're going to have a rough time unless you have some interest in learning about the platform and how to program for it.

And, to be completely honest, there's far too many Linux fanbois who have the exact same approach to MySQL and PHP - they hit a problem, they reinvent a square wheel because they've never once read through the manual just to find out what's already included in the box. Or they ask the internet for advice on the best way to file down their square peg to put it in the round hole.

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 1:41 AM by Worf

Another way is to play around with programs. See one that does something cool? Make a note of it. If it's open source, grab a copy and move on, knowing that the "how did they do it" is locked away in there.

Or if not, break out the tools and spy.

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 2:09 AM by Anonymous Coward

I think the Windows documentation is actually pretty good. It's all in one place, well-structured, indexed, logically ordered and pretty consistent.

I would also like to take the opportunity to link to one of Raymond's other articles that I feel the people in the article need to read before proceeding:

http://blogs.msdn.com/oldnewthing/archive/2004/11/30/272262.aspx

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 2:21 AM by amazed at your arrogance

Yes, sometimes people don't do their legwork. Maybe, though, you should take it as an opportunity to recognize that most people don't give a crap about your api, they jsut want to do something that they care about. So, instead figure out why it wasn't obvious, apparently to most of the programmers replying in the comments, how to use your api correctly. Maybe a blog post on how you failed to make your api intuitive and helpful for people who want to write apps.

Just a thought. But much easier to dish on people.

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 3:13 AM by aaawww

the win32 API is intuitive and helpful.

Perfectly indexed and cross linked.

With examples, good practices, common pitfall for each hard to understand call.

The problems lies in laziness of those self-called programmers which sells themselves as expert and doesn't even know where to start a search in the documentation.

look at this google search

http://www.google.it/search?rlz=1C1CHMG_itIT291IT303&sourceid=chrome&ie=UTF-8&q=window+under+cursor

the first result for me is a visual basic example. guess which method this code calls?

sure, you have to know that the platform api is uniform across languages, but hey, an "expert" win32 programmer is no more required to know this, in your opinion...

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 4:53 AM by Daran

[i]Tuesday, August 04, 2009 11:41 PM by DK

There are two ways to find functions that you need to do something.

1. Learn to read the reference documentations.

2. Learn to ask experts.[/i]

Step 0. Use Google

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 7:29 AM by HagenP

[quote]

I wonder how it is these people manage to write programs at all.

I get the impression they write code by asking a million tiny questions and cutting and pasting together all the replies.

[/quote]

Exactly - they "frankenstein together" their code.

From reading OSR's NTDEV group, it looks to me like some software companies from cheap-labor countries employ (at least some) "highly skilled software developers" who exclusively work this way.

The student questions are already bad - at least they changed somewhat from "send response urgent with complete source and full working program" to "how to make it work" for frankensteined-together code snippets from the NET and other news posts.

But what _really_ scares me are developers for "real-world" app and driver software who obviously work exactly the same way. Next time they might design your car's braking control software. Ugh.

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 7:50 AM by DaveShaw

"I wonder how it is these people manage to write programs at all. I get the impression they write code by asking a million tiny questions and cutting and pasting together all the replies."

Some people do this, except they ask google instead... It's called popfly.

Dave

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 7:50 AM by Brian Tkatch

"After all, if everything you wanted a program to do already existed ready-made, it wouldn't be called programming any more. It would be called shopping."

Of course, hence the TV shopping channel. It's also TV programming, but the shows are ready made. Every other channel, however, are still in progress. :)

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 8:00 AM by Olivier

@Warren Porter : seriously, go get a new job if you can't read and/or search documentations...

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 8:49 AM by internet

You have to work *with* the morons on the internet, not against them. If you ask politly, noone usually bothers to answer at all.

1. Complain in public that the platform/framework/api/environment doesn't support what you want to do.

2. Some asshat will "disprove" your statement.

3. Problem solved.

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 11:31 AM by DWalker59

@Alexandre:  Just because someone added a comment to an MSDN documentation page, doesn't necessarily mean that "Microsoft" is "aware" of the problem in a meaningful sense.

Ideally, a Bug report or a Connect issue should be opened.  That way, someone from the right department within Microsoft will be given the task of responding to the issue, and maybe it will be added as a bug and considered for future fixing.

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 1:10 PM by arnshea

Microsoft puts out some of the best documentation I've ever seen.  I don't know if it was always this way but compared to man pages and GNU info for the various unix-like OSes, MSDN is lightyears ahead.

If you never use the documentation, you never get really good at using it.  Once you're good at it it's gets much easier to work with ANY part of windows because so much of it is so well documented.

When the system is working people like this aren't still programming 5 to 10 years later.  They probably don't enjoy "putting the blocks together" so in some sense deserve sympathy because they're in a job that they really don't enjoy.

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 2:42 PM by Jason Gerstorff

I think that its easy to develop a habit of asking for help before you've exhausted all available resources. Some hand-holding is helpful for new programmers, but its probably best to tell repeat offenders how to approach the problem, rather than the exact solution.

I'm sure this guy was just not adept at navigating MSDN. It does take a while to learn to find what you're looking for.

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 2:52 PM by GrumpyYoungMan

@Warren Porter

@amazed at your arrogance

"Give a man a fish and he's fed for a day.  Teach a man to fish and he'll berate you angrily for not giving him a fish."

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 4:32 PM by Bob

Remember Raymond, not everyone is a Windows API expert like you.

MSDN is not exactly clean and easy to use.

[That's fine, but at least try before giving up. -Raymond]

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 4:57 PM by MS

The real problem is that nobody teaches people how to learn and discover on their own anymore.  (Or maybe they never did.  I dunno.)

I found this amusing, though, a blog that lists hundreds of "plz send me teh codez": http://plzsendmethecode.blogspot.com/

# re: Programming means that sometimes you have to snap two blocks together

Wednesday, August 05, 2009 5:00 PM by MS

Well, not really hundreds.  Closer to a few dozen.

# re: Programming means that sometimes you have to snap two blocks together

Thursday, August 06, 2009 1:18 AM by Worf

Yeah. no one explores anymore.

I once gave new hires a simple quiz on things that they can find on the server... it's all there, but unless you read and index the stuff and not monkey around, you'll never know. I found those people became much better exployees after I showed them how to explore our servers.

Just simple stuff, like where can I find the datasheet for a chip by manufacturer (our datasheets are arranged by manufacturer, but I don't expect people to know the manufacturer on the first day. It also gives them something they can search against...).

Mostly because I got frustrated at those who kept pestering me for datasheets or other stuff without even doing a cursory look at the server where I showed them the other datasheet.

# re: Programming means that sometimes you have to snap two blocks together

Thursday, August 06, 2009 2:39 AM by Toukarin

"I wonder how it is these people manage to write programs at all. I get the impression they write code by asking a million tiny questions and cutting and pasting together all the replies."

Sadly that's how the majority of how the 'programmers' in the industry work, and contribute to thedailywtf.com.

And that's precisely how a colleague of mine does 'programming'. And he has a PhD in Computer Science from one of the top 50 universities. Incredible.

# re: Programming means that sometimes you have to snap two blocks together

Thursday, August 06, 2009 3:07 AM by steveg

"I wonder how it is these people manage to write programs at all. I get the impression they write code by asking a million tiny questions and cutting and pasting together all the replies."

The internet's to blame. If technical documentation was still in book form people would be more inclined to browse. IMO. That's how I learnt the Win16 API, browsing through the paper manuals.

Heck, I don't even read a book on the toilet anymore, just my laptop...

# re: Programming means that sometimes you have to snap two blocks together

Thursday, August 06, 2009 3:39 AM by Anon

> I've watched for the past decade or so as programming forums fielded homework questions. There is always someone who will answer.

>

> So now we've got quite a few "graduates" that just got someone else to do their homework for them.

The sad thing about these people is that they will quickly get promoted to management where they will brag about how the "come from a technical background" when really all they ever did was pass any work they had to someone else. Of course the worst case is that they end up analyzing requests from customers. Since they have no idea how to solve problems, they also have no idea how to do time estimates. Needless to say it is considered very rude to comment on their abilities or lack thereof.

Meh, the software industry sucks.

# re: Programming means that sometimes you have to snap two blocks together

Thursday, August 06, 2009 5:48 AM by porter

>> There are thousands of API calls. Do you really expect every programmer to know exactly which one to call for any possible problem that may come up?

There are thousands of places in this city. Do you seriously expect every taxi driver to know them how to get to them?

Yes, that's their job.

# re: Programming means that sometimes you have to snap two blocks together

Thursday, August 06, 2009 8:02 AM by Torvin

@Dan: why do you need an overlay anyways? maybe SetCapture() would be just enough for you?

# re: Programming means that sometimes you have to snap two blocks together

Thursday, August 06, 2009 8:56 PM by GregM

DWalker, it was reported to connect, back in February, and was fixed for Windows 7.  The backport to Vista was rejected.  (I found this information through a Google search on GetCursorPos and LARGEADDRESSAWARE.)

Thanks to the person who reported about it here, this is the second time in a couple months that we've replaced GetCursorPos calls in our application, the first time to use GetMessagePos instead, and this time to work around the bug.

# re: Programming means that sometimes you have to snap two blocks together

Thursday, August 06, 2009 11:37 PM by ulric

I have a question about this LargeAddressAware and GetCursorPos issue.

what's the risk of getting high memory address if your 32-bit process is not using a lot memory?  I mean, the addresses we get are not physical addresses, so the load of the system should not really matter, should it?

# re: Programming means that sometimes you have to snap two blocks together

Saturday, August 08, 2009 12:43 AM by Falcon

First, since Raymond has previously indicated that he welcomes people pointing out typos, there's one in this entry's subtitle: "Not everything is ready-mady." (At least it looks like one!)

@porter: I wouldn't expect a taxi driver to know every place and/or street address off the top of their head, however they should know the city's general layout. This, combined with with directories/maps/GPS navigators, would be the optimal strategy, which fits in with Raymond's point in the article. I myself would not blindly follow a GPS navigator's directions - I can optimise the route using knowledge that they don't have!

As a passenger, I'll often give a taxi driver directions, particularly when we get close to home - being arrogant by asserting that they should know how to get there and refusing to assist them would not help me efficiently achieve my goal of getting to the destination.

In many cases where I use a taxi service, I could very well make the trip as a driver myself (a common obstacle to driving being prior ingestion of ethanol!)

# re: Programming means that sometimes you have to snap two blocks together

Monday, August 10, 2009 10:16 AM by HagenP

DWalker59:

"Ideally, a Bug report or a Connect issue should be opened.  That way, someone from the right department within Microsoft will be given the task of responding to the issue, and maybe it will be added as a bug and considered for future fixing."

For a lot of things Microsoft does not make it exactly easy to open a bug report. Also I have seen Connect (WDK) issues untouched for several months.

However, I've had some excellent experiences giving feedback to MSDN documentation pages.

New Comments to this post are disabled
 
Page view tracker