The MASM Forum

General => The Workshop => Topic started by: Dinadan Dinadan on March 12, 2014, 09:14:55 PM

Title: How to use SetParent for a non-parent Window?
Post by: Dinadan Dinadan on March 12, 2014, 09:14:55 PM
Hi,

I think it's correct forum to ask this question, right?  :biggrin:

I try create a non-parent Window (a textbox with no parent window) with the following code.
but the textbox not appear on the Window which it's handle is hwnd.


   hTextBox1 = CreateWindow(
                              TEXT("EDIT"),
                              TEXT(""),
                              WS_VISIBLE|WS_CHILD|WS_BORDER|ES_AUTOHSCROLL,
                              0,90,200,20,
                              NULL, 1, NULL, NULL
                             );
   SetParent(hTextBox1,hwnd);
   DWORD dwStyle = GetWindowLong(hTextBox1, GWL_STYLE);
   dwStyle |= WS_VISIBLE|WS_CHILD|WS_BORDER|ES_AUTOHSCROLL;
   SetWindowLong(hTextBox1, GWL_STYLE, dwStyle);
   ShowWindow(hTextBox1, SW_SHOW);


But if I create that textbox with the following code, it can appear.

   hTextBox1 = CreateWindow(
                              TEXT("EDIT"),
                              TEXT(""),
                              WS_VISIBLE|WS_CHILD|WS_BORDER|ES_AUTOHSCROLL,
                              0,90,200,20,
                              hwnd, 1, NULL, NULL     [b]<--- put hwnd there[/b]
                             );


How to use SetParent correctly?
So, my TextBox (in the first code) can appear on the window.
Thank you for your kind.

Sincerely yours,
Title: Re: How to use SetParent for a non-parent Window?
Post by: dedndave on March 13, 2014, 07:05:52 AM
is there some reason for not creating the child window with the parent handle ?

i've never had a need to try what you are doing
if i am creating a parent window, i don't set WS_CHILD, and i use NULL as hwndParent
if i am creating a child window, i do set WS_CHILD, and i use hwnd of the parent as hwndParent

that seems pretty simple   :biggrin:
Title: Re: How to use SetParent for a non-parent Window?
Post by: dedndave on March 13, 2014, 07:12:40 AM
couple notes....

i always use CreateWindowEx
the code in CreateWindow calls CreateWindowEx, anyways

your issue may be that you have not initialized hwndMain yet
you may be doing something like this....
    INVOKE  CreateWindowEx......    ;create main window
    mov     hwndMain,eax            ;store the handle

if that's the case, hwndMain has not been initialized during execution of WM_CREATE in WndProc

here's how i do it....
remove that line of code shown in the above example that stores the handle
then, in WndProc (main window), WM_CREATE....
    .if uMsg==WM_CREATE
        mov     eax,hWnd
        mov     hwndMain,eax
;
;rest of create code

the WndProc hWnd parameter has the window handle before it has been created   :t
Title: Re: How to use SetParent for a non-parent Window?
Post by: MichaelW on March 13, 2014, 09:00:39 AM
Quote from: Dinadan Dinadan on March 12, 2014, 09:14:55 PM
I try create a non-parent Window (a textbox with no parent window) with the following code.
but the textbox not appear on the Window which it's handle is hwnd.

When you see a problem like this the first thing you need to do is check the return value for the relevant function call.

;==============================================================================
; Build as a console app.
;==============================================================================
    include \masm32\include\masm32rt.inc
;==============================================================================
    .data
      hInstance dd 0
    .code
;==============================================================================
DlgProc proc hwndDlg:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD
    SWITCH uMsg
        CASE WM_INITDIALOG

            invoke CreateWindowEx,WS_EX_CLIENTEDGE,chr$("EDIT"),0,
                   WS_VISIBLE or WS_CHILD or ES_MULTILINE,
                   10,10,200,100,NULL,1000,0,NULL
            printf("%d\n", eax)
            printf("%s\n", LastError$())

            invoke CreateWindowEx,WS_EX_CLIENTEDGE,chr$("EDIT"),0,
                   WS_VISIBLE or WS_CHILD or ES_MULTILINE,
                   10,10,200,100,hwndDlg,1001,0,NULL
            printf("%d\n", eax)

        CASE WM_CLOSE
            invoke EndDialog, hwndDlg, 0
    ENDSW
    return 0
DlgProc endp
;==============================================================================
start:
;==============================================================================
    invoke GetModuleHandle, NULL
    mov hInstance, eax
    Dialog 0,"MS Sans Serif",10,\
           WS_OVERLAPPEDWINDOW or DS_CENTER,\
           0,0,0,100,75,1024
    invoke GetModuleHandle, NULL
    CallModalDialog eax,0,DlgProc,NULL
    exit
;==============================================================================
end start


0
Cannot create a top-level child window.

67130


http://msdn.microsoft.com/en-us/library/windows/desktop/ms632680(v=vs.85).aspx

Title: Re: How to use SetParent for a non-parent Window?
Post by: Dinadan Dinadan on March 13, 2014, 11:35:54 AM
Hi dedndave,

Quoteis there some reason for not creating the child window with the parent handle ?
I want to know how to use SetParent in my first code so my textbox can appear like my second code (if I compiled it).

Quoteyour issue may be that you have not initialized hwndMain yet
of course, I already initialized hwndMain, which in my code it's is hwnd.

Best,
Title: Re: How to use SetParent for a non-parent Window?
Post by: dedndave on March 13, 2014, 12:05:01 PM
QuoteChanges the parent window of the specified child window

it would appear you are using it correctly,
but you can only modify the parent of an existing child
to create the child, it has to have a parent ("initial parent")

no matter what kind of GUI app you are writing, it makes sense to create the main window, first
so - you can use that as the initial parent to create children, then change them if desired
Title: Re: How to use SetParent for a non-parent Window?
Post by: TWell on March 13, 2014, 01:24:26 PM
Control needs parent window ?
Non-parent Window must be different kind of window ?
Title: Re: How to use SetParent for a non-parent Window?
Post by: dedndave on March 13, 2014, 02:18:38 PM
don't confuse him, Timppa   :biggrin:

this seems like a very simple concept

parent to child relationship can't be that hard
notice that a child window may also have children

it's kinda like human beings, except there is no mother window   :lol: