sdk skeleton for PureBasic

Started by Mike Mayerhoffer, October 09, 2015, 01:07:01 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mike Mayerhoffer

I purchased it for its price 89 dollar at todays rates. Free upgrades for life [roll eyes] Anyhow this compiles 32/64bit write once.
PureBasic targets 3 platforms as a write once solution. So a pure sdk layout was questionable.

This what I cooked up and some notes.  So use with caution , I will scrutinize it when I
have more time. My biggest concern is how the default variables work out in the long run.


Enjoy !



EnableExplicit ; aka Dim all, it is a must use

; =' comment
;. = \ in 'type or structer ' in Pure usage point.y = point\y
;no foward reference single pass compiler - reason stated
; sub and function = Procedure
; funtion aka Procedure return is defined by ProcedureReturn value
; local name as string = protected.s name  b = byte ...
;use api by adding underscore to api name e.g SendMessage_ no includes needed
;api defines has # sign prefix e.g #WM_CLOSE
; like in c OR = '|'  e.g #CS_HREDRAW | #CS_VREDRAW
; pointer is defined and used by *pointer, *who , *whatever = address of variable pointer
;a pointer is just an address to a vairable of any type long, string and so on...
; gadets = ddt stuff and it covers alot of items from buttons to MDI and even a web browser
; built in gui maker
; appears the gadet concept is write once, use on 64,32 MAC linux



Global wce.WNDCLASSEX, hInstance, hPrevInstance, lpCmdLine,iCmdShow ,msg.MSG 



Declare  WndProc ( hWnd ,  uMsg , wParam , lParam )
Declare WinMain ( hInstance , hPrevInstance , lpCmdLine ,iCmdShow )

;  only way to get hinstance far as I can tell.. more to learn about PureBasic
hInstance = GetModuleHandle_(0)

WinMain ( hInstance , hPrevInstance , lpCmdLine , iCmdShow )

Procedure WinMain ( hInstance , hPrevInstance , lpCmdLine ,  iCmdShow      )
; Procedure WinMain ( ) ' this can de used
 
    ; Program entry point

    Protected wce.WndClassEx
    Protected.s AppName ;check this out makit a pointer to a api by @szAppName
    Protected hWnd     

    ; Setup and register a window class for the main window
    ; CODEPTR is used to pass the address of the function that will
    ; receive all messages sent to any window created with this class
    AppName         = "HelloWin"
   
    wce\cbSize        = SizeOf(WNDCLASSEX)
    wce\STYLE         = #CS_HREDRAW | #CS_VREDRAW
    wce\lpfnWndProc   = @WndProc()
    wce\cbClsExtra    = 0
    wce\cbWndExtra    = 0
    wce\hInstance     = hInstance
    wce\hIcon         = LoadIcon_(0, @"HelloWin.ico" )
    wce\hCursor       = LoadCursor_(0,  #IDC_ARROW)
    wce\hbrBackground = 0
    wce\lpszMenuName  = 0
    wce\lpszClassName = @AppName
    wce\hIconSm       = LoadIcon_(hInstance,  #IDI_APPLICATION)

    RegisterClassEx_ (@wce)

    ; Create a window using the registered class
    hWnd = CreateWindowEx_(0,
                        AppName,               ; window class name or "HelloWin"
                        "da Hello Program",      ; window caption
                        #WS_OVERLAPPEDWINDOW,     ; window style
                        #CW_USEDEFAULT,           ; initial x position
                        #CW_USEDEFAULT,          ; initial y position
                        #CW_USEDEFAULT,           ; initial x size
                        #CW_USEDEFAULT,         ; initial y size
                        0,                   ; parent window handle
                        0,                    ; window menu handle
                        hInstance,                ; program instance handle
                        0)               ; creation parameters
   
   ;hWnd=0 ;force error checking
    If hWnd = 0 ;  ; exit on failure no then if support
    MessageBox_(0, "Failed to create winow!", AppName, #MB_ICONERROR )
        End 1 ; this terminates and returns 1 for winmain
      EndIf
     
      ;had to force icon to display on 32bit - more research needed
; SendMessage_(hWnd, #WM_SETICON, 0, LoadIcon_(hInstance, 1))
    ; Display the window on the screen
    ShowWindow_ (hWnd, #SW_SHOWDEFAULT)
    UpdateWindow_ (hWnd)

    ; Main message loop
  ;remove from here to........
   
     While GetMessage_(Msg, 0, 0, 0)
        TranslateMessage_ (msg)
        DispatchMessage_ (msg)
    Wend

    ProcedureReturn  msg\wParam
;.........here and you can use the pump as is below
  EndProcedure
 
;      While GetMessage_(Msg, 0, 0, 0)
;         TranslateMessage_ (msg)
;         DispatchMessage_ (msg)
;     Wend

;==============================================================================
Procedure DrawGradient( hDC )

    ; Custom draw procedure for gradiend fill
 

    Protected rectFill.RECT
    Protected rectClient.RECT
    Protected.f fStep
    Protected hBrush
    Protected lOnBand

    GetClientRect_ (    WindowFromDC_ (hDC) , rectClient )
   
   
    fStep = rectClient\bottom / 100

    For lOnBand = 0 To 99
      SetRect_  (rectFill, 0, lOnBand * fStep, rectClient\right + 1, (lOnBand + 1) * fStep )
     
        hBrush = CreateSolidBrush_(RGB(0, 128, 192 - lOnBand))
        FillRect_ (hDC, rectFill, hBrush)
        DeleteObject_ (hBrush)
    Next

EndProcedure



Procedure WndProc ( hWnd ,  uMsg ,  wParam ,  lParam  )


    Protected.i hDC   
    Protected pPaint.PAINTSTRUCT
    Protected tRect.RECT
    Protected.i result

    Select  uMsg

    Case #WM_CREATE

    Case #WM_PAINT
        hDC = BeginPaint_(hWnd, pPaint)
        GetClientRect_ (hWnd, tRect)
        SetBkMode_ (hDC, #TRANSPARENT)
        SetTextColor_ ( hDC, #Yellow )
        DrawText_ ( hDC, "Hello, Windows!", -1, tRect, #DT_SINGLELINE | #DT_CENTER | #DT_VCENTER)
        EndPaint_( hWnd, pPaint  )
        ;'End 1 bad bad don't do that, instead maybe this speeds up things or not
       ; Goto abort

    Case #WM_ERASEBKGND
        hDC = wParam
        DrawGradient (hDC )             ; Pass the DC of the region to repaint
       ;Goto abort ; this should speeds up things or not

 
        Case #WM_CLOSE
         DestroyWindow_(hWnd)
         Result  = 0
      Case #WM_DESTROY
         PostQuitMessage_(0)
         Result  = 0
       Default
         ;abort:
         Result  = DefWindowProc_(hWnd, uMsg, wParam, lParam)
   EndSelect
   ProcedureReturn Result
   EndProcedure



Bob Houle

A lot of work (unless you MUST use the WinAPI)  :)

Here's the same effect with 33 lines of code:
; Netmaestro March 2011
OpenWindow(0, 0, 0, 640, 480, "", #PB_Window_SizeGadget | #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
ImageGadget(0,0,0,0,0,0)
DisableGadget(0,1)

Procedure Paint(Window)
  CreateImage(0,WindowWidth(window), WindowHeight(window))
 
  StartDrawing(ImageOutput(0))
    DrawingMode(#PB_2DDrawing_Gradient)     
    BackColor($000000)
    FrontColor($0000FF)
    LinearGradient(0, WindowHeight(window)/2, WindowWidth(window), WindowHeight(window)/2)
    Box(0,0,WindowWidth(window), WindowHeight(window))
  StopDrawing()
  SetGadgetState(0,ImageID(0))
EndProcedure

Paint(0)


Repeat
  Select WaitWindowEvent()
    Case #PB_Event_Repaint
      Flag = 1
    Case #PB_Event_SizeWindow
      If Flag
        Paint(EventWindow())
      EndIf
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver

Bob Houle

If you really want to work with the SDK...

Here's a ZIP file of all the examples using the famous book/bible from Charles Petzold. (5th edition)

* in PureBasic of course  ;D


Israel Vega

Thanks...is a great contribution.  May be other great programmers from PowerBasic forum have an interest in PureBasic.

Although I do not use the Windows API, I think for me it is sufficient to use
statements included in the compiler ... that are the same for OSX, Linux and Windows. I think for me PureBASIC it is the best compiler
after PowerBASIC. PureBasic has many advantages besides that ... can also be programmed in using the Web to SpiderBASIC very close
to PureBasic code.