不好意思:
    初次学API,出了个问题,我新建一个Dialog作为主界面,在循环消息处理中以DestroyWindow(hWnd);DestroyWindow(GetParent(hWnd)); 来退出程序,但是每次退出后,我的任务管理器中照样存在线程。现在我把代码贴出来,请高手指点!代码:
// Test002.cpp : Defines the entry point for the application.
//
#include <stdafx.h>
#include "resource.h"
#include "Login.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#define MAX_LOADSTRING 100// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL                DialogRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
LRESULT   CALLBACK  DlgProc(HWND, UINT, WPARAM, LPARAM);int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  // TODO: Place code here.
MSG msg;
HACCEL hAccelTable; // Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TEST002, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
//DialogRegisterClass(hInstance)
HWND hWnd_Main;
//(WNDPROC)WndProc Param ,0L (DLGPROC) (DLGPROC)DlgProc
hWnd_Main = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_LOGIN),NULL,(DLGPROC)DlgProc); ShowWindow(hWnd_Main, nCmdShow);
UpdateWindow(hWnd_Main);
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TEST002); // Main message loop:
while (GetMessage(&msg, NULL, 0, 0)) 
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} return msg.wParam;
}
//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); 
//(WNDPROC)WndProc
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TEST002);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_TEST002;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = NULL; return RegisterClassEx(&wcex);
}//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND - process the application menu
//  WM_PAINT - Paint the main window
//  WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{ switch (message)
{
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
{
DestroyWindow(hWnd);
return TRUE;
}
break;
default:
return DlgProc(hWnd,message,wParam,lParam);
}
return false;
}
LRESULT   CALLBACK   DlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//int wmId, wmEvent;
switch (message)
     {
     case WM_INITDIALOG :
return TRUE ; //
          
     case WM_COMMAND :
switch (LOWORD (wParam))
{
case IDOK :
case IDCANCEL :
DestroyWindow(hWnd); 
DestroyWindow(GetParent(hWnd)); 
EndDialog(hWnd, 0) ; //
break ; //
}
case WM_CLOSE:
DestroyWindow(hWnd); 
DestroyWindow(GetParent(hWnd)); ;
break;   
     }
     return FALSE ; //
}

解决方案 »

  1.   

    DestroyWindow(GetParent(hWnd));   这个什么意思??用EndDialog 可以退出
      

  2.   

    非模态对话框是需要用DestroyWindow退出的,这个没错。
    但是这个DestroyWindow(GetParent(hWnd)); 是销毁对话框的父窗口,这个对话框的父窗口可是桌面,不过GetParent(hWnd)返回是NULL,DestroyWindow(NULL)没什么影响。之所以程序没退出是没有PostQiutMessage,WinMain里面的while (GetMessage(&msg, NULL, 0, 0)) 等不到WM_QUIT消息,一直在那等着,所以程序没办法结束。
    需要在WM_DESTROY消息里面调用一下PostQiutMessage(0);
      

  3.   

    开始用EndDialog(hWnd, 0) ;他退出
    后来在任务管理中还是有进程存在
    我加了DestroyWindow(hWnd); 来销毁句柄
    可是还是不行
    我以为父类句柄存在
    就用DestroyWindow(GetParent(hWnd));   
    所以就把父类句柄删掉
    可还是不行
      

  4.   

    ringphone(临风) :多谢!
    已经搞定了
    本人刚学API,不知可不可以交个朋友!以方便请教!
    skype:qixueqi
    E-mail:[email protected]
    如果可以请留下联系地址。
    谢谢
      

  5.   

    顺便问一下
    一般来说API程序不都要窗体注册吗
    在我这个程序里:MyRegisterClass(hInstance);
    这个注册函数不用好像对程序也没影响
    请问这是为什么?
    另:我这个程序是以dialog为主界面,在有些书上说那就需要两个注册函数,一个是注册窗体,一个是注册dialog的,是这样的吗?原因?
      

  6.   

    1.只有用CreateWindowEx创建窗口时才需注册窗口类.CreateDialog/CreateDialogParam不需要.
    2.在DlgProc的WM_DESTROY: 中PostQuitMessage(0);
    3. 在GetMessage循环里加入:if(IsDialogMessage(hDlg, &msg)) continue;
    4. LRESULT CALLBACK WndProc(...)多余.