C++ WinCon Setting Buffer Size issue

Discussion in 'Programmer's Corner' started by TheFox, Aug 13, 2011.

  1. TheFox

    Thread Starter Active Member

    Apr 29, 2009
    66
    5
    I am trying to set the Windows Console's buffer's buffer size, via a simple game. However, I don't think I am going about it the right way. I am using the program just to learn about Windows programming with c++. I use VC++ 2008.

    I think my issue is here:
    Code ( (Unknown Language)):
    1.  
    2. [SIZE=2]HANDLE BufferHandle = CreateConsoleScreenBuffer( [/SIZE]
    3. [SIZE=2]GENERIC_READ | [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// read/write access [/COLOR][/SIZE]
    4. [/COLOR][/SIZE][SIZE=2]GENERIC_WRITE, [/SIZE]
    5. [SIZE=2]FILE_SHARE_READ | [/SIZE]
    6. [SIZE=2]FILE_SHARE_WRITE, [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// shared [/COLOR][/SIZE]
    7. [/COLOR][/SIZE][SIZE=2]NULL, [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// default security attributes [/COLOR][/SIZE]
    8. [/COLOR][/SIZE][SIZE=2]CONSOLE_TEXTMODE_BUFFER, [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// must be TEXTMODE [/COLOR][/SIZE]
    9. [/COLOR][/SIZE][SIZE=2]NULL); [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// reserved; must be NULL [/COLOR][/SIZE]
    10. [/COLOR][/SIZE][SIZE=2]COORD BufferSize;[/SIZE]
    11. [SIZE=2]BufferSize.X = 6;[/SIZE]
    12. [SIZE=2]BufferSize.Y = 75;[/SIZE]
    13. [SIZE=2]HWND console = GetConsoleWindow();[/SIZE]
    14. [SIZE=2]StatusCheck = SetConsoleScreenBufferSize(BufferHandle, BufferSize);[/SIZE]
    15. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](StatusCheck == FALSE)[/SIZE]
    16. [SIZE=2]{[/SIZE]
    17. [SIZE=2]std::cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Buffer Size failed \n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    18. [SIZE=2]system([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"pause"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    19. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] -2;[/SIZE]
    20. [SIZE=2]}[/SIZE]
    21.  
    Here is the full code:
    Code ( (Unknown Language)):
    1.  
    2. [SIZE=2][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<iostream>[/COLOR][/SIZE]
    3. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#define[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] _WIN32_WINNT 0x0500[/SIZE]
    4. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<windows.h>[/COLOR][/SIZE]
    5. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<string>[/COLOR][/SIZE]
    6. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#define[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ACTOR [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]'o'[/COLOR][/SIZE]
    7. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]struct[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] VECTOR2[/SIZE]
    8. [SIZE=2]{ [/SIZE]
    9. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]unsigned[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]float[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] X, Y;[/SIZE]
    10. [SIZE=2]};[/SIZE]
    11. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ClsPlayer[/SIZE]
    12. [SIZE=2]{[/SIZE]
    13. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]:[/SIZE]
    14. [SIZE=2]VECTOR2 Pos;[/SIZE]
    15. [SIZE=2]ClsPlayer([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]float[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] X = 0.0f, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]float[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Y = 0.0f)[/SIZE]
    16. [SIZE=2]{[/SIZE]
    17. [SIZE=2]Pos.X = X;[/SIZE]
    18. [SIZE=2]Pos.Y = Y;[/SIZE]
    19. [SIZE=2]}[/SIZE]
    20. [SIZE=2]};[/SIZE]
    21. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] DrawScreen(VECTOR2 Location, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]char[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Actor)[/SIZE]
    22. [SIZE=2]{[/SIZE]
    23. [SIZE=2]system([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"cls"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    24. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] I = 0; I != Location.Y; ++I)[/SIZE]
    25. [SIZE=2]{[/SIZE]
    26. [SIZE=2]std::cout<< [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    27. [SIZE=2]}[/SIZE]
    28. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] I = 0;I != Location.X; ++I)[/SIZE]
    29. [SIZE=2]{[/SIZE]
    30. [SIZE=2]std::cout<< [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    31. [SIZE=2]}[/SIZE]
    32. [SIZE=2]std::cout << Actor;[/SIZE]
    33. [SIZE=2]}[/SIZE]
    34. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] main()[/SIZE]
    35. [SIZE=2]{[/SIZE]
    36. [SIZE=2]ClsPlayer* Player = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ClsPlayer();[/SIZE]
    37. [SIZE=2]BOOL StatusCheck;[/SIZE]
    38. [SIZE=2]HANDLE BufferHandle = CreateConsoleScreenBuffer( [/SIZE]
    39. [SIZE=2]GENERIC_READ | [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// read/write access [/COLOR][/SIZE]
    40. [/COLOR][/SIZE][SIZE=2]GENERIC_WRITE, [/SIZE]
    41. [SIZE=2]FILE_SHARE_READ | [/SIZE]
    42. [SIZE=2]FILE_SHARE_WRITE, [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// shared [/COLOR][/SIZE]
    43. [/COLOR][/SIZE][SIZE=2]NULL, [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// default security attributes [/COLOR][/SIZE]
    44. [/COLOR][/SIZE][SIZE=2]CONSOLE_TEXTMODE_BUFFER, [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// must be TEXTMODE [/COLOR][/SIZE]
    45. [/COLOR][/SIZE][SIZE=2]NULL); [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// reserved; must be NULL [/COLOR][/SIZE]
    46. [/COLOR][/SIZE][SIZE=2]COORD BufferSize;[/SIZE]
    47. [SIZE=2]BufferSize.X = 6;[/SIZE]
    48. [SIZE=2]BufferSize.Y = 75;[/SIZE]
    49. [SIZE=2]HWND console = GetConsoleWindow();[/SIZE]
    50. [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//ShowWindow(console, 0);[/COLOR][/SIZE]
    51. [/COLOR][/SIZE][SIZE=2]StatusCheck = MoveWindow(console, 100,100, 500, 500, TRUE);[/SIZE]
    52. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](StatusCheck == FALSE)[/SIZE]
    53. [SIZE=2]{[/SIZE]
    54. [SIZE=2]std::cout<< [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]" Window size failed\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    55. [SIZE=2]system([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"pause"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    56. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] -1;[/SIZE]
    57. [SIZE=2]}[/SIZE]
    58. [SIZE=2]StatusCheck = SetConsoleScreenBufferSize(BufferHandle, BufferSize);[/SIZE]
    59. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](StatusCheck == FALSE)[/SIZE]
    60. [SIZE=2]{[/SIZE]
    61. [SIZE=2]std::cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Buffer Size failed \n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    62. [SIZE=2]system([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"pause"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    63. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] -2;[/SIZE]
    64. [SIZE=2]}[/SIZE]
    65.  
    66.  
    67. [SIZE=2]StatusCheck = SetConsoleTitle(TEXT([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"The o Game."[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]));[/SIZE]
    68. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](StatusCheck == FALSE)[/SIZE]
    69. [SIZE=2]{[/SIZE]
    70. [SIZE=2]std::cout << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Title failed \n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    71. [SIZE=2]system([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"pause"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    72. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] -3;[/SIZE]
    73. [SIZE=2]}[/SIZE]
    74.  
    75. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]while[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](1)[/SIZE]
    76. [SIZE=2]{[/SIZE]
    77. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (GetKeyState(VK_UP) & 0x80)[/SIZE]
    78. [SIZE=2]{[/SIZE]
    79. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](Player ->Pos.Y > 0)[/SIZE]
    80. [SIZE=2]{[/SIZE]
    81. [SIZE=2]--Player->Pos.Y;[/SIZE]
    82. [SIZE=2]}[/SIZE]
    83. [SIZE=2]}[/SIZE]
    84. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (GetKeyState(VK_DOWN) & 0x80)[/SIZE]
    85. [SIZE=2]{[/SIZE]
    86. [SIZE=2]++Player->Pos.Y;[/SIZE]
    87. [SIZE=2]}[/SIZE]
    88. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (GetKeyState(VK_LEFT) & 0x80)[/SIZE]
    89. [SIZE=2]{[/SIZE]
    90. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](Player->Pos.X > 0)[/SIZE]
    91. [SIZE=2]{[/SIZE]
    92. [SIZE=2]--Player->Pos.X;[/SIZE]
    93. [SIZE=2]}[/SIZE]
    94. [SIZE=2]}[/SIZE]
    95. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (GetKeyState(VK_RIGHT) & 0x80)[/SIZE]
    96. [SIZE=2]{[/SIZE]
    97. [SIZE=2]++Player->Pos.X;[/SIZE]
    98. [SIZE=2]}[/SIZE]
    99.  
    100. [SIZE=2]DrawScreen(Player->Pos, ACTOR);[/SIZE]
    101. [SIZE=2]Sleep(25);[/SIZE]
    102. [SIZE=2]}[/SIZE]
    103. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]delete[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Player;[/SIZE]
    104. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 0;[/SIZE]
    105. [SIZE=2]}[/SIZE]
    106.  
    107. [/SIZE]

     
Loading...