您的位置:首页 > 其它

win32 带登录界面的最简单版本计算器

2014-12-19 10:56 295 查看
最简单的加减乘除计算,目的是为了复习win32的用法:

界面:











***步骤:

第一步:建立一个win32工程

第二步:插入两个对话框 登录对话框 和 计算对话框

第三步:编写代码实现即可,主要就是熟悉win32 的几个api函数:

// 1218.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <winuser.h>
#include "resource.h"
#include <string.h>
#include <stdio.h>

void OnCaculate(HWND hwndDlg)
{
	int ret;
	int nLeft = GetDlgItemInt(hwndDlg,IDC_EDIT1,&ret,true);
	int nRight = GetDlgItemInt(hwndDlg,IDC_EDIT2,&ret,true);
	int Result = nLeft + nRight;
	SetDlgItemInt(hwndDlg,IDC_EDIT3,Result,true);

}
void OnCaculate2(HWND hwndDlg)
{
	int ret;
	int nLeft = GetDlgItemInt(hwndDlg,IDC_EDIT4,&ret,true);
	int nRight = GetDlgItemInt(hwndDlg,IDC_EDIT5,&ret,true);
	int Result = nLeft - nRight;
	SetDlgItemInt(hwndDlg,IDC_EDIT6,Result,true);

}

void OnCaculate3(HWND hwndDlg)
{
	int ret;
	int nLeft = GetDlgItemInt(hwndDlg,IDC_EDIT7,&ret,true);
	int nRight = GetDlgItemInt(hwndDlg,IDC_EDIT8,&ret,true);
	int Result = nLeft * nRight;
	SetDlgItemInt(hwndDlg,IDC_EDIT9,Result,true);

}
void OnCaculate4(HWND hwndDlg)
{
	int ret;
	int nLeft = GetDlgItemInt(hwndDlg,IDC_EDIT10,&ret,true);
	int nRight = GetDlgItemInt(hwndDlg,IDC_EDIT11,&ret,true);
	if(nRight == 0)
		MessageBox(hwndDlg,"除数不能为0","提示消息",0);
	else
	{
		int Result = nLeft / nRight;
		SetDlgItemInt(hwndDlg,IDC_EDIT12,Result,true);
	}
}

//初始化对话框,移动对话框的位置
void OnInitDialog(HWND hwndDlg)
{
	RECT rect;
	GetWindowRect(hwndDlg,&rect);
	rect.left+= 400;
	rect.top += 300;
	rect.right += 400;
	rect.bottom += 300;
	MoveWindow(hwndDlg,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,FALSE);//FALSE 不需要重绘

}
BOOL CALLBACK DialogProc(
  HWND hwndDlg,  // handle to dialog box
  UINT uMsg,     // message
  WPARAM wParam, // first message parameter
  LPARAM lParam  // second message parameter
)
{
	switch(uMsg)
	{
	case WM_INITDIALOG:
			OnInitDialog(hwndDlg);
			return TRUE;
	case WM_COMMAND:
		{
			WORD nID = LOWORD(wParam);
			switch(nID)
			{
			
			case IDCANCEL:
				EndDialog(hwndDlg,0);
				break;
			case IDOK:
				OnCaculate(hwndDlg);
				break;
			case IDC_OK1:
				OnCaculate2(hwndDlg);
				break;
			case IDC_Ok2:
				OnCaculate3(hwndDlg);
				break;
			case IDC_OK3:
				OnCaculate4(hwndDlg);
				break;
			}
		}
		return TRUE;
	}

	return FALSE;
}

//初始化登录框,一定登录框的位置
void OnInitLogin(HWND hwndDlg)
{
	RECT rect;
	GetWindowRect(hwndDlg,&rect);
	rect.left+= 400;
	rect.top += 300;
	rect.right += 400;
	rect.bottom += 300;
	MoveWindow(hwndDlg,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,FALSE);//FALSE 不需要重绘
}

//登录判断,判断输入的帐号密码是否正确
int isTrue(HWND hwndDlg)
{
    char str[20];
	GetDlgItemText(hwndDlg,IDC_ACCOUNT_EDIT,str,sizeof(str));
	
	int nPassword = GetDlgItemInt(hwndDlg,IDC_PASSWORD_EDIT,NULL,FALSE);

	if(strcmp(str,"admin") == 0 && nPassword == 1234567)
		return TRUE;
	else
		return FALSE;
}

BOOL CALLBACK DialogProcLogin(	HWND hwndDlg,  // handle to dialog box
								UINT uMsg,     // message
								WPARAM wParam, // first message parameter
								LPARAM lParam  // second message parameter
								)
{
	switch(uMsg)
	{
		
	case WM_INITDIALOG: //对话框显示之前的操作
		OnInitLogin(hwndDlg);
		return TRUE;
	case WM_COMMAND:

		WORD nID = LOWORD(wParam);
		
		switch(nID)
		{
			case IDCANCEL:
			//	EndDialog(hwndDlg,IDCANCEL);
				return TRUE;
			case IDOK:
				if(isTrue(hwndDlg) == 1)
				{
					MessageBox(hwndDlg,"密码正确","提示消息",MB_OK);
					EndDialog(hwndDlg,IDCANCEL);
				}
				else
				{ 
					MessageBox(hwndDlg,"密码输入错误","提示消息",MB_OK);
				}
				return TRUE;
		}
	}

		return FALSE;
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{

	DialogBox(hInstance,(LPCSTR)IDD_LOGIN_DIALOG,NULL,DialogProcLogin);
	DialogBox(hInstance,(LPCSTR)IDD_MY_DIALOG,NULL,DialogProc);
	return 0;
}


在VC6下***的,代码可运行!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐