多屏幕下控制台位置

来源:互联网 发布:淘客软件排名 编辑:程序博客网 时间:2024/06/05 14:12

基本上我们现在都有两个屏幕了吧。一个笔计本的屏幕,一个外接的屏幕。
这时候特别的方便。但是会有一个问题。就是如果在GUI的情况下开了一个Console,更特别的是在调一些全屏的游戏的时候,没法看console的输出的,默认的console的位置是在主屏幕的,当我们程序一全屏,就盖在下面了。特别是在DirectX下面的时候,拉都拉不出来。
所以,就研究了下分屏幕的位置处理。
主要的API 
EnumDisplayMonitors(NULL, NULL, EnumProc, 0);
然后就没然后了。
基本上EnumDisplayMonitors会用回调函数
EnumProc(
HMONITOR hMonitor,
HDC hdcMonitor,
LPRECT lprcMonitor,
LPARAM dwData
)
这样子,我们就可以遍历得到屏幕句柄了。hMonitor是屏幕句柄,lprcMonitor是指向屏幕虚拟坐标准的RECT,dwData判断是不是主屏幕。

没了。


#ifndef __YYTOOLMUTISCREEN_H__#define __YYTOOLMUTISCREEN_H__#include <windows.h>namespace yyTool{class CMutiScreen{public:CMutiScreen();static int m_nNumber;//屏幕数量static BOOL __stdcall EnumProc(HMONITOR hMonitor,HDC hdcMonitor,LPRECT lprcMonitor,LPARAM dwData);//回调函数static HMONITOR m_hPrimior;//主屏幕句柄static HMONITOR m_hOther;//副屏幕句柄enum OtherPosition{SCREEN_LEFT=0,SCREEN_RIGHT,SCREEN_TOP,SCREEN_DOWN};//副屏幕相对于主屏幕的位置OtherPosition GetOtherPosition();//得到副屏幕相对于主屏幕的位置RECT GetRect(HMONITOR hMonitor);//得到窗口的虚拟坐标。};void YYSetConsole(void);//设置名为“DebugConsole”的位置为第二屏幕的控制台};#endif


#include "YyToolMutiScreen.h"#include <tchar.h>#include <cstdio>#include <cstdlib>#include <clocale>namespace yyTool{HMONITOR CMutiScreen::m_hPrimior=NULL;HMONITOR CMutiScreen::m_hOther=NULL;int  CMutiScreen::m_nNumber=0;CMutiScreen::CMutiScreen(){EnumDisplayMonitors(NULL, NULL, EnumProc, 0);}BOOL CALLBACK CMutiScreen::EnumProc(HMONITOR hMonitor,HDC hdcMonitor,LPRECT lprcMonitor,LPARAM dwData){MONITORINFOEX mi;ZeroMemory(&mi, sizeof(mi));mi.cbSize = sizeof(mi);GetMonitorInfo(hMonitor, &mi);if(mi.dwFlags&MONITORINFOF_PRIMARY){CMutiScreen::m_hPrimior=hMonitor;CMutiScreen::m_nNumber++;}else{CMutiScreen::m_hOther=hMonitor;CMutiScreen::m_nNumber++;}return TRUE;}CMutiScreen::OtherPosition CMutiScreen::GetOtherPosition(){MONITORINFOEX Pri;MONITORINFOEX Other; ZeroMemory(&Pri, sizeof(Pri));Pri.cbSize = sizeof(Pri);GetMonitorInfo(CMutiScreen::m_hPrimior, &Pri);ZeroMemory(&Other,sizeof(Other));Other.cbSize=sizeof(Other);GetMonitorInfo(CMutiScreen::m_hOther,&Other);if(Other.rcMonitor.right<=Pri.rcMonitor.left)return SCREEN_LEFT;if(Other.rcMonitor.bottom<=Pri.rcMonitor.top)return SCREEN_TOP;if(Other.rcMonitor.left>=Pri.rcMonitor.right)return SCREEN_RIGHT;if(Other.rcMonitor.top>=Pri.rcMonitor.bottom)return SCREEN_DOWN;return SCREEN_LEFT;}RECT CMutiScreen::GetRect( HMONITOR hMonitor ){MONITORINFOEX Pri;ZeroMemory(&Pri, sizeof(Pri));Pri.cbSize = sizeof(Pri);GetMonitorInfo(hMonitor, &Pri);return Pri.rcMonitor;}void YYSetConsole(void){AllocConsole();/*_tfreopen(_T("CONOUT$"), _T("w+"),stdout); _tfreopen(_T("CONIN$"), _T("r+"),stdin); */_tfreopen(_T("CONOUT$"), _T("w+t"),stdout);   _tfreopen(_T("CONIN$"), _T("r+t"),stdin);  _tfreopen(_T("CONOUT$"),_T("w+t"),stderr);   _tsetlocale(LC_ALL,_T("chs"));  CMutiScreen cm;if(cm.m_nNumber>=2){RECT rc=cm.GetRect(cm.m_hOther);LPCTSTR title=_T("DebugConsole");SetConsoleTitle(title);HWND hWnd= FindWindow(NULL,title);hWnd=GetConsoleWindow();if(hWnd!=NULL){::SetWindowPos(hWnd,HWND_TOP,rc.left+200,rc.top+200,0,0,SWP_NOSIZE|SWP_SHOWWINDOW );}}}}

在代码中加上YYSetConsole()之后就万事大吉了。可以用cout,cin 或者 _tprintf了

原创粉丝点击