Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лабы / 2 / lab.03.by mice / claw / win_errors

.h
Скачиваний:
10
Добавлен:
17.04.2013
Размер:
1.93 Кб
Скачать
#ifndef WIN_ERRORS_INCLUDED
#define WIN_ERRORS_INCLUDED

#include <afx.h>
#include <afxwin.h>
#include <cstringt.h>
#include <time.h>


static int get_os_error_str(CString &str, const int ercode) {
	char *os_msg;
	int rev = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|
				  FORMAT_MESSAGE_ALLOCATE_BUFFER|
				  FORMAT_MESSAGE_ARGUMENT_ARRAY,
				  NULL, ercode, 0, (LPSTR)&os_msg, 0, NULL);
	if (0 == rev) return -1;
	str.Format("%s", os_msg);
	LocalFree(os_msg);

	return 0;
}


static int show_error_msg(const char *const str, int ercode = 0, CWnd *wnd = NULL) {
	if (0 == ercode) ercode = GetLastError();
	SetLastError(0);
	CString t;
	if (0 != get_os_error_str(t, ercode)) t = "[unknown error or no error at all]";

	CString msg_str;
	msg_str.Format("Error: %s\n\nWith message: %s", str, t);

	HWND hwnd;
	CString er_cap;
	if (NULL != wnd) {
		hwnd = wnd->GetSafeHwnd();
		wnd->GetWindowText(t);
		er_cap.Format("Error [%s]", t);
	} else {
		hwnd = NULL;
		er_cap = "Claw error";
	}

	MessageBox(hwnd, msg_str, er_cap, MB_OK|MB_APPLMODAL|MB_ICONERROR);

	return 0;
}


static int cl_enable_window(const HWND hwnd, BOOL is_enable) {
	if (FALSE == is_enable) {
		PostMessage(hwnd, WM_CANCELMODE, 0, 0);
	}
	PostMessage(hwnd, WM_ENABLE, is_enable, 0);

	return 0;
}


static int size_to_str(const unsigned int sz, CString &str) {
	if (sz < 1024) {
		str.Format("%u b", sz);
	} else if (sz < 1024*1024) {
		str.Format("%.3f kb", (float)sz/(1024));
	} else if (sz < 1024*1024*1024) {
		str.Format("%.3f mb", (float)sz/(1024*1024));
	} else {
		str.Format("%.3f gb", (float)sz/(1024*1024*1024));
	}

	return 0;
}


static void clock_to_str(const clock_t t, CString &str) {
	int s = (int)(t / CLOCKS_PER_SEC);
	int secs = s % 60; s /= 60;
	int mins = s % 60; s /= 60;
	int hors = s % 60;
	str.Format("%02i:%02i:%02i", hors, mins, secs);
}



#endif
Соседние файлы в папке claw