手记

我的第一个Windows程序

从一开始学习C语言就一直在黑乎乎的控制台程序转悠,虽然一直很有成就感,可一直觉得怪怪的,于是开始尝试做一下带图形界面的程序,毕竟接触的是C,所以自然而然选择了Windows程序设计,我设计的是非常非常简单的程序,就是把33到126的字符错位然后对应输出,就相当于给字符加密了。一开始用控制台程序写完整体代码,然后百度到一个简单的Windows对话框程序,照猫画虎,我直接复制别人的代码然后开始理解代码的意思,最先看懂的部分是case IDOK,我知道当触发OK键的时候执行下面代码,于是我开始找如何获取控件内容的函数GetDlgItemText();尽管找到了这个函数,可是百度到的东西还是看不懂,传递实参的时候总是匹配不到该类型,问同学才知道有字符集UNICODE这个东西,所以我不能直接传字符串,我于是在这个陷阱停留了很久很久,直到我想到把UNICODE这个东西取消。
取消UNICODE之后,可以直接传递字符串,接下来是输出字符串,因为一开始我只想实现输入和输出,并没有考虑加入我的加密代码,这是我的编程习惯,简化编程内容,一步一步增加代码,先确保部分代码不出错,再整合代码。在输出控件的时候又出现了问题,就是Sratic control这个控件,一直出现未定义的报错,后来百度到需要更改这个控件的名字才可以使用。

#include <windows.h>
#include "resource.h"
BOOL CALLBACK DlgMain(HWND, UINT, WPARAM, LPARAM);//HWND窗口句柄
int strlength(char * str)
{//字符串长度判断
	int a = 0;
	while (*str++ != '\0')
		a++;
	return(a);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{//主函数,从这里开始执行,调用DialogBox函数MAKEINTRESOURCE(IDD_DIALOG1),括号内是对话框的名称
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgMain);
	return 0;
}
//CALLBACK回调函数 
BOOL CALLBACK DlgMain(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_CLOSE:
	{//当点击×时候执行结束函数
		EndDialog(hwnd, 0);
		return 0;
	}
	case WM_COMMAND:
	{
		switch (LOWORD(wParam))
		{
		case IDOK:
		{//当点击OK按钮时
			int cnum = 0, i = 0;
			char str[30];
			char error[] = "Please enter letters,numbers,or common characters.";
			char item[] = "ERROR";
			GetDlgItemText(hwnd, IDC_EDIT1, str, 30);//获得edit control控件内的内容保存在字符串str内,并且由于更改过UNICODE可以使用字符串,
			cnum = strlength(str);//如何更改UNICODE:项目右键-属性-配置属性-常规-项目默认值-字符集
			for (; i < cnum; i++)//判断是否存在非法字符 例如中文
				if (str[i] < 33 || str[i] > 126){
				MessageBox(hwnd, error, item, 0);//弹出消息框内容为error标题item
				return 0;
				}
			for (i = 0; i < cnum; i++){
				switch (str[i])
				{
				case 33:
					str[i] = 57;
					break;
				case 34:
					str[i] = 58;
					break;
				case 35:
					str[i] = 59;
					break;
				case 36:
					str[i] = 60;
					break;
				case 37:
					str[i] = 61;
					break;
				case 38:
					str[i] = 62;
					break;
				case 39:
					str[i] = 63;
					break;
				case 40:
					str[i] = 64;
					break;
				case 41:
					str[i] = 65;
					break;
				case 42:
					str[i] = 66;
					break;
				case 43:
					str[i] = 67;
					break;
				case 44:
					str[i] = 68;
					break;
				case 45:
					str[i] = 69;
					break;
				case 46:
					str[i] = 70;
					break;
				case 47:
					str[i] = 71;
					break;
				case 48:
					str[i] = 72;
					break;
				case 49:
					str[i] = 73;
					break;
				case 50:
					str[i] = 74;
					break;
				case 51:
					str[i] = 75;
					break;
				case 52:
					str[i] = 76;
					break;
				case 53:
					str[i] = 77;
					break;
				case 54:
					str[i] = 78;
					break;
				case 55:
					str[i] = 79;
					break;
				case 56:
					str[i] = 80;
					break;
				case 57:
					str[i] = 33;
					break;
				case 58:
					str[i] = 34;
					break;
				case 59:
					str[i] = 35;
					break;
				case 60:
					str[i] = 36;
					break;
				case 61:
					str[i] = 37;
					break;
				case 62:
					str[i] = 38;
					break;
				case 63:
					str[i] = 39;
					break;
				case 64:
					str[i] = 40;
					break;
				case 65:
					str[i] = 41;
					break;
				case 66:
					str[i] = 42;
					break;
				case 67:
					str[i] = 43;
					break;
				case 68:
					str[i] = 44;
					break;
				case 69:
					str[i] = 45;
					break;
				case 70:
					str[i] = 46;
					break;
				case 71:
					str[i] = 47;
					break;
				case 72:
					str[i] = 48;
					break;
				case 73:
					str[i] = 49;
					break;
				case 74:
					str[i] = 50;
					break;
				case 75:
					str[i] = 51;
					break;
				case 76:
					str[i] = 52;
					break;
				case 77:
					str[i] = 53;
					break;
				case 78:
					str[i] = 54;
					break;
				case 79:
					str[i] = 55;
					break;
				case 80:
					str[i] = 56;
					break;
				case 81:
					str[i] = 104;
					break;
				case 82:
					str[i] = 105;
					break;
				case 83:
					str[i] = 106;
					break;
				case 84:
					str[i] = 107;
					break;
				case 85:
					str[i] = 108;
					break;
				case 86:
					str[i] = 109;
					break;
				case 87:
					str[i] = 110;
					break;
				case 88:
					str[i] = 111;
					break;
				case 89:
					str[i] = 112;
					break;
				case 90:
					str[i] = 113;
					break;
				case 91:
					str[i] = 114;
					break;
				case 92:
					str[i] = 115;
					break;
				case 93:
					str[i] = 116;
					break;
				case 94:
					str[i] = 117;
					break;
				case 95:
					str[i] = 118;
					break;
				case 96:
					str[i] = 119;
					break;
				case 97:
					str[i] = 120;
					break;
				case 98:
					str[i] = 121;
					break;
				case 99:
					str[i] = 122;
					break;
				case 100:
					str[i] = 123;
					break;
				case 101:
					str[i] = 124;
					break;
				case 102:
					str[i] = 125;
					break;
				case 103:
					str[i] = 126;
					break;
				case 104:
					str[i] = 81;
					break;
				case 105:
					str[i] = 82;
					break;
				case 106:
					str[i] = 83;
					break;
				case 107:
					str[i] = 84;
					break;
				case 108:
					str[i] = 85;
					break;
				case 109:
					str[i] = 86;
					break;
				case 110:
					str[i] = 87;
					break;
				case 111:
					str[i] = 88;
					break;
				case 112:
					str[i] = 89;
					break;
				case 113:
					str[i] = 90;
					break;
				case 114:
					str[i] = 91;
					break;
				case 115:
					str[i] = 92;
					break;
				case 116:
					str[i] = 93;
					break;
				case 117:
					str[i] = 94;
					break;
				case 118:
					str[i] = 95;
					break;
				case 119:
					str[i] = 96;
					break;
				case 120:
					str[i] = 97;
					break;
				case 121:
					str[i] = 98;
					break;
				case 122:
					str[i] = 99;
					break;
				case 123:
					str[i] = 100;
					break;
				case 124:
					str[i] = 101;
					break;
				case 125:
					str[i] = 102;
					break;
				case 126:
					str[i] = 103;
					break;
				default:
					break;
				}
			}
			SetDlgItemText(hwnd, 12389, str);//在控件12389输出内容str,必须更改该控件的名称才能输出
			return 0;
		}
		}
	}
	}
	return FALSE;
}
0人推荐
随时随地看视频
慕课网APP