How can i use variables declared in WinMain function in WndProc function ?

New to FreeBASIC? Post your questions here.
dodicat
Posts: 5152
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How can i use variables declared in WinMain function in WndProc function ?

Postby dodicat » Mar 06, 2018 16:47

err is as good as a global variable.
Agreed __FB_ARGV__ is not global.(But I did say only main module)
There might be others.
The help files would not really be expected to cope with a search for others of this family (if any).
(I have had a quick try)
Err

Code: Select all

 
screen 19

sub zero
    var z= cast(any ptr,cast(integer,err))
    put(100,100),z,trans
    imagedestroy z
    z=0
    end sub

sub one
    var z= cast(any ptr,cast(integer,err))
    circle z,(150,150),100,3,,,,f
end sub


sub two
    err=cast(integer,imagecreate(300,300))
end sub


two
one
zero
sleep
 
fxm
Posts: 8347
Joined: Apr 22, 2009 12:46
Location: Paris (suburb), FRANCE

Re: How can i use variables declared in WinMain function in WndProc function ?

Postby fxm » Mar 06, 2018 17:21

IMHO, there is confusion coming from text of the description paragraph of ERR.
ERR is not an actual function as are ERL, ERFN and ERMN.
deltarho[1859]
Posts: 1541
Joined: Jan 02, 2017 0:34
Location: UK

Re: How can i use variables declared in WinMain function in WndProc function ?

Postby deltarho[1859] » Mar 06, 2018 18:18

dodicat wrote:I have tested with string messages also, they are easily passed to the wndproc.

OK. What about a user-defined type.

I have been looking at New(Placement) or am I using a sledgehammer to crack a nut? It is being particularly resistant at the moment.
dodicat
Posts: 5152
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How can i use variables declared in WinMain function in WndProc function ?

Postby dodicat » Mar 06, 2018 19:10

Try, for udt

Code: Select all

#Include Once "windows.bi"


Type v
    As Long x,y,z
End Type
Declare Function  WinMain(Byval hInstance As HINSTANCE, _
Byval hPrevInstance As HINSTANCE, _
Byval szCmdLine As Zstring Ptr, _
Byval iCmdShow As Integer ) As Integer
End WinMain( GetModuleHandle( null ), null, Command( ), SW_NORMAL )

Function WndProc (Byval hWnd As HWND, _
    Byval wMsg As UINT, _
    Byval wParam As WPARAM, _
    Byval lParam As LPARAM ) As LRESULT
   
    Function = 0
   
   
    Select Case wMsg
   
    ' ##########################
    Case WM_User + 999
        Dim As Ulong x
        x = wParam
        Print "In WndProc: ";"x = ";x
       
    Case WM_User + 1000
        Dim As Ulong x
        x = wParam
        Print "In WndProc: ";"y = ";x
       
    Case WM_User + 998
        Dim As zstring Ptr pul   
        Dim As Integer ul
        ul = wParam
        pul=Cast(zstring Ptr,ul)
        Print "In WndProc: ";"z = ";*pul
       
    Case WM_User + 997
        Dim As v Ptr udt
        udt = Cast(v Ptr,wParam)
        Print "In WndProc: ";"udt = ";udt->x,udt->y,udt->z
       
        ' #########################
    Case  WM_PAINT
        Dim rct As RECT
        Dim pnt As PAINTSTRUCT
        Dim hDC As HDC
       
        hDC = BeginPaint( hWnd, @pnt )
        GetClientRect( hWnd, @rct )
       
        DrawText( hDC, _
        "Hello" , _
        -1, _
        @rct, _
        DT_SINGLELINE Or DT_CENTER Or DT_VCENTER )
       
        EndPaint( hWnd, @pnt )
       
        Exit Function           
       
    Case WM_DESTROY
        PostQuitMessage( 0 )
        Exit Function
    End Select
   
    Function = DefWindowProc( hWnd, wMsg, wParam, lParam )   
   
End Function

'':::::
Function WinMain ( Byval hInstance As HINSTANCE, _
    Byval hPrevInstance As HINSTANCE, _
    Byval szCmdLine As Zstring Ptr, _
    Byval iCmdShow As Integer ) As Integer   
   
    Dim wMsg As MSG
    Dim wcls As WNDCLASS     
    Dim hWnd As HWND
   
    Dim As hwnd button
   
    Function = 0
   
    With wcls
        .style         = CS_HREDRAW Or CS_VREDRAW
        .lpfnWndProc   = @WndProc
        .cbClsExtra    = 0
        .cbWndExtra    = 0
        .hInstance     = hInstance
        .hIcon         = LoadIcon( NULL, IDI_APPLICATION )
        .hCursor       = LoadCursor( NULL, IDC_ARROW )
        .hbrBackground = GetStockObject( WHITE_BRUSH )
        .lpszMenuName  = NULL
        .lpszClassName = @"HelloWin"
    End With
   
    If( RegisterClass( @wcls ) = False ) Then
        MessageBox( null, "Failed To register wcls", "Error", MB_ICONERROR )
        Exit Function
    End If
   
    hWnd = CreateWindowEx( 0, _
    @"HelloWin", _
    "The Hello Program", _
    WS_OVERLAPPEDWINDOW, _
    500, _
    500, _
    200, _
    200, _
    NULL, _
    NULL, _
    hInstance, _
    NULL )
   
    ' ###################################
    Dim As Integer x = 666,y=333,z=Cast(Integer,Strptr("some Text"))
    Dim As v udt=Type(3,4,5)
   
    Print "In WinMain: ";"x = ";x
    Print "In WinMain: ";"y = ";y
    Print "In WinMain: ";"z = ";*Cast(zstring Ptr,z)
    Print  "In WinMain: ";"udt = ";udt.x,udt.y,udt.z
    Print
    PostMessage Hwnd, WM_USER + 999, x, 0
    PostMessage Hwnd, WM_USER + 1000, y, 0
    PostMessage Hwnd, WM_USER + 998, z, 0
    PostMessage Hwnd, WM_USER + 997, Cast(Integer,@udt), 0
    ' ###################################
    ShowWindow( hWnd, iCmdShow )
    UpdateWindow( hWnd )
   
    While( GetMessage( @wMsg, NULL, 0, 0 ) <> False ) 
        TranslateMessage( @wMsg )
        DispatchMessage( @wMsg )
    Wend
   
    Function = wMsg.wParam
   
    End Function 
deltarho[1859]
Posts: 1541
Joined: Jan 02, 2017 0:34
Location: UK

Re: How can i use variables declared in WinMain function in WndProc function ?

Postby deltarho[1859] » Mar 06, 2018 20:27

@dodicat

That is very nearly the perfect answer to the opening post. Very nice UDT passing.

From a presentation point of view I would have had v.z as a double.

There is one thing missing: v.y as negative. wParam cannot handle that but lParam can.

wParam: 32/64 bit unsigned
lParam: 32/64 bit signed.
adele
Posts: 46
Joined: Jun 13, 2015 19:33

Re: How can i use variables declared in WinMain function in WndProc function ?

Postby adele » Mar 07, 2018 0:02

@mr swiss

reading manual is fine. even finer is to have a try :

Code: Select all

Function dummy() As TypeOf ( Err () )
   Err=99:Return Err()
End function

Print "Initital ERR: ";Err()
Err=222:Print Err
Err=111:Var xxx=dummy()
Print Err(); " =" ;xxx
Sleep:STOP


just to remember: after each single IO-action err (as var) ist set to new err().number, mostly and hopefully zero==0
but, see above, not doing IO preserves the value set by the user.

Adi
fxm
Posts: 8347
Joined: Apr 22, 2009 12:46
Location: Paris (suburb), FRANCE

Re: How can i use variables declared in WinMain function in WndProc function ?

Postby fxm » Mar 07, 2018 6:16

fxm wrote:IMHO, there is confusion coming from text of the description paragraph of ERR.
ERR is not an actual function as are ERL, ERFN and ERMN.
(not an actual function returning a static variable by reference)

For any actual function 'f()':
- '#Print Typeof(f)' provides the type of the return value.
- '#Print Typeof(@f)' provides the type of the function.

'#Print Typeof(@ERR)' induces the error message:
error 24: Invalid data types, for @ or VARPTR in '#Print Typeof(@ERR)'
MrSwiss
Posts: 2763
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How can i use variables declared in WinMain function in WndProc function ?

Postby MrSwiss » Mar 07, 2018 14:25

@adele,
adele wrote:reading manual is fine. even finer is to have a try
Generally I'd agree, however, your try isn't either "proofing" or "disproofing",
what's been written, in the DOC (only error is: "function" in description).

The point discussed here, is: does FBC have (or not have) "internal" global variables.

My try (regarding to fxm's statement: "Static As Long Err"), proofes it's correctness:

Code: Select all

Static As Long ErrX ' proof of fxm's statements, by MrSwiss -- 2018-03-07

' this Sub can be called a: workaround, making ErrX appear, to be Global
Sub ChgErrX(ByVal NewX As Long, ByRef ErrX As Long) ' internal use of FBC (simulation)
    ErrX = NewX
    ' if ErrX is not used as parameter:
    ' FBC error 41: Variable not declared, ErrX in 'ErrX = NewX'
    ' this proofes: that ErrX is NOT Global! (it is module-level, only)
End Sub
   
Print ErrX
' simulate a Err-Code set by I/O statement (FBC internal)
If ErrX = 0 Then ChgErrX(-32, ErrX)
Print ErrX
' straight access below
If ErrX <> 0 Then ErrX = -92
Print ErrX

Scope
    ' module-level in scope, is OK! too
    If ErrX < 0 Then ErrX = 16 Else ErrX = -16
    Print ErrX & " from scope block"    ' expected: 16, OK!
End Scope

' straight change (once again)
ErrX = 1291 ' module-level is OK! as expected
Print ErrX
Sleep
' ----- EOF -----
ErrX, in the code is used as a replacement of Err (prevent conflicts, only).
It also proofes the point, that Err isn't a "global variable". A workaround is needed/used ...
fxm
Posts: 8347
Joined: Apr 22, 2009 12:46
Location: Paris (suburb), FRANCE

Re: How can i use variables declared in WinMain function in WndProc function ?

Postby fxm » Mar 07, 2018 15:06

But you cannot access ErrX from a procedure without passing it as an argument, that is not the case of Err.
Err works as a global variable, except you can (it's recommended) use end empty parentheses (as for a function) when it's not an assignment.
MrSwiss
Posts: 2763
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How can i use variables declared in WinMain function in WndProc function ?

Postby MrSwiss » Mar 07, 2018 15:36

fxm wrote:Err works as a global variable
This is not necessarily, the same as: Err is a global variable

Which one, of the two, is now correct?!? (you stated: "Static As Long")

The particular workaround used, internally, to make it appear to: work as global var.
is not, currently, of main-interest (just on the side-line).
fxm
Posts: 8347
Joined: Apr 22, 2009 12:46
Location: Paris (suburb), FRANCE

Re: How can i use variables declared in WinMain function in WndProc function ?

Postby fxm » Mar 07, 2018 15:45

MrSwiss wrote:Which one, of the two, is now correct?!? (you stated: "Static As Long")
I never stated: "Static As Long"
MrSwiss
Posts: 2763
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How can i use variables declared in WinMain function in WndProc function ?

Postby MrSwiss » Mar 07, 2018 15:54

Not that precisely, but:
fxm wrote:returning a static variable by reference
stating:
Static ByRef and, since the data-type is a Long ... same difference, no?

Rephrased question:
Is Err a internal global variable? Answer: [yes|no], only, please!

I don't accept excuses, lame or otherwise ...
fxm
Posts: 8347
Joined: Apr 22, 2009 12:46
Location: Paris (suburb), FRANCE

Re: How can i use variables declared in WinMain function in WndProc function ?

Postby fxm » Mar 07, 2018 16:04

MrSwiss wrote:Not that precisely, but:
fxm wrote:returning a static variable by reference
stating:
Static ByRef and, since the data-type is a Long ... same difference, no?

If you remove the negation at the beginning of my sentence, it changes the original meaning:
fxm wrote:
fxm wrote:IMHO, there is confusion coming from text of the description paragraph of ERR.
ERR is not an actual function as are ERL, ERFN and ERMN.
(not an actual function returning a static variable by reference)
MrSwiss
Posts: 2763
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How can i use variables declared in WinMain function in WndProc function ?

Postby MrSwiss » Mar 07, 2018 16:07

You just don't want to answer the real question (below in blue)!

Rephrased question:
Is Err a internal global variable? Answer: [yes|no] only, please!


Your answer is exactly the kind of "excuse post", not accepted (lame excuse even)!
Just a attempted, deflection tactic ...
fxm
Posts: 8347
Joined: Apr 22, 2009 12:46
Location: Paris (suburb), FRANCE

Re: How can i use variables declared in WinMain function in WndProc function ?

Postby fxm » Mar 07, 2018 16:37

First I am answering as I want (I am not connected as a Boolean)!
It is normal for me to protest when you change the meaning of my statement before quote it.

About Err, I do not know the answer (yes/no) to your specific request.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 3 guests