Enddialog的一个问题

luocan1986 2009-08-14 11:00:59
调用enddialog后 发现已经成功了 但是对话框的界面还在?这是什么问题
...全文
565 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
路人乙2019 2009-08-14
  • 打赏
  • 举报
回复
最好不要用Enddialog
路人乙2019 2009-08-14
  • 打赏
  • 举报
回复
是SDK方式哦,可以用sendmessage WM_CLOSE.
luocan1986 2009-08-14
  • 打赏
  • 举报
回复
#include "EClock.h"
#include <Windows.h>
#include <CommCtrl.h>


HINSTANCE hInst;
HWND hWnd;
HDC hdc;

//全局的时间。。用于闹钟处理
int hour, minute, second;
//Forword declaration of functions
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
LRESULT DoMeasureItemListWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT DoDrawItemListWnd (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK Time(HWND, UINT, WPARAM, LPARAM);

int iRtDialog;

extern DWORD WINAPI DrawClock(PVOID pARG);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;

if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}

while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}

/*
* FUNCTION: MyRegisterClass()
*
* PURPOSE: Registers the window class.
*/
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;

return RegisterClass(&wc);
}

/*
* FUNCTION: InitInstance(HINSTANCE, int)
*
* PURPOSE: Saves instance handle and creates main window
*
* COMMENTS:
*
* In this function, we save the instance handle in a global variable and
* create and display the main program window.
*/
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
//HWND hWnd;
hInst = hInstance;

hWnd = FindWindow(TEXT("Clock"), TEXT("Clock"));
if (hWnd)
{
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
return 0;
}

if (!MyRegisterClass(hInstance, TEXT("Clock")))
{
return FALSE;
}

hWnd = CreateWindow(TEXT("Clock"), TEXT("Clock"), WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}


ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}


/*
* FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
*
* 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)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
//HDC hdc;
RECT rect;
HBRUSH hBr, hOldBr, hNewBr;
int i;
TCHAR szOut[64];
TCHAR tHalarm[64];
TCHAR tMalarm[64];
TCHAR tSalarm[64];
TCHAR tDisTime[128];
char cAlarm[64];

memset(cAlarm, 0, 64 * sizeof(char));


HWND hCombo,hWndTemp;


//线程定义
HANDLE hThread1;
DWORD dwThread1ID = 0;
int nParameter = 5;

hNewBr = CreateSolidBrush(RGB(0, 0xaa, 0));

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDC_SETBTN1:

DialogBox(hInst, (LPCTSTR)IDD_DIALOG2, hWnd, (DLGPROC)Time);


break;

case IDM_FILE_EXIT:
DestroyWindow(hWnd);
break;

case IDC_SETBTN2:

//Alarm Clock 时间

GetDlgItemText(hWnd,IDC_COMBOX2, tSalarm, 10);

WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK,tSalarm, -1, cAlarm, sizeof(cAlarm), NULL, NULL );
second = atoi(cAlarm);

GetDlgItemText(hWnd,IDC_COMBOX1, tMalarm, 10);

WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK,tMalarm, -1, cAlarm, sizeof(cAlarm), NULL, NULL );
minute = atoi(cAlarm);

GetDlgItemText(hWnd,IDC_COMBOX3, tHalarm, 10);

WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK,tHalarm, -1, cAlarm, sizeof(cAlarm), NULL, NULL );
hour = atoi(cAlarm);

wsprintf(tDisTime, L"Hour:%2s Minute:%2s Second:%2s", tHalarm, tMalarm, tSalarm);

SetDlgItemText(hWnd, IDC_EDIT2, tDisTime);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;

case WM_CREATE:

SetTimer(hWnd, 1, 300, NULL);


//创建各种控件
CreateWindow (TEXT ("EDIT"), TEXT (""),
ES_AUTOHSCROLL | ES_READONLY | BS_NOTIFY | WS_VISIBLE | WS_CHILD,
150,20, 200, 20,
hWnd, (HMENU)IDC_EDIT1, hInst, NULL);


CreateWindow (TEXT ("EDIT"), TEXT (""),
ES_AUTOHSCROLL | ES_READONLY | BS_NOTIFY | WS_VISIBLE | WS_CHILD,
170,370, 200, 20,
hWnd, (HMENU)IDC_EDIT2, hInst, NULL);

CreateWindow (TEXT ("STATIC"), TEXT ("CurTime: "),
BS_PUSHBUTTON | BS_NOTIFY | WS_VISIBLE | WS_CHILD,
95, 20, 50, 20,
hWnd, (HMENU)IDC_SETBTN1, hInst, NULL);


CreateWindow (TEXT ("STATIC"), TEXT ("Alarm Clock: "),
BS_PUSHBUTTON | BS_NOTIFY | WS_VISIBLE | WS_CHILD,
80, 370, 80, 20,
hWnd, (HMENU)IDC_SETBTN2, hInst, NULL);

CreateWindow (TEXT ("COMBOBOX"), TEXT ("COMBOBOX"),
WS_VSCROLL | WS_VISIBLE | WS_CHILD | WS_BORDER,
480, 120, 50, 90,
hWnd, (HMENU)IDC_COMBOX1, hInst, NULL);

CreateWindow (TEXT ("COMBOBOX"), TEXT ("COMBOBOX"),
WS_VSCROLL | WS_VISIBLE | WS_CHILD | WS_BORDER,
560, 120, 50, 90,
hWnd, (HMENU)IDC_COMBOX2, hInst, NULL);

CreateWindow (TEXT ("COMBOBOX"), TEXT ("COMBOBOX"),
WS_VSCROLL | WS_VISIBLE | WS_CHILD | WS_BORDER,
400, 120, 50, 90,
hWnd, (HMENU)IDC_COMBOX3, hInst, NULL);

CreateWindow (TEXT ("BUTTON"), TEXT ("Set Time"),
BS_PUSHBUTTON | BS_NOTIFY | WS_VISIBLE | WS_CHILD,
380, 250, 100, 20,
hWnd, (HMENU)IDC_SETBTN1, hInst, NULL);

CreateWindow (TEXT ("BUTTON"), TEXT ("Set Alarm"),
BS_PUSHBUTTON | BS_NOTIFY | WS_VISIBLE | WS_CHILD,
500, 250, 100, 20,
hWnd, (HMENU)IDC_SETBTN2, hInst, NULL);


//给combobox加选项
for (i = 0; i < 60; i++)
{

wsprintf (szOut, TEXT ("%d"), i);
SendDlgItemMessage (hWnd, IDC_COMBOX1, CB_ADDSTRING, 0,
(LPARAM)szOut);
SendDlgItemMessage (hWnd, IDC_COMBOX2, CB_ADDSTRING, 0,
(LPARAM)szOut);


//24小时
if (i > 24)
{
continue;
}
SendDlgItemMessage (hWnd, IDC_COMBOX3, CB_ADDSTRING, 0,
(LPARAM)szOut);
}
// Set initial selection.
SendDlgItemMessage (hWnd, IDC_COMBOX1, CB_SETCURSEL, 0, 0);
SendDlgItemMessage (hWnd, IDC_COMBOX2, CB_SETCURSEL, 0, 0);
SendDlgItemMessage (hWnd, IDC_COMBOX3, CB_SETCURSEL, 0, 0);

break;

case WM_TIMER:

//Timer时间到,要求重绘
InvalidateRect(hWnd, &rect, FALSE);

break;

case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);

//先画一个背景圆
hBr = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
hOldBr = (HBRUSH) SelectObject(hdc, hNewBr);
Ellipse(hdc, 50, 50, 350, 350);



//画三颗针.估计得创建一个线程来启动该函数,因为如果它是个无限循环的话,那么主消息会得不到处理

DrawClock((PVOID)nParameter);

//hThread1 = CreateThread(NULL, 0, DrawClock, (PVOID)nParameter, 0, &dwThread1ID);
if (hThread1 == NULL)
{
return 0;
}
CloseHandle(hThread1);
EndPaint(hWnd, &ps);
break;

case MYMSG_ALARM:
DialogBox(hInst, (LPCTSTR)IDD_DIALOG1, hWnd, (DLGPROC)Time);
PlaySound(TEXT("MouseClick"), NULL, SND_SYNC);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}


// Mesage handler for the About box.
LRESULT CALLBACK Time(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
RECT rt, rt1;
int DlgWidth, DlgHeight; // dialog width and height in pixel units
int NewPosX, NewPosY;

switch (message)
{
case WM_INITDIALOG:
// trying to center the About dialog
if (GetWindowRect(hWnd, &rt1)) {
GetClientRect(GetParent(hWnd), &rt);
DlgWidth = rt1.right - rt1.left;
DlgHeight = rt1.bottom - rt1.top ;
NewPosX = (rt.right - rt.left - DlgWidth)/2;
NewPosY = (rt.bottom - rt.top - DlgHeight)/2;

// if the About box is larger than the physical screen
if (NewPosX < 0) NewPosX = 0;
if (NewPosY < 0) NewPosY = 0;
SetWindowPos(hWnd, 0, NewPosX, NewPosY,
0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
return TRUE;

case WM_COMMAND:
if ((LOWORD(wParam) == IDC_BUTTON1 ) || (LOWORD(wParam) == IDC_BUTTON2 ) || (LOWORD(wParam) == IDOK) )
{

iRtDialog = EndDialog(hWnd, 2);
return TRUE;
}
break;
}
return FALSE;
}

贴个程序吧。。刚开始写程序。。也不太明白。
Ghost90 2009-08-14
  • 打赏
  • 举报
回复
如果是 对话框程序
调用 EndDialog(2);退出程序是没问题的。

如果调用平台函数 ::EndDialog(m_hWnd,2);程序界面可以消失单程序不能退出。要想退出程序

EndModalLoop(2); //结束模态对话框的消息循环
::EndDialog(m_hWnd,2);

至于对话框界面还存在 不是你修改了OnPaint或 OnEraseBkgnd 函数
ljz888666555 2009-08-14
  • 打赏
  • 举报
回复
等待你主窗口的调用。
luocan1986 2009-08-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ljz888666555 的回复:]
EndDialog不会立即关闭对话框。它设置了一个标记,用以指定在当前消息处理程序返回时就关闭对话框。
[/Quote]

那意思是消息处理函数 有可能没返回?
ljz888666555 2009-08-14
  • 打赏
  • 举报
回复
EndDialog不会立即关闭对话框。它设置了一个标记,用以指定在当前消息处理程序返回时就关闭对话框。

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧