Return PB's WinMain instance?

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.

This one's probably for Fred :)

First things first: I don't know if this should work, but would it be possible to have a PB function that will return the value of our program's WinMain 'instance'? I was hoping to try and process messages, etc, by myself (more for learning purposes than anything else), but it would also be very useful if we could at least define an 'over-ride' window class (either for all future windows opened, or to supply as an extra [optional?] parameter to OpenWindow), so we can define the look and 'actions' of the window in advance, rather than having to hack it afterwards with SetWindowLong, etc (which I've never had much success with!)... and of course, supplying an over-ride WndProc could be useful too :wink:

Code: Select all

Procedure MyWndProc (hWnd, message, wParam, lParam)

    Select message

      Case #WM_CREATE
        MessageBox_ (#NULL, "Hit Esc To Close This Window.", "So You Know", #MB_OK)

      Case #WM_DESTROY
        PostQuitMessage_ (0)

      Case #WM_SYSKEYDOWN
      Case #WM_SYSKEYUP
      Case #WM_SYSCHAR

      Default
        ProcedureReturn DefWindowProc_ (hWnd, message, wParam, lParam)
        
    EndSelect
    
    ProcedureReturn (0)

EndProcedure

; -----------------------------------------------------------------------------
hInstance = 0 ; AAAAAAAAAHHHHHHHHHHH!!!! Need to get this from PB's WinMain!
; -----------------------------------------------------------------------------

DefType.WNDCLASS wndclass

wndclass\style = #CS_HREDRAW | #CS_VREDRAW
wndclass\lpfnWndProc = @MyWndProc ()
wndclass\cbClsExtra = 0
wndclass\cbWndExtra = 0

wndclass\hInstance = hInstance ; **************

wndclass\hIcon = LoadIcon_ (#NULL, #IDI_APPLICATION)
wndclass\hCursor = LoadCursor_ (#NULL, #IDC_ARROW)
wndclass\hbrBackground = CreateSolidBrush_ ($AAAAAA)
wndclass\lpszMenuName  = #NULL

w$ = "MyWindowName"
wndclass\lpszClassName = @w$

If RegisterClass_ (@wndclass) = 0
    MessageBox_ (#NULL, "Doh!", "Error", #MB_OK)
    End
EndIf

; Crash occurs here!
; -----------------------------------------------------------------------------
hWindow = CreateWindowEx_ (#NULL, "MyWindowName", "My Title Bar", #WS_OVERLAPPEDWINDOW, 0, 0, 200, 100, #NULL, #NULL, hInstance, #NULL)
; -----------------------------------------------------------------------------

If hWindow

    ShowWindow_ (hWindow, #SW_SHOW)
    
    While done = #FALSE

        If PeekMessage_ (@msg.MSG, #NULL, 0, 0, #PM_REMOVE)
    
            If msg\message = #WM_QUIT
                done = #TRUE
            Else
                TranslateMessage_ (@msg)
                DispatchMessage_ (@msg)
            EndIf
               
            PostQuitMessage_ (0)
            done = #TRUE

        EndIf
        
   Wend
    
   End
    
EndIf
--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

You can use SetClassLong_(WindowID(), #GCL_WNDPROC, @MyWndProc()) to do that after OpenWindow(), but you'll have to process #WM_CREATE and so on.

Edit: sorry, you don't need to handle #WM_CREATE if you use OpenWindow, only the rest of the messages.

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.

Ah, cool, thanks... :)


--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

To get the main hinstance: GetModuleHandle_(0).

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.

Thanks Fred -- I did think of that, but I didn't know if it was the same thing :)


--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
Post Reply