如何在application中创建console窗口

coding-cpde 2004-04-21 12:11:10
请问如何在Windows Application中使用CreateWindow创建出类似控制台的窗口

一个比较简单的问题,希望大家不要笑话 :)
...全文
133 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xstring 2004-04-21
  • 打赏
  • 举报
回复
AllocConsole ()之后如果想使用stdin, stdout, stderr这些东西

可以这样

#include <io.h>
#include ...

void setup_stdio ()
{
int fd_stdin = _open_oosfhandle (GetStdHandle (STD_INPUT_HANDLE), _O_RDONLY);
int fd_stdout = _open_oosfhandle (GetStdHandle (STD_OUTPUT_HANDLE), _O_TEXT);
int fd_stderr = _open_oosfhandle (GetStdHandle (STD_ERROR_HANDLE), _O_TEXT);
_dup2 (fd_stdin, 0);
_dup2 (fd_stdout, 1);
_dup2 (fd_stderr, 2);
_close (fd_stdin);
_close (fd_stdout);
_close (fd_stderr);
*stdin = *_fdopen (0, "r");
*stdin = *_fdopen (1, "w");
*stderr = *_fdopen (2, "w");
};

调用setup_stdio后,就可以使用printf, scanf,之类的函数了

xstring 2004-04-21
  • 打赏
  • 举报
回复
要想去掉控制台窗口,就FreeConsole ()一下
xstring 2004-04-21
  • 打赏
  • 举报
回复
不用CreateWindow
AllocConsole ()一下就有了。

coding-cpde 2004-04-21
  • 打赏
  • 举报
回复
请问xstring(麻雀) 你说的
void setup_stdio ()
{
....
}
怎么使用?我把它加在主程序中提示“error C2065: '_open_oosfhandle' : undeclared identifier”,加在stdafx.cpp中也提示错误。

谢谢!
panzhaoping 2004-04-21
  • 打赏
  • 举报
回复
??
xstring 2004-04-21
  • 打赏
  • 举报
回复
GetConsoleWindow从win2000才开始有

所以你应该定义

#define _WIN32_WINNT 0x500

把它加在stdafx.h中最前面
ymbymb 2004-04-21
  • 打赏
  • 举报
回复
你用下面的方法试试!
AllocConsole();
char buf[100];
GetConsoleTitle(buf, 100);
CWnd *pWnd = FindWindow(NULL, buf);
HWND hwnd = pWnd->GetSafeHwnd();
coding-cpde 2004-04-21
  • 打赏
  • 举报
回复
我想使用GetConsoleWindow这个函数,但是在编译的时候不能通过,可能是该函数要求Platform SDK: DLLs, Processes, and Threads。还有其他的方法吗?
modnar 2004-04-21
  • 打赏
  • 举报
回复
up
coding-cpde 2004-04-21
  • 打赏
  • 举报
回复
感谢你的回复

不过我想用CreateWindow,是因为我想得到窗口的HWND,用什么方法可以得到控制台窗口的句柄?

谢谢!

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧