Advertisement
Ham62

GenBinaryWebContentFile.bas

Aug 21st, 2017
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define fbc -s gui
  2. #include once "windows.bi"
  3. #include once "win\commctrl.bi"
  4.  
  5. '********* Enumerating the Window's Control ID's *********'
  6. enum WindowControls
  7.   wcMain
  8.  
  9.   wcFileLabel       'Label saying which input is filename
  10.   wcFileTxtBox      'To type filename to write
  11.  
  12.   wcTitleLabel      'Laben infront of title input
  13.   wcTitleTxtBox     'Page Title input
  14.  
  15.   wcContentLabel    'Label above content input
  16.   wcContentTxtBox   'Page Content input
  17.  
  18.   wcSubmitBtn       'Submit button
  19.   wcLast
  20. end enum
  21.  
  22. Declare sub WinMain()
  23. Dim Shared as hwnd CTL(wcLast)      'controls
  24. Dim Shared as hinstance APPINSTANCE  'instance
  25. Dim Shared as hfont MyFont           'Font
  26. Dim Shared as string AppName        'AppName (window title 'prefix')
  27.  
  28. AppName = "Website Content File Generator"
  29. InitCommonControls()
  30. APPINSTANCE = GetModuleHandle(null)
  31. WinMain()
  32.  
  33. dim shared as any ptr ButProc
  34.  
  35. Sub CreateContentFile()
  36.     Dim Filename as zString * 100
  37.     Dim PageTitle as zString * 100
  38.     Dim PageContent as zString * 65535
  39.    
  40.     GetWindowText(ctl(wcFileTxtBox), Filename, 100)
  41.     GetWindowText(ctl(wcTitleTxtBox), PageTitle, 100)
  42.     GetWindowText(ctl(wcContentTxtBox), PageContent, 65535)
  43.    
  44.     If (Open(Filename for binary access write as #1)) Then
  45.         MessageBox(null, !"Error: Could not open file:\r\n"+Filename, AppName, MB_ICONERROR)
  46.         return
  47.     End If
  48.    
  49.     Put #1,, "BWCF"     'Write header... stands for "Binary Web Content File"
  50.    
  51.     Put #1,, Chr(&H01)                       ' Indicates page title
  52.     Put #1,, Len(PageTitle)                  ' Length of title to read
  53.     Put #1,, Left(PageTitle, Len(PageTitle)) ' Actual title
  54.    
  55.     Put #1,, Chr(&H02)                       ' Indicates page contents
  56.     Put #1,, Len(PageContent)                ' Length of content to read
  57.     Put #1,, Left(PageContent, Len(PageContent)) ' Actual content
  58.    
  59.     Close #1
  60.    
  61.     MessageBox(null,"Wrote file successfully!", AppName, MB_ICONINFORMATION)
  62. End Sub
  63.  
  64. '***********************************************'
  65. '******** Routines for WinAPI functions ********'
  66. '***********************************************'
  67. Function WndProc (hWnd as HWND, message as UINT, wParam as WPARAM, lParam as LPARAM) as LRESULT
  68.     Select Case message
  69.     Case WM_CREATE  'Window was created
  70.         Scope   'Calculate Client Area Size
  71.             Dim as rect RcWnd = any, RcCli = Any, RcDesk = any
  72.             GetClientRect(hWnd, @RcCli)
  73.             GetClientRect(GetDesktopWindow(), @RcDesk)
  74.             GetWindowRect(hWnd, @RcWnd)
  75.             'Window Rect is in SCREEN coordinate.... make right/bottom become WID/HEI
  76.             with RcWnd
  77.                 .right -= .left: .bottom -= .top
  78.                 .right += (.right-RcCli.right)  'Add difference cli/wnd
  79.                 .bottom += (.bottom-RcCli.bottom)   'add difference cli/wnd
  80.                 var CenterX = (RcDesk.right-.right)\2
  81.                 var CenterY = (RcDesk.bottom-.bottom)\2
  82.                 SetWindowPos(hwnd,null,CenterX,CenterY,.right,.bottom,SWP_NOZORDER)
  83.             end with
  84.         end Scope
  85.        
  86.         'To help with creating controls
  87.         #define CreateControl( mID , mExStyle , mClass , mCaption , mStyle , mX , mY , mWid , mHei ) CTL(mID) = CreateWindowEx(mExStyle,mClass,mCaption,mStyle,mX,mY,mWid,mHei,hwnd,cast(hmenu,mID),APPINSTANCE,null)
  88.  
  89.         '***** Define style constants *****
  90.         const cBase = WS_VISIBLE OR WS_CHILD
  91.         const cLabel = cBase OR WS_TABSTOP
  92.         const cButtonStyle = cBase OR BS_MULTILINE              'Standard button style
  93.         const cSingleLnTxtBox = cBase OR WS_HSCROLL OR ES_AUTOHSCROLL
  94.         const cMultiLnTxtBox = cBase OR ES_MULTILINE OR WS_VSCROLL OR ES_AUTOVSCROLL
  95.         const cEx1 = WS_EX_CLIENTEDGE
  96.        
  97.         '***** Create objects on window *****
  98.         CreateControl(wcFileLabel, null, "static", "Filename:", cLabel, 10, 10, 45, 15)
  99.         CreateControl(wcFileTxtBox, cEx1, WC_EDIT,  null, cSingleLnTxtBox, 60, 8, 430, 20)
  100.  
  101.         CreateControl(wcTitleLabel, null, "static", "Title:", cLabel, 10, 40, 25, 15)
  102.         CreateControl(wcTitleTxtBox, cEx1, WC_EDIT,  null, cSingleLnTxtBox, 60, 38, 430, 20)
  103.        
  104.         CreateControl(wcContentLabel, null, "static", "Content:", cLabel, 10, 70, 40, 20)
  105.         CreateControl(wcContentTxtBox, cEx1, WC_EDIT, null, cMultiLnTxtBox, 60, 70, 430, 215)
  106.  
  107.         CreateControl(wcSubmitBtn, null, WC_BUTTON, "Generate File", cButtonStyle, 60, 295, 75, 30)
  108.        
  109.         '***** Create fonts *****
  110.         var hDC = GetDC(hWnd)
  111.         var nHeight = -MulDiv(8, GetDeviceCaps(hDC, LOGPIXELSY), 72)    'Calculate size for DPI
  112.         MyFont = CreateFont(nHeight,0,0,0,FW_NORMAL,0,0,0,DEFAULT_CHARSET,0,0,0,0,"MS Sans Serif")
  113.        
  114.         '***** Set font for all controls *****
  115.         For CNT as Integer = wcMain to wcLast-1
  116.             SendMessage(CTL(CNT),WM_SETFONT,cast(wparam,MyFont),true)
  117.         Next CNT
  118.         ReleaseDC(hWnd,hDC)
  119.        
  120.         '***** Apply extra styling *****
  121.  
  122.     Case WM_COMMAND 'Event happened to a control (child window)
  123.         Select case hiword(wparam)
  124.         case BN_CLICKED 'button click
  125.             select case lparam
  126.             case CTL(wcSubmitBtn)
  127.                 CreateContentFile()
  128.             end Select
  129.         End Select
  130.        
  131.     Case WM_CLOSE,WM_DESTROY    'Window was closed/destroyed
  132.         PostQuitMessage(0)  'to quit
  133.         return 0
  134.     End Select
  135.     '***** If we get here a default predefined action will happen *****
  136.     return DefWindowProc(hWnd,message,wParam,lParam)
  137. End Function
  138.  
  139. '******************************'
  140. '***** WinMain Subroutine *****'
  141. '******************************'
  142. Sub WinMain ()
  143.     Dim wMsg as MSG
  144.     Dim wcls as WNDCLASS
  145.     Dim as HWND hWnd
  146.    
  147.     'Setup window class
  148.     With wcls
  149.         .style          = CS_HREDRAW OR CS_VREDRAW OR CS_SAVEBITS
  150.         .lpfnWndProc    = @WndProc
  151.         .cbClsExtra     = 0
  152.         .cbWndExtra     = 0
  153.         .hInstance      = APPINSTANCE
  154.         .hIcon          = LoadIcon(APPINSTANCE, "FB_PROGRAM_ICON")
  155.         .hCursor        = LoadCursor(NULL, IDC_ARROW)
  156.         .hbrBackground  = cast(hBrush, COLOR_BTNFACE + 1)
  157.         .lpszMenuName   = NULL
  158.         .lpszClassName  = strptr(AppName)
  159.     End With
  160.    
  161.     'Rehister window class
  162.     If (RegisterClass(@wcls)=FALSE) Then
  163.         MessageBox(null,"Failed to register wcls!", AppName, MB_ICONERROR)
  164.         Exit Sub
  165.     End If
  166.    
  167.     Const wWidth = 500, wHeight = 340
  168.     Const cWindowStyle = WS_VISIBLE OR WS_TILEDWINDOW OR WS_CLIPCHILDREN _
  169.                          XOR WS_THICKFRAME XOR WS_MAXIMIZEBOX 'No resize or maximize
  170.        
  171.     'Create the window and show it
  172.     hWnd = CreateWindowEx(WS_EX_COMPOSITED OR WS_EX_LAYERED,AppName,AppName, _
  173.     cWindowStyle, 200,200,wWidth,wHeight,null,null,APPINSTANCE,NULL)
  174.     SetforegroundWindow(hWnd)
  175.    
  176.     'Process windows messages
  177.     '***** All messages(events) will be read converted/dispatched here *****
  178.     UpdateWindow(hWnd)
  179.  
  180.     while (GetMessage(@wMsg, NULL, 0, 0) <> FALSE)
  181.         TranslateMessage(@wMsg)
  182.         DispatchMessage(@wMsg)
  183.     wend
  184.    
  185. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement