' This is a modified version of the original
' wnd.bas written by William Yu.

' It's been newly revised so that it now
' works under both... Win98 AND WinXP.
' I'd like to personally thank Danny
' (aka beachtiger102) for providing the
' solution for getting this to work
' under Win98.

' Note: the IDI_WINLOGO displays properly
'       under Win98. Under WinXP... it will
'       display a generic icon. According to
'       Microsoft Support... this is "by design"
'       and "only" applies to WinXP. Refer to...
'       http://support.microsoft.com/?kbid=305944

' Pure Windows
' Uses only Window's API calls to create windows

$TYPECHECK ON
$INCLUDE "RAPIDQ.INC"

'-- Some Standard Cursor IDs
CONST IDC_ARROW = 32512&
CONST IDC_IBEAM = 32513&
CONST IDC_WAIT = 32514&
CONST IDC_CROSS = 32515&
CONST IDC_UPARROW = 32516&
CONST IDC_SIZE = 32540&
CONST IDC_ICON = 32541&
CONST IDC_SIZENWSE = 32542&

'-- Standard Icon IDs
CONST IDI_APPLICATION = 32512&
CONST IDI_HAND = 32513&
CONST IDI_QUESTION = 32514&
CONST IDI_EXCLAMATION = 32515&
CONST IDI_ASTERISK = 32516&
CONST IDI_WINLOGO = 32517&

'-- Class Styles
CONST CS_VREDRAW = 1
CONST CS_HREDRAW = 2
CONST CS_KEYCVTWINDOW = 4
CONST CS_DBLCLKS = 8
CONST CS_OWNDC = &H20
CONST CS_CLASSDC = &H40
CONST CS_PARENTDC = &H80
CONST CS_NOKEYCVT = &H100
CONST CS_NOCLOSE = &H200
CONST CS_SAVEBITS = &H800
CONST CS_BYTEALIGNCLIENT = &H1000
CONST CS_BYTEALIGNWINDOW = &H2000
CONST CS_GLOBALCLASS = &H4000
CONST CS_IME = &H10000

CONST CW_USEDEFAULT = &H80000000

'-- Window styles
CONST WS_CAPTION = &HC00000
CONST WS_CHILD = &H40000000
CONST WS_VISIBLE = &H10000000
CONST WS_BORDER = &H800000
CONST WS_SYSMENU = &H80000
CONST WS_THICKFRAME = &H40000
CONST WS_SIZEBOX = WS_THICKFRAME
CONST WS_CLIPCHILDREN = &H2000000

CONST WS_MINIMIZEBOX = &H20000
CONST WS_MAXIMIZEBOX = &H10000

CONST WS_EX_DLGMODALFRAME = 1
CONST WS_EX_NOPARENTNOTIFY = 4
CONST WS_EX_TOPMOST = 8
CONST WS_EX_ACCEPTFILES = &H10
CONST WS_EX_TRANSPARENT = &H20
CONST WS_EX_MDICHILD = &H40&  ' adding the & symbol at the end of this constant solved the WinXP problem.
CONST WS_EX_TOOLWINDOW = &H80
CONST WS_EX_WINDOWEDGE = &H100
CONST WS_EX_CLIENTEDGE = &H200
CONST WS_EX_CONTEXTHELP = &H400

CONST WS_EX_RIGHT = &H1000
CONST WS_EX_LEFT = 0
CONST WS_EX_RTLREADING = &H2000
CONST WS_EX_LTRREADING = 0
CONST WS_EX_LEFTSCROLLBAR = &H4000
CONST WS_EX_RIGHTSCROLLBAR = 0

CONST WS_EX_CONTROLPARENT = &H10000
CONST WS_EX_STATICEDGE = &H20000
CONST WS_EX_APPWINDOW = &H40000

'-- Button styles
CONST BS_PUSHBUTTON = 0
CONST BS_DEFPUSHBUTTON = 1
CONST BS_CHECKBOX = 2
CONST BS_AUTOCHECKBOX = 3
CONST BS_RADIOBUTTON = 4
CONST BS_3STATE = 5
CONST BS_AUTO3STATE = 6
CONST BS_GROUPBOX = 7
CONST BS_USERBUTTON = 8
CONST BS_AUTORADIOBUTTON = 9
CONST BS_OWNERDRAW = 11
CONST BS_LEFTTEXT = &H20
CONST BS_TEXT = 0
CONST BS_ICON = &H40
CONST BS_BITMAP = &H80
CONST BS_LEFT = &H100
CONST BS_RIGHT = &H200
CONST BS_CENTER = 768
CONST BS_TOP = &H400
CONST BS_BOTTOM = &H800
CONST BS_VCENTER = 3072
CONST BS_PUSHLIKE = &H1000
CONST BS_MULTILINE = &H2000
CONST BS_NOTIFY = &H4000
CONST BS_FLAT = &H8000
CONST BS_RIGHTBUTTON = BS_LEFTTEXT

CONST WM_DESTORY = 2

Const COLOR_WINDOW = 5  ' using this constant solved the problem for both Win98 AND WinXP.

TYPE TWNDCLASSEX
    cbSize AS LONG
    style AS LONG
    lpfnWndProc AS LONG
    cbClsExtra AS LONG
    cbWndExtra AS LONG
    hInstance AS LONG
    hIcon AS LONG
    hCursor AS LONG
    hbrBackground AS LONG
    lpszMenuName AS LONG
    lpszClassName AS LONG
    hIconSm AS LONG
END TYPE

TYPE TMSG
    hwnd AS LONG
    message AS LONG
    wParam AS LONG
    lParam AS LONG
    time AS DWORD
    x as LONG
    y as LONG
END TYPE

DECLARE FUNCTION RegisterClassEx LIB "USER32" ALIAS "RegisterClassExA" _
                 (WndClass AS TWNDCLASSEX) AS WORD

DECLARE FUNCTION GetMessage LIB "USER32" ALIAS "GetMessageA" _
                 (lpMsg AS TMSG, hWnd AS LONG, wMsgFilterMin AS LONG, _
                  wMsgFilterMax AS LONG) AS LONG

DECLARE FUNCTION LoadCursor LIB "USER32" ALIAS "LoadCursorA" _
                 (hInst AS LONG, lpCursorName AS LONG) AS LONG

DECLARE FUNCTION LoadIcon LIB "USER32" ALIAS "LoadIconA" _
                 (hInst AS LONG, lpIconName AS LONG) AS LONG

DECLARE FUNCTION CreateWindowEx LIB "USER32" ALIAS "CreateWindowExA" _
                 (ExStyle&, ClassName$, WindowName$, Style&, X&, Y&, _
                  Width&, Height&, WndParent&, hMenu&, hInstance&, Param&) AS LONG

DECLARE FUNCTION UpdateWindow LIB "USER32" ALIAS "UpdateWindow" _
                 (hWnd AS LONG) AS LONG

DECLARE FUNCTION DefWindowProc LIB "USER32" ALIAS "DefWindowProcA" _
                 (hWnd AS LONG, Msg AS LONG, wParam AS LONG, lParam AS LONG) AS LONG

DECLARE FUNCTION PostQuitMessage LIB "USER32" ALIAS "PostQuitMessage" _
                 (nExitCode AS LONG) AS LONG

DECLARE FUNCTION TranslateMessage LIB "USER32" ALIAS "TranslateMessage" _
                 (lpMsg AS TMSG) AS LONG

DECLARE FUNCTION DispatchMessage LIB "USER32" ALIAS "DispatchMessageA" _
                 (lpMsg AS TMSG) AS LONG

Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" _
                 (ByVal lpModuleName As String) As Long 

DIM WndClassEx AS TWNDCLASSEX
DIM Msg AS TMSG

DEFSTR ClassName$ = "RapidQ"

DEFINT Handle, ChildHandle, Button1

FUNCTION WindowProc (hWnd AS LONG, uMsg AS LONG, wParam AS LONG, lParam AS LONG) AS LONG
    Result = DefWindowProc(hWnd, uMsg, wParam, lParam)
    IF uMsg = WM_DESTORY THEN
       PostQuitMessage(0)
    END IF
END FUNCTION

WITH WndClassEx
    .cbSize = SIZEOF(WndClassEx)
    .style = CS_CLASSDC or CS_PARENTDC
    .lpfnWndProc = CODEPTR(WindowProc)
    .cbClsExtra = 0
    .cbWndExtra = 0
    .hInstance = GetModuleHandle(Application.ExeName)  ' Danny provided this solution for the Win98 problem
    .hIcon = LoadIcon(0, IDI_WINLOGO)
    .hCursor = LoadCursor(0, IDC_CROSS)
    .hbrBackground = COLOR_WINDOW
    .lpszMenuName = 0
    .lpszClassName = VARPTR(ClassName$)
    .hIconSm = LoadIcon(0, IDI_WINLOGO)
END WITH

RegisterClassEx(WndClassEx)

Handle = CreateWindowEx(WS_EX_TOPMOST, ClassName$, "Pure Rapid-Q", _
                        WS_CLIPCHILDREN OR WS_VISIBLE OR WS_SIZEBOX OR WS_CAPTION OR WS_SYSMENU OR WS_MINIMIZEBOX OR WS_MAXIMIZEBOX, _
                        CW_USEDEFAULT, CW_USEDEFAULT, 320, 240, _
                        0, 0, WndClassEx.hInstance, 0)

ChildHandle = CreateWindowEx(WS_EX_MDICHILD, ClassName$, "Pure Rapid-Q", _
                        WS_CHILD OR WS_VISIBLE OR WS_SIZEBOX OR WS_CAPTION OR WS_SYSMENU OR WS_MINIMIZEBOX OR WS_MAXIMIZEBOX, _
                        CW_USEDEFAULT, CW_USEDEFAULT, 200, 200, _
                        Handle, 0, WndClassEx.hInstance, 0)

Button1 = CreateWindowEx(WS_EX_WINDOWEDGE, "Button", "OK", WS_CHILD OR WS_VISIBLE OR WS_BORDER OR BS_PUSHLIKE OR BS_TEXT, _
                         220, 20, 75, 25, Handle, 0, WndClassEx.hInstance, 0)

UpdateWindow(Handle)

WHILE (GetMessage(Msg, 0, 0, 0))    '-- Main message loop
    TranslateMessage(Msg)
    DispatchMessage(Msg)
WEND

