Des算法C++实现

VS2013新建Win32项目,选择空项目,添加头文件和源文件,为了美观程序使用duilib界面库,有些地方需要配置一下。

参考教程:http://www.cnblogs.com/Alberl/p/3342030.html

 

Des算法是加密64位明文的。本例将输入的8位字符通过ASCII码转化为64位二进制数。若超过8个字符或不足8个字符,就填充空格至8的位数个字符,每8个加密。


DES.h:

#pragma once
#include <UIlib.h>
#include <Windows.h>
using namespace DuiLib;

#ifdef _DEBUG
#   ifdef _UNICODE
#       pragma comment(lib, "DuiLib_ud.lib")
#   else
#       pragma comment(lib, "DuiLib_d.lib")
#   endif
#else
#   ifdef _UNICODE
#       pragma comment(lib, "DuiLib_u.lib")
#   else
#       pragma comment(lib, "DuiLib.lib")
#   endif
#endif

#include <iostream>
#include<math.h>

const int IP_Table[65] = { 0,
58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7
};

const int Ebox[49] = { 0,
32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1
};
//PC1选位表(密钥生成置换表1)
const int PC_1[57] = { 0,
57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
};
//PC2选位表(密钥生成置换表2)
const int PC_2[49] = { 0,
14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
};
//左移位数表 
const int LeftMove[17] = {
	0, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
};

const int P[33]={0,
16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
};

int SBox[8][4][16] = {
	// S1 
	{ { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 },
	{ 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 },
	{ 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 },
	{ 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 } },
	//S2
	{ { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 },
	{ 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 },
	{ 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 },
	{ 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 } },
	//S3
	{ { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 },
	{ 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 },
	{ 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 },
	{ 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 } },
	//S4
	{ { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 },
	{ 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 },
	{ 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4 },
	{ 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 } },
	//S5
	{ { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 },
	{ 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 },
	{ 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 },
	{ 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 } },
	//S6
	{ { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 },
	{ 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 },
	{ 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 },
	{ 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 } },
	//S7
	{ { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 },
	{ 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 },
	{ 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 },
	{ 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 } },
	//S8
	{ { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 },
	{ 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 },
	{ 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 },
	{ 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } }
};
const int IP_1[65] = { 0,
40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25
};

/*void output(CDuiString str, int a[], int n)//没有界面时测试用
{
	cout << str << ":";
	for (int i = 1; i <= n; i++)
	{
		cout << a[i];
	}
	cout << endl;
}*/
void  char_to_bit(char c, int temp[])//一个字符(1字节)转化为二进制(8位)
{
	for (int i = 0; i<8; i++){
		int a = ((c >> i) & 1);
		temp[7 - i] = a;
	}
}

//字符串CDuiString转二进制int[](56位)
void str_to_bit(CDuiString str, int strbit[]){
	int len = str.GetLength();
	int i, j;
	int temp[8];
	for (i = 0; i<len; i++){
		char c = str[i];
		char_to_bit(c, temp);
		for (j = 1; j <= 8; j++){
			strbit[i * 8 + j] = temp[j - 1];
		}
	}

}

void divisionbit(int str_bit[], int leftbit[], int rightbit[]){
	for (int i = 1; i<65; i++){
		if (i<33){
			leftbit[i] = str_bit[i];
		}
		else{
			rightbit[i - 32] = str_bit[i];
		}
	}

}
void ip_change(int str[])  //ip变换
{
	int temp[65];
	for (int i = 1; i<65; i++){
		temp[i] = str[IP_Table[i]];
	}
	for (int i = 1; i<65; i++){
		str[i] = temp[i];
	}
}

void pc1_change(int str[], int strkey[])  //pc1变换
{
	int temp[57];
	for (int i = 1; i<57; i++){
		temp[i] = str[PC_1[i]];
	}
	for (int i = 1; i<57; i++){
		strkey[i] = temp[i];
	}

}

//产生56位密钥
void key_56(CDuiString str, int strkey[])
{
	int str_bit[57], str_t[65];
	str_to_bit(str, str_bit);
	int m = 1, i, j;
	for (i = 1, j = 1; i < 57; i++, j++)
	{
		if (j % 8 == 0)j++;
		str_t[j] = str_bit[i];
	}
	int sum = 0;
	for (i = 1; i < 65; i++)
	{

		if (i % 8 == 0)
		{
			str_t[i] = (sum + 1) % 2;
			sum = 0;
			i++;
		}
		sum += str_t[i];
	}
	pc1_change(str_t, strkey);//pc1产生56位str_bit

}


void extend(int str[], int extstr[])//e扩展
{
	int i;
	for (i = 1; i <= 48; i++){
		extstr[i] = str[Ebox[i]];
	}

}


void divisionkey(CDuiString str, int leftkey[], int rightkey[]){
	int strkey[57], strbit[65];


	key_56(str, strkey);
	//str_to_bit(str, strbit);
	//pc1_change(strkey, strbit);

	for (int i = 1; i<57; i++){
		if (i<29){
			leftkey[i] = strkey[i];
		}
		else{
			rightkey[i - 28] = strkey[i];
		}
	}

}

void keymovebit(int strkey[], int leftkey[], int rightkey[], int i)//循环左移
{
	int temp1[2];
	int temp2[2];
	if ((LeftMove[i]) == 1){
		temp1[0] = leftkey[1];
		temp1[1] = rightkey[1];
		for (i = 1; i<28; i++){
			leftkey[i] = leftkey[i + 1];
			rightkey[i] = rightkey[i + 1];
		}
		leftkey[28] = temp1[0];
		rightkey[28] = temp1[1];

	}
	else if ((LeftMove[i]) == 2){
		temp1[0] = leftkey[1];
		temp1[1] = leftkey[2];
		temp2[0] = rightkey[1];
		temp2[1] = rightkey[2];

		for (i = 1; i + 2<29; i++){
			leftkey[i] = leftkey[i + 2];
			rightkey[i] = rightkey[i + 2];
		}
		leftkey[27] = temp1[0];
		leftkey[28] = temp1[1];

		rightkey[27] = temp2[0];
		rightkey[28] = temp2[1];
	}

	int t[57], j;
	for (j = 1; j < 57; j++)
	{
		if (j < 29)t[j] = leftkey[j];
		else t[j] = rightkey[j - 28];
	}
	for (j = 1; j<49; j++){
		strkey[j] = t[PC_2[j]];  //pc2变换
	}
	//output("第一轮产生的密钥", strbit);
}

void yihuo(int arr[], int brr[], int crr[], int n)//异或操作
{
	int i;
	for (i = 1; i<n; i++){
		crr[i] = (arr[i] + brr[i]) % 2;
	}
}
int SboxSelect(int s[][6], int i, int num)//s盒替换
{
	int x = s[i][0] * 2 + s[i][5];
	int y = s[i][1] * 8 + s[i][2] * 4 + s[i][3] * 2 + s[i][4];
	return SBox[num][x][y];
}

void Pbox(int str[])//p变化
{
	int tem[33];
	int i;
	for (i = 1; i<33; i++){
		tem[i] = str[P[i]];
	}

	for (i = 1; i<33; i++){
		str[i] = tem[i];
	}
}


void intto2(int t, int str[])  //将单个int转化为二进制
{
	for (int j = 0; j < 4; j++)
	{
		str[3 - j] = t % 2;
		t = t / 2;
	}
}

void int_to_2(int t[], int str[])//将多个int转化为二进制
{
	int str1[8][4];
	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			intto2(t[i], str1[i]);
			//str[(i + 1) * 4 - j] = m;
		}
	}
	for (int i = 0, k = 1; i < 8; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			str[k] = str1[i][j];
			k++;
		}
	}
}


void stepbit(int leftbit[], int rightbit[], int key[])//16个循环的步骤
{
	int temp1[49], temp2[50];
	extend(rightbit, temp1);
	yihuo(key, temp1, temp2, 49);
	int s[8][6], t[8];
	for (int n = 0, k = 1; n < 8; n++)
	{
		for (int m = 0; m < 6; m++)
		{
			s[n][m] = temp2[k];
			k++;
		}
	}
	for (int i = 0; i < 8; i++)
	{
		t[i] = SboxSelect(s, i, i); //S盒变化
	}
	int temp[33];
	int_to_2(t, temp);//S和变换后的32位1-32
	Pbox(temp);;
	yihuo(leftbit, temp, temp1, 33);
	for (int i = 1; i < 33; i++)
	{
		leftbit[i] = rightbit[i];
	}
	for (int i = 1; i < 33; i++)
	{
		rightbit[i] = temp1[i];
	}
}

void addtomi(int leftbit[], int rightbit[], int str[])//最终合并加ip逆变化
{
	int temp[65], i;
	for (i = 1; i < 65; i++)
	{
		if (i < 33)
		{
			temp[i] = leftbit[i];
		}
		else
		{
			temp[i] = rightbit[i - 32];
		}
	}
	for (i = 1; i < 65; i++)//ip逆变化
	{
		str[i] = temp[IP_1[i]];
	}
}

void copyint(int s[], int t[])//用于密钥的复制保存
{
	for (int i = 1; i < 49; i++)
	{
		s[i] = t[i];
	}
}

在main.cpp里实现界面控件的消息响应

#pragma once
#include "DES.h"
//#include <UIlib.h>
//using namespace DuiLib;

#ifdef _DEBUG
#   ifdef _UNICODE
#       pragma comment(lib, "DuiLib_ud.lib")
#   else
#       pragma comment(lib, "DuiLib_d.lib")
#   endif
#else
#   ifdef _UNICODE
#       pragma comment(lib, "DuiLib_u.lib")
#   else
#       pragma comment(lib, "DuiLib.lib")
#   endif
#endif

class CDuiFrameWnd : public CWindowWnd, public INotifyUI
{
protected:
	CPaintManagerUI m_PaintManager;

public:
	CDuiFrameWnd() { };
	UINT GetClassStyle() const { return UI_CLASSSTYLE_DIALOG; };
	void OnFinalMessage(HWND hWnd) { delete this; };
	 
	void Init()
	{
		m_pCloseBtn = static_cast<CButtonUI*>(m_PaintManager.FindControl(_T("close")));
		m_pMinBtn = static_cast<CButtonUI*>(m_PaintManager.FindControl(_T("min")));
		m_pjiamiBtn = static_cast<CButtonUI*>(m_PaintManager.FindControl(_T("jiami")));
		m_pjiemiBtn = static_cast<CButtonUI*>(m_PaintManager.FindControl(_T("jiemi")));
		m_pkey = static_cast<CEditUI*>(m_PaintManager.FindControl(_T("key")));
		m_pmingwen = static_cast<CRichEditUI*>(m_PaintManager.FindControl(_T("mingwen")));
		m_pmiwen = static_cast<CRichEditUI*>(m_PaintManager.FindControl(_T("miwen")));
		m_pmingwen->SetFont(0);
	}
	//WindowClassName  
	LPCTSTR GetWindowClassName()const{ return _T("DUIWND"); }
	//响应控件消息  
	void Notify(TNotifyUI& msg)
	{
		if (msg.sType == _T("click")) {
			if (msg.pSender == m_pCloseBtn)
			{
				PostQuitMessage(0);
				return;
			}
			else if (msg.pSender == m_pMinBtn)
			{
				SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
				return;
			}
			else if (msg.pSender==m_pjiamiBtn)
			{
				//if (m_pmingwen)
				if ((m_pmingwen->GetText()).IsEmpty())
				{
					MessageBox(NULL, L"输入8位明文(如:perfect!)", NULL, 0);
					return;
				}
				if ((m_pkey->GetText()).IsEmpty())
				{
					MessageBox(NULL, L"输入7位密钥(如:zjut666)", NULL, 0);
					return;
				}
				else
				{
					
					if ((m_pkey->GetText()).GetLength() < 7)
					{
						MessageBox(NULL, L"输入7位密钥(如:zjut666)", NULL, 0);
						return;
					}
					CDuiString strlong = m_pmingwen->GetText();
					CDuiString key = m_pkey->GetText();
					int strbit[65], leftbit[33], rightbit[33];
					int leftkey[29], rightkey[29], key48[50];//保存左右28位密钥
					CDuiString str;
					m_pmiwen->SetText(L"");
					int duan = ((m_pmingwen->GetText().GetLength()-1) / 8) + 1;
					for (int l = 0; l < duan; l++)
					{
						str = L"00000000";
						for (int k = 0; k < 8; k++)
						{
							if (l * 8 + k == m_pmingwen->GetText().GetLength())
							{
								str.SetAt( k,_T(' '));
							}
							else
							{
								str.SetAt(k, m_pmingwen->GetText().GetAt(l * 8 + k));
							}

						}
						str_to_bit(str, strbit);//64位明文
						ip_change(strbit);
						divisionkey(key, leftkey, rightkey);
						divisionbit(strbit, leftbit, rightbit);
						for (int number = 1; number <= 16; number++)
						{
							keymovebit(key48, leftkey, rightkey, number);//strkey 1-48位位Kn字密钥,正确
							stepbit(leftbit, rightbit, key48);
						}
						addtomi(rightbit, leftbit, strbit);//左右ip逆变换
						CDuiString mi=L"0000000000000000000000000000000000000000000000000000000000000000";
						for (int i = 1; i < 65; i++)
						{
							if (strbit[i] == 1)
								mi.SetAt(i-1, _T('1'));
							else
								mi.SetAt(i-1, _T('0'));
						}
						m_pmiwen->AppendText(mi);
					}
				}
			}
			else if (msg.pSender == m_pjiemiBtn)
			{
				if ((m_pmingwen->GetText()).IsEmpty())
				{
					MessageBox(NULL, L"输入64位密文", NULL, 0);
					return;
				}
				if ((m_pkey->GetText()).IsEmpty())
				{
					MessageBox(NULL, L"输入7位密钥(如:zjut666)", NULL, 0);
					return;
				}
				else
				{

					if ((m_pkey->GetText()).GetLength() < 7)
					{
						MessageBox(NULL, L"输入7位密钥(如:zjut666)", NULL, 0);
						return;
					}
					m_pmiwen->SetText(L"");
					CDuiString strlong = m_pmingwen->GetText();
					CDuiString key = m_pkey->GetText();
					int strbit[65], leftbit[33], rightbit[33];
					int leftkey[29], rightkey[29], key48[50];//保存左右28位密钥
					int key_t[16][49];
					CDuiString str;
					int duan = (m_pmingwen->GetText().GetLength()  / 64);
					for (int t = 0; t < duan; t++)
					{
						str = L"0000000000000000000000000000000000000000000000000000000000000000";
						for (int j = 0; j < 64; j++)
						{
							
							str.SetAt(j, m_pmingwen->GetText().GetAt(t * 64 + j));
						}
						for (int i = 1; i < 65; i++)
						{
							if (str.GetAt(i - 1) == _T('1'))
								strbit[i] = 1;
							else
								strbit[i] = 0;
						}
						ip_change(strbit);
						divisionbit(strbit, leftbit, rightbit);
						divisionkey(key, leftkey, rightkey);
						for (int number = 1; number <= 16; number++)
						{
							keymovebit(key48, leftkey, rightkey, number);//strkey 1-48位位Kn字密钥
							copyint(key_t[number - 1], key48);
						}
						for (int m = 15; m >= 0; m--)
						{
							stepbit(leftbit, rightbit, key_t[m]);
						}
						addtomi(rightbit, leftbit, strbit);
						CDuiString mi = L"00000000";
						char a[100];
						TCHAR p[100];
						int res = 0, mid = 1;
						for (int i = 1; i <= 64; i++)
						{
							if (i % 8 == 0)
							{
								res += strbit[i];

								a[i / 8 - 1] = (char)res;
								res = 0;
							}
							else{
								res += strbit[i] * pow(2, 8 - i % 8);
							}

						}
						MultiByteToWideChar(CP_ACP, 0, a, -1, p, 100);
						for (int j = 0; j<8; j++)
						{
							if (p[j]>0 && p[j] < 255)
							{
								mi.SetAt(j, p[j]);
								
							}
							else
							{
								mi.SetAt(j, _T(' '));
							}
							
						}
						m_pmiwen->AppendText(mi);



					}
					
					

				}
			}
		}	
	}
	//响应窗口消息  
	LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
		styleValue &= ~WS_CAPTION;
		::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);

		m_PaintManager.Init(m_hWnd);
		CDialogBuilder builder;
		CControlUI* pRoot = builder.Create(_T("duilib.xml"), (UINT)0, NULL, &m_PaintManager);
		ASSERT(pRoot && "Failed to parse XML");
		m_PaintManager.AttachDialog(pRoot);
		m_PaintManager.AddNotifier(this);

		Init();
		return 0;
	}

	LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		::PostQuitMessage(0L);
		bHandled = FALSE;
		return 0;
	}

	LRESULT OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		if (::IsIconic(*this)) bHandled = FALSE;
		return (wParam == 0) ? TRUE : FALSE;
	}

	LRESULT OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		return 0;
	}

	LRESULT OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		return 0;
	}

	LRESULT OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		POINT pt; pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam);
		::ScreenToClient(*this, &pt);

		RECT rcClient;
		::GetClientRect(*this, &rcClient);

		RECT rcCaption = m_PaintManager.GetCaptionRect();
		if (pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
			&& pt.y >= rcCaption.top && pt.y < rcCaption.bottom) {
			CControlUI* pControl = static_cast<CControlUI*>(m_PaintManager.FindControl(pt));
			if (pControl && _tcscmp(pControl->GetClass(), DUI_CTR_BUTTON) != 0)
				return HTCAPTION;
		}

		return HTCLIENT;
	}

	LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		SIZE szRoundCorner = m_PaintManager.GetRoundCorner();
		if (!::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0)) {
			CDuiRect rcWnd;
			::GetWindowRect(*this, &rcWnd);
			rcWnd.Offset(-rcWnd.left, -rcWnd.top);
			rcWnd.right++; rcWnd.bottom++;
			HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy);
			::SetWindowRgn(*this, hRgn, TRUE);
			::DeleteObject(hRgn);
		}

		bHandled = FALSE;
		return 0;
	}

	LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
	{
		LRESULT lRes = 0;
		BOOL bHandled = TRUE;
		switch (uMsg) {
		case WM_CREATE:        lRes = OnCreate(uMsg, wParam, lParam, bHandled); break;
		case WM_DESTROY:       lRes = OnDestroy(uMsg, wParam, lParam, bHandled); break;
		case WM_NCACTIVATE:    lRes = OnNcActivate(uMsg, wParam, lParam, bHandled); break;
		case WM_NCCALCSIZE:    lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); break;
		case WM_NCPAINT:       lRes = OnNcPaint(uMsg, wParam, lParam, bHandled); break;
		case WM_NCHITTEST:     lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled); break;
		case WM_SIZE:          lRes = OnSize(uMsg, wParam, lParam, bHandled); break;
		default:
			bHandled = FALSE;
		}
		if (bHandled) return lRes;
		if (m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes)) return lRes;
		return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
	}


private:
	CButtonUI* m_pCloseBtn;
	CButtonUI* m_pMinBtn;
	CButtonUI* m_pjiamiBtn;
	CButtonUI* m_pjiemiBtn;
	CRichEditUI* m_pmingwen;
	CRichEditUI* m_pmiwen;
	CEditUI* m_pkey;
};


int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	CPaintManagerUI::SetInstance(hInstance);
	CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath() );

	HRESULT Hr = ::CoInitialize(NULL);
	if (FAILED(Hr)) return 0;

	CDuiFrameWnd* pFrame = new CDuiFrameWnd();
	if (pFrame == NULL) return 0;
	pFrame->Create(NULL, NULL, UI_WNDSTYLE_DIALOG, 0);
	pFrame->CenterWindow();
	pFrame->ShowWindow(true);
	CPaintManagerUI::MessageLoop();

	::CoUninitialize();
	return 0;
}

duilib.xml的内容:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<Window size="470,290" sizebox="4,4,6,6" caption="0,0,0,30" roundcorner="8,8">
<Font shared="true" id="0" name="幼圆" size="16" default="true" /> 
	<VerticalLayout width="300" height="500" bkimage="skin\skin1.jpg" enabled="true">
			
		<Button name="close" float="true" width="25" height="25" pos="444,3,0,0" normalimage="skin\sysbtn_close_normal.png" hotimage="skin\sysbtn_close_hover.png" pushedimage="skin\sysbtn_close_down.png"borderround="4,4" />
		<Button name="min" float="true" width="25" height="25" pos="415,3,0,0" normalimage="skin\sysbtn_min_normal.png" hotimage="skin\sysbtn_min_hover.png" pushedimage="skin\sysbtn_min_down.png" borderround="4,4"/>
		
		<RichEdit name="mingwen" float="true" pos="20,35,450,125" bordercolor="0xFFFFFFFF" bordersize="1" borderround="6,6" bordervisible="true" vscrollbar="true" autovscroll="true"/>
		<Text text="密钥:" float="true" pos="20,140,100,165" align="center"/>
		
		<Edit name="key" float="true" pos="100,135,180,165" maxchar="7" align="center" borderround="6,6" bordervisible="true"/>
		<Button name="jiami" float="true"  pos="300,135,370,165" text="加密" normalimage="skin\normal_button.png" hotimage="skin\hot_button.png" pushedimage="skin\down_button.png" borderround="6,6"/>
		<Button name="jiemi" float="true"  pos="380,135,450,165" text="解密"  normalimage="skin\normal_button.png" hotimage="skin\hot_button.png" pushedimage="skin\down_button.png" borderround="6,6"/>
		<RichEdit name="miwen" float="true" pos="20,175,450,270" bordercolor="0xFFFFFFFF" bordersize="1" borderround="6,6" bordervisible="true" vscrollbar="true" autovscroll="true" />
		

	</VerticalLayout>
</Window>

刚刚学习Duilib库,不是很懂,本文作笔记。





  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值