0

Alright. I am making some decent progress here. I am trying to add a toolbar to my basic text editor that I am making using an internet tutorial. Anyways the problem is that I can get the bitmaps to display but they appear to be just that... bitmaps. When I click on them, the edit window overrides and I can type text in and just generally not interact with the buttons. Anyways, here's the main body of code, the menu and the header contents:

Main:

#include <windows.h>
#include <Commdlg.h>
#include <commctrl.h>
#include "Header.h"


const char g_szClassName[] = "myWindowClass";
HWND hEdit;

BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName)
{
    HANDLE hFile;
    BOOL bSuccess = FALSE;

    hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
        OPEN_EXISTING, 0, NULL);
    if(hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwFileSize;

        dwFileSize = GetFileSize(hFile, NULL);
        if(dwFileSize != 0xFFFFFFFF)
        {
            LPSTR pszFileText;

            pszFileText = (LPSTR) GlobalAlloc(GPTR, dwFileSize + 1);
            if(pszFileText != NULL)
            {
                DWORD dwRead;

                if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
                {
                    pszFileText[dwFileSize] = 0; // Add null terminator
                    if(SetDlgItemText(hEdit, IDC_MAIN_EDIT,  pszFileText))
                        bSuccess = TRUE; // It worked!
                }
                GlobalFree(pszFileText);
            }
        }
        CloseHandle(hFile);
    }
    return bSuccess;
}

BOOL SaveTextFileFromEdit(HWND hEdit, LPCTSTR pszFileName)
{
    HANDLE hFile;
    BOOL bSuccess = FALSE;

    hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if(hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwTextLength;

        dwTextLength = GetWindowTextLength(GetDlgItem(hEdit, IDC_MAIN_EDIT));
        // No need to bother if there's no text.
        if(dwTextLength > 0)
        {
            LPSTR pszText;
            DWORD dwBufferSize = dwTextLength + 1;

            pszText = (LPSTR) GlobalAlloc(GPTR, dwBufferSize);
            if(pszText != NULL)
            {
                if(GetWindowText(GetDlgItem(hEdit, IDC_MAIN_EDIT), pszText, dwBufferSize))
                {
                    DWORD dwWritten;

                    if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                        bSuccess = TRUE;
                }
                GlobalFree(pszText);
            }
        }
        CloseHandle(hFile);
    }
    return bSuccess;
}

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE:
            {
                HWND hEdit;

                hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);

                if(hEdit == NULL)
                {
                    MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
                }
                SendMessage(hEdit, WM_SETFONT, (WPARAM)NULL, MAKELPARAM(FALSE, 0));
            }
            break;
        case WM_SIZE:
            {
                HWND hEdit;
                RECT rcClient;

                GetClientRect(hwnd, &rcClient);

                hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
                SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
            }
            break;
        case WM_CLOSE:
            switch (MessageBox (hwnd, "Do you want to save first?", "Wait Up", MB_YESNOCANCEL | MB_DEFBUTTON3 | MB_ICONQUESTION))
            {
            case IDYES:
                {
                    OPENFILENAME ofn;
                    char szFileName[MAX_PATH] = "";

                    ZeroMemory(&ofn, sizeof(ofn));

                    ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
                    ofn.hwndOwner = hwnd;
                    ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
                    ofn.lpstrFile = szFileName;
                    ofn.nMaxFile = MAX_PATH;
                    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
                    ofn.lpstrDefExt = "txt";

                    if(GetOpenFileName(&ofn))
                    {
                        switch (LoadTextFileToEdit(hwnd, szFileName))
                        {
                            case IDCANCEL:
                                {}break;
                            default:
                                DestroyWindow(hwnd);
                                break;
                        }
                    }
                }
                break;
            case IDNO:
                DestroyWindow(hwnd);
                break;
            case IDCANCEL:
                break;
            }
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case ID_FILE_OPEN:
                    {
                        OPENFILENAME ofn;
                        char szFileName[MAX_PATH] = "";

                        ZeroMemory(&ofn, sizeof(ofn));

                        ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
                        ofn.hwndOwner = hwnd;
                        ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
                        ofn.lpstrFile = szFileName;
                        ofn.nMaxFile = MAX_PATH;
                        ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
                        ofn.lpstrDefExt = "txt";

                        if(GetOpenFileName(&ofn))
                        {
                            LoadTextFileToEdit(hwnd, szFileName);
                        }
                    }
                    break;
                case ID_FILE_SAVE:
                    {

                    }
                    break;
                case ID_FILE_SAVEAS:
                    {
                        OPENFILENAME ofn;
                        char szFileName[MAX_PATH] = "";

                        ZeroMemory(&ofn, sizeof(ofn));

                        ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
                        ofn.hwndOwner = hwnd;
                        ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
                        ofn.lpstrFile = szFileName;
                        ofn.nMaxFile = MAX_PATH;
                        ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
                        ofn.lpstrDefExt = "txt";

                        if(GetSaveFileName(&ofn))
                        {
                            SaveTextFileFromEdit(hwnd, szFileName);
                        }
                    }
                    break;
                case ID_FILE_EXIT:
                    switch (MessageBox (hwnd, "Do you want to save first?", "Wait Up", MB_YESNOCANCEL | MB_DEFBUTTON3 | MB_ICONQUESTION))
                    {
                    case IDYES:
                        {
                            OPENFILENAME ofn;
                            char szFileName[MAX_PATH] = "";

                            ZeroMemory(&ofn, sizeof(ofn));

                            ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
                            ofn.hwndOwner = hwnd;
                            ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
                            ofn.lpstrFile = szFileName;
                            ofn.nMaxFile = MAX_PATH;
                            ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
                            ofn.lpstrDefExt = "txt";

                            if(GetOpenFileName(&ofn))
                            {
                                switch (LoadTextFileToEdit(hwnd, szFileName))
                                {
                                    case IDCANCEL:
                                        {}break;
                                    default:
                                        DestroyWindow(hwnd);
                                        break;
                                }
                            }
                        }
                        break;
                    case IDNO:
                        DestroyWindow(hwnd);
                        break;
                    case IDCANCEL:
                        break;
                    }
            }
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //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+1);
    wc.lpszMenuName  = MAKEINTRESOURCE(ID_EDITOR_MENU);
    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
    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "Generic Text Editor", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 750, 500, NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    INITCOMMONCONTROLSEX InitCtrlEx;

    InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
    InitCtrlEx.dwICC  = ICC_BAR_CLASSES; ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    HWND hTool = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU)ID_MAIN_TOOL, GetModuleHandle(NULL), NULL);
         SendMessage(hTool, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);

    TBBUTTON tbb[3];
    TBADDBITMAP tbab;

    tbab.hInst = HINST_COMMCTRL;
    tbab.nID = IDB_STD_SMALL_COLOR;
    SendMessage(hTool, TB_ADDBITMAP, 0, (LPARAM)&tbab);

    ZeroMemory(tbb, sizeof(tbb));
    tbb[0].iBitmap = STD_FILENEW;
    tbb[0].fsState = TBSTATE_ENABLED;
    tbb[0].fsStyle = TBSTYLE_BUTTON;
    tbb[0].idCommand = ID_FILE_NEW;

    tbb[1].iBitmap = STD_FILEOPEN;
    tbb[1].fsState = TBSTATE_ENABLED;
    tbb[1].fsStyle = TBSTYLE_BUTTON;
    tbb[1].idCommand = ID_FILE_OPEN;

    tbb[2].iBitmap = STD_FILESAVE;
    tbb[2].fsState = TBSTATE_ENABLED;
    tbb[2].fsStyle = TBSTYLE_BUTTON;
    tbb[2].idCommand = ID_FILE_SAVEAS;

    SendMessage(hTool, TB_ADDBUTTONS, sizeof(tbb)/sizeof(TBBUTTON), (LPARAM)&tbb);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

Header:

#define IDC_MAIN_EDIT 100

#define ID_EDITOR_MENU 1000

#define ID_FILE_NEW 1010
#define ID_FILE_OPEN 1011
#define ID_FILE_SAVE 1012
#define ID_FILE_SAVEAS 1013
#define ID_FILE_EXIT 1014

#define ID_EDIT_UNDO 1020
#define ID_EDIT_REDO 1021
#define ID_EDIT_CUT 1022
#define ID_EDIT_COPY 1023
#define ID_EDIT_PASTE 1024
#define ID_EDIT_FIND 1025
#define ID_EDIT_SELALL 1026

#define ID_FORMAT_WWRAP 1030
#define ID_FORMAT_STATBAR 1031
#define ID_FORMAT_HOTKEY 1032

#define ID_VIEW_ZOOM 1040

#define ID_HELP_CONTROLS 1050
#define ID_HELP_FAQ 1051

#define ID_MAIN_TOOL 2000

Menu:

#include "Header.h"

ID_EDITOR_MENU MENU
{
    POPUP "File"
    {
        MENUITEM "New", ID_FILE_NEW
        MENUITEM "Open", ID_FILE_OPEN
        MENUITEM "Save", ID_FILE_SAVE
        MENUITEM "Save As", ID_FILE_SAVEAS
        MENUITEM "Exit", ID_FILE_EXIT
    }
    POPUP "Edit"
    {
        MENUITEM "Undo", ID_EDIT_UNDO
        MENUITEM "Redo", ID_EDIT_REDO
        MENUITEM "Cut", ID_EDIT_CUT
        MENUITEM "Copy", ID_EDIT_COPY
        MENUITEM "Paste", ID_EDIT_PASTE
        MENUITEM "Find", ID_EDIT_FIND
        MENUITEM "Select All", ID_EDIT_SELALL
    }
    POPUP "Format"
    {
        MENUITEM "Word Wrap", ID_FORMAT_WWRAP
        MENUITEM "Status Bar", ID_FORMAT_STATBAR
        MENUITEM "Hot Keys", ID_FORMAT_HOTKEY
    }
    POPUP "View"
    {
        MENUITEM "Zoom", ID_VIEW_ZOOM
    }
    POPUP "Help"
    {
        MENUITEM "Controls", ID_HELP_CONTROLS
        MENUITEM "FAQ", ID_HELP_FAQ
    }
}

Thanks ahead of time to anyone who reads and thanks again to anyone who can help.

1
Contributor
3
Replies
4
Views
4 Years
Discussion Span
Last Post by Some Dude
0

Figured out that the toolbar is functional but it would seem that the edit control is taking its space and recieving a higher priority... hmmmm....

0

I'm still wondering/trying to figure out how to make the toolbar coexist with the edit window.

0

Well.... It was incredibly simple... but snnneeeeaaaky. It didn't occur to me to resize the window and set it's orientation lower.

First of - I added this to case WM_SIZE: so the toolbar would resize too.

RECT rcTool;
GetClientRect(hwnd, &rcTool);
hEdit = GetDlgItem(hwnd, ID_MAIN_TOOL);
SetWindowPos(hTool, NULL, 0, 0, rcTool.right, rcTool.bottom, SWP_NOZORDER);

This is also the same place to fix the edit window's orientation and size because it apparently supersedes that of WM_CREATE.

In hEdit’s RECT I figured the number 30 by trial and eroor... I also used a variable instead of 30 but there's no real need for that at this point.

SetWindowPos(hEdit, NULL, 0, 30, rcEdit.right, rcEdit.bottom - 30, SWP_NOZORDER);

Sorry if there are any syntax errors in this and that but I think I got it and it worked for me so yea... solved.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.