imgview.bas 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. '-----------------------------------------------------------------------------
  2. ' Minimal Image viewer using DevIL
  3. ' DevIL Sample program using Powerbasic.
  4. '
  5. ' by Peter Scheutz, Scheutz & Clementsen Design
  6. ' Web: http://www.scheutz.dk
  7. ' e-mail: sourcecode@scheutz.dk
  8. '
  9. ' Last modified: 06/22/2002
  10. ' Based on DevIL Ver. 1.6.1 PB incs
  11. ' Filename: imgview.bas
  12. '
  13. ' Released under the GNU Lesser General Public License:
  14. ' http://www.gnu.org/copyleft/lesser.html
  15. '
  16. ' For information on use and download of DevIL goto: http://openil.sourceforge.net/
  17. ' (Get docs and "End User Package")
  18. '
  19. ' Report errors in the Powerbasic includes to e-mail above.
  20. '
  21. ' For general help on Powerbasic and DevIL, try the forums at:
  22. ' http://www.powerbasic.com
  23. '-----------------------------------------------------------------------------
  24. #Register None
  25. #Compile Exe "imgview.exe"
  26. Option Explicit
  27. #Include "win32api.inc"
  28. #Include "il.inc"
  29. #Include "ilu.inc"
  30. #Include "ilut.inc"
  31. Global hDCBmp As Long
  32. Global pic As BITMAP
  33. CallBack Function mainCallback()
  34. Local ps As PAINTSTRUCT
  35. Local hDC As Long
  36. Select Case CbMsg
  37. Case %WM_PAINT
  38. hDC = BeginPaint( CbHndl, ps )
  39. If hDCBmp<>0 Then
  40. BitBlt hDC, 0, 0, pic.bmWidth, pic.bmHeight , hDCBmp, 0, 0, %SRCCOPY
  41. End If
  42. Call EndPaint( CbHndl, ps )
  43. End Select
  44. End Function
  45. Sub SetWindowClientSize (ByVal hWnd As Long,ByVal PixX As Long,ByVal PixY As Long)
  46. ' set window by client size in pixels
  47. ' centers window and clips to screen
  48. Local cRect As RECT
  49. Local diffX As Long
  50. Local diffY As Long
  51. GetWindowRect hWnd,cRect
  52. diffX=cRect.nRight-cRect.nLeft
  53. diffY=cRect.nBottom-cRect.nTop
  54. GetClientRect hWnd,cRect
  55. diffX=diffX-(cRect.nRight-cRect.nLeft)
  56. diffY=diffY-(cRect.nBottom-cRect.nTop)
  57. PixX=PixX+diffX
  58. PixY=PixY+diffY
  59. If PixX> GetSystemMetrics(%SM_CXSCREEN) Then PixX = GetSystemMetrics(%SM_CXSCREEN)
  60. If PixY> GetSystemMetrics(%SM_CYSCREEN) Then PixY = GetSystemMetrics(%SM_CYSCREEN)
  61. SetWindowPos hWnd, 0, (GetSystemMetrics(%SM_CXSCREEN) - PixX) / 2, _
  62. (GetSystemMetrics(%SM_CYSCREEN) - PixY) / 2, PixX, PixY, 0
  63. End Sub
  64. Function PbMain() As Long
  65. Local ILErr As Dword
  66. Local hBmp As Long
  67. Local hWnd As Long
  68. Local hDC As Long
  69. Local hBmpOld As Long
  70. Local errString As Asciiz*1024
  71. If Len(Command$)=0 Then
  72. MsgBox "Usage: imgview.exe filename"
  73. Exit Function
  74. End If
  75. ilInit
  76. ilEnable %IL_FORMAT_SET
  77. ilEnable %IL_ORIGIN_SET
  78. ilEnable %IL_TYPE_SET
  79. ilFormatFunc %IL_RGB
  80. ilOriginFunc %IL_ORIGIN_LOWER_LEFT
  81. ilTypeFunc %IL_UNSIGNED_BYTE
  82. ilEnable %IL_CONV_PAL
  83. Do
  84. ILErr = ilGetError()
  85. If ILErr <>0 Then
  86. errString = iluErrorString(ILErr)
  87. MsgBox "Error in Set or enable:" & $CRLF & errString
  88. End If
  89. Loop While (ILErr <> 0)
  90. Dialog New 0, "DevIL Mini imageviewer in PB " & Command$, , , 100, 100 , %WS_SYSMENU Or %WS_CAPTION To hWnd
  91. hDC = getDC (hWnd)
  92. If hDC<>0 Then
  93. hBmp = ilutWinLoadImage (Command$,getDC (hWnd) )
  94. ILErr=ilGetError()
  95. If ILErr<>0 Then
  96. errString = iluErrorString(ILErr)
  97. MsgBox "Error in ilutWinLoadImage:" & $CRLF & errString
  98. Exit Function
  99. Else
  100. GetObject hBmp, SizeOf( pic ), pic
  101. SetWindowClientSize hWnd ,pic.bmWidth, pic.bmHeight
  102. hDCBmp = CreateCompatibleDC( hDC )
  103. hBmpOld = SelectObject( hDCBmp, hBmp )
  104. Dialog Show Modal hWnd Call mainCallback
  105. End If
  106. End If
  107. If hBmp < > 0 Then
  108. SelectObject hDCBmp, hBmpOld
  109. DeleteObject hBmp
  110. hBmp = 0
  111. End If
  112. End Function