Author Topic: FreeBASIC COM  (Read 3627 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
FreeBASIC COM
« on: September 26, 2017, 11:37:10 PM »
I took a peek at Paul's and Jose WinFBE project and notice a OCX calender example.

Code: FreeBasic
  1. ' ########################################################################################
  2. ' Microsoft Windows
  3. ' Contents: Embedded MonthView Calendar OCX
  4. ' Compiler: FreeBasic 32 bit only
  5. ' Copyright (c) 2016 Jos� Roca. Freeware. Use at your own risk.
  6. ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
  7. ' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  8. ' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  9. ' ########################################################################################
  10.  
  11. #define UNICODE
  12. #INCLUDE ONCE "Afx/CWindow.inc"
  13. #INCLUDE ONCE "Afx/COleCon/COleCon.inc"
  14. USING Afx
  15.  
  16. DECLARE FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
  17.                           BYVAL hPrevInstance AS HINSTANCE, _
  18.                           BYVAL szCmdLine AS ZSTRING PTR, _
  19.                           BYVAL nCmdShow AS LONG) AS LONG
  20.  
  21.    END WinMain(GetModuleHandleW(NULL), NULL, COMMAND(), SW_NORMAL)
  22.  
  23. ' // Forward declaration
  24. DECLARE FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
  25.  
  26. ' ========================================================================================
  27. ' Main
  28. ' ========================================================================================
  29. FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
  30.                   BYVAL hPrevInstance AS HINSTANCE, _
  31.                   BYVAL szCmdLine AS ZSTRING PTR, _
  32.                   BYVAL nCmdShow AS LONG) AS LONG
  33.  
  34.    ' // Set process DPI aware
  35.    ' // The recommended way is to use a manifest file
  36. '   AfxSetProcessDPIAware
  37.  
  38.    ' // Creates the main window
  39.    DIM pWindow AS CWindow
  40.    ' -or- DIM pWindow AS CWindow = "MyClassName" (use the name that you wish)
  41.    pWindow.Create(NULL, "COleCon - Embedded MonthView Calendar OCX", @WndProc)
  42.    ' // Sizes it by setting the wanted width and height of its client area
  43.    pWindow.SetClientSize(580, 360)
  44.    ' // Centers the window
  45.    pWindow.Center
  46.  
  47.    DIM wszLibName AS WSTRING * 260 = ExePath & "\MSCOMCT2.OCX"
  48.    DIM CLSID_MSComCtl2_MonthView AS CLSID = (&h232E456A, &h87C3, &h11D1, {&h8B, &hE3,&h00, &h00, &hF8, &h75, &h4D, &hA1})
  49.    DIM IID_MSComCtl2_MonthView AS CLSID = (&h232E4565, &h87C3, &h11D1, {&h8B, &hE3,&h00, &h00, &hF8, &h75, &h4D, &hA1})
  50.    DIM RTLKEY_MSCOMCT2 AS WSTRING * 260 = "651A8940-87C5-11d1-8BE3-0000F8754DA1"
  51.    DIM pOleCon AS COleCon PTR = NEW COleCon(@pWindow, 1001, wszLibName, CLSID_MSComCtl2_MonthView, _
  52.        IID_MSComCtl2_MonthView, RTLKEY_MSCOMCT2, 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
  53.  
  54.    ' // Displays the window and dispatches the Windows messages
  55.    FUNCTION = pWindow.DoEvents(nCmdShow)
  56.  
  57. END FUNCTION
  58. ' ========================================================================================
  59.  
  60. ' ========================================================================================
  61. ' Main window procedure
  62. ' ========================================================================================
  63. FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
  64.  
  65.    SELECT CASE uMsg
  66.  
  67.       CASE WM_COMMAND
  68.          SELECT CASE GET_WM_COMMAND_ID(wParam, lParam)
  69.             CASE IDCANCEL
  70.                ' // If ESC key pressed, close the application by sending an WM_CLOSE message
  71.                IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN
  72.                   SendMessageW hwnd, WM_CLOSE, 0, 0
  73.                   EXIT FUNCTION
  74.                END IF
  75.          END SELECT
  76.  
  77.       CASE WM_SIZE
  78.          ' // Optional resizing code
  79.          IF wParam <> SIZE_MINIMIZED THEN
  80.             ' // Retrieve a pointer to the CWindow class
  81.             DIM pWindow AS CWindow PTR = AfxCWindowPtr(hwnd)
  82.             ' // Move the position of the button
  83.             IF pWindow THEN pWindow->MoveWindow GetDlgItem(hwnd, 1001), _
  84.                0, 0, pWindow->ClientWidth, pWindow->ClientHeight, CTRUE
  85.          END IF
  86.  
  87.         CASE WM_DESTROY
  88.          ' // Ends the application by sending a WM_QUIT message
  89.          PostQuitMessage(0)
  90.          EXIT FUNCTION
  91.  
  92.    END SELECT
  93.  
  94.    ' // Default processing of Windows messages
  95.    FUNCTION = DefWindowProcW(hwnd, uMsg, wParam, lParam)
  96.  
  97. END FUNCTION
  98. ' ========================================================================================
  99.  

Seems like a lot of work. Here is a Script BASIC equivalent with a twist of using the .NET calendar control in C# and made COM visible.

Code: ScriptBasic
  1. import com.inc
  2.  
  3. cs = CreateObject("Sample.Sample")
  4.  
  5. if cs = 0 then
  6.         print "Failed to create the C# com object did you register the dll with regasm and have .NET installed?"
  7.         return
  8. end if
  9.  
  10. d = CallByName(cs, "GetDate")
  11. print "User Selected date: ", d
  12.  



 
« Last Edit: September 26, 2017, 11:40:06 PM by John »