CA1K Productions
CA1K Productions

Reputation: 47

C++ Win32 not retrieving edit data even though edit is in scope

I am a little new to C++, so I am starting out with a text editor program. I am developing this project in NetBeans IDE. Currently the application is all in the main source file. When I compile and run the project, I get no errors and the project proceeds to function.

However, I have some sort of error or bug that lies within the application as it is running. This bug is associated with the Edit Control and the GetWindowText() method. The problem is located in the saveFile() function. What I am making of the GetWindowText() function is to test to see if I can retrieve text from the Edit Control and use it for my needs. I tried to emulate it through a simple MessageBox() function, and I got nothing.

Source code [FULL]:

/* 
 * File:   main.cpp
 * Author: CA1K
 *
 * Created on November 1, 2015, 9:58 PM
 */

#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <string>

using namespace std;

// [ PROGRAM PROCEDURES ]
//=========================================================
/*Really what these are used for is to address conditions to
 *the WS_COMMAND case, these are registered as integers
 */
#define IDC_MAIN_EDIT 0//edit ID
#define EXIT_PROC 1//the exit procedure
#define ABOUT_PROC 2//the trigger for the "about" window
#define CLONE_PROC 3//spawn a new window
#define SAVE_PROC 4//save procedure
#define OPEN_PROC 5//copy procedure
//=========================================================

// [ DEVELOPER FRIENDLY VARIABLES ]
//=========================================================
const char g_szClassName[] = "CPadx1"; //window class name
const char g_Title[] = "CPad"; //window title
const char g_About[] = "CPad is a program developed by CA1K. It is entirely made from the win32 API. For more programs, go to ca1k.xkotto.com.";
int dim = 600;//window dimensions(square)
TCHAR buffer[512];
//=========================================================

// [ OBJECT BUILDING ]
//=========================================================
void makeMenu(HWND hwnd){
HMENU hMenubar;
HMENU hMenu;
HMENU hMenu2;
hMenubar = CreateMenu();
hMenu = CreateMenu();
hMenu2 = CreateMenu();
AppendMenuW(hMenu, MF_STRING, SAVE_PROC, L"&Save/Save as");
AppendMenuW(hMenu, MF_STRING, OPEN_PROC, L"&Open file");
AppendMenuW(hMenu, MF_SEPARATOR, 0, NULL);
AppendMenuW(hMenu, MF_STRING, CLONE_PROC, L"&New Window");
AppendMenuW(hMenu, MF_SEPARATOR, 0, NULL);
AppendMenuW(hMenu, MF_STRING, ABOUT_PROC, L"&About");
AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR)hMenu, L"&File");
AppendMenuW(hMenu2, MF_STRING, EXIT_PROC, L"&Exit");
AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR)hMenu2, L"&Config");
SetMenu(hwnd, hMenubar);
}

void dispWnd(HWND hwnd){//this function spawns a new window
HINSTANCE hInstance;
HWND newChild = CreateWindowEx(0, g_szClassName, g_Title, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, dim, dim, hwnd, NULL, hInstance, NULL);
HWND GetParent(HWND hwnd);
ShowWindow(newChild, 1);
UpdateWindow(newChild);
}

void makeEdit(HWND hwnd, HWND hEdit){//this function creates the edit
HINSTANCE hInstance;
RECT rect;
int pwidth;
int pheight;
if(GetWindowRect(hwnd, &rect))
{
pwidth = rect.right - rect.left;
pheight = rect.bottom - rect.top;
}
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",
WS_CHILD|WS_VISIBLE|ES_AUTOVSCROLL|ES_AUTOHSCROLL|ES_MULTILINE,
0,0,pwidth,pheight,hwnd,(HMENU)IDC_MAIN_EDIT,
hInstance,NULL);
}

void saveFile(HWND hEdit){
GetWindowText(hEdit, buffer, 512);
MessageBox(NULL, buffer, "Test", MB_ICONINFORMATION);
}

//=========================================================

// [ SOFTWARE PROCESSING ]
//=========================================================
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND winEdit;
switch(msg)
{
    /*WINDOW ACTIONS*/
    case WM_CREATE:{//on window creation
        makeMenu(hwnd);
        makeEdit(hwnd,winEdit);
    }
    break;
    case WM_SIZE:
        makeEdit(hwnd,winEdit);
    break;
    case WM_COMMAND://window actions
        switch(LOWORD(wParam))
        {
            case ABOUT_PROC:
                MessageBox(NULL, g_About, "About", MB_ICONINFORMATION);
            break;
            case EXIT_PROC:
                PostQuitMessage(0);//exit program
            break;
            case CLONE_PROC:
                dispWnd(hwnd);
            break;
            case SAVE_PROC:
                saveFile(winEdit); 
            break;
        }
    break;
    case WM_CLOSE://on window close
        DestroyWindow(hwnd);
    break;
    case WM_DESTROY://on window destroy
        PostQuitMessage(0);
    break;
    default://default method
        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  = 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
hwnd = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    g_szClassName,
    g_Title,
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, dim, dim,
    NULL, NULL, hInstance, NULL);

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

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

}
//=========================================================

I am questioning whether I have the scopes wrong, or something else. Feed back would be much appreciated.

-CA1K

Upvotes: 0

Views: 85

Answers (1)

lost_in_the_source
lost_in_the_source

Reputation: 11237

You need to make winEdit a static variable. This is because you use it in more than one message, like WM_CREATE and WM_COMMAND. Then, you need to pass a pointer to an HWND to your makeEdit function:

makeEdit(hwnd, &winEdit);

So your overall window procedure should look like this:

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND winEdit;
switch(msg)
{
    /*WINDOW ACTIONS*/
    case WM_CREATE:{//on window creation
        makeMenu(hwnd);
        makeEdit(hwnd, &winEdit);
    }
    break;
    case WM_SIZE:
        makeEdit(hwnd,winEdit);
    break;
    case WM_COMMAND://window actions
        switch(LOWORD(wParam))
        {
            case ABOUT_PROC:
                MessageBox(NULL, g_About, "About", MB_ICONINFORMATION);
            break;
            case EXIT_PROC:
                PostQuitMessage(0);//exit program
            break;
            case CLONE_PROC:
                dispWnd(hwnd);
            break;
            case SAVE_PROC:
                saveFile(winEdit); 
            break;
        }
    break;
    case WM_CLOSE://on window close
        DestroyWindow(hwnd);
    break;
    case WM_DESTROY://on window destroy
        PostQuitMessage(0);
    break;
    default://default method
        return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}

Upvotes: 1

Related Questions