title: What are the ways of creating a window in Haskell? level zero
body: <pre><code>    {- 2012-05-16
  
      ghc --make -optl-mwindows fileName.hs
  
  option -mwindows is passed to the linker!
  attempting to read from stdin with -mwindows may cause a runtime error
  any output on stdout/stderr will be lost.
  ghc links console app with stdout/stderr as default
  -}
  
  
  
  --import Graphics.Win32
  import Graphics.Win32 hiding (messageBox, c_MessageBox) -- bugfix
  import System.Win32.DLL
  import Control.Exception (bracket)
  import Foreign
  import System.Exit
  
  
  
  
  -- bugfix whole msg box
  messageBox :: HWND -&gt; String -&gt; String -&gt; MBStyle -&gt; IO MBStatus
  messageBox wnd text caption style =
    withTString text $ \ c_text -&gt;
    withTString caption $ \ c_caption -&gt;
    failIfZero "MessageBox" $ c_MessageBox wnd c_text c_caption style
  foreign import stdcall safe "windows.h MessageBoxW"
    c_MessageBox :: HWND -&gt; LPCTSTR -&gt; LPCTSTR -&gt; MBStyle -&gt; IO MBStatus
  
  
  main :: IO ()
  main = do
      mainInstance &lt;- getModuleHandle Nothing
      hwnd &lt;- createWindow_ 200 200 wndProc mainInstance
      createButton_ hwnd mainInstance
      messagePump hwnd
  
  wndProc :: HWND -&gt; WindowMessage -&gt; WPARAM -&gt; LPARAM -&gt; IO LRESULT
  wndProc hwnd wmsg wParam lParam
      | wmsg == wM_DESTROY = do
          sendMessage hwnd wM_QUIT 1 0
          return 0
      | wmsg == wM_COMMAND &amp;&amp; wParam == 3 = do
          messageBox nullPtr "You pressed me." "Haskell msg" 0
          return 0
      | otherwise = defWindowProc (Just hwnd) wmsg wParam lParam
  
  
  createWindow_ :: Int -&gt; Int -&gt; WindowClosure -&gt; HINSTANCE -&gt; IO HWND
  createWindow_ width height wndProc mainInstance = do
      let winClass = mkClassName "Window Empty"
      icon &lt;- loadIcon Nothing iDI_APPLICATION
      cursor &lt;- loadCursor Nothing iDC_ARROW
      bgBrush &lt;- createSolidBrush (rgb 255 0 0)
      registerClass (cS_VREDRAW + cS_HREDRAW, mainInstance, Just icon, Just cursor, Just bgBrush, Nothing, winClass)
      w &lt;- createWindow winClass "Window Empty" wS_OVERLAPPEDWINDOW Nothing Nothing (Just width) (Just height) Nothing Nothing mainInstance wndProc
      showWindow w sW_SHOWNORMAL
      updateWindow w
      return w
  
  
  createButton_ :: HWND -&gt; HINSTANCE -&gt; IO ()
  createButton_ hwnd mainInstance = do
      hBtn &lt;- createButton "Button test" wS_EX_CLIENTEDGE (bS_PUSHBUTTON + wS_VISIBLE + wS_CHILD) (Just 50) (Just 80) (Just 80) (Just 20) (Just hwnd) (Just (castUINTToPtr 3)) mainInstance
      return ()
  
  
  messagePump :: HWND -&gt; IO ()
  messagePump hwnd = allocaMessage $ \ msg -&gt;
      let pump = do
          getMessage msg (Just hwnd) `catch` \ _ -&gt; exitWith ExitSuccess
          translateMessage msg
          dispatchMessage msg
          pump
      in pump
  </code></pre>
  
  <p>Original link is <a href="https://stackoverflow.com/questions/7304317/message-box-error-foreign-import-unsafe">here</a></p>
  
  <p>Usage: copy/paste code, save it in a file, compile with <code>ghc --make -optl-mwindows fileName.hs</code> and it will create nice little window. It's basic C/C++ like <a href="http://msdn.microsoft.com/en-us/library/bb384843.aspx" rel="nofollow noreferrer">here</a>.</p>
  
  <p>This and two more examples below are <strong>ONLY raw</strong> createWindow code I could find written in Haskell :(</p>
  
  <p>My rethoric questions:</p>
  
  <ul>
  <li><p>I understand that C++ process quite well. You create come functions, winProc will call it if some win_msg is true...<br>
  But, it's not the only way. Soon enough MS put that in mfc classes. And we have EventListeners that do basically the same thing. Instead of directly testing win_msg you create/addEventListener, pass desired function and it works.<br>
  But grouping of code is nicer and easier for maintenance and it's more OO like.</p></li>
  <li><p>What are Haskell's ways for Haskellising winProc? There probably are ways to mimic addEventListener(evt, my_func).<br>
  How would that code look like? How many different solutions are there? Is it usable?</p></li>
  <li><p>And more important, is there some Haskell like (better) way that I an not aware of?</p></li>
  <li>In what ways you can use that code, improve it a bit and create something like wxWidgets or gtk, but extremely simplified, easy to understand, etc.</li>
  </ul>