我可以使用现有项目代码在 C++ 中创建 window
can I create a window in c++ using existing project code
我想知道是否可以使用我现有的项目代码创建一个 window。这是一个学校项目。然而,我已经完成了实际的编码部分,只是想让项目更漂亮,可以这么说。在此先感谢大家的支持。
这是实际代码,以备不时之需。文章有点长,请注意:) 再次感谢您
#define NOMINMAX
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <windows.h>
#include <Windows.h>
#include <chrono>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int key[3][3];
double inverted[3][3];
int store[1][3] = { 0 };
int conv[666];
int random1()
{
unsigned long long int xRan;
srand(time(NULL));
xRan = rand() % 9999 + 1;
return xRan;
}
int random2()
{
unsigned long long int xRan;
xRan = rand() % 9999 + 1;
return xRan;
}
int random3()
{
int xRan;
xRan = rand() % 9999 + 1;
return xRan;
}
void clear_screen(char fill = ' ') {
COORD tl = { 0, 0 };
CONSOLE_SCREEN_BUFFER_INFO s;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(console, &s);
DWORD written, cells = s.dwSize.X * s.dwSize.Y;
FillConsoleOutputCharacter(console, fill, cells, tl, &written);
FillConsoleOutputAttribute(console, s.wAttributes, cells, tl, &written);
SetConsoleCursorPosition(console, tl);
}
int convert(char letter) {
int conv;
conv = (int)letter;
return conv;
}
void reverseMult(double inv[3][3], int decode[1][3])
{
store[0][0] = decode[0][0] * inv[0][0] + decode[0][1] * inv[1][0] + decode[0][2] * inv[2][0] + 0.5;
store[0][1] = decode[0][0] * inv[0][1] + decode[0][1] * inv[1][1] + decode[0][2] * inv[2][1] + 0.5;
store[0][2] = decode[0][0] * inv[0][2] + decode[0][1] * inv[1][2] + decode[0][2] * inv[2][2] + 0.5;
}
void matrixMult(int q, int w, int e, int a[3][3])
{
int A[1][3] = { q, w, e };
int B[3][3] = {
{ a[0][0], a[0][1], a[0][2] },
{ a[1][0], a[1][1], a[1][2] },
{ a[2][0], a[2][1], a[2][2] }
};
store[0][0] = A[0][0] * B[0][0] + A[0][1] * B[1][0] + A[0][2] * B[2][0];
store[0][1] = A[0][0] * B[0][1] + A[0][1] * B[1][1] + A[0][2] * B[2][1];
store[0][2] = A[0][0] * B[0][2] + A[0][1] * B[1][2] + A[0][2] * B[2][2];
//cout << store[0][0] << endl << store[0][1] << endl << store[0][2] << endl << endl;
}
char reverseConv(int x){
char conv;
conv = (char)x;
return conv;
}
void inverse(int key[3][3], double det){
int cofactor[3][3] = {
{ (key[1][1] * key[2][2] - key[1][2] * key[2][1]), -(key[1][0] * key[2][2] - key[1][2] * key[2][0]), (key[1][0] * key[2][1] - key[1][1] * key[2][0]) },
{ -(key[0][1] * key[2][2] - key[0][2] * key[2][1]), (key[0][0] * key[2][2] - key[0][2] * key[2][0]), -(key[0][0] * key[2][1] - key[0][1] * key[2][0]) },
{ (key[0][1] * key[1][2] - key[0][2] * key[1][1]), -(key[0][0] * key[1][2] - key[0][2] * key[1][0]), (key[0][0] * key[1][1] - key[0][1] * key[1][0]) }
};
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
inverted[i][j] =det * cofactor[i][j];
}
}
}
int main()
{
while (1){
cout << "Would you like to encrypt or decrypt?(e/d)\n " << endl;
string ende;
cin >> ende;
clear_screen();
if (ende == "e")
{
cout << "Please enter a name for the message: " << endl << endl;
string file;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, file);
clear_screen();
file += ".txt";
ofstream encrypt;
encrypt.open(file);
string message;
cout << "Please enter the message you would like to encrypt: " << endl << endl;
//cin.ignore(numeric_limits<streamsize>::max(), '\n'); --- Not needed anymore, uncomment if you cannot input message
getline(cin, message);
if (message.length() % 3 != 0)
message += ' ';
if (message.length() % 3 != 0)
message += ' ';
clear_screen();
for (int i = 0; i < message.length(); ++i)
{
conv[i] = convert(message[i]);
}
int det = 0;
while (1){
key[0][0] = random1(); //1
key[0][1] = random2(); //2
key[0][2] = random3(); //3
key[1][0] = random1() * 13 / 7; //4
key[1][1] = random2() * 23 / 7; //5
key[1][2] = random3() * 33 / 7; //6
key[2][0] = random1() * 18 / 15; //7
key[2][1] = random2() * 18 / 12; //8
key[2][2] = random3() * 18 / 10; //9
det = key[0][0] * key[1][1] * key[2][2] + key[0][1] * key[1][2] * key[2][0] + key[0][2] * key[1][0] * key[2][1]
- key[0][2] * key[1][1] * key[2][0] - key[0][0] * key[1][2] * key[2][1] - key[0][1] * key[1][0] * key[2][2];
if (det != 0)
break;
}
encrypt << key[0][0] << ' ' << key[0][1] << ' ' << key[0][2] << ' '
<< key[1][0] << ' ' << key[1][1] << ' ' << key[1][2] << ' '
<< key[2][0] << ' ' << key[2][1] << ' ' << key[2][2] << endl << endl;
int a, b, c;
int count = 0;
for (int i = 0; i < (message.length)() / 3; ++i)
{
int counting = 0;
a = conv[count];
count++;
b = conv[count];
count++;
c = conv[count];
count++;
matrixMult(a, b, c, key);
encrypt << store[0][counting] << ' ';
counting++;
encrypt << store[0][counting] << ' ';
counting++;
encrypt << store[0][counting] << endl;
}
encrypt.close();
Sleep(750);
cout << "Your message has been encrypted." << endl;
Sleep(750);
cout << "Please check " << file << " for the encrypted message and key" << endl << endl;
Sleep(750);
}
if (ende == "d")
{
cout << "Please enter the name of the file you would like to decrypt: " << endl << endl;
string file;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, file);
clear_screen();
file += ".txt";
ifstream decrypt;
decrypt.open(file);
int key[3][3];
for (int i = 0; i < 3; ++i)
{
for (int k = 0; k < 3; ++k){
decrypt >> key[k][i];
}
}
int det = 0;
det = key[0][0] * key[1][1] * key[2][2] + key[0][1] * key[1][2] * key[2][0] + key[0][2] * key[1][0] * key[2][1] - key[0][2] * key[1][1] * key[2][0] - key[0][0] * key[1][2] * key[2][1] - key[0][1] * key[1][0] * key[2][2];
double detInv = 1;
detInv /= det;
//double inv;
inverse(key, detInv);
int out[1][3];
int count = 0;
while (!decrypt.eof()){
for (int i = 0; i < 3; ++i)
{
decrypt >> out[0][i];
}
reverseMult(inverted, out);
count++;
char a, b, c;
a = reverseConv(store[0][0]);
b = reverseConv(store[0][1]);
c = reverseConv(store[0][2]);
if (decrypt.eof())
break;
cout << a << b << c;
}
}
cout << endl << endl;
cout << "Would you like to continue?(y/n) ";
char again;
cin >> again;
if (again != 'y')
exit(0);
clear_screen();
}
return 0;
}
是的,您可以将项目转换为窗口应用程序。您有两个选择:
- 使用Windows(原生)API
- 使用图形框架
Windows API
WindowsAPI是创建windows的直接方法。但是,代码很多,注入缺陷的机会很多。这是了解窗口系统如何工作的很好的学习经验。得到 Petzold 的书。
GUI 框架
那里有很多 GUI frameworks。这些 C++ 框架使用面向对象的编程简化了 GUI 和 Widget 的创建。那里有很多,所以在互联网上搜索 "GUI Framework C++ review"。
不同的编程视角
在你现在的项目中,OS执行程序,语句按顺序执行。 windowing 系统基于 event driven 编程。总之,您的 GUI 正在等待事件发生。
您的项目的一个简单示例是带有单个按钮的 window。当用户单击按钮时,窗口系统 向按钮 event handler 发送消息 。事件处理程序是一个将执行您的代码的函数。
正如 Thomas 所说,是的,您可以将代码迁移到 Windows 应用程序,方法是使用 Win32 本机或使用 C++ GUI 框架(QT、wxWindows、.. .).
但是,您需要投入时间来学习其中一种解决方案。我建议学习C++ Framework,用低级Win32编程api今天不是很常用。
虽然这是题外话,但我会建议对您的代码进行一些改进。
首先,goto不要用,一会儿换掉。你可以替换
again:
xRan = rand() % 9999 + 1;
if (xRan <1)
goto again;
由
do{
xRan = rand() % 9999 + 1;
} while (xRan < 1);
请注意,在这种情况下,goto 或 while 是无用的,因为 xRan 将始终优于或等于 1(rand() 始终 returns 正值)
您还可以将非常长的函数 convert 和 reverseConv 替换为结构值的常量数组(结构包含一个 int 和一个 const char*)。 convert 和 reverseConvert 函数只会解析数组以找到合适的匹配项。
我想知道是否可以使用我现有的项目代码创建一个 window。这是一个学校项目。然而,我已经完成了实际的编码部分,只是想让项目更漂亮,可以这么说。在此先感谢大家的支持。
这是实际代码,以备不时之需。文章有点长,请注意:) 再次感谢您
#define NOMINMAX
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <windows.h>
#include <Windows.h>
#include <chrono>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int key[3][3];
double inverted[3][3];
int store[1][3] = { 0 };
int conv[666];
int random1()
{
unsigned long long int xRan;
srand(time(NULL));
xRan = rand() % 9999 + 1;
return xRan;
}
int random2()
{
unsigned long long int xRan;
xRan = rand() % 9999 + 1;
return xRan;
}
int random3()
{
int xRan;
xRan = rand() % 9999 + 1;
return xRan;
}
void clear_screen(char fill = ' ') {
COORD tl = { 0, 0 };
CONSOLE_SCREEN_BUFFER_INFO s;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(console, &s);
DWORD written, cells = s.dwSize.X * s.dwSize.Y;
FillConsoleOutputCharacter(console, fill, cells, tl, &written);
FillConsoleOutputAttribute(console, s.wAttributes, cells, tl, &written);
SetConsoleCursorPosition(console, tl);
}
int convert(char letter) {
int conv;
conv = (int)letter;
return conv;
}
void reverseMult(double inv[3][3], int decode[1][3])
{
store[0][0] = decode[0][0] * inv[0][0] + decode[0][1] * inv[1][0] + decode[0][2] * inv[2][0] + 0.5;
store[0][1] = decode[0][0] * inv[0][1] + decode[0][1] * inv[1][1] + decode[0][2] * inv[2][1] + 0.5;
store[0][2] = decode[0][0] * inv[0][2] + decode[0][1] * inv[1][2] + decode[0][2] * inv[2][2] + 0.5;
}
void matrixMult(int q, int w, int e, int a[3][3])
{
int A[1][3] = { q, w, e };
int B[3][3] = {
{ a[0][0], a[0][1], a[0][2] },
{ a[1][0], a[1][1], a[1][2] },
{ a[2][0], a[2][1], a[2][2] }
};
store[0][0] = A[0][0] * B[0][0] + A[0][1] * B[1][0] + A[0][2] * B[2][0];
store[0][1] = A[0][0] * B[0][1] + A[0][1] * B[1][1] + A[0][2] * B[2][1];
store[0][2] = A[0][0] * B[0][2] + A[0][1] * B[1][2] + A[0][2] * B[2][2];
//cout << store[0][0] << endl << store[0][1] << endl << store[0][2] << endl << endl;
}
char reverseConv(int x){
char conv;
conv = (char)x;
return conv;
}
void inverse(int key[3][3], double det){
int cofactor[3][3] = {
{ (key[1][1] * key[2][2] - key[1][2] * key[2][1]), -(key[1][0] * key[2][2] - key[1][2] * key[2][0]), (key[1][0] * key[2][1] - key[1][1] * key[2][0]) },
{ -(key[0][1] * key[2][2] - key[0][2] * key[2][1]), (key[0][0] * key[2][2] - key[0][2] * key[2][0]), -(key[0][0] * key[2][1] - key[0][1] * key[2][0]) },
{ (key[0][1] * key[1][2] - key[0][2] * key[1][1]), -(key[0][0] * key[1][2] - key[0][2] * key[1][0]), (key[0][0] * key[1][1] - key[0][1] * key[1][0]) }
};
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
inverted[i][j] =det * cofactor[i][j];
}
}
}
int main()
{
while (1){
cout << "Would you like to encrypt or decrypt?(e/d)\n " << endl;
string ende;
cin >> ende;
clear_screen();
if (ende == "e")
{
cout << "Please enter a name for the message: " << endl << endl;
string file;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, file);
clear_screen();
file += ".txt";
ofstream encrypt;
encrypt.open(file);
string message;
cout << "Please enter the message you would like to encrypt: " << endl << endl;
//cin.ignore(numeric_limits<streamsize>::max(), '\n'); --- Not needed anymore, uncomment if you cannot input message
getline(cin, message);
if (message.length() % 3 != 0)
message += ' ';
if (message.length() % 3 != 0)
message += ' ';
clear_screen();
for (int i = 0; i < message.length(); ++i)
{
conv[i] = convert(message[i]);
}
int det = 0;
while (1){
key[0][0] = random1(); //1
key[0][1] = random2(); //2
key[0][2] = random3(); //3
key[1][0] = random1() * 13 / 7; //4
key[1][1] = random2() * 23 / 7; //5
key[1][2] = random3() * 33 / 7; //6
key[2][0] = random1() * 18 / 15; //7
key[2][1] = random2() * 18 / 12; //8
key[2][2] = random3() * 18 / 10; //9
det = key[0][0] * key[1][1] * key[2][2] + key[0][1] * key[1][2] * key[2][0] + key[0][2] * key[1][0] * key[2][1]
- key[0][2] * key[1][1] * key[2][0] - key[0][0] * key[1][2] * key[2][1] - key[0][1] * key[1][0] * key[2][2];
if (det != 0)
break;
}
encrypt << key[0][0] << ' ' << key[0][1] << ' ' << key[0][2] << ' '
<< key[1][0] << ' ' << key[1][1] << ' ' << key[1][2] << ' '
<< key[2][0] << ' ' << key[2][1] << ' ' << key[2][2] << endl << endl;
int a, b, c;
int count = 0;
for (int i = 0; i < (message.length)() / 3; ++i)
{
int counting = 0;
a = conv[count];
count++;
b = conv[count];
count++;
c = conv[count];
count++;
matrixMult(a, b, c, key);
encrypt << store[0][counting] << ' ';
counting++;
encrypt << store[0][counting] << ' ';
counting++;
encrypt << store[0][counting] << endl;
}
encrypt.close();
Sleep(750);
cout << "Your message has been encrypted." << endl;
Sleep(750);
cout << "Please check " << file << " for the encrypted message and key" << endl << endl;
Sleep(750);
}
if (ende == "d")
{
cout << "Please enter the name of the file you would like to decrypt: " << endl << endl;
string file;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, file);
clear_screen();
file += ".txt";
ifstream decrypt;
decrypt.open(file);
int key[3][3];
for (int i = 0; i < 3; ++i)
{
for (int k = 0; k < 3; ++k){
decrypt >> key[k][i];
}
}
int det = 0;
det = key[0][0] * key[1][1] * key[2][2] + key[0][1] * key[1][2] * key[2][0] + key[0][2] * key[1][0] * key[2][1] - key[0][2] * key[1][1] * key[2][0] - key[0][0] * key[1][2] * key[2][1] - key[0][1] * key[1][0] * key[2][2];
double detInv = 1;
detInv /= det;
//double inv;
inverse(key, detInv);
int out[1][3];
int count = 0;
while (!decrypt.eof()){
for (int i = 0; i < 3; ++i)
{
decrypt >> out[0][i];
}
reverseMult(inverted, out);
count++;
char a, b, c;
a = reverseConv(store[0][0]);
b = reverseConv(store[0][1]);
c = reverseConv(store[0][2]);
if (decrypt.eof())
break;
cout << a << b << c;
}
}
cout << endl << endl;
cout << "Would you like to continue?(y/n) ";
char again;
cin >> again;
if (again != 'y')
exit(0);
clear_screen();
}
return 0;
}
是的,您可以将项目转换为窗口应用程序。您有两个选择:
- 使用Windows(原生)API
- 使用图形框架
Windows API
WindowsAPI是创建windows的直接方法。但是,代码很多,注入缺陷的机会很多。这是了解窗口系统如何工作的很好的学习经验。得到 Petzold 的书。
GUI 框架
那里有很多 GUI frameworks。这些 C++ 框架使用面向对象的编程简化了 GUI 和 Widget 的创建。那里有很多,所以在互联网上搜索 "GUI Framework C++ review"。
不同的编程视角
在你现在的项目中,OS执行程序,语句按顺序执行。 windowing 系统基于 event driven 编程。总之,您的 GUI 正在等待事件发生。
您的项目的一个简单示例是带有单个按钮的 window。当用户单击按钮时,窗口系统 向按钮 event handler 发送消息 。事件处理程序是一个将执行您的代码的函数。
正如 Thomas 所说,是的,您可以将代码迁移到 Windows 应用程序,方法是使用 Win32 本机或使用 C++ GUI 框架(QT、wxWindows、.. .).
但是,您需要投入时间来学习其中一种解决方案。我建议学习C++ Framework,用低级Win32编程api今天不是很常用。
虽然这是题外话,但我会建议对您的代码进行一些改进。
首先,goto不要用,一会儿换掉。你可以替换
again:
xRan = rand() % 9999 + 1;
if (xRan <1)
goto again;
由
do{
xRan = rand() % 9999 + 1;
} while (xRan < 1);
请注意,在这种情况下,goto 或 while 是无用的,因为 xRan 将始终优于或等于 1(rand() 始终 returns 正值)
您还可以将非常长的函数 convert 和 reverseConv 替换为结构值的常量数组(结构包含一个 int 和一个 const char*)。 convert 和 reverseConvert 函数只会解析数组以找到合适的匹配项。