将表单添加到控制台应用程序.

编程入门 行业动态 更新时间:2024-10-26 15:13:05
本文介绍了将表单添加到控制台应用程序.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个图形控制台应用程序,如果我可以更改表单中的各种值,然后单击表单上的开始"按钮,然后执行控制台应用程序,那就更好了. 假设我有一个像这样的简单c ++程序(可以在turbo c ++中执行)

I have a graphics console application which could be better if I could change various values in it in a form, then execute the console application after clicking a "start" button on the form. Suppose I have a simple c++ program like this (which could execute in turbo c++)

//addition.cpp #include<iostream.h> int x=10; int y=100; int main() { int sum; sum=x+y; cout << "\n The sum of x and y is " << sum; return 0; }</iostream.h>

如您所知,如果我编译并运行该程序,它将打开一个控制台窗口,该窗口显示文本"x和y的和为110"并退出. 我现在想要的是拥有一个首先打开的Windows窗体. Windows窗体包含: -2文本框.一个接受值"x",另一个接受"y". -一个提交"按钮. 当我单击提交"按钮时,additional.cpp的全局变量的值(int x和y)应更改为我在文本框中编写的内容,以便控制台相应地显示结果. 现在,我不想改变该程序的工作方式(例如某位出色的家伙建议我将结果发布在警报消息中),就像我已经告诉您的那样,这是为基于图形的程序创建前端我已经建好了. 简而言之,该程序包括一个重力作用在其上的弹跳球.我希望用户能够从前端形式更改各种参数,例如球的初始位置,重力常数,弹性等. 无论如何,如果能获得指导或逐步入门来运行此简单加法程序,我将不胜感激. PS:-我有一些Visual C ++ 2010 Express Edition的使用知识,尽管详细的演练不会对您造成伤害. -我是本科生,并且对工作场所使用的各种技术术语没有任何经验.到目前为止,我在繁忙的学术计划中学习了VC ++,没有薪水,这是我认为非常不错的成就.​​ -我将Allegro库用于图形.再一次,从头开始学习这一内容. 最好的问候, Divy

As you know, if I compile and run this program, it should open a console window which displays the text "The sum of x and y is 110" and quits. What I want now, is to have a windows form that opens first. The windows form contains: -2 text boxes. One accepts the value "x" and the other accepts "y". -A "submit" button. When I click the "submit" button, the global variables'' (int x and y) values in addition.cpp should have changed to whatever I have written in the text boxes so that the console displays the result accordingly. Now, I don''t want to change the way this program works (like some brilliant bloke suggesting me to post the result in an alert message instead) as I already told you, this is for creating front-end for a graphics based program I already built. In brief, this program consists of a bouncing ball with gravity acting on it. I want the user to be able to change various parameters like initial position of the ball, gravitational constant, elasticity etc from a front-end form. Anyway for now, I would appreciate it if I could get a guide or a walkthrough to get this simple addition program running. PS: -I have some working knowledge of Visual C++ 2010 express edition although a detailed walkthrough couldn''t hurt. -I am an undergrad student, and I have no prior experience with all sorts of technical jargon used at the workplace. Having learnt VC++ so far amidst my busy academic schedule and no pay is something I would consider, a pretty great achievement. -I used Allegro libraries for the graphics. Again, learned this one from scratch. Best regards, Divy

推荐答案

我认为在这种情况下,您应该创建Windows应用程序.然后,该应用程序将创建另一个应用程序(现有应用程序)并退出. 可以使用CreateProcess. msdn.microsoft/en-us/library/ms682425 (v = vs.85).aspx [ ^ ] I think that in this case, you should create a Windows application. That application would then create another application (the existing application) and exit. CreateProcess might be used. msdn.microsoft/en-us/library/ms682425(v=vs.85).aspx[^]

创建新Window应用程序的更好方法.然后将旧的控制台基本代码移植到Window Base Application:) Better way to create new Window Application. And port your old console base code to Window Base Application :)

好吧,我很无聊,外面一片漆黑,所以我不能再飞3或太阳升起时的4个小时.我将为您提供各种指导,我将如何做. 我刚刚按照上一篇文章中的建议研究了控制台功能(窗口).我发现在使用gui应用程序中的控制台窗口之前,似乎只需要使用其中两个功能即可.但是-为了使printf或cout输出出现在此控制台中,还有更多工作要做-如MSDN页面底部针对AllocConsole所述.遵循该建议,并阅读页面底部有关AttachConsole的评论,我们将转到页面 [ ^ ]详细介绍了如何连接这些(和其他)功能添加到新创建的控制台窗口. 要测试所需的功能,我 1)创建了Windows32 GUI应用程序. 2)添加了3个按钮-2个虚拟变量和1个按钮来创建控制台窗口 3)连接第三个按钮以调用此功能: Okay then, I''m bored and it''s dark outside so I can''t go flying for another 3 or 4 hours when the sun comes up. I''ll see what I can do about giving you a walk-through of sorts. I''ve just looked into the Console Functions (windows) as suggested in my last post. I have found that it appears that I only need to use 2 of the functions before I can use a console window from a gui app. HOWEVER - in order to get printf or cout output to appear in this console, there is a little more work to do - as mentioned at the bottom of the MSDN page for AllocConsole. Following the suggestion, and reading the comment at the bottom of the page for AttachConsole we are directed to a page [^] that details how to connect these (and other) functions to the newly created console window. To test the requested functionality, I 1) Created a windows32 gui app. 2) Added 3 buttons - 2 dummies and 1 to create a console window 3) Wired the 3rd button up to call this function: void handleConsoleButton() { MessageBox(NULL,"Creating the console","msg - handleConsoleButton", MB_OK); FreeConsole(); RedirectIOToConsole(); printf("Hello, World!\n"); }

当然,这是在主窗口的功能中执行的,如下所示:

This is of course, carried out in the main window''s function as shown here:

/* 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_DESTROY: PostQuitMessage (0); /* send a WM_QUIT to the message queue */ break; case WM_COMMAND: switch LOWORD(wParam) { case IDC_BTN1: MessageBox(NULL,"text1","msg", MB_OK); break; case IDC_BTN2: MessageBox(NULL,"text2","msg", MB_OK); break; case IDC_BTN3: handleConsoleButton(); break; } default: /* for messages that we don't deal with */ return DefWindowProc (hwnd, message, wParam, lParam); } return 0; }

函数RedirectIOToconsole来自我链接到的页面,并由此定义:

The function RedirectIOToconsole comes from the page I linked to and is defined thusly:

#define MAX_CONSOLE_LINES 500 void RedirectIOToConsole() { int hConHandle; long lStdHandle; CONSOLE_SCREEN_BUFFER_INFO coninfo; FILE *fp; // allocate a console for this app AllocConsole(); // set the screen buffer to be big enough to let us scroll text GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo); coninfo.dwSize.Y = MAX_CONSOLE_LINES; SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize); // redirect unbuffered STDOUT to the console lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE); hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); fp = _fdopen( hConHandle, "w" ); *stdout = *fp; setvbuf( stdout, NULL, _IONBF, 0 ); // redirect unbuffered STDIN to the console lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE); hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); fp = _fdopen( hConHandle, "r" ); *stdin = *fp; setvbuf( stdin, NULL, _IONBF, 0 ); // redirect unbuffered STDERR to the console lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE); hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); fp = _fdopen( hConHandle, "w" ); *stderr = *fp; setvbuf( stderr, NULL, _IONBF, 0 ); // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog // point to console as well ios::sync_with_stdio(); }

当然,您的里程可能会有所不同.尽管我最感谢您为我提供了推动力,但最终(一点点)学习了有关控制台功能的知识.干杯!

Naturally, your mileage may vary. Though I mujst say thanks for providing the impetus to me finally learning (a little, little bit) about the Console Functions. Cheers!

更多推荐

将表单添加到控制台应用程序.

本文发布于:2023-11-13 21:49:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585410.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   表单   应用程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!