hwnd and hdc

Posted on 2003-03-12
Medium Priority
Last Modified: 2008-03-17
Sometime, we need the hdc of a windows, wich is easy to get with GetDC
sometime we need the hwnd (handle) in order to get the DC.

How do we get the hwnd ?  for form or something from our project, it's easy, it's a property.  How do I get the hwnd of the Main Windows of Win2k for exemple?  from something I don'T know about ?

I know how to list all process currently running, I could get the hwnd from there, but is there any other mean ?

I want to use peekmessage to intercept and cancel all click from the mouse OUTSIDE my program.  but peekmessage use hwnd.

I can get the DC of Windows 2k with GetDC(0) but the hwnd=0 doesn't work with peekmessage.

what do I do wrong ?
Question by:Genetic_Wolf
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 8120402
To get hWnd of a form not in ur project try using the FindWindow API

Here's the module declaration:
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Then u call it in ur form:

hWnd = FindWindow(vbNullString, sWindowText)

sWindowText is the caption of the form you are looking for.
(For Example:
hWnd = FindWindow(vbNullString, "My Computer")

Good Luck
LVL 10

Accepted Solution

Jason Evans earned 160 total points
ID: 8120425

Hi there.

OK as an example, to get the handle for the main Windows 'screen', you can use tha API:

Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Long

This will return the hWnd for the Windows desktop.

In order to to do the same for another application, outside your app, then you can the FindWindow APi:

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

So for example I can get the hWnd for Notepad by passing the text of the caption found in Notepad.

hWnd =  FindWindow(vbNullString, "Notepad - Untitled.txt")

This might be what your looking for. Hope this helps.

Author Comment

ID: 8151659
thanks for your comment, I will try these as soon as possible and take action concerning this.

I should however clarify something.  What I'm trying to do in fact is to do a peekmessage on ALL program therefor, I think that submiting it to the 'main' windows should do the trick.  that's why I need to get the hwnd of main windows to do a peekmsg.


hWnd = FindWindow(vbNullString, "My Computer")

look fine, but I don't have the name of Main Windows

Myclarfar:  look fine, I think that should work, I will try it.

Author Comment

ID: 8167479
Thanks MrClyfar

This is exactly what I was looking for,  unfortunately, it does not solve my peekmessage probleme, but It was the function I was looking for to retrieve the handle of Main Windows.


Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question