News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Dialog Handle

Started by Hunk, April 23, 2012, 10:49:36 AM

Previous topic - Next topic

Hunk

Is it possible to get the handle of a dialog box by using its Control ID only?

fearless

Could maybe use CreateDialogParam as it returns a handle to the dialog and uses the dialogs control id as a parameter: http://msdn.microsoft.com/en-us/library/windows/desktop/ms645445%28v=vs.85%29.aspx

Maybe something like:
MyOtherDlgProc PROTO :DWORD, :DWORD, :DWORD, :DWORD

.const
IDD_OTHERDIALOG EQU 1000 ; dialog resource is set to 1000

.data?
hOtherDlg dd ? ; dialog handle

.code
Invoke CreateDialogParam, hInstance, IDD_OTHERDIALOG, NULL, OFFSET MyOtherDlgProc, NULL
mov hOtherDlg, eax ; save as handle to dialog in our var

Also as an alternative you could always save your dialogs handle from WM_INITDIALOG of your dialog to a global var, that can be accessed easily?
.ELSEIF eax == WM_INITDIALOG
   mov eax, hWin
   mov hOtherDlg, eax ; got hWin for this dialog, so save it for later use
ƒearless

dedndave

GetDlgItem function (you need the handle of the parent window, of course)
it works for non-dialog windows, as well
http://msdn.microsoft.com/en-us/library/windows/desktop/ms645481%28v=vs.85%29.aspx

fearless

lol, i was just thinking that as well dave, GetDlgItem might be an easier option.
ƒearless

donkey

A dialog box does not in itself have a control ID, only controls have that. It does however have a resource ID and GetDlgItem will not return the handle of the dialog with that since it can be used to create multiple dialogs from a single resource. Since the resource ID is only used at creation to load the template and is not saved by Windows there is no way I know of to extract a handle from it. You will have to use fearless's suggestion and save the handle of the dialog when it is created or walk the window list for your program using FindWindow or some such function using the dialog title or class.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

dedndave

it might, if it is a child window

i mean, apparently, he has a control ID of something and wants the handle   :bg

donkey

Quote from: dedndave on April 23, 2012, 02:40:21 PM
it might, if it is a child window   :P

Outside of simple tab controls I have never seen a dialog that has the WS_CHILD style set, though they may be used in property sheets, not much experience with those. I think he might be confusing a resource ID with a control ID.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

dedndave

yah - the best way to get a dialog box handle is when you receive WM_INITDIALOG in the DialogProc
get it from the hwndDlg parameter and store it in a global

NoCforMe

You want the handle to a dialog box that's actually open, right? Well, it's not what you asked for, but it's really easy to get this, and you don't even need the dialog ID. Just  use


    INVOKE GetWindow, MainWinHandle, GW_ENABLEDPOPUP
    MOV dialogHandle, EAX


This gets the handle to the currently-enabled popup of your main window, which would be the dialog. Now, if you need to distinguish among several different dialogs which could be up, that's a different story, but if you know a particular dialog is open, this will get you its handle.