Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
  • 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] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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
Course of the Month10 days, 18 hours left to enroll

572 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