我可以使用现有的项目代码在c ++中创建一个窗口

时间:2015-03-05 15:29:34

标签: c++ winapi

我想知道我是否可以使用现有的项目代码创建一个窗口。这是一个学校项目。但是,我已经完成了实际的编码部分,只是想让项目更加精彩,所以说。非常感谢您提供所有支持。

以下是实际代码,以防有任何帮助。这有点长,所以要警告:)再次,谢谢你提前

#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;
} 

2 个答案:

答案 0 :(得分:2)

是的,您可以将项目转换为Windowing应用程序。你有两个选择:

  • 使用Windows (原生) API
  • 使用图形框架

Windows API

Windows API是创建窗口的直接方法。但是,它有很多代码,很多机会可以注入缺陷。关于Windowing系统如何工作,这是一个很好的学习经验。获得Petzold的书。

GUI Framework

那里有很多GUI frameworks。这些C ++框架使用面向对象的编程简化了GUI和Widget的创建。那里有很多,所以在互联网上搜索&#34; GUI Framework C ++ review&#34;。

不同的编程视角

在您当前的项目中,操作系统执行程序并按顺序执行语句。窗口系统基于event driven编程。总之,您的GUI正在等待事件发生。

项目的一个简单示例是一个带有单个按钮的窗口。当用户点击按钮时,窗口系统会向按钮event handler发送消息。事件处理程序是一个执行代码的函数。

答案 1 :(得分:0)

正如托马斯所说,是的,您可以将代码迁移到Windows应用程序,无论是使用Win32本地还是使用C ++ GUI Framework(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()始终返回正值)

此外,您可以通过常量的struct值数组替换convert和reverseConv非常长的函数(struct包含int和const char *)。 convert和reverseConvert函数只会解析数组以找到正确的匹配。