News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

How to use SetParent for a non-parent Window?

Started by Dinadan Dinadan, March 12, 2014, 09:14:55 PM

Previous topic - Next topic

Dinadan Dinadan

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,

dedndave

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:

dedndave

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

MichaelW

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

Well Microsoft, here's another nice mess you've gotten us into.

Dinadan Dinadan

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,

dedndave

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

TWell

Control needs parent window ?
Non-parent Window must be different kind of window ?

dedndave

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: