Fortran Coder

查看: 8075|回复: 2
打印 上一主题 下一主题

[绘图界面库] Intel Fortran 调用API函数创建窗体,同时创建控制台输出(

[复制链接]

3

帖子

2

主题

0

精华

入门

F 币
45 元
贡献
21 点
跳转到指定楼层
楼主
发表于 2016-11-5 15:48:55 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
看到论坛有人 写过 窗体绘制云图,利用控制台输出,这里利用windows API 实现该功能
源码如下
[Fortran] 纯文本查看 复制代码
function WinMain (hCurrentInst, hPreviousInst, lpszCmdLine, nCmdShow)
    !DEC$ ATTRIBUTES STDCALL,DECORATE,ALIAS:"WinMain" :: WinMain
    use Windows_MDL9966
    implicit none

    integer(SINT) :: WinMain
    integer(HANDLE), intent(IN) :: hCurrentInst, hPreviousInst
    integer(LPCSTR), intent(IN) :: lpszCmdLine
    integer(SINT), intent(IN) :: nCmdShow

    integer(HANDLE) :: hWnd ! Window
    integer(HANDLE) :: hInstance
    integer(SINT) :: ret,retclass
    integer(BOOL) :: bret,AllocConsole_ret
    type(T_MSG) :: msg      ! Message
    type(T_WNDCLASSEX)::wndclass
    character(len=*), parameter :: szClassName = "Windows"C
    
    AllocConsole_ret=AllocConsole()!
    ! 创建控制台在这里
    if(AllocConsole_ret/=0) print *,'创建控制台成功'

hInstance = GetModuleHandle(NULL)
    wndclass.cbSize = sizeof(wndclass)
    wndclass.style = CS_OWNDC
    wndclass.lpfnWndProc = loc(WindowProc)
    wndclass.cbClsExtra = 0
    wndclass.cbWndExtra = 0
    wndclass.hInstance = hInstance
    wndclass.hIcon = LoadIcon(0_HANDLE, int(IDI_WINLOGO,LPVOID))
    wndclass.hCursor = LoadCursor(0_HANDLE, int(IDC_ARROW,LPVOID))
    wndclass.hbrBackground = (COLOR_WINDOW + 1) !NULL
    wndclass.lpszMenuName = NULL
    wndclass.lpszClassName = loc(szClassName)
    wndclass.hIconSm = NULL

    retclass = RegisterClassEx(wndclass)
    
    if(retclass/=0) print *,'RegisterClass成功'

    WinMain = 0
    hWnd = CreateWindowEx(0,&
    szClassName, &
    'hello' // CHAR(0), &
    WS_OVERLAPPEDWINDOW, 200,0,400,400,&
    NULL,&
    NULL,&
    hInstance,&
    null&
    )
    if (hwnd == NULL) return

    bret = ShowWindow(hWND, SW_SHOW)
    bret = UpdateWindow(hWnd)

    mainloop: do while (Isrun)!
        if (PeekMessage(msg, hWnd, 0, 0, PM_REMOVE) /= 0) then
            if (msg%message == WM_QUIT) exit mainloop
            bret = TranslateMessage(msg)
            bret = DispatchMessage(msg)
        end if
        call sleep(10) ! sleep for 10ms
    end do mainloop

    return

    end function WinMain


回调函数
[Fortran] 纯文本查看 复制代码
    module Windows_MDL9966

    use IFWINA ! Alternate version of ifwin that renames MSFWIN$ routines that conflict with QuickWin
    use, intrinsic :: ISO_C_BINDING
    
    logical :: Isrun = .TRUE. 
    
    contains
    integer(LONG) function WindowProc (hWnd, uMsg, wParam, lParam)
    use IFWINA ! Alternate version of ifwin that renames MSFWIN$ routines that conflict with QuickWin
    use, intrinsic :: ISO_C_BINDING
    !DEC$ ATTRIBUTES STDCALL :: WindowProc
    integer(HANDLE) :: hWnd    ! Window handle
    integer(UINT) :: uMsg      ! Message
    integer(fWPARAM) :: wParam ! Message parameter
    integer(fLPARAM) :: lParam ! Message parameter
    integer(HANDLE) :: hret
    integer(BOOL):: rectret
    integer(SINT) :: DrawTextret
    type(T_PAINTSTRUCT) :: ps
    type(T_RECT)::rect
    
    WindowProc = FALSE

    select case (uMsg)

    case (WM_CLOSE)
        Isrun = .false.
        call PostQuitMessage(0)
        return
        
    case (WM_PAINT)  
hret = BeginPaint(hWnd, ps)
        rectret=GetClientRect(hwnd, rect) 
        DrawTextret=DrawText( hret, "Hello, 这是我自己的窗口!"//char(0), -1, rect,&
        IOR(DT_SINGLELINE, IOR( DT_CENTER , DT_VCENTER ))) 
        hret = EndPaint(hWnd, ps)
        if(hret==1) print *,'窗体重绘'
        return 

    end select

    ! Not ours to handle  
    WindowProc = DefWindowProc( hWnd, uMsg, wParam, lParam )

    end function WindowProc
    
    end module Windows_MDL9966



分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

3

帖子

2

主题

0

精华

入门

F 币
45 元
贡献
21 点
沙发
 楼主| 发表于 2016-11-5 16:05:42 | 只看该作者
这是可能会出现一些麻烦,UI 所以推荐创建 多线程
实现多线程 代码如下
[Fortran] 纯文本查看 复制代码
    module Windows_MDL

    use IFWINA ! Alternate version of ifwin that renames MSFWIN$ routines that conflict with QuickWin
    use, intrinsic :: ISO_C_BINDING
    integer(HANDLE) :: hThread1,hThread2
    logical :: Isrun = .TRUE. 

    contains
    integer(LONG) function WindowProc (hWnd, uMsg, wParam, lParam)
    use IFWINA ! Alternate version of ifwin that renames MSFWIN$ routines that conflict with QuickWin
    use, intrinsic :: ISO_C_BINDING
    !DEC$ ATTRIBUTES STDCALL :: WindowProc
    integer(HANDLE) :: hWnd    ! Window handle
    integer(UINT) :: uMsg      ! Message
    integer(fWPARAM) :: wParam ! Message parameter
    integer(fLPARAM) :: lParam ! Message parameter
    integer(HANDLE) :: hret
    integer(BOOL):: rectret
    integer(SINT) :: DrawTextret
    type(T_PAINTSTRUCT) :: ps
    type(T_RECT)::rect
    
    WindowProc = FALSE

    select case (uMsg)

    case (WM_CLOSE)
        Isrun = .false.
        hret=closehandle(hThread1)
        hret=closehandle(hThread2)
        call PostQuitMessage(0)
        return
        
    case (WM_PAINT)  !         //处理窗口区域无效时发来的消息
        hret = BeginPaint(hWnd, ps)
        rectret=GetClientRect(hwnd, rect) 
        DrawTextret=DrawText( hret, "Hello, 这是我自己的窗口!"//char(0), -1, rect,&
        IOR(DT_SINGLELINE, IOR( DT_CENTER , DT_VCENTER ))) 
        hret = EndPaint(hWnd, ps)
        if(hret==1) print *,'窗体重绘'
        return 

    end select

    ! Not ours to handle  
    WindowProc = DefWindowProc( hWnd, uMsg, wParam, lParam )

    end function WindowProc
   
    
    integer(LONG) function Test_ThreadProc1(lpParam) 
    !DEC$ ATTRIBUTES STDCALL:: Test_ThreadProc1
    integer,intent(in)::lpParam
    do while(.true.)
    print *,'this is Thread 1'
    call sleep(100)
    end do
    Test_ThreadProc1=10000
    end function Test_ThreadProc1
    
    integer(LONG) function Test_ThreadProc2(lpParam) 
    !DEC$ ATTRIBUTES STDCALL:: Test_ThreadProc2
    integer,intent(in)::lpParam
    do while(.true.)
    print *,'this is Thread 2'
    call sleep(100)
    end do
    Test_ThreadProc2=10000
    end function Test_ThreadProc2
    
    
    end module Windows_MDL


    function WinMain (hCurrentInst, hPreviousInst, lpszCmdLine, nCmdShow)
    !DEC$ ATTRIBUTES STDCALL,DECORATE,ALIAS:"WinMain" :: WinMain
    use Windows_MDL
    implicit none

    integer(SINT) :: WinMain
    integer(HANDLE), intent(IN) :: hCurrentInst, hPreviousInst
    integer(LPCSTR), intent(IN) :: lpszCmdLine
    integer(SINT), intent(IN) :: nCmdShow

    integer(HANDLE) :: hWnd ! Window
    integer(HANDLE) :: hInstance
    integer(SINT) :: ret,retclass
    integer(BOOL) :: bret,AllocConsole_ret
    type(T_MSG) :: msg      ! Message
    type(T_WNDCLASSEX)::wndclass
    character(len=*), parameter :: szClassName = "Windows"C
    
    
    
    AllocConsole_ret=AllocConsole()
    
    if(AllocConsole_ret/=0) print *,'创建控制台成功'
    hThread1=CreateThread(NULL,0,loc(Test_ThreadProc1),NULL,0,NULL)
    hThread2=CreateThread(NULL,0,loc(Test_ThreadProc2),NULL,0,NULL)
    
    
    
    hInstance = GetModuleHandle(NULL)
    wndclass.cbSize = sizeof(wndclass)
    wndclass.style = CS_OWNDC
    wndclass.lpfnWndProc = loc(WindowProc)
    wndclass.cbClsExtra = 0
    wndclass.cbWndExtra = 0
    wndclass.hInstance = hInstance
    wndclass.hIcon = LoadIcon(0_HANDLE, int(IDI_WINLOGO,LPVOID))
    wndclass.hCursor = LoadCursor(0_HANDLE, int(IDC_ARROW,LPVOID))
    wndclass.hbrBackground = (COLOR_WINDOW + 1) !NULL
    wndclass.lpszMenuName = NULL
    wndclass.lpszClassName = loc(szClassName)
    wndclass.hIconSm = NULL

    retclass = RegisterClassEx(wndclass)
    
    if(retclass/=0) print *,'RegisterClass成功'

    WinMain = 0
    hWnd = CreateWindowEx(0,&
    szClassName, &
    'hello' // CHAR(0), &
    WS_OVERLAPPEDWINDOW, 200,0,400,400,&
    NULL,&
    NULL,&
    hInstance,&
    null&
    )
    if (hwnd == NULL) return

    bret = ShowWindow(hWND, SW_SHOW)
    bret = UpdateWindow(hWnd)

    mainloop: do while (Isrun)
        
        if (PeekMessage(msg, hWnd, 0, 0, PM_REMOVE) /= 0) then
            if (msg%message == WM_QUIT) exit mainloop
            bret = TranslateMessage(msg)
            bret = DispatchMessage(msg)
        end if
        call sleep(10) ! sleep for 10ms
    end do mainloop

    return

    end function WinMain




QQ截图20161105160501.jpg (51.56 KB, 下载次数: 213)

QQ截图20161105160501.jpg

2022

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1598 元
贡献
689 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

板凳
发表于 2016-11-5 16:12:50 | 只看该作者
用这种原生的 Win API 就是很麻烦。简简单单的一个窗口,动辄百十行代码。

还是出门左转找 C#,QT,Delphi 这些吧
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-11-23 11:29

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表