PBwin 10 bare window in SDK style.

Started by hutch--, June 09, 2011, 06:23:57 AM

Previous topic - Next topic

hutch--

You can write basic like crayon scribblings and it will more often than not work OK but you can write the real McCoy[tm] in standard SDK style if you prefer that style of code.

Here is a simple example of a bare window with an icon, manifest file and a version control block as well as the option to build it as either ANSI or UNICODE.


' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    #COMPILE EXE
    #DIM ALL            ' comment out if you don't program this style
    %UNICODE = 1        ' comment out to build ANSI string version
    #INCLUDE ONCE "WIN32API.INC"
    #RESOURCE "barewin.pbr"

    GLOBAL hInstance    as DWORD
    GLOBAL hWnd         as DWORD
    GLOBAL hIcon        as DWORD
    GLOBAL hCursor      as DWORD
    GLOBAL Sw           as DWORD
    GLOBAL Sh           as DWORD

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

FUNCTION PBmain() AS LONG

    #REGISTER NONE

    LOCAL pMsg          as DWORD
    LOCAL Wid           as DWORD
    LOCAL Hgt           as DWORD
    LOCAL Msg           as tagMSG
    LOCAL wcex          as WNDCLASSEX

    #IF %DEF(%UNICODE)
      LOCAL szClass   as WSTRINGZ * 64          ' UNICODE strings
      LOCAL szDisplay as WSTRINGZ * 128
    #ELSE
      LOCAL szClass   as STRINGZ * 64           ' ANSI strings
      LOCAL szDisplay as STRINGZ * 128
    #ENDIF

    hInstance = GetModuleHandle(ByVal %NULL)
    szDisplay = "PowerBASIC API Window"
    szClass   = "pbwin_API_class"

    hIcon   = LoadIcon(hInstance,ByVal 5)
    hCursor = LoadCursor(%NULL,ByVal %IDC_ARROW)

    wcex.cbSize        = SIZEOF(wcex)
    wcex.style         = %CS_BYTEALIGNCLIENT or %CS_BYTEALIGNWINDOW
    wcex.lpfnWndProc   = CODEPTR(WndProc)
    wcex.cbClsExtra    = 0
    wcex.cbWndExtra    = 0
    wcex.hInstance     = hInstance
    wcex.hIcon         = hIcon
    wcex.hCursor       = hCursor
    wcex.hbrBackground = %COLOR_BTNFACE + 1
    wcex.lpszMenuName  = %NULL
    wcex.lpszClassName = VarPtr(szClass)
    wcex.hIconSm       = hIcon

    RegisterClassEx wcex

  ' ---------------------------------------------------
  ' set the size of the window and center it on startup
  ' ---------------------------------------------------

    Sw = GetSystemMetrics(%SM_CXSCREEN)
    Sh = GetSystemMetrics(%SM_CYSCREEN)

  ' -----------------------------------------------
  ' Set width & Height to percentage of screen size
  ' -----------------------------------------------
    Wid = Sw * .70   
    Hgt = Sh * .70

  ' ---------------------------------------
  ' limit the aspect ratio for wide screens
  ' ---------------------------------------
    If Wid > Hgt * 1.4 Then
      Wid = Hgt * 1.4
    End If

    hWnd = CreateWindowEx( _
               %WS_EX_LEFT, _           ' extended style
               szClass, _               ' window class name
               szDisplay, _             ' window title
               %WS_OVERLAPPEDWINDOW, _  ' window style
               Sw\2-(Wid\2), _          ' initial x position
               Sh\2-(Hgt\2), _          ' initial y position
               Wid, _                   ' initial x size
               Hgt, _                   ' initial y size
               %NULL, _                 ' parent window handle
               %NULL, _                 ' window menu handle
               hInstance, _             ' program instance handle
               ByVal %NULL)             ' creation parameters

  ' ---------------------------------------------------

    ShowWindow hWnd, %SW_SHOW
    UpdateWindow hWnd

    pMsg = VarPtr(Msg)                  ' get a pointer to the MSG structure

    While GetMessage(ByVal pMsg,%NULL,0,0) <> 0
      TranslateMessage ByVal pMsg
      DispatchMessage  ByVal pMsg
    Wend

    FUNCTION = Msg.wParam

END FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

FUNCTION WndProc(ByVal hWin as DWORD,ByVal Msg as DWORD, _
                 ByVal wParam as LONG,ByVal lParam as LONG) as DWORD

    Select Case as LONG Msg             ' perform INTEGER compare

      Case %WM_DESTROY
        PostQuitMessage 0
        Exit Function

    END Select

    FUNCTION = DefWindowProc(hWin,Msg,wParam,lParam)

END FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Twister

Bleh! Who follows coding standards? I would rather stick with my crayon scribbles and macaroni art. :bg

hutch--

 :bg

< I would rather stick with my crayon scribbles and macaroni art.

I would not have said so but I will take your word for it.  :P
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

i have to ask...
why not just write it in asm ?

hutch--

Well, it is high level code but in the case of direct Windows API functions, the ASM notation its not of any advantage.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php