gpt4 book ai didi

C++ Win32 不检索编辑数据,即使编辑在范围内

转载 作者:行者123 更新时间:2023-11-28 05:59:20 25 4
gpt4 key购买 nike

我对 C++ 有点陌生,所以我开始使用文本编辑器程序。我正在 NetBeans IDE 中开发这个项目。目前应用程序都在主源文件中。当我编译并运行该项目时,没有出现任何错误,并且该项目继续运行。

但是,我在应用程序运行时遇到了某种错误或错误。此错误与 Edit ControlGetWindowText() 方法有关。问题出在 saveFile() 函数中。我对 GetWindowText() 函数所做的是测试我是否可以从 Edit Control 检索文本并将其用于我的需要。我试图通过一个简单的 MessageBox() 函数来模拟它,但我一无所获。

源代码[完整]:

/* 
* 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;

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

我在质疑是我的范围有误还是其他原因。非常感谢您提供反馈。

-CA1K

最佳答案

您需要将 winEdit 设为静态变量。这是因为您在多个消息中使用它,例如 WM_CREATEWM_COMMAND。然后,您需要将指向 HWND 的指针传递给您的 makeEdit 函数:

makeEdit(hwnd, &winEdit);

所以你的整个窗口过程应该是这样的:

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;
}

关于C++ Win32 不检索编辑数据,即使编辑在范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33579206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com