-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcmdmax.c
173 lines (119 loc) · 4.45 KB
/
cmdmax.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*++
Copyright (c) 2013 by Antoni Sawicki
Module Name:
cmdmax.c
Abstract:
Maximize cmd.exe Window to match the screen size
Allow setting arbitrary window position and size parameters
Version:
2.0 - added parameters for user specified position / size / buffer size
1.0 - initial release
Author:
Antoni Sawicki <[email protected]>
License:
Apache 2.0
--*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "user32.lib")
HWND (__stdcall *CmdGetConsoleWindow)(void);
HWND
MyGetConsoleWindow(
void
)
/*++
Routine Description:
This is a makeshift version of GetConsoleWindow() function substituted
for Windows < 2000 which do not have the OS supplied version.
Lack of the Windows routine is detected and substituted at runtime rather than compile time.
--*/
{
char winname[128];
HWND cmdwin=NULL;
int n=0;
_snprintf(winname, sizeof(winname), "CMDMAX:%d", GetCurrentProcessId());
SetConsoleTitle(winname);
do {
n++;
Sleep(100);
cmdwin=FindWindow(NULL, winname);
} while (!cmdwin && n<600);
return cmdwin;
}
int
main(
int argc,
char **argv
)
/*++
Routine Description:
This routine is the main program for CmdMax.
The arguments accepted by cmdmax are:
pos_x Window Position -> Left in pixels, or 'n' will prevent moving
pos_y Window Position -> Top in pixels, or 'n' will prevent moving
win_w Window Size -> Width in characters
win_h Window Size -> Height in characters
buf_w Screen Buffer Size -> Width in characters, if smaller than win_w will be set to win_w
buf_h Screen Buffer Size -> Height in characters, if smaller than win_h will be set to win_h
No arguments will maximize the window.
--*/
{
HANDLE hConsole;
COORD conbuf, conmax;
SMALL_RECT conpos, winpos;
int winmov=1;
ZeroMemory(&conbuf, sizeof(COORD));
ZeroMemory(&conmax, sizeof(COORD));
ZeroMemory(&conpos, sizeof(SMALL_RECT));
ZeroMemory(&winpos, sizeof(SMALL_RECT));
CmdGetConsoleWindow = (void *) GetProcAddress(GetModuleHandle("kernel32.dll"), "GetConsoleWindow" );
if(!CmdGetConsoleWindow)
CmdGetConsoleWindow = (void *) MyGetConsoleWindow;
hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
if(argc==1) {
conmax=GetLargestConsoleWindowSize(hConsole);
winpos.Left=0;
winpos.Top=0;
conpos.Left=0;
conpos.Top=0;
conpos.Right=conmax.X-5;
conpos.Bottom=conmax.Y-5;
conbuf.X=conpos.Right+1;
conbuf.Y=3001;
} else if(argc==7) {
winpos.Left=strtol(argv[1], NULL, 10);
winpos.Top=strtol(argv[2], NULL, 10);
conpos.Left=0;
conpos.Top=0;
conpos.Right=strtol(argv[3], NULL, 10);
conpos.Bottom=strtol(argv[4], NULL, 10);
conbuf.X=strtol(argv[5], NULL, 10)+1;
conbuf.Y=strtol(argv[6], NULL, 10)+1;
if(conbuf.X < conpos.Right) conbuf.X=conpos.Right+1;
if(conbuf.Y < conpos.Bottom) conbuf.Y=conpos.Bottom+1;
if(argv[1][0]=='n' && argv[2][0]=='n')
winmov=0;
} else {
printf("Usage: cmdmax [<pos_x> <pos_y> <win_w> <win_h> <buf_w> <buf_h>]\n"
" pos_x, pos_y are in pixels\n"
" win_w, win_h, buf_w, buf_h are in characters\n"
" buf_w, buf_h must be greater or equal to win_w, win_h\n"
" if both pos_x, pos_y are set to letter 'n' won't move the window\n"
" if no parameters specified, a maximum possible size will be used\n\n"
"Examples:\n"
" cmdmax (will maximize the window)\n"
" cmdmax 100 100 80 24 80 24 (no scrollbars)\n"
" cmdmax n n 80 24 80 5000 (add buffer without moving)\n\n");
ExitProcess(0);
}
if(getenv("CMDMAX_DEBUG"))
printf("%d %d %d %d %d %d\n", winpos.Left, winpos.Top, conpos.Right, conpos.Bottom, conbuf.X-1, conbuf.Y-1);
if(winmov)
SetWindowPos(CmdGetConsoleWindow(), HWND_TOP, winpos.Left, winpos.Top, 0, 0, SWP_NOSIZE);
SetConsoleScreenBufferSize(hConsole, conbuf);
SetConsoleWindowInfo(hConsole, TRUE, &conpos);
SetConsoleScreenBufferSize(hConsole, conbuf);
SetConsoleWindowInfo(hConsole, TRUE, &conpos);
return 0;
}