Jump to content

My GUI app is not working correctly..

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
BlueMelon

BlueMelon

    Newbie

  • Members
  • PipPip
  • 28 posts
Watch the 2 vids.

Vid1:
Attached File  472335..gif   22.73K   55 downloads

Vid2:
Attached File  472334..gif   70.9K   63 downloads


As you can see, Vid1 IS infact working completely great. But Vid2 is only taking the first 3 chars... Weird.
Could anyone help me out? I am trying to make the console APP, in GUI form.
(This is NOT homework, its just personnel fun)

Vid1:
#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>

using namespace std;
char str[255];
int many;


ofstream file ("Permuted.txt");

void swap(char* first, char* second)
{
        char ch = *second;
        *second = *first;
        *first = ch;
}

int permute(char* set, int begin, int end)
{
        int i;
        int range = end - begin;
        if (range == 1) {
            
            
                file<<set<<endl;
                cout << set << endl;
        } else {
                for(i=0; i<range; i++) {
                        swap(&set[begin], &set[begin+i]);               //initial swap
                        permute(set, begin+1, end);                             //recursion
                        swap(&set[begin], &set[begin+i]);       //swap back
                }
        }
        return 0;
}



int main()
{
    SetConsoleTitleA("Permutation - BlueMelon");
    std::cout << "######################################################\n";
    std::cout << "# Instructions:                                      #\n";
    std::cout << "#      Enter a set of letters or numbers to get all  #\n";
    std::cout << "#      possible combinations                         #\n";
    std::cout << "######################################################\n\n";
    
        

        cout << "Please enter set: ";
        cin.getline(str, 255); //take string
        
        

        cout<< "\n.txt File will be saved where you ran the program.\n";
        system("PAUSE");
        permute(str, 0, strlen(str)); //permute the string
        cout<<"\nDone!\n";
        system("PAUSE");
        file.close();
        return 0;
}
Vid2:
#include <windows.h>
#include <iostream>
#include <string>

//Windows Theme/////////
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
///////////////////////
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);



LPCWSTR Form_Title = L"Permutation - BlueMelon"; // Form Title

using namespace std;

HWND    LABEL1;
HWND    TXTBOX1;
HWND    TXTBOX2;
HWND    PERMUTEBTN;

#define LBL1 99
#define TXT1 100
#define TXT2 101
#define BTNPERMUTE 102

int permute(char* set, int begin, int end);
void swap(char* first, char* second);

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
{
    WNDCLASSEXW wClass;
    ZeroMemory(&wClass,sizeof(WNDCLASSEXW));

    wClass.cbClsExtra = NULL;
    wClass.cbSize = sizeof(WNDCLASSEXW);
    wClass.cbWndExtra = NULL;
    wClass.hbrBackground =(HBRUSH)COLOR_WINDOW;
    wClass.hCursor = LoadCursor(NULL,IDC_ARROW);
    //wClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    //wClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wClass.hInstance = hInst;
    wClass.lpfnWndProc = (WNDPROC)WinProc;
    wClass.lpszClassName = L"Window Class";
    wClass.lpszMenuName = NULL;
    wClass.style = CS_HREDRAW|CS_VREDRAW;

    if(!RegisterClassEx(&wClass))
    {
        int nResult=GetLastError();
        MessageBoxA(NULL,"Window class creation failed","Window Class Failed",MB_ICONERROR);
        return 0;
    }

    HWND hWnd = CreateWindowExW(NULL, // Creating your window
                L"Window Class",
                Form_Title, 
                WS_VISIBLE|WS_OVERLAPPED|WS_SYSMENU,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                500,/*width*/
                500,/*height*/
                NULL,
                NULL,
                hInst,
                NULL);

    if(!hWnd)
    {
        MessageBoxA(NULL,"Window creation failed","Window Creation Failed",MB_ICONERROR);
        return 0;
    }

    ShowWindow(hWnd,nShowCmd);

    MSG msg;
    ZeroMemory(&msg,sizeof(MSG));

    TXTBOX1 = CreateWindowExW(WS_EX_CLIENTEDGE,
            L"EDIT",
            L"",
            WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL|WS_HSCROLL|WS_VSCROLL,
            3,//x
            20,//y
            479,//width
            160,//height
            hWnd,
            (HMENU)TXT1,
            GetModuleHandle(NULL),
            NULL);

    LABEL1 = CreateWindowExW(NULL,
            L"Static",
            L"Enter String to be Permuted: ",
            WS_CHILD | WS_VISIBLE ,
            1, 
            1,
            190, 
            16,
            hWnd,
            (HMENU)LBL1,
            GetModuleHandle(NULL),
            NULL);

    PERMUTEBTN = CreateWindowExW(NULL, 
            L"BUTTON",
            L"Permute",
            WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
            3,/*x*/
            182,/*y*/
            100,/*width*/
            24,/*height*/
            hWnd,
            (HMENU)BTNPERMUTE,
            GetModuleHandle(NULL),
            NULL);
    TXTBOX2 = CreateWindowExW(WS_EX_CLIENTEDGE,
            L"EDIT",
            L"",
            WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL|WS_HSCROLL|WS_VSCROLL,
            3,//x
            208,//y
            479,//width
            160,//height
            hWnd,
            (HMENU)TXT2,
            GetModuleHandle(NULL),
            NULL);



    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg); 
    }

    return 0;
}



LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
    switch(msg) // msg loop
    {
        case WM_COMMAND:
            {
                switch(LOWORD(wParam)) 
                    {
                        case BTNPERMUTE:
                            {

                                int length = GetWindowTextLengthA(TXTBOX1);
                                char *text = (char *) malloc(sizeof(char) * length+1);

                                if (GetWindowTextA(TXTBOX2,text,sizeof(text)) != NULL)
                                {
                                    SetWindowTextA(TXTBOX2,NULL);
                                }

                                GetWindowTextA(TXTBOX1,text,sizeof(text));
                                permute(text, 0, strlen(text));
                            }
                

                    default:{break;}
                    }
                break;
        
                

            case WM_DESTROY: // When user exists app
                {
                    PostQuitMessage(0);
                    return 0;
                }
                break;
                
            }
    }

    return DefWindowProc(hWnd,msg,wParam,lParam);
}

void swap(char* first, char* second)
{
        char ch = *second;
        *second = *first;
        *first = ch;
}

int permute(char* set, int begin, int end)
{
        
        int range = end - begin;
        if (range == 1) {
                
            
            char temptext[100000]; // Will fix the [100000] Later.
            GetWindowTextA(TXTBOX2,temptext,sizeof(temptext));

            if(GetWindowTextA(TXTBOX2,temptext,sizeof(temptext)) != NULL)
            {
            
            //MessageBoxA(NULL,set,"Window Creation Failed",MB_ICONERROR);
            strcat(temptext,"\r\n");
            strcat(temptext,set);
            
            SetWindowTextA(TXTBOX2,temptext);
            }else
            {
            strcat(temptext,set);
            SetWindowTextA(TXTBOX2,temptext);
            }
            

        } else {
                for(int i=0; i<range; i++) {
                        swap(&set[begin], &set[begin+i]);               //initial swap
                        permute(set, begin+1, end);                             //recursion
                        swap(&set[begin], &set[begin+i]);       //swap back
                }
        }
        return 0;
}
Posted Image

#2
BlueMelon

BlueMelon

    Newbie

  • Members
  • PipPip
  • 28 posts
Fixed simple error, Thanks moudi.

Reason: I was getting size without knowing there was a null terminator.. All I had to do was +1 to it.

Thanks Again Moudi.

Edit: Now it only works for 4 chars...

Edit 2: All working now, just removed the memory allocation. No need for replys, thanks.
If someone could close thread, that would be nice.

Edited by BlueMelon, 26 March 2010 - 08:28 AM.