读书人

API 线程不能关闭解决办法

发布时间: 2012-03-25 20:55:16 作者: rapoo

API 线程不能关闭
不好意思:
初次学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:
ATOMMyRegisterClass(HINSTANCE hInstance);
BOOL DialogRegisterClass(HINSTANCE hInstance);
BOOLInitInstance(HINSTANCE, int);
LRESULT CALLBACKWndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACKAbout(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 ; //
}

[解决办法]
你只是销毁窗口 并没有关闭消息循环的操作
------解决方案--------------------


case WM_DESTROY:
PostQuitMessage(0);
break;

读书人网 >C++

热点推荐