void GetConsoleBufferSize(COORD *dwSize) { CONSOLE_SCREEN_BUFFER_INFO screenInfo; CONSOLE_FONT_INFO fontInfo; HANDLE conOut = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(conOut, &screenInfo); GetCurrentConsoleFont(conOut, FALSE, &fontInfo); dwSize->X = (screenInfo.srWindow.Right - screenInfo.srWindow.Left + 1) * fontInfo.dwFontSize.X; dwSize->Y = (screenInfo.srWindow.Bottom - screenInfo.srWindow.Top + 1) * fontInfo.dwFontSize.Y; }
/* ************************************ * VOID GetConsoleInfo(HANDLE hOutput) * 功能 获取控制台信息 * 参数 HANDLE hOutput,控制台句柄 **************************************/ VOID GetConsoleInfo(HANDLE hOutput) { CONSOLE_FONT_INFO cfi; DWORD dwProcessList[32]; DWORD dwAttachedProcess; TCHAR szOutputBuffer[1024]; HWND hConsoleWindows; DWORD dwWritten; DWORD i; CHAR szConsoleTitle[MAX_PATH]; // 附属的进程,不考虑多于32个的情况 dwAttachedProcess = GetConsoleProcessList(dwProcessList,32); if(dwAttachedProcess==0) { MyErrorExit("GetConsoleProcessList"); } // 标题 if( !GetConsoleTitle(szConsoleTitle, MAX_PATH) ) { MyErrorExit("GetConsoleTitle"); } // 窗口句柄 hConsoleWindows = GetConsoleWindow(); // 字体 GetCurrentConsoleFont(hOutput,FALSE,&cfi); wsprintf(szOutputBuffer,"Now %u attached Processes: ",dwAttachedProcess); for(i=0;i<dwAttachedProcess;i++) { wsprintf(szOutputBuffer+lstrlen(szOutputBuffer),"%u, ",dwProcessList[i]); } // 构造字符串 wsprintf(szOutputBuffer+lstrlen(szOutputBuffer), "\nConsoleTitle is %s,\nWindow Handle is 0x%.8X\n" "Font is %u, Font Size X = %u, Y = %u\n", szConsoleTitle,hConsoleWindows, cfi.nFont,cfi.dwFontSize.X,cfi.dwFontSize.Y); // 显示获取的信息 if(!WriteConsole(hOutput,szOutputBuffer,lstrlen(szOutputBuffer),&dwWritten,NULL)) { MyErrorExit("WriteConsole"); } }
// gets the y coordinate of the cell the mouse is in int Console::GetMouseCellY() { GetCursorPos(&cur_point); windowpoint.x = 0; windowpoint.y = 0; ClientToScreen(hWindow,&windowpoint); GetCurrentConsoleFont(hScreen, false, &font); fontSize = GetConsoleFontSize(hScreen, font.nFont); if (fontSize.Y != 0) mouse.y = (cur_point.y - windowpoint.y)/fontSize.Y; else { // else it is in fullscreen GetConsoleScreenBufferInfo(hScreen,&ScrBufInfo); // get the window handle to the whole screen fullscr_h = GetDesktopWindow(); // get the size of the whole screen GetClientRect(fullscr_h, &fullscr_size); mouse.y = cur_point.y/((fullscr_size.bottom/ScrBufInfo.dwSize.Y)-3); } return mouse.y; }
int main (int argc, char *argv[]) { //Get a console handle HWND ConsoleWindow = GetConsoleWindow(); //Get a STD handle HWND StdHandle = GetStdHandle (STD_OUTPUT_HANDLE); //Set cursor invisible CONSOLE_CURSOR_INFO CURSOR; CURSOR.dwSize = 1; CURSOR.bVisible = FALSE; SetConsoleCursorInfo (StdHandle, &CURSOR); //Get Current Font for (;;) { CONSOLE_FONT_INFO GETFONT; GetCurrentConsoleFont (StdHandle, FALSE, &GETFONT); COORD Fontsize = GetConsoleFontSize (StdHandle, GETFONT.nFont); SHORT Font_X = Fontsize.X; SHORT Font_Y = Fontsize.Y; if (Font_X != 8 || Font_Y != 12) { system ("chcp 437 > NUL"); MessageBox (ConsoleWindow, "Please change Console Font to Raster Font ( 8 x 12 ).", "Notice", MB_TOPMOST | MB_OK | MB_ICONINFORMATION); Envir_Error (); printf ("\rPress any key to continue..."); system ("pause>nul"); } else break; } //Change Settings //SetWindowLong (ConsoleWindow, GWL_STYLE, WS_THICKFRAME); //SetWindowLong (ConsoleWindow, GWL_STYLE, WS_CAPTION); //SetWindowPos (ConsoleWindow, HWND_TOPMOST, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_SHOWWINDOW); SetWindowPos (ConsoleWindow, HWND_TOP, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_SHOWWINDOW); //Get a handle to device context HDC DeviceContext = GetDC (ConsoleWindow); //Color COLORREF Color_White = RGB (255,255,255); COLORREF Color_Black = RGB ( 0 , 0 , 0 ); //Title and Resize system ("mode con cols=75 lines=50"); system ("title Console Snake [ Developer: KVD 2013 ]"); system ("color 0A"); //Multi-Thread pthread_t Key_Stroke_Func; pthread_create (&Key_Stroke_Func, NULL, (void*)Get_Key_Stroke, NULL); //Initialization Initialize:; Initialization (DeviceContext, argv[1]); //Main for (;;) { //Terminate / PlayAgain if (Flag_Terminate == True) { return 0; } else if (Flag_PlayAgain == True) { Flag_PlayAgain = False; Clear_Screen (DeviceContext); goto Initialize; } //Draw if (Move.Condition == UnRead) { Move.Condition = Read; Change_Str (DeviceContext, ConsoleWindow); Draw_Square (Draw.X, Draw.Y, Draw.Direction, Color_White, Erase.X, Erase.Y, Erase.Direction, Color_Black, User_Def_Delay, DeviceContext); } else { Predict_Func (Draw.Direction); Change_Str (DeviceContext, ConsoleWindow); Draw_Square (Draw.X, Draw.Y, Draw.Direction, Color_White, Erase.X, Erase.Y, Erase.Direction, Color_Black, User_Def_Delay, DeviceContext); } //End if (Is_Snake (Draw.X, Draw.Y) == True || Is_Block (Draw.X, Draw.Y) == True) { MessageBox (ConsoleWindow, "GAME OVER", "Notice", MB_TOPMOST | MB_OK | MB_ICONSTOP); Clear_String (); Clear_Screen (DeviceContext); goto Initialize; } } //End system ("pause>nul"); return 0; }
co_rc_t console_widget_NT_t::set_window(console_window_t * W) { CONSOLE_CURSOR_INFO cci; CONSOLE_FONT_INFO cfi; COORD fs; HWND hwnd; RECT r; window = W; input = GetStdHandle(STD_INPUT_HANDLE); SetConsoleMode(input, 0); output = GetStdHandle(STD_OUTPUT_HANDLE); GetCurrentConsoleFont(output, 0, &cfi); fs = GetConsoleFontSize(output, cfi.nFont); r.top = 0; r.left = 0; r.bottom = fs.Y * 25; r.right = fs.X * 80; AdjustWindowRect(&r, WS_CAPTION|WS_SYSMENU|WS_THICKFRAME |WS_MINIMIZEBOX|WS_MAXIMIZEBOX, 0); hwnd = GetConsoleWindow(); SetWindowPos(hwnd, HWND_TOP, 0, 0, r.right - r.left, r.bottom - r.top, SWP_NOMOVE|SWP_SHOWWINDOW); GetConsoleCursorInfo(output, &cursor); cci = cursor; cci.bVisible = 0; SetConsoleCursorInfo(output, &cci); size.X = 80 ; size.Y = 25 ; region.Top = 0; region.Left = 0; region.Right = 79; region.Bottom = 24; if( ! SetConsoleWindowInfo( output , TRUE , ®ion ) ) co_debug("SetConsoleWindowInfo() error code %d\n", GetLastError()); screen = (CHAR_INFO *) co_os_malloc(sizeof (CHAR_INFO) * size.X * size.Y); buffer = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, 0, CONSOLE_TEXTMODE_BUFFER, 0); if( ! SetConsoleScreenBufferSize( buffer , size ) ) co_debug("SetConsoleScreenBufferSize() error %d\n", GetLastError()); SetConsoleMode(buffer, 0); cci.bVisible = true; cci.dwSize = 10; SetConsoleCursorInfo(buffer, &cci); SetConsoleActiveScreenBuffer(buffer); return CO_RC(OK); }