📄 unitconversion.c
字号:
#include <stdio.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#define IDC_UNIT1 100
#define IDC_UNIT2 101
#define IDC_TYPE 110
#define IDC_NUMBER1 111
#define IDC_NUMBER2 120
#define IDC_STATIC1 121
#define IDC_QUIT 130
#define IDC_COMBOBOX1 131
#define IDC_COMBOBOX2 140
#define IDC_COMBOBOX3 141
#define IDC_CHAR 150
//static BITMAP bmp;
//对话框模板
static DLGTEMPLATE DlgInitProgress =
{
WS_BORDER | WS_CAPTION,
WS_EX_NONE,
0,0, 320, 216,
"单位换算",
0,0,
11,NULL,
0
};
//空间模板数组
static CTRLDATA CtrlInitProgress [] =
{
{
CTRL_STATIC,
WS_VISIBLE | SS_SIMPLE,
10,10,35,25,
IDC_TYPE,
"类型",
0
},
{
CTRL_STATIC,
WS_VISIBLE | SS_SIMPLE,
10,40,35,25,
IDC_UNIT1,
"单位",
0
},
{
CTRL_STATIC,
WS_VISIBLE | SS_SIMPLE,
10,70,35,25,
IDC_NUMBER1,
"数量",
0
},
{
CTRL_STATIC,
WS_VISIBLE,
10,100,35,25,
IDC_UNIT2,
"单位",
0
},
{
CTRL_STATIC,
WS_VISIBLE | SS_SIMPLE | SS_NOTIFY,
10,130,35,25,
IDC_NUMBER2,
"数量",
0
},
{
CTRL_STATIC,
WS_VISIBLE | WS_BORDER | SS_SIMPLE,
60,130,103,25,
IDC_STATIC1,
"",
0
},
{
CTRL_COMBOBOX,
WS_VISIBLE | CBS_DROPDOWNLIST | CBS_NOTIFY,
60,10,120,25,
IDC_COMBOBOX1,
"",
0
},
{
CTRL_COMBOBOX,
WS_VISIBLE | CBS_DROPDOWNLIST | CBS_NOTIFY,
60,40,120,25,
IDC_COMBOBOX2,
"",
0
},
{
CTRL_COMBOBOX,
WS_VISIBLE | CBS_DROPDOWNLIST | CBS_NOTIFY,
60,100,120,25,
IDC_COMBOBOX3,
"",
0
},
{
CTRL_SLEDIT,
WS_VISIBLE |WS_BORDER | WS_TABSTOP ,
60,70,103,25,
IDC_CHAR,
"",
0
},
{
"button",
WS_TABSTOP | WS_VISIBLE | BS_DEFPUSHBUTTON,
70,170,60,25,
IDC_QUIT,
"退出",
0
}
};
static const char* type [] =
{
"长度换算",
"面积换算",
"容积换算",
"重量换算",
};
static const char* length [] =
{
"米",
"厘米",
"英寸"
};
static const char* square [] =
{
"公亩",
"公顷",
"英亩"
};
static const char* cubage [] =
{
"升",
"立方米",
"立方英寸"
};
static const char* weight [] =
{
"公斤",
"克",
"磅"
};
//进行换算用
static const double Unit[4][3][3] =
{
{
1, 0.01, 0.0254,
100, 1, 2.54,
39.37, 0.39, 1
},
{
1, 100, 40.47,
0.01, 1, 0.4047,
0.0247, 2.4711, 1
},
{
1, 1000, 3.79,
0.001, 1, 0.0038,
0.2642, 264.17, 1
},
{
1, 0.001, 0.4534,
1000, 1, 453.59,
2.2046, 0.0022, 1
}
};
//如果在编辑框中输入值后在静态文本框上显示换算后的值,换算过程如下
static void edit_notif_proc (HWND hWnd, int id, int nc, DWORD add_data)
{
double len; //编辑框中的输入值
char buff[60]; //存放编辑框中的字符串
int cur_sel1, cur_sel2, cur_sel3; //存放下拉框当前选项值
if(id == IDC_CHAR && nc == EN_CHANGE)
{
//获取下拉框当前选项索引值
cur_sel1 = SendMessage (GetDlgItem(GetParent(hWnd),IDC_COMBOBOX1), CB_GETCURSEL, 0, 0);
cur_sel2 = SendMessage (GetDlgItem(GetParent(hWnd),IDC_COMBOBOX2), CB_GETCURSEL,0, 0);
cur_sel3 = SendMessage (GetDlgItem(GetParent(hWnd),IDC_COMBOBOX3), CB_GETCURSEL,0, 0);
if (cur_sel2 == -1)
{
cur_sel2 = 0;
}
if (cur_sel3 == -1)
{
cur_sel3 = 0;
}
GetWindowText (hWnd, buff, 30); //获取编辑框中的字符串存放在buff中
len = (double) atoi(buff); //数值字符串转换成对应的数值
len = len / Unit[cur_sel1][cur_sel2][cur_sel3]; //换算
sprintf(buff, "%.5f", len); //将数值转换成字符串存放在buff中
SetDlgItemText(GetParent(hWnd), IDC_STATIC1, buff); //将buff中的值显示到静态文本框中
}
}
static void combobox_notif_proc (HWND hWnd, int id, int nc, DWORD add_data)
{
int i;
int cur_sel;
//如果类型下拉框有改变执行以下过程
if (id == IDC_COMBOBOX1 && nc == CBN_SELCHANGE)
{
cur_sel = SendMessage (hWnd, CB_GETCURSEL, 0, 0); //获取类型下拉框中的当前选项索引值
SendDlgItemMessage(GetParent(hWnd), IDC_COMBOBOX2, CB_RESETCONTENT, 0, 0); //清空单位下拉框中的选项
SendDlgItemMessage(GetParent(hWnd), IDC_COMBOBOX3, CB_RESETCONTENT, 0, 0);
if(cur_sel == 0)
{
SetWindowText (GetDlgItem (GetParent(hWnd), IDC_COMBOBOX2), length[0]); //设置单位下拉框1中初始显示的内容
SetWindowText (GetDlgItem (GetParent(hWnd), IDC_COMBOBOX3), length[0]); //设置单位下拉框2中初始显示的内容
for (i = 0; i < 3; i++)
{
SendDlgItemMessage(GetParent(hWnd), IDC_COMBOBOX2, CB_ADDSTRING, 0, (LPARAM)length[i]);
SendDlgItemMessage(GetParent(hWnd), IDC_COMBOBOX3, CB_ADDSTRING, 0, (LPARAM)length[i]);
}
}
if(cur_sel == 1)
{
SetWindowText (GetDlgItem (GetParent(hWnd), IDC_COMBOBOX2), square[0]);
SetWindowText (GetDlgItem (GetParent(hWnd), IDC_COMBOBOX3), square[0]);
for (i = 0; i < 3; i++)
{
SendDlgItemMessage(GetParent(hWnd), IDC_COMBOBOX2, CB_ADDSTRING, 0, (LPARAM)square[i]);
SendDlgItemMessage(GetParent(hWnd), IDC_COMBOBOX3, CB_ADDSTRING, 0, (LPARAM)square[i]);
}
}
if(cur_sel == 2)
{
SetWindowText (GetDlgItem (GetParent(hWnd), IDC_COMBOBOX2), cubage[0]);
SetWindowText (GetDlgItem (GetParent(hWnd), IDC_COMBOBOX3), cubage[0]);
for (i = 0; i < 3; i++)
{
SendDlgItemMessage(GetParent(hWnd), IDC_COMBOBOX2, CB_ADDSTRING, 0, (LPARAM)cubage[i]);
SendDlgItemMessage(GetParent(hWnd), IDC_COMBOBOX3, CB_ADDSTRING, 0, (LPARAM)cubage[i]);
}
}
if(cur_sel == 3)
{
SetWindowText (GetDlgItem (GetParent(hWnd), IDC_COMBOBOX2), weight[0]);
SetWindowText (GetDlgItem (GetParent(hWnd), IDC_COMBOBOX3), weight[0]);
for (i = 0; i < 3; i++)
{
SendDlgItemMessage(GetParent(hWnd), IDC_COMBOBOX2, CB_ADDSTRING, 0, (LPARAM)weight[i]);
SendDlgItemMessage(GetParent(hWnd), IDC_COMBOBOX3, CB_ADDSTRING, 0, (LPARAM)weight[i]);
}
}
}
}
static int InitDialogBoxProc(HWND hDlg, int message, WPARAM wParam, LPARAM lParam)
{
//HDC hdc;
int i, j;
//hdc=GetClientDC(hDlg);
switch(message)
{
case MSG_INITDIALOG:
//if(LoadBitmap(HDC_SCREEN, &bmp,"21.gif"))
// return -1;
for (i = 0; i < 4; i++)
{
SendDlgItemMessage(hDlg, IDC_COMBOBOX1, CB_ADDSTRING, 0, (LPARAM)type[i]); //向下拉框中添加选项
}
SendDlgItemMessage(hDlg, IDC_COMBOBOX1, CB_SETCURSEL, 0, 0); //设置下拉框的默认显示
SetWindowText (GetDlgItem (hDlg, IDC_COMBOBOX2), length[0]);
SetWindowText (GetDlgItem (hDlg, IDC_COMBOBOX3), length[0]);
for (j = 0; j < 3; j++)
{
SendDlgItemMessage(hDlg, IDC_COMBOBOX2, CB_ADDSTRING, 0, (LPARAM)length [j]);
SendDlgItemMessage(hDlg, IDC_COMBOBOX3, CB_ADDSTRING, 0, (LPARAM)length [j]);
}
//注册回调函数
SetWindowAdditionalData (hDlg, lParam);
SetNotificationCallback (GetDlgItem (hDlg, IDC_COMBOBOX1), combobox_notif_proc);
SetNotificationCallback (GetDlgItem (hDlg, IDC_CHAR), edit_notif_proc);
return 1;
case MSG_COMMAND:
switch(wParam)
{
case IDC_QUIT: //点击退出按钮产生这个消息
EndDialog(hDlg, wParam); //结束对话框
// UnloadBitmap(&bmp);
break;
}
break;
//case MSG_ERASEBKGND:
//FillBoxWithBitmap(hdc, 0,0,100,30,&bmp);
//break;
}
return DefaultDialogProc(hDlg, message, wParam, lParam);
}
static void InitDialogBox(HWND hWnd)
{
DlgInitProgress.controls = CtrlInitProgress; //将控件模板数组赋值给对话框模板的控件指针
DialogBoxIndirectParam(&DlgInitProgress, hWnd, InitDialogBoxProc, 0L); //此函数绘制对话框
}
int UnitConversion(HWND hWnd)
{
InitDialogBox(hWnd);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -