//////////////////////////////////////////
// VirWinProc.h
// Copyright (c) 06/23/2002 by BCCSkelton
//////////////////////////////////////////
//☆元々は空であった関数に、以下の様に処理を記述します

/////////////////////////////////
//主ウィンドウCMyWndの関数の定義
//ウィンドウメッセージ関数
/////////////////////////////////
bool CMyWnd::OnCreate(WPARAM wParam, LPARAM lParam) {

	//コモンコントロールの初期化
	InitCommonControls();

	//ツールバー登録−Init(hWnd, hIinstance, ID, (以下省略可）Style)
	TBar.Init(m_hWnd, m_hInstance, TOOLBAR);
	//ツールバーボタン用ビットマップ追加
	TBar.AddBmp(m_hInstance, "IDI_CUSTOM", 10);
	//ツールバーボタン追加
	//カスタムビットマップを使う等数を増やす場合はマニュアルで修正します
	TBBUTTON tbb[10];
	ZeroMemory(tbb, sizeof(tbb));
	tbb[0].iBitmap = TBar.m_id;
	tbb[0].fsState = TBSTATE_ENABLED;
	tbb[0].fsStyle = TBSTYLE_BUTTON;
	tbb[0].idCommand = IDM_EXIT;
	tbb[1].fsStyle = TBSTYLE_SEP;	//セパレーター
	tbb[2].iBitmap = TBar.m_id + 1;
	tbb[2].fsState = TBSTATE_ENABLED;
	tbb[2].fsStyle = TBSTYLE_BUTTON;
	tbb[2].idCommand = IDM_CLEAR;
	tbb[3].iBitmap = TBar.m_id + 2;
	tbb[3].fsState = TBSTATE_ENABLED;
	tbb[3].fsStyle = TBSTYLE_BUTTON;
	tbb[3].idCommand = IDM_DOT;
	tbb[4].iBitmap = TBar.m_id + 3;
	tbb[4].fsState = TBSTATE_ENABLED | TBSTATE_PRESSED;
	tbb[4].fsStyle = TBSTYLE_BUTTON;
	tbb[4].idCommand = IDM_LINE;
	tbb[5].iBitmap = TBar.m_id + 4;
	tbb[5].fsState = TBSTATE_ENABLED;
	tbb[5].fsStyle = TBSTYLE_BUTTON;
	tbb[5].idCommand = IDM_BOX;
	tbb[6].iBitmap = TBar.m_id + 5;
	tbb[6].fsState = TBSTATE_ENABLED;
	tbb[6].fsStyle = TBSTYLE_BUTTON;
	tbb[6].idCommand = IDM_CIRCLE;
	tbb[7].iBitmap = TBar.m_id + 6;
	tbb[7].fsState = TBSTATE_ENABLED;
	tbb[7].fsStyle = TBSTYLE_BUTTON;
	tbb[7].idCommand = IDM_ELLIPSE;
	tbb[8].iBitmap = TBar.m_id + 7;
	tbb[8].fsState = TBSTATE_ENABLED;
	tbb[8].fsStyle = TBSTYLE_BUTTON;
	tbb[8].idCommand = IDM_ARC;
	tbb[9].iBitmap = TBar.m_id + 8;
	tbb[9].fsState = TBSTATE_ENABLED;
	tbb[9].fsStyle = TBSTYLE_BUTTON;
	tbb[9].idCommand = IDM_CHORD;
	TBar.AddButtons(10, tbb);

	//ステータスバー登録−Init(hWnd, hIinstance, ID, (以下省略可）Style)
	SBar.Init(m_hWnd, m_hInstance, STATUSBAR);
	//ステータスバー区画設定
	int sec[2] = {200, 400};
	SBar.SetSection(2, sec);
	//ステータスバー文字列設定
	SBar.SetText(0, "Virtual Window : Selected Item --->");
	SBar.SetText(1, "Virtual");

	//仮想ウィンドウの初期化
	cvs.SetCanvas(m_hWnd);

	//直線を初期図形にする
	SendMessage(SBar.GetHandle(), SB_SETTEXT, 1, (LPARAM)SelStr);

	//外部ビットマップを読みこむ
	Back.LoadBMP(m_hWnd, "IDI_BACK");
	Front.LoadBMP(m_hWnd, "IDI_FRONT");

	return TRUE;
}

bool CMyWnd::OnLButtonDown(WPARAM wParam, LPARAM lParam) {

	SetCursor(LoadCursor(NULL, IDC_CROSS));
	if(!Again) {
		xPos[0] = LOWORD(lParam);
		yPos[0] = HIWORD(lParam);
		Again = TRUE;
	}
	else {
		xPos[2] = LOWORD(lParam);
		yPos[2] = HIWORD(lParam);
		Again = FALSE;
	}
	cvs.DragRect(LOWORD(lParam), HIWORD(lParam), TRUE);
	return TRUE;
}

bool CMyWnd::OnLButtonUp(WPARAM wParam, LPARAM lParam) {

	switch(Selected) {
		case IDM_DOT:
			Again = FALSE;
			break;
		case IDM_LINE:
			cvs.Line(xPos[0], yPos[0], LOWORD(lParam), HIWORD(lParam));
			Again = FALSE;
			break;
		case IDM_BOX:
			cvs.Box(xPos[0], yPos[0], LOWORD(lParam), HIWORD(lParam), BrType);
			Again = FALSE;
			break;
		case IDM_CIRCLE:
			cvs.Circle(xPos[0], yPos[0], (int)sqrt(
				(double)((xPos[0] - LOWORD(lParam)) * (xPos[0] - LOWORD(lParam)) +
						(yPos[0] - HIWORD(lParam)) * (yPos[0] - HIWORD(lParam)))),
						BrType
			);
			Again = FALSE;
			break;
		case IDM_ELLIPSE:
			cvs.Ellipsed(xPos[0], yPos[0], LOWORD(lParam), HIWORD(lParam), BrType);
			Again = FALSE;
			break;
		case IDM_ARC:
			if(Again) {
				xPos[1] = LOWORD(lParam);
				yPos[1] = HIWORD(lParam);
				break;
			}
			else
				cvs.Arced(xPos[0], yPos[0],
						xPos[1], yPos[1],
						xPos[2], yPos[2],
						LOWORD(lParam), HIWORD(lParam));
			break;
		case IDM_CHORD:
			if(Again) {
				xPos[1] = LOWORD(lParam);
				yPos[1] = HIWORD(lParam);
				break;
			}
			else
				cvs.Chorded(xPos[0], yPos[0],
						xPos[1], yPos[1],
						xPos[2], yPos[2],
						LOWORD(lParam), HIWORD(lParam), BrType);
			break;
		case IDM_GETBITMAP:
			cvs.GetBMP(xPos[0], yPos[0], LOWORD(lParam) - xPos[0],
					HIWORD(lParam) - yPos[0]);
			//保存用に仮想ウィンドウCDCクラスdcのと同じビットマップを取得
			temp.GetBMP(m_hWnd, cvs.hDC(), xPos[0], yPos[0],
					LOWORD(lParam) - xPos[0],
					HIWORD(lParam) - yPos[0]);
			Again = FALSE;
			break;
		case IDM_PSET:
			cvs.PutBMP(xPos[0], yPos[0], 0);
			Again = FALSE;
			break;
		case IDM_AND:
			cvs.PutBMP(xPos[0], yPos[0], 1);
			Again = FALSE;
			break;
		case IDM_OR:
			cvs.PutBMP(xPos[0], yPos[0], 2);
			Again = FALSE;
			break;
		case IDM_XOR:
			cvs.PutBMP(xPos[0], yPos[0], 3);
			Again = FALSE;
			break;
		case IDM_FIT:
			cvs.FitBMP(xPos[0], yPos[0], LOWORD(lParam) - xPos[0],
					HIWORD(lParam) - yPos[0], 0);
			Again = FALSE;
			break;
		case IDM_OUTSIDEBMP:
			cvs.PutBMP(Back.Handle(), xPos[0], yPos[0], 1);
			cvs.PutBMP(Front.Handle(), xPos[0], yPos[0], 2);
			Again = FALSE;
			break;
	}
	cvs.DragRect(LOWORD(lParam), HIWORD(lParam), FALSE);
	return TRUE;
}

bool CMyWnd::OnRButtonDown(WPARAM wParam, LPARAM lParam) {

	if(!temp.m_hBMP)			//取得画像が無ければ何もしない
		return FALSE;
	temp.SaveBMP("temp.bmp");	//取得画像をtemp.bmpで保存
	cvs.Font(cmndlg.GetFont(m_hWnd));
	cvs.PrintText(LOWORD(lParam), HIWORD(lParam), "ビットマップをセーブしました");
	return TRUE;
}

bool CMyWnd::OnMouseMove(WPARAM wParam, LPARAM lParam) {

	if(wParam & MK_LBUTTON) {
		SetCursor(LoadCursor(NULL, IDC_CROSS));
		if(Selected == IDM_DOT)
			cvs.Dot(LOWORD(lParam), HIWORD(lParam));
		else
			cvs.DragRect(LOWORD(lParam), HIWORD(lParam), TRUE);
	}
	else
		SetCursor(LoadCursor(NULL, IDC_ARROW));
	return TRUE;
}

bool CMyWnd::OnSize(WPARAM wParam, LPARAM lParam) {

	TBar.AutoSize();
	SBar.AutoSize();
	return TRUE;
}

bool CMyWnd::OnPaint(WPARAM wParam, LPARAM lParam) {

	PAINTSTRUCT paint;
	cvs.OnPaint(BeginPaint(m_hWnd, &paint));
	EndPaint(m_hWnd, &paint);
	return TRUE;
}

bool CMyWnd::OnClose(WPARAM wParam, LPARAM lParam) {

	if(MessageBox(m_hWnd, "終了しますか？", "終了確認",
					MB_YESNO | MB_ICONQUESTION) == IDYES) {
		DestroyWindow(m_hWnd);
		return TRUE;
	}
	return FALSE;
}

/////////////////////////////////
//主ウィンドウCMyWndの関数の定義
//メニュー項目、コントロール関数
/////////////////////////////////
bool CMyWnd::OnExit() {

	PostMessage(m_hWnd, WM_CLOSE, 0, 0);
	return TRUE;
}

bool CMyWnd::OnClear() {

	cvs.Clear();
	return TRUE;
}

bool CMyWnd::OnDot(WPARAM wParam) {

	strcpy(SelStr, "Dot");
	CheckChange(m_hWnd, SelStr, Selected, LOWORD(wParam));
	Selected = IDM_DOT;
	return TRUE;
}

bool CMyWnd::OnLine(WPARAM wParam) {

	strcpy(SelStr, "Line");
	CheckChange(m_hWnd, SelStr, Selected, LOWORD(wParam));
	Selected = IDM_LINE;
	return TRUE;
}

bool CMyWnd::OnBox(WPARAM wParam) {

	strcpy(SelStr, "Box");
	CheckChange(m_hWnd, SelStr, Selected, LOWORD(wParam));
	Selected = IDM_BOX;
	return TRUE;
}

bool CMyWnd::OnCircle(WPARAM wParam) {

	strcpy(SelStr, "Circle");
	CheckChange(m_hWnd, SelStr, Selected, LOWORD(wParam));
	Selected = IDM_CIRCLE;
	return TRUE;
}

bool CMyWnd::OnEllipse(WPARAM wParam) {

	strcpy(SelStr, "Ellipse");
	CheckChange(m_hWnd, SelStr, Selected, LOWORD(wParam));
	Selected = IDM_ELLIPSE;
	return TRUE;
}

bool CMyWnd::OnArc(WPARAM wParam) {

	strcpy(SelStr, "Arc");
	CheckChange(m_hWnd, SelStr, Selected, LOWORD(wParam));
	Selected = IDM_ARC;
	return TRUE;
}

bool CMyWnd::OnChord(WPARAM wParam) {

	strcpy(SelStr, "Chord");
	CheckChange(m_hWnd, SelStr, Selected, LOWORD(wParam));
	Selected = IDM_CHORD;
	return TRUE;
}

bool CMyWnd::OnGetbitmap(WPARAM wParam) {

	strcpy(SelStr, "Get Bitmap");
	CheckChange(m_hWnd, SelStr, Selected, LOWORD(wParam));
	Selected = IDM_GETBITMAP;
	MessageBox(m_hWnd, "画像を取得したら、右マウスクリックで"
					"temp.bmpというファイルを保存できます。",
					"メッセージ", MB_OK | MB_ICONINFORMATION);
	return TRUE;
}

bool CMyWnd::OnPset(WPARAM wParam) {

	strcpy(SelStr, "Put Bitmap - Pset");
	CheckChange(m_hWnd, SelStr, Selected, LOWORD(wParam));
	Selected = IDM_PSET;
	return TRUE;
}

bool CMyWnd::OnAnd(WPARAM wParam) {

	strcpy(SelStr, "Put Bitmap - And");
	CheckChange(m_hWnd, SelStr, Selected, LOWORD(wParam));
	Selected = IDM_AND;
	return TRUE;
}

bool CMyWnd::OnOr(WPARAM wParam) {

	strcpy(SelStr, "Put Bitmap - Or");
	CheckChange(m_hWnd, SelStr, Selected, LOWORD(wParam));
	Selected = IDM_OR;
	return TRUE;
}

bool CMyWnd::OnXor(WPARAM wParam) {

	strcpy(SelStr, "Put Bitmap - Xor");
	CheckChange(m_hWnd, SelStr, Selected, LOWORD(wParam));
	Selected = IDM_XOR;
	return TRUE;
}

bool CMyWnd::OnFit(WPARAM wParam) {

	strcpy(SelStr, "Fit Bitmap - Pset");
	CheckChange(m_hWnd, SelStr, Selected, LOWORD(wParam));
	Selected = IDM_FIT;
	return TRUE;
}

bool CMyWnd::OnOutsidebmp(WPARAM wParam) {

	strcpy(SelStr, "Outside Bitmap - Pset");
	CheckChange(m_hWnd, SelStr, Selected, LOWORD(wParam));
	Selected = IDM_OUTSIDEBMP;
	return TRUE;
}

bool CMyWnd::OnBlack() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	Col = 0;
	cvs.Color(Col);
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnDblue() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	Col = 1;
	cvs.Color(Col);
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnDred() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	Col = 2;
	cvs.Color(Col);
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnDmazenda() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	Col = 3;
	cvs.Color(Col);
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnDgreen() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	Col = 4;
	cvs.Color(Col);
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnDcian() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	Col = 5;
	cvs.Color(Col);
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnDyellow() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	Col = 6;
	cvs.Color(Col);
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnDwhite() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	Col = 7;
	cvs.Color(Col);
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnGray() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	Col = 8;
	cvs.Color(Col);
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnBlue() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	Col = 9;
	cvs.Color(Col);
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnRed() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	Col = 10;
	cvs.Color(Col);
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnMazenda() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	Col = 11;
	cvs.Color(Col);
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnGreen() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	Col = 12;
	cvs.Color(Col);
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnCian() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	Col = 13;
	cvs.Color(Col);
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnYellow() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	Col = 14;
	cvs.Color(Col);
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnWhite() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	Col = 15;
	cvs.Color(Col);
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnSelec() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 2), Col,
				MF_UNCHECKED | MF_BYPOSITION);
	cvs.Color(-1);
	return TRUE;
}

bool CMyWnd::OnTransparent() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 3), BrType,
				MF_UNCHECKED | MF_BYPOSITION);
	BrType = 0;
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 3), BrType,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnSolid() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 3), BrType,
				MF_UNCHECKED | MF_BYPOSITION);
	BrType = 1;
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 3), BrType,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnHatch1() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 3), BrType,
				MF_UNCHECKED | MF_BYPOSITION);
	BrType = 2;
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 3), BrType,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnHatch2() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 3), BrType,
				MF_UNCHECKED | MF_BYPOSITION);
	BrType = 3;
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 3), BrType,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnHatch3() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 3), BrType,
				MF_UNCHECKED | MF_BYPOSITION);
	BrType = 4;
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 3), BrType,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnHatch4() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 3), BrType,
				MF_UNCHECKED | MF_BYPOSITION);
	BrType = 5;
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 3), BrType,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnHatch5() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 3), BrType,
				MF_UNCHECKED | MF_BYPOSITION);
	BrType = 6;
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 3), BrType,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnHatch6() {

	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 3), BrType,
				MF_UNCHECKED | MF_BYPOSITION);
	BrType = 7;
	CheckMenuItem(GetSubMenu(GetMenu(m_hWnd), 3), BrType,
				MF_CHECKED | MF_BYPOSITION);
	return TRUE;
}

bool CMyWnd::OnVersion() {

	MessageBox(m_hWnd, "仮想ウィンドウサンプルプログラム\n"
				"By BCCCSkelton", "ヘルプ", MB_OK | MB_ICONINFORMATION);
	return TRUE;
}
