Announcement

Collapse
No announcement yet.

CreateWindow, adding Static or any Control

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • CreateWindow, adding Static or any Control

    I'm using CreateWindow to generate a user-interface, I'm having toruble adding any control to the window i create, whether it be a Static, or an Editbox, I'm setting the hWnd in createwindow as the hWnd of the main window, I'm even putting %WS_CHILD sometimes as a style, anyone? Do I need to show the control after I create it, is it there and I just do not see it? Hints, help, examples, thanks...



    ------------------

  • #2
    Here's a sample. Maybe you've forgot the %WS_VISIBLE style?
    Otherwise, try: ShowWindow hWndLabel, %SW_SHOW
    Code:
      hLabel = CreateWindowEx(%WS_EX_CLIENTEDGE, "STATIC", "", _
                              %WS_CHILD OR %WS_VISIBLE, _
                              x, y, w, h, _
                              hWndMain, %ID_LABEL, hInst, BYVAL %NULL)

    ------------------

    Comment


    • #3
      I've posted the source below to what I'm doing, thanks Borje but it still doesn't seem to be showing the label (or even creating it?) Lost, I appreciate any and all help..

      Thanks in advance..

      Code:
      Global hMain   As Long
      Global hStatic As Long
      Global hInst   As Long
      ' * Constants
      %ID_LABEL = 101
      '--------------------------------------------------------------------------------
      Function wndProc (ByVal hWnd As Long, ByVal wMsg As Long, _
                        ByVal wParam As Long, ByVal lParam As Long) As Long
      
         Dim wndClass As WndClassEx
      
         Select Case wMsg
            Case %WM_CREATE
                    hStatic = CreateWindowEx(%WS_EX_CLIENTEDGE, "Static", "", _
                                 %WS_CHILD Or %WS_VISIBLE, _
                                 %CW_USEDEFAULT, %CW_USEDEFAULT, _
                                 %CW_USEDEFAULT, %CW_USEDEFAULT, _
                                 hMain, %ID_LABEL, hInst, ByVal %NULL)
      
                                ' UpdateWindow hMain
      
            Case %WM_DESTROY
                 PostQuitMessage 0
         End Select
      
         Function = DefWindowProc(hWnd, wMsg, wParam, lParam)
      
      End Function
      '--------------------------------------------------------------------------------
      Function WinMain (ByVal hInstance As Long, ByVal hPrevInstance As Long, _
                        lpCmdLine As Asciiz Ptr, ByVal iCmdShow As Long) As Long
      
         Dim Msg         As tagMsg
         Dim wndClass    As WndClassEx
         Dim szClassName As Asciiz * 7
         Dim hMenu       As Long
      
         hInst = hInstance
         szClassName            = "Hobbit"
         wndClass.cbSize        = SizeOf(wndClass)
         wndClass.style         = 0'%CS_HREDRAW Or %CS_VREDRAW
         wndClass.lpfnWndProc   = CodePtr(wndProc)
         wndClass.cbClsExtra    = 0
         wndClass.cbWndExtra    = 0
         wndClass.hInstance     = hInstance
         wndClass.hIcon         = LoadIcon(hInstance, "PROGRAM")
         wndClass.hCursor       = LoadCursor(%NULL, ByVal %IDC_ARROW)
         wndClass.hbrBackground = %COLOR_WINDOW
         wndClass.lpszMenuName  = %NULL
         wndClass.lpszClassName = VarPtr(szClassName)
         wndClass.hIconSm       = LoadIcon(hInstance, ByVal %IDI_APPLICATION)
      
         RegisterClassEx wndClass
      
         hMain = CreateWindow(szClassName, "Hobbit 0.1: Builder", _
                                 %WS_CAPTION Or %WS_DLGFRAME Or %WS_SYSMENU, _
                                 400, 400, _
                                 %CW_USEDEFAULT, %CW_USEDEFAULT, _
                                 %HWND_DESKTOP, %NULL, hInstance, ByVal %NULL)
      
         ShowWindow hMain, iCmdShow
         UpdateWindow hMain
      
         While GetMessage(Msg, %NULL, 0, 0)
               TranslateMessage Msg
               DispatchMessage Msg
         Wend
      
         Function = Msg.wParam
      
      End Function                                       
      
      ------------------
      [This message has been edited by Matt Grace (edited March 09, 2001).]

      Comment


      • #4
        Matt,

        I see your problem; Change the parameter from hMain to hWnd.

        Code:
        Case %WM_CREATE              
        hStatic = CreateWindowEx(%WS_EX_CLIENTEDGE, "Static", "", _
                                   %WS_CHILD Or %WS_VISIBLE, _
                                   %CW_USEDEFAULT, %CW_USEDEFAULT, _
                                   %CW_USEDEFAULT, %CW_USEDEFAULT, _
                                   hMain, %ID_LABEL, hInst, ByVal %NULL)
                                  ' UpdateWindow hMain
        Regards,
        Jules
        Best regards
        Jules
        www.rpmarchildon.com

        Comment


        • #5
          Jules, though that really shouldn't matter, i changed both parameters, and still nothing, anyone have an idea still? The source I'm using is the exact replica of what I posted above.. appreciate everyones input/help.. still can't seem to add a control.

          ------------------

          Comment


          • #6
            Matt:

            Two things... don't create the child window inside the %WM_CREATE handler of your wndProc...

            -AND-

            using %CW_USEDEFAULT for the X & Y position parameters or the Width & Height parameters in the
            CreateWindowEx function is valid only for overlapped windows. If you use %CW_USEDEFAULT for a
            child window, those parameters will be set to zero. Try this example...
            Code:
            #COMPILE EXE
            #REGISTER NONE
            #INCLUDE "Win32Api.Inc"
            
            
            GLOBAL hMain   AS LONG
            GLOBAL hStatic AS LONG
            GLOBAL hInst   AS LONG
            
            
            %ID_LABEL = 101
            
            
            FUNCTION wndProc(BYVAL hWnd AS LONG, BYVAL wMsg AS LONG, _
                             BYVAL wParam AS LONG, BYVAL lParam AS LONG) AS LONG
               SELECT CASE wMsg
               CASE %WM_CREATE
                  '
               CASE %WM_DESTROY
                  PostQuitMessage 0
               END SELECT
               FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)
            END FUNCTION
            
            
            FUNCTION WINMAIN(BYVAL hInstance AS LONG, BYVAL hPrevInstance AS LONG, _
                             lpCmdLine AS ASCIIZ PTR, BYVAL iCmdShow AS LONG) AS LONG
               DIM Msg         AS tagMsg
               DIM wndClass    AS WndClassEx
               DIM szClassName AS ASCIIZ * 7
               DIM hMenu       AS LONG
               hInst = hInstance
               szClassName            = "Hobbit"
               wndClass.cbSize        = SIZEOF(wndClass)
               wndClass.style         = 0
               wndClass.lpfnWndProc   = CODEPTR(wndProc)
               wndClass.cbClsExtra    = 0
               wndClass.cbWndExtra    = 0
               wndClass.hInstance     = hInstance
               wndClass.hIcon         = LoadIcon(hInstance, "PROGRAM")
               wndClass.hCursor       = LoadCursor(%NULL, BYVAL %IDC_ARROW)
               wndClass.hbrBackground = %COLOR_WINDOW
               wndClass.lpszMenuName  = %NULL
               wndClass.lpszClassName = VARPTR(szClassName)
               wndClass.hIconSm       = LoadIcon(hInstance, BYVAL %IDI_APPLICATION)
               RegisterClassEx wndClass
               hMain = CreateWindow(szClassName, "Hobbit 0.1: Builder", _
                                    %WS_CAPTION OR %WS_DLGFRAME OR %WS_SYSMENU, _
                                    400, 400, _
                                    %CW_USEDEFAULT, %CW_USEDEFAULT, _
                                    %HWND_DESKTOP, %NULL, hInstance, BYVAL %NULL)
            
            
               hStatic = CreateWindowEx(%WS_EX_CLIENTEDGE, "Static", "", _
                                        %WS_CHILD OR %WS_VISIBLE, _
                                        10, 10, _
                                        200, 100, _
                                        hMain, %ID_LABEL, hInst, BYVAL %NULL)
            
            
               ShowWindow hMain, iCmdShow
               UpdateWindow hMain
            
            
               WHILE GetMessage(Msg, %NULL, 0, 0)
                  TranslateMessage Msg
                  DispatchMessage Msg
               WEND
               FUNCTION = Msg.wParam
            END FUNCTION
            Timm

            [This message has been edited by Timm Motl (edited March 09, 2001).]
            mailto:[email protected]
            Tsunami Record Manager

            Comment


            • #7
              Matt, this compiles fine on my notebook Win98SE...

              Code:
              #COMPILE EXE
              #INCLUDE "WIN32API.INC"
              
                
              Global hMain   As Long
              Global hStatic As Long
              Global hInst   As Long
               
              ' * Constants
              %ID_LABEL = 101
               
              '--------------------------------------------------------------------------------
              Function wndProc (ByVal hWnd As Long, ByVal wMsg As Long, _
                                ByVal wParam As Long, ByVal lParam As Long) As Long
               
                 Dim wndClass As WndClassEx
               
                 Select Case wMsg
                    Case %WM_CREATE
                            hStatic = CreateWindowEx(%WS_EX_CLIENTEDGE, "Static", "", _
                                         %WS_CHILD Or %WS_VISIBLE, _
                                         10, 10, _
                                         100, 100, _
                                         hWnd, %ID_LABEL, hInst, ByVal %NULL)
              
                                       
               
                    Case %WM_DESTROY
                         PostQuitMessage 0
                 End Select
               
                 Function = DefWindowProc(hWnd, wMsg, wParam, lParam)
               
              End Function
               
              '--------------------------------------------------------------------------------
              Function WinMain (ByVal hInstance As Long, ByVal hPrevInstance As Long, _
                                lpCmdLine As Asciiz Ptr, ByVal iCmdShow As Long) As Long
               
                 Dim Msg         As tagMsg
                 Dim wndClass    As WndClassEx
                 Dim szClassName As Asciiz * 7
                 Dim hMenu       As Long
               
                 hInst = hInstance
               
                 szClassName            = "Hobbit"
                 wndClass.cbSize        = SizeOf(wndClass)
                 wndClass.style         = %CS_HREDRAW Or %CS_VREDRAW
                 wndClass.lpfnWndProc   = CodePtr(wndProc)
                 wndClass.cbClsExtra    = 0
                 wndClass.cbWndExtra    = 0
                 wndClass.hInstance     = hInstance
                 wndClass.hIcon         = LoadIcon(hInstance, "PROGRAM")
                 wndClass.hCursor       = LoadCursor(%NULL, ByVal %IDC_ARROW)
                 wndClass.hbrBackground = %COLOR_WINDOW
                 wndClass.lpszMenuName  = %NULL
                 wndClass.lpszClassName = VarPtr(szClassName)
                 wndClass.hIconSm       = LoadIcon(hInstance, ByVal %IDI_APPLICATION)
               
                 RegisterClassEx wndClass
               
                 hMain = CreateWindowEx(0,szClassName, "Hobbit 0.1: Builder", _
                                         %WS_CAPTION Or %WS_DLGFRAME Or %WS_SYSMENU, _
                                         400, 400, _
                                         400, 400, _
                                         %HWND_DESKTOP, %NULL, hInstance, ByVal %NULL)
               
                 ShowWindow hMain, iCmdShow
                 UpdateWindow hMain
               
                 While GetMessage(Msg, %NULL, 0, 0)
                       TranslateMessage Msg
                       DispatchMessage Msg
                 Wend
               
                 Function = Msg.wParam
               
              End Function
              Best regards
              Jules
              www.rpmarchildon.com

              Comment


              • #8
                Thank you both Tim and Jules, putting the create code after the initial main hwnd create code worked great, but now I ask, If I have to create controls after that code, what is the %WM_CREATE portion for, just initializing form related variables or data, or is it for creating certain types of windows (controls?) hmmm.

                Sorry for the bother, but you know how it goes, the quest is never ending :P

                ------------------

                Comment


                • #9
                  You don't *have* to do anything with WM-CREATE; but it *is* the logical time to do general purpose initialization.

                  Controls you can add anytime with CreateWindow(Ex).

                  MCM


                  Michael Mattias
                  Tal Systems Inc. (retired)
                  Racine WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    Two things... don't create the child window inside the %WM_CREATE
                    handler of your wndProc...
                    That is not good advice.
                    As Micheal pointed out you can created controls at anytime but there are
                    exceptions most notably when the parent receives WM_DESTROY, WM_NCDESTROY
                    WM_GETMINMAXINFO(the first one due to a bug).
                    The window procedure of the parent is a very good place to create child
                    windows. It is also used to initialize state variables for the parent
                    window. Jules version is more robust and does not need code to check for
                    the existence of the parent window before creating the child controls.
                    The window procedure can indicate that initialization failed by returning
                    -1 after processing WM_CREATE. If -1 is returned the parent window is
                    destroyed and CreateWindowEx returns NULL to its caller.

                    Matt's code was failing because of the structure of his code and as Tim
                    rightly pointed out his use of CW_USEDEFAULT for the metrics for a child
                    control.

                    In the code snippet shown below Matt is using hMain in WM_CREATE before it
                    has been initialized. That is, hMain equals zero because any code in
                    WM_CREATE is executed before the CreateWindowEx in WinMain returns. When
                    CreateWindowEx returns hMain = hWnd.

                    Tim's code will be robust if the following check is added before creating
                    child windows.
                    IF ISTRUE hMain THEN
                    ...
                    END IF
                    Code:
                    Function wndProc (ByVal hWnd As Long, ByVal wMsg As Long, _
                                      ByVal wParam As Long, ByVal lParam As Long) As Long
                       Dim wndClass As WndClassEx
                       Select Case wMsg
                          Case %WM_CREATE
                                  hStatic = CreateWindowEx(%WS_EX_CLIENTEDGE, "Static", "", _
                                               %WS_CHILD Or %WS_VISIBLE, _
                                               %0, 0, _
                                               100, 100, _
                                               hMain, %ID_LABEL, hInst, ByVal %NULL)
                    ...
                    End Function
                      
                    Function WinMain (ByVal hInstance As Long, ByVal hPrevInstance As Long, _
                                      lpCmdLine As Asciiz Ptr, ByVal iCmdShow As Long) As Long
                    ...
                    
                       hMain = CreateWindow(szClassName, "Hobbit 0.1: Builder", _
                                               %WS_CAPTION Or %WS_DLGFRAME Or %WS_SYSMENU, _
                                               400, 400, _
                                               %CW_USEDEFAULT, %CW_USEDEFAULT, _
                                               %HWND_DESKTOP, %NULL, hInstance, ByVal %NULL)
                    ...
                    End Function
                    ------------------
                    Dominic Mitchell
                    Dominic Mitchell
                    Phoenix Visual Designer
                    http://www.phnxthunder.com

                    Comment


                    • #11
                      Dominic;

                      Jules version is more robust and does not need code to check for
                      the existence of the parent window before creating the child controls.
                      I had a good teacher. I learned that lesson from you about 14 months ago when
                      I was struggling to learn!!!

                      Regards,
                      Jules


                      Best regards
                      Jules
                      www.rpmarchildon.com

                      Comment


                      • #12
                        Thanks for the compliments Jules.

                        ------------------
                        Dominic Mitchell
                        Dominic Mitchell
                        Phoenix Visual Designer
                        http://www.phnxthunder.com

                        Comment

                        Working...
                        X