Another annoying thread

This is a discussion on Another annoying thread within the Windows Programming forums, part of the Platform Specific Boards category; Sorry, I realize I posted very similar code a few days ago, but I have another question about it. Code: ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Another annoying thread

    Sorry, I realize I posted very similar code a few days ago, but I have another question about it.

    Code:
    // Windows.cpp
    
    #include <windows.h>
    #include "Controls.h"
    
    const char g_szClassName[] = "WindowClass";
    HWND hWindowHandle;
    
    char STSUsername[256];  // STS = String to store
    char STSPassword[256];
    
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////// Control1 procedure //////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    
    BOOL CALLBACK Control1DlgProc(HWND hControl1Handle, UINT Message1, WPARAM wParam, LPARAM lParam)
    {
        switch(Message1)
        {
            case WM_INITDIALOG:
    
                return TRUE;
            
                case WM_COMMAND:
                
                    switch(LOWORD(wParam))
                    {
                        case IDYES:
                            EndDialog(hControl1Handle, IDYES);
                            break;
                        
                        case IDNO:
                            EndDialog(hControl1Handle, IDNO);
                            break;
                            
                        default:
                            EndDialog(hControl1Handle, IDNO);
                            break;
                    }
            
                break;
            
            default:
                return FALSE;
        }
        
        return TRUE;
    }
    
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////// Control2 procedure //////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    
    BOOL CALLBACK Control2DlgProc(HWND hControl2Handle, UINT Message2, WPARAM wParam, LPARAM lParam)
    {
        switch(Message2)
        {
            case WM_INITDIALOG:
                
                SetDlgItemText(hControl2Handle, IDC_Username, "Administrator");
                
                case WM_COMMAND:
                    
                    switch(LOWORD(wParam))
                    {
                        case IDOK:
                            
                            GetDlgItemText(hControl2Handle,IDC_Username,STSUsername,200);
                            GetDlgItemText(hControl2Handle,IDC_Password,STSPassword,200);
                            
                            EndDialog(hControl2Handle, IDOK);
                            break;
                        
                        case IDCANCEL:
                            EndDialog(hControl2Handle, IDCANCEL);
                            break;
                        
                        default:
                            EndDialog(hControl2Handle, IDCANCEL);
                    }
                
                break;
                
        default:
            return FALSE;
                
        }
        
        return TRUE;
    }    
                
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    ///////////////////////// Step 4: the Window Procedure /////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    
    LRESULT CALLBACK WndProc(HWND hMainProcHandle, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CREATE:
                
                int Control1_Result;
                
                Control1_Result = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_Control1), hMainProcHandle, Control1DlgProc);
                
                if(Control1_Result == -1)
                {
                    MessageBox(hWindowHandle, "Dialog failed!", "Error", MB_OK | MB_ICONINFORMATION);
                    PostQuitMessage(0);
                }    
                
                else if (Control1_Result == IDYES)
                {
                    ////////////////////////////////////////////////////////////////
                    ////////////////////////////////////////////////////////////////
                    ////////////////////////////////////////////////////////////////
                    
                    int Control2_Result;
                    
                    Control2_Result = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_Control2), hMainProcHandle, Control2DlgProc);
                    
                    while (Control2_Result != IDCANCEL && (STSUsername != "Username" && STSPassword != "Password"))
                    {
                        MessageBox(hWindowHandle, "     Invlaid username or password.                   \n               Please try again.", "Windows Application", MB_ICONEXCLAMATION | MB_OK);
                        Control2_Result = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_Control2), hMainProcHandle, Control2DlgProc);
                    }    
                    
                    PostQuitMessage(0);
                    
                    ////////////////////////////////////////////////////////////////
                    ////////////////////////////////////////////////////////////////
                    ////////////////////////////////////////////////////////////////
                }
                
                else
                {
                    PostQuitMessage(0);
                }    
                
                break;
            
            case WM_CLOSE:
                DestroyWindow(hMainProcHandle);
                break;
                
            case WM_DESTROY:
                PostQuitMessage(0);
                break;
                
            default:
                return DefWindowProc(hMainProcHandle, msg, wParam, lParam);
        }
        
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        MSG MainMessage;
    
        //Step 1: Registering the Window Class
        
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 11);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        
        hWindowHandle = CreateWindowEx(
            WS_EX_CLIENTEDGE | WS_EX_TOPMOST,
            g_szClassName,
            "Windows Application",
            WS_DLGFRAME,
            0, 0, 240, 240,
            NULL, NULL, hInstance, NULL);
    
        if(hWindowHandle == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
        
        // Step 3: The Message Loop
        
        while(GetMessage(&MainMessage, NULL, 0, 0) > 0)
        {
            TranslateMessage(&MainMessage);
            DispatchMessage(&MainMessage);
        }
        
        return MainMessage.wParam;
    }
    Code:
    // Controls.rc
    
    #include <afxres.h>
    #include "Controls.h"
    
    IDD_Control1 DIALOG DISCARDABLE  0, 0, 240, 65
    
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU | DS_CENTER
    CAPTION "Windows Application"
    FONT 8, "MS Sans Serif"
    
    BEGIN
        
        DEFPUSHBUTTON   "&Yes", IDYES, 174, 18, 50, 14
        PUSHBUTTON      "&No", IDNO, 174, 35, 50, 14
        GROUPBOX        "Login", IDC_STATIC, 7, 7, 225 ,52
        CTEXT           "Do you wish to login to Windows Application?", IDC_STATIC, 16, 26, 120, 20
        
    END
    
    ////////////////////////////////////////////////////////////////////////////////
    
    IDD_Control2 DIALOG DISCARDABLE 0, 0, 150, 150
    
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU | DS_CENTER
    CAPTION "Windows Application"
    FONT 8, "MS Sans Serif"
    
    BEGIN
    
        GROUPBOX    "Login", IDC_STATIC, 7, 7, 136, 136
        LTEXT       "Username:", IDC_STATIC, 16, 32, 33, 7
        LTEXT       "Password:", IDC_STATIC, 16, 45, 33, 7
        DEFPUSHBUTTON  "OK", IDOK, 28, 121, 50, 14
        PUSHBUTTON  "Cancel", IDCANCEL, 85, 121, 50, 14
        
        EDITTEXT    IDC_Username, 56, 32, 80, 8, ES_AUTOHSCROLL
        EDITTEXT    IDC_Password, 56, 45, 80, 8, ES_AUTOHSCROLL | ES_PASSWORD
        
    END
    And a header that works and shouldn't have anything to do with my problem. Oh, by the way, here it is: Whenever I click on either the username or password edit boxes, the dialog closes. Is this meant to happen? Is there some code that needs to be added to prevent this from happening? I've looked through a few examples, and in them, it only mentioned the edit boxes in the .rc file, so I did the same. I'm on XP Pro, and using Dev-C++ if it matters.

    Thanks in advance for any future replies.
    Necrofear

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Remove the default switch and corresponding EndDialog statements from the WM_COMMAND handler for Control2DlgProc; it's unlikely the modal dialog will even be created with that there. Even if the dialog box is created then just about anything that fires that WM_COMMAND message will result in the destruction of that dialog box.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Awesome

    Thanks a million for that suggestion, 'cause even though I haven't got access to a compiler at the moment, I see no reason for that not to work! Cheers

    Oh, one more question though. I've looked at a few of these login windows in other programs, and there's always a "remember my password" checkbox. I'm confused as to how these work. What does it do? Encrypt it then save it in a txt file or something? I require no code, just any knowlage anyone could provide.

    (Though having said that: knowing how, unlucky/inexpierienced at programming, I am, chances are that it won't work as I've made some stupid error, and there will be yet another idiotic post by me on these message boards. So any examples are more than welcome if you feel like it.)

    Thanks again Ken, and thanks for any helpful replies!
    Necrofear.

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Why encrypt it if all one has to do is open the program?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Eh?

    I'm a bit confused.

    Surely, if they open the program, it will just appear as stars. They cannot read this over say someones shoulder, so they do not know the password. I said encrypt it so then they can't just open my documents and read it. Plus, if they don't have access to the program, they can't even run it. If however they do have access to it, then the admins or whatever wouldn't be stupid enough as to click remember my password. That's my theory anyway. Please let me know if there are any loop holes in it.

    Cheers

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    There are programs out there that can see through the stars, astericks, circles, whatever.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    So what do these remember password checkboxes do then?
    Cheers Necrofear

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    To prevent shoulder-surfing. I'd need to have you leave the computer, me boot up the program, run it, find the password, maybe write it down, then exit.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-26-2009, 10:48 PM
  2. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 04:14 AM
  3. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  4. Shared memory implementation using thread
    By kumars in forum C Programming
    Replies: 5
    Last Post: 06-18-2008, 04:24 AM
  5. Replies: 12
    Last Post: 05-17-2003, 05:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21