 ' http://www.oxygenbasic.org/forum/index.php?topic=1636.msg17936;topicseen#msg17936
'  Centerwindow.o2bas 
 $ filename "Centerwindow.exe"
     
   
    uses rtl64
     
    uses WinUtil

' for randomizer
! GetTickCount lib "kernel32.dll" alias "GetTickCount" () as dword
! srand cdecl lib "msvcrt.dll" (int seed)
! rand cdecl lib "msvcrt.dll" () as int

    uses corewin
    Use Chaos 
    seed=GetTickCount


    #autodim off
     
    % ID_BUTTON1=100
    sys hButton1

'   for redrawing the window
    %RDW_INVALIDATE   0x0001
    %RDW_ERASE           0x0004

   '============================
' RGB function for O2
function RGB(int rcc, gcc, bcc) as int
    return (rcc + gcc*256 + bcc*65536)
end Function 

' ============================== 
'  RANDOMIZE TIMER; use once per the entire script
sub Randomize() 
  srand GetTickCount()
end sub



  '==========================================
' draws with color gradient with random colors
SUB DrawGradientRnd (BYVAL hDC AS DWORD)
   LOCAL rectFill AS RECT, rectClient AS RECT, fStep AS SINGLE
  LOCAL  hBrush AS DWORD, lOnBand AS LONG
   GetClientRect WindowFromDC(hDC), rectClient
   fStep = rectClient.bottom / 75

 '  local LastBand as Long
  ' LastBand =  irnd( 189, 230 )

   FOR lOnBand = 0 TO  199
      SetRect rectFill, 0, lOnBand * fStep, rectClient.right + 1, (lOnBand + 1) * fStep
      ' paint the background -- change the first 2 colors R and G
      ' to vary the color gradient
      hBrush = CreateSolidBrush(rgb(230-  irnd( 9, 30 ), 240, 230 - lOnBand))
      Fillrect hDC, rectFill, hBrush
      DeleteObject hBrush
   NEXT

END SUB


    declare sub CenterWindow(sys hwnd, int place=0)
     
    sys hInstance=inst
     Randomize()
    MainWindow 400,300,WS_OVERLAPPEDWINDOW
     
     



'============================================
    function WndProc(sys hwnd, uMsg, wParam, lParam) as sys callback
       int x
       local hdc as sys

        select uMsg
        
            case WM_CREATE
                hButton1 = CreateWindowEx (0,"button", "Start Demo",
                                           WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP,
                                          150, 200, 100, 30, hwnd, ID_BUTTON1,
                                          hInstance, NULL)
        
            case WM_COMMAND
                if loword(wParam) = ID_BUTTON1 then
                                
                   for x=0 to 4
                       RedrawWindow(hwnd, NULL, NULL, RDW_ERASE or RDW_INVALIDATE)
                     CenterWindow(hwnd, x)
                     Sleep 1000 'wait a second
                   next
                   CenterWindow(hwnd)  
                end if   

            
 'graphic/drawing events
 'sent when the window background must be erased such as resizing
       CASE %WM_ERASEBKGND     
          hDC = wParam
  '      Pass the DC of the region to repaint
          DrawGradientRnd  hDC            
          FUNCTION = 1
          EXIT FUNCTION



            case WM_CLOSE
                DestroyWindow(hwnd)
            
            case WM_DESTROY
                PostQuitMessage(0)
            
            case else
                return DefWindowProc(hwnd, uMsg, wParam, lParam)
                
        end select
        
        return 0
    end function
     


    ==============================
    sub CenterWindow(sys hwnd, int place=0)
        '0=center, 1=left,2=top,3=right,4=bottom centered
        int spinfo[4]
        RECT rc
     
        GetWindowRect(hwnd,&rc)
     
        int hpos=rc.left,vpos=rc.top
        int width=rc.right-rc.left
        int height=rc.bottom-rc.top
     
        SystemParametersInfo 0x0030,0,@spinfo,0    'SPI_GETWORKAREA=0x30
     
        indexbase 1
        int horz=spinfo[1]+(spinfo[3]-spinfo[1]-width)>>1
        int vert=spinfo[2]+(spinfo[4]-spinfo[2]-height)>>1
     
        select place
        case 0 : hpos=horz : vpos=vert             'center
        case 1 : hpos=spinfo[1] : vpos=vert        'left center
        case 2 : hpos=horz : vpos=spinfo[2]        'top center
        case 3 : hpos=spinfo[3]-width : vpos=vert  'right center 
        case 4 : hpos=horz : vpos=spinfo[4]-height 'bottom center
        end select
            
        SetWindowPos (hWnd, 0, hpos, vpos, width, height, 4) 'SWP_NOZORDER=4
    end sub