Haskell Win32问题:尽管程序发送WM_QUIT消息,但应用程序仍未退出

时间:2016-12-01 08:43:11

标签: haskell winapi

我在Windows 8上使用GHC 8.0进行编译。我编写了一个简单的Win32程序来显示窗口,但是当我关闭窗口时,程序不会退出。代码如下。有人可以帮忙吗?

module Main where

import System.Win32.DLL
import Graphics.Win32
import Data.Bits  
import Control.Monad
import Control.Exception
import System.Exit
import Graphics.Win32.Window.PostMessage

main = do 
  hInstance <- getModuleHandle Nothing
  let classname = mkClassName "H3WindowClass"
  let
    wndclass = (
      cS_OWNDC .|. cS_HREDRAW .|. cS_VREDRAW,
      hInstance,
      Nothing,
      Nothing,
      Nothing,
      Nothing,
      classname)
  registerClass wndclass



  let opts = wS_OVERLAPPEDWINDOW .|. wS_VISIBLE
  hwnd <- createWindowEx 0 classname "H3" opts Nothing Nothing Nothing Nothing Nothing Nothing hInstance window   

  allocaMessage $ \msg -> mwhilst (getMessage msg (Just hwnd)) $ do
    translateMessage msg
    dispatchMessage msg


mwhilst :: Monad m => m Bool -> m a -> m ()
mwhilst mb act = do
  b <- mb
  if b then act >> mwhilst mb act else return ()


window :: WindowClosure
window hwnd msg wparam lparam
  | msg == wM_CLOSE = do
      postQuitMessage 0
      return 0
  | otherwise = defWindowProc (Just hwnd) msg wparam lparam

1 个答案:

答案 0 :(得分:1)

WM_CLOSE的处理程序中,您应该调用DestroyWindow来销毁窗口。您还需要通过调用WM_DESTROY来处理随后的PostQuitMessage消息。

window :: WindowClosure
window hwnd msg wparam lparam
  | msg == wM_CLOSE = do
      destroyWindow hwnd 
      return 0
  | msg == wM_DESTROY = do
      postQuitMessage 0
      return 0
  | otherwise = defWindowProc (Just hwnd) msg wparam lparam

MSDN涵盖此模式,例如:https://msdn.microsoft.com/en-gb/library/windows/desktop/ff381396.aspx