所以我自己有一个项目,其中有两个文件,AT.c 和 main.c(我使用 DEV-C++)。 AT.c 包含我需要的所有计算内容,使用命令行进行 I/O,并且本身工作得相对良好。但是,由于我需要该程序的 GUI,因此我还获得了 main.c 文件,该文件基本上创建了一个带有文本框的窗口,供我输入数字。
无论如何,现在我有点卡住了,因为我不知道如何让 AT.c 从这些文本框中读取值而不是使用 scanf 方法。我想我应该在两个代码之间做一些声明或链接,但我还没有找到任何可以帮助的东西。
代码是 main.c 是在 Dev-C++ 中创建新的 Windows 应用程序项目时获得的起始代码。我只更改了 LRESULTCALLBACK 部分。
#define ID_BUTTON 1
#define ID_TEXTBOX 2
static HWND hwndA;
static HWND hwndB;
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:{
CreateWindow(TEXT("Button"), TEXT("Generate"),
WS_CHILD | WS_VISIBLE,
10, 160, 80, 20,
hwnd, (HMENU) ID_BUTTON, NULL, NULL
);
hwndA = CreateWindow(TEXT("EDIT"), TEXT(""),
WS_VISIBLE | WS_CHILD | WS_BORDER,
190, 130, 70, 15,
hwnd, (HMENU) ID_TEXTBOX, NULL, NULL
);
hwndB = CreateWindow(TEXT("EDIT"), TEXT(""),
WS_VISIBLE | WS_CHILD | WS_BORDER,
260, 130, 70, 15,
hwnd, (HMENU) ID_TEXTBOX, NULL, NULL
);
break;
}
case WM_COMMAND:{
if (LOWORD(wParam) == ID_BUTTON) {
char wot[256];
GetWindowText(hwndA, wot, 4);
SetWindowText(hwndB, wot);
}
}
break;
}
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
AT.c虽然很大,但计算程序非常简单。 喜欢
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int a, b, c;
main(){
scanf("%d", &a);
scanf("%d", &b);
printf("\n%d", a+b);
system("pause");
}
只是一堆 if 和循环。
最佳答案
我假设您已将 AT.C
和 MAIN.C
添加到您的项目中。你可以很容易地交流,它们写在不同的文件中并不重要,除非我不明白这个问题。
在文件1.C中:
#include "headers.h"
//..
char Input_Text[256];
void Function_in_file1(const char *buf)
{
//..
}
//..
在文件2.C中:
#include "headers.h"
//..
extern char Input_Text[256];
void Function_in_file1(const char *buf);//let the compiler know this is a valid function and you wrote it somewhere else
//...
case WM_COMMAND:{
if (LOWORD(wParam) == ID_BUTTON) {
char temp[50];
GetWindowText(hwndA, temp, 4);
Function_in_file1(temp); //call Function_in_file1() in file1.C
//or
GetWindowText(hwndA, Input_Text, 4);//Input_Text is available in file1.C
}
}
关于c - 在文本框和程序源代码之间传递信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29336093/