When clicking button, it triggers an action but when I click on the "About" or "Exit" buttons it does execute the same action

Ahmed Osama 120 Reputation points
2023-02-27T13:02:28.81+00:00

Here's my code:

// WindowsProject1.cpp : Defines the entry point for the application.
//
#include <windows.h>
#include <shellapi.h>
#include "framework.h"
#include "WindowsProject1.h"
#include <string>
#include <commctrl.h>

#define MAX_LOADSTRING 100

LPWSTR clsn[MAX_PATH];
char fin[64300];
char scin[64300];
HWND hWndEdit;
HWND hWndEdit2;


// Global Variables:
HINSTANCE hInst;                                // current instance
WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_WINDOWSPROJECT1, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINDOWSPROJECT1));

    MSG msg;
    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}


//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINDOWSPROJECT1));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_WINDOWSPROJECT1);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}
//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    hInst = hInstance; // Store instance handle in our global variable

    HWND hWnd;

    if (!(hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, NULL, NULL, hInstance, NULL)))
    {
        MessageBox(NULL, TEXT("CreateWindow Failed!"), TEXT("Error"), MB_ICONERROR);
        return EXIT_FAILURE;
    }

    HFONT font = CreateFont(14, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
        OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
        DEFAULT_PITCH | FF_DONTCARE, TEXT("Segoe UI"));

    hWndEdit = CreateWindowExW(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT(""),
        WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL, 410, 180, 180,
        20, hWnd, NULL, NULL, NULL);

    SendMessage(hWndEdit, WM_SETFONT, (LPARAM)font, TRUE);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    hWndEdit2 = CreateWindowExW(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT(""),
        WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL, 410, 200, 180,
        20, hWnd, NULL, NULL, NULL);

    SendMessage(hWndEdit2, WM_SETFONT, (LPARAM)font, TRUE);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);


    HWND hWndButton = CreateWindowExW(WS_EX_WINDOWEDGE, TEXT("Button"), TEXT("Run as administator"),
        WS_CHILD | WS_VISIBLE | BS_TEXT, 440, 220, 120,
        20, hWnd, NULL, NULL, NULL);

    SendMessage(hWndButton, WM_SETFONT, (LPARAM)font, TRUE);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);
   
    HWND hWndLabel = CreateWindowExW(WS_EX_LTRREADING, TEXT("Static"), TEXT("Application: "),
        WS_CHILD | WS_VISIBLE, 338, 180, 65,
        20, hWnd, NULL, NULL, NULL);

    SendMessage(hWndLabel, WM_SETFONT, (LPARAM)font, TRUE);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    HWND hWndLabel2 = CreateWindowExW(WS_EX_LTRREADING, TEXT("Static"), TEXT("Parameters: "),
        WS_CHILD | WS_VISIBLE, 338, 200, 65,
        20, hWnd, NULL, NULL, NULL);

    SendMessage(hWndLabel2, WM_SETFONT, (LPARAM)font, TRUE);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    return TRUE;
}


//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE: Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
    {
        if (HIWORD(wParam) == BN_CLICKED) {
            GetWindowTextA(hWndEdit, &fin[0], 64300);
            GetWindowTextA(hWndEdit2, &scin[0], 64300);
            if (scin == (LPSTR)"") {
                ShellExecuteA(NULL, "runas", &fin[0], NULL, NULL, SW_SHOWNORMAL);
            }
            else {
                ShellExecuteA(NULL, "runas", &fin[0], &scin[0], NULL, SW_SHOWNORMAL);
            }
            if (GetLastError() != 0) {
                int i{};
                char hexstr[_MAX_U64TOSTR_BASE2_COUNT];
                int mx;
                LPSTR messageBuffer = (LPSTR)"";
                size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                    NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), messageBuffer, 0, NULL);

                std::string message(messageBuffer, size);
                _itoa_s((int)GetLastError(), hexstr, _countof(hexstr), 16);
                _TASKDIALOGCONFIG tdc = { sizeof(_TASKDIALOGCONFIG) };
                TASKDIALOG_BUTTON buttons[] = {
             { 1000, L"&Accept" },
             { 1001, L"&Close" }
                };
                LPCWSTR wndTitle = szTitle;
                LPCWSTR szHeader = szTitle + (WCHAR)"encountered a fatal error";
                LPCWSTR szBodyText = (LPCWSTR)"An error has occured, verify input to make sure it's correct";
                tdc.pszExpandedInformation = (PCWSTR)"Code: \n" + (WCHAR)"0x" + (WCHAR)hexstr + (WCHAR)"\n Description: " + (WCHAR)message.c_str();
                tdc.pszMainIcon = (PCWSTR)"IDI_ASTRICK";
                tdc.hwndParent = hWnd;
                tdc.dwFlags = TDF_CAN_BE_MINIMIZED | TDF_EXPAND_FOOTER_AREA | TDF_USE_HICON_MAIN | TDF_SIZE_TO_CONTENT;
                tdc.cButtons = 2;
                tdc.pButtons = buttons;
                tdc.pszWindowTitle = wndTitle;
                tdc.pszMainInstruction = szHeader;
                tdc.pszContent = szBodyText;
            }

        }
        int wmId = LOWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
    }
        break;
    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code that uses hdc here...

        RECT rect = { 385, 205, 385, 205 };
        SetTextColor(hdc, RGB(0, 0, 0));
        SetBkMode(hdc, TRANSPARENT);
        SetBkColor(hdc, RGB(255, 255, 255)); // white
        DrawTextW(hdc, L"Application", -1, &rect, DT_LEFT);

        RECT rect2 = { 385, 225, 385, 205 };
        SetTextColor(hdc, RGB(0, 0, 0));
        SetBkMode(hdc, TRANSPARENT);
        SetBkColor(hdc, RGB(255, 255, 255)); // white
        DrawTextW(hdc, L"Parameters", -1, &rect2, DT_LEFT);

        EndPaint(hWnd, &ps);
    }
    break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
    }


// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

My problem is when you press a menu item, which is handled by this block:

 int wmId = LOWORD(wParam);

        // Parse the menu selections:

        switch (wmId)

        {

        case IDM_ABOUT:

            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);

            break;

        case IDM_EXIT:

            DestroyWindow(hWnd);

            break;

        default:

            return DefWindowProc(hWnd, message, wParam, lParam);

        }

    }

        break;

It executes this block:

    switch (message)

    {

    case WM_COMMAND:

    {

        if (HIWORD(wParam) == BN_CLICKED) {

            GetWindowTextA(hWndEdit, &fin[0], 64300);

            GetWindowTextA(hWndEdit2, &scin[0], 64300);

            if (scin == (LPSTR)"") {

                ShellExecuteA(NULL, "runas", &fin[0], NULL, NULL, SW_SHOWNORMAL);

            }

            else {

                ShellExecuteA(NULL, "runas", &fin[0], &scin[0], NULL, SW_SHOWNORMAL);

            }

            if (GetLastError() != 0) {

                int i{};

                char hexstr[_MAX_U64TOSTR_BASE2_COUNT];

                int mx;

                LPSTR messageBuffer = (LPSTR)"";

                size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,

                    NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), messageBuffer, 0, NULL);



                std::string message(messageBuffer, size);

                _itoa_s((int)GetLastError(), hexstr, _countof(hexstr), 16);

                _TASKDIALOGCONFIG tdc = { sizeof(_TASKDIALOGCONFIG) };

                TASKDIALOG_BUTTON buttons[] = {

             { 1000, L"&Accept" },

             { 1001, L"&Close" }

                };

                LPCWSTR wndTitle = szTitle;

                LPCWSTR szHeader = szTitle + (WCHAR)"encountered a fatal error";

                LPCWSTR szBodyText = (LPCWSTR)"An error has occured, verify input to make sure it's correct";

                tdc.pszExpandedInformation = (PCWSTR)"Code: \n" + (WCHAR)"0x" + (WCHAR)hexstr + (WCHAR)"\n Description: " + (WCHAR)message.c_str();

                tdc.pszMainIcon = (PCWSTR)"IDI_ASTRICK";

                tdc.hwndParent = hWnd;

                tdc.dwFlags = TDF_CAN_BE_MINIMIZED | TDF_EXPAND_FOOTER_AREA | TDF_USE_HICON_MAIN | TDF_SIZE_TO_CONTENT;

                tdc.cButtons = 2;

                tdc.pButtons = buttons;

                tdc.pszWindowTitle = wndTitle;

                tdc.pszMainInstruction = szHeader;

                tdc.pszContent = szBodyText;

            }



        }

This app runs an app as adminsitator, so it executes that code when I press About or Exit, too.

How I prevent this action and make the button action only execute when the button is clicked and not anything else?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,441 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,553 questions
{count} votes