#include "stdafx.h" #include <windows.h> #include <windowsx.h> #include "resource.h" #include "MainDlg.h" BOOL isCancel=FALSE; HWND hwnd1; BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog); HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand); HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose); } return FALSE; } BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam) { hwnd1=hwnd; return TRUE; } DWORD WINAPI ThreadFun(LPVOID Lpparam)//要运行的耗时操作,如果要用 Lpparam 需要显式转换 { SetDlgItemText(hwnd1,IDC_EDIT1,"正在连接服务器…"); Sleep(4000); if(isCancel==TRUE) { SetDlgItemText(hwnd1,IDC_EDIT1,"登陆已取消!"); return -1; } SetDlgItemText(hwnd1,IDC_EDIT1,"正在验证密码…"); Sleep(4000); if(isCancel==TRUE) { SetDlgItemText(hwnd1,IDC_EDIT1,"登陆已取消!"); return -1; } SetDlgItemText(hwnd1,IDC_EDIT1,"验证通过!"); return 0; } void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) { switch(id) { case IDC_OK: { isCancel=FALSE; CreateThread(NULL,0,ThreadFun,0,0,0);//新的线程运行函数,第4个参数可以给函数传参数值 } break; case IDC_btnCancel: { isCancel=TRUE; } break;; default: break; } } void Main_OnClose(HWND hwnd) { EndDialog(hwnd, 0); }
时间: 2024-10-25 07:04:07