         title   xxxx - Dialog as Main Window

         .586
         .model flat, stdcall
         option casemap :none   ; Case sensitive

            include  \AsmEditDir\include\Windows.inc
            include  \HutchDir\include\user32.inc
            include  \HutchDir\include\GDI32.inc
            include  \HutchDir\include\kernel32.inc
            include  \HutchDir\include\comdlg32.inc
            include  \HutchDir\include\COMCTL32.inc

   include  \AsmEditDir\include\DSPMACRO.asm

         includelib  \HutchDir\lib\user32.lib
         includelib  \HutchDir\lib\GDI32.lib
         includelib  \HutchDir\lib\kernel32.lib
         includelib  \HutchDir\lib\comdlg32.lib
         includelib  \HutchDir\lib\COMCTL32.lib

WinMain        PROTO  :DWORD, :DWORD, :DWORD, :DWORD
WndProc        PROTO  :DWORD, :DWORD, :DWORD, :DWORD
SetColor       PROTO  :DWORD, :DWORD, :DWORD, :DWORD

.const
EditID         equ 1
IDM_EXIT       equ 2
IDM_COLOR      equ 3

.data
RichEdit       db  'RichEdit20A',0
RichEdDLL      db  'RICHED20.DLL',0

ClassName      db  'DLGCLASS',0
MainDlg        db  'MAINDLG',0
dlgname        db  'SetColors',0
MenuName       db  'MainMenu',0
AppName        db  'xxxx',0
szError1       db  'The RICHED20.DLL was not found!',0
szText         db  0Dh,0Ah
               db  ' H A V E   A   N I C E   D A Y !',0

.data?
hInst          dd  ?
CommandLine    dd  ?
hIcon          dd  ?
MainExit       dd  ?
hDlg           dd  ?
hWnd           dd  ?
hREdDLL        dd  ?
hREdit         dd  ?


.code

start:

      INVOKE     GetModuleHandle, NULL
         mov     hInst, eax
      INVOKE     GetCommandLine
         mov     CommandLine, eax

           call     InitCommonControls          ; Initialize the common ctrl lib
         INVOKE     LoadLibrary, addr RichEdDLL ; Load the Riched20.dll
            mov     hREdDLL, eax
         .if !eax
            INVOKE     MessageBox, NULL, addr szError1, addr AppName, MB_OK or MB_ICONERROR
            jmp     NoGo
      .endif

      INVOKE     WinMain, hInst ,NULL, CommandLine, SW_SHOWDEFAULT
         mov     MainExit, eax
      INVOKE     FreeLibrary, hREdDLL

NoGo:
      INVOKE     ExitProcess, MainExit

;________________________________________________________________________________
WinMain proc  hinst:DWORD, hPrevInst, CmdLine, CmdShow
LOCAL    wc:WNDCLASSEX
LOCAL    msg:MSG

         mov     wc.cbSize, sizeof WNDCLASSEX
         mov     wc.style, CS_HREDRAW or CS_VREDRAW
         mov     wc.lpfnWndProc, offset WndProc
         mov     wc.cbClsExtra, NULL
         mov     wc.cbWndExtra, DLGWINDOWEXTRA
        push     hInst
         pop     wc.hInstance
         mov     wc.hbrBackground, COLOR_WINDOW+1
         mov     wc.lpszMenuName, offset MenuName
         mov     wc.lpszClassName, offset ClassName
      INVOKE     LoadIcon, NULL, IDI_APPLICATION
;      INVOKE     LoadIcon, hInst, 200
         mov     wc.hIcon, eax
         mov     wc.hIconSm, eax
      INVOKE     LoadCursor, NULL, IDC_ARROW
         mov     wc.hCursor, eax
      INVOKE     RegisterClassEx, addr wc

      INVOKE     CreateDialogParam, hInst, addr MainDlg, NULL, addr WndProc, NULL
         mov     hDlg, eax
         mov     hWnd, eax
      INVOKE     ShowWindow, hDlg, SW_SHOWNORMAL
      INVOKE     UpdateWindow, hDlg

      .while TRUE
         INVOKE     GetMessage, addr msg, NULL, 0, 0
            .BREAK .IF (!eax)
               INVOKE     TranslateMessage, addr msg
               INVOKE     DispatchMessage, addr msg
      .endw
         mov     eax, msg.wParam
         ret
WinMain endp

;________________________________________________________________________________
WndProc proc  uses edx  hwnd:DWORD, wMsg, wParam, lParam

      .if wMsg == WM_CREATE
         INVOKE     CreateWindowEx, NULL, addr RichEdit, NULL,\
                    WS_VISIBLE or WS_CHILD or ES_LEFT or ES_MULTILINE or\
                    ES_AUTOHSCROLL or ES_AUTOVSCROLL,\
                    0, 0, 0, 0, hwnd, EditID, hInst, NULL
            mov     hREdit, eax

         INVOKE     SendMessage, hREdit, EM_EXLIMITTEXT, 0, 100000
         INVOKE     SetFocus, hREdit

      .elseif wMsg == WM_SIZE
            mov     eax, lParam
            mov     edx, eax
            shr     edx, 16
            and     eax, 0ffffh
         INVOKE     MoveWindow, hREdit, 0, 0, eax, edx, TRUE

      .elseif wMsg == WM_DESTROY
        INVOKE     PostQuitMessage, NULL

      .elseif wMsg == WM_COMMAND
            mov     eax, wParam
           cwde                         ; Only low word contains command

         .if eax == IDM_COLOR
            INVOKE     DialogBoxParam, hInst, addr dlgname, 0, addr SetColor, 0

         .elseif eax == IDM_EXIT
            INVOKE     DestroyWindow, hwnd
         .endif
      .else

DefWin:
         INVOKE     DefWindowProc, hwnd, wMsg, wParam, lParam
            ret
      .endif

         xor    eax, eax
         ret
WndProc endp

;________________________________________________________________________________
SetColor proc  hdlg:DWORD, uMsg, wParam, lParam
LOCAL    charF:CHARFORMAT2

      .if uMsg == WM_COMMAND
         .if wParam == IDCANCEL
               jmp     GetOut

         .elseif wParam == 401              ; SetColors
            INVOKE     SendMessage, hREdit, EM_SETBKGNDCOLOR, 0, 00000000h ;00GGBBRRh
               mov     charF.cbSize, sizeof charF
               mov     charF.dwEffects, 0
               mov     charF.yHeight, 240
               mov     charF.dwMask, CFM_FACE or CFM_SIZE or CFM_COLOR
               mov     charF.crTextColor, 0000ff00h
               mov     charF.crBackColor, 00000000h
            INVOKE     SendMessage, hREdit, EM_SETCHARFORMAT, SCF_ALL, addr charF
            INVOKE     SendMessage, hREdit, WM_SETTEXT, NULL, addr szText
            INVOKE     SetFocus, hREdit
               jmp     GetOut
         .endif

      .elseif uMsg == WM_CLOSE

GetOut:
         INVOKE     EndDialog, hdlg, wParam ; End the dialog with wparam as return
            mov     eax, TRUE               ; Return
            jmp     SetColorRet             ; with TRUE

      .endif

Ret0:
         xor     eax, eax

SetColorRet:
         ret
SetColor endp

end start
;INVOKE     MessageBox, NULL, addr szText, addr AppName, MB_OK
