代码
#include <iostream>
#include <windows.h>
#include <windowsx.h>
#include <string>
#include <fstream>
#include <sstream>
const int Button_Open_ID = 10001;
const int Button_Save_ID = 10002;
const int Button_Run_ID = 10003;
const int Edit_Path_ID = 10004;
const int Edit_Code_ID = 10005;
class Widget
{
public:
HWND hwnd = NULL;
float x = 0;
float y = 0;
float width = 0;
float height = 0;
public:
void setText(std::string text)
{
SetWindowText(hwnd, text.data());
}
std::string getText()
{
int length = GetWindowTextLength(hwnd) + 1;
char* text = new char[length];
GetWindowText(hwnd, text, length);
return std::string(text);
}
void setSize(int w, int h)
{
int nx = x * float(w);
int ny = y * float(h);
int nw = width * float(w);
int nh = height * float(h);
MoveWindow(hwnd, nx, ny, nw, nh, false);
}
};
HWND mainWindow = NULL;
Widget Button_Open;
Widget Button_Save;
Widget Button_Run;
Widget Edit_Path;
Widget Edit_Code;
std::string filepath = "";
HWND initWindow(HINSTANCE instance);
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
void onCreate(HWND hwnd, CREATESTRUCT* cs);
void onDestroy(HWND hwnd);
void onCommand(HWND hwnd, int id, int event, HWND widget);
void onWindowPosChanging(HWND hwnd);
std::string getFileText();
void saveToFile();
void runCode();
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR cmdLine, int cmdShow)
{
mainWindow = initWindow(hInstance);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
HWND initWindow(HINSTANCE instance)
{
WNDCLASSEX wc;
memset(&wc, 0, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.hInstance = instance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "WindowClass";
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClassEx(&wc);
return CreateWindowEx(WS_EX_CLIENTEDGE, "WindowClass", "CppComplier", WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, instance, NULL);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg)
{
case WM_CREATE:
onCreate(hwnd, (LPCREATESTRUCT)lp);
break;
case WM_DESTROY:
onDestroy(hwnd);
break;
case WM_COMMAND:
onCommand(hwnd, LOWORD(wp), HIWORD(wp), (HWND)lp);
break;
case WM_WINDOWPOSCHANGING:
onWindowPosChanging(hwnd);
break;
case WM_WINDOWPOSCHANGED:
onWindowPosChanging(hwnd);
break;
default:
return DefWindowProc(hwnd, msg, wp, lp);
break;
}
return 0;
}
void onCreate(HWND hwnd, CREATESTRUCT* cs)
{
float w = 640;
float h = 480;
Button_Open =
{
CreateWindowEx(WS_EX_WINDOWEDGE, "button", "Open", WS_VISIBLE | WS_CHILD | WS_BORDER | BS_CENTER | BS_VCENTER, 340, 0, 100, 50, hwnd, (HMENU)Button_Open_ID, cs->hInstance, NULL),
340.0f / w,
0.0f / h,
100.0f / w,
50.0f / h,
};
Button_Save =
{
CreateWindowEx(WS_EX_WINDOWEDGE, "button", "Save", WS_VISIBLE | WS_CHILD | WS_BORDER | BS_CENTER | BS_VCENTER, 440, 0, 100, 50, hwnd, (HMENU)Button_Save_ID, cs->hInstance, NULL),
440.0f / w,
0.0f / h,
100.0f / w,
50.0f / h,
};
Button_Run =
{
CreateWindowEx(WS_EX_WINDOWEDGE, "button", "Run", WS_VISIBLE | WS_CHILD | WS_BORDER | BS_CENTER | BS_VCENTER, 540, 0, 100, 50, hwnd, (HMENU)Button_Run_ID, cs->hInstance, NULL),
540.0f / w,
0.0f / h,
100.0f / w,
50.0f / h,
};
Edit_Path =
{
CreateWindowEx(WS_EX_WINDOWEDGE, "edit", "", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT | ES_MULTILINE, 0, 0, 340, 50, hwnd, (HMENU)Edit_Path_ID, cs->hInstance, NULL),
0.0f / w,
0.0f / h,
340.0f / w,
50.0f / h,
};
Edit_Code =
{
CreateWindowEx(WS_EX_WINDOWEDGE, "edit", "", WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|ES_MULTILINE|ES_WANTRETURN, 0, 50, 630, 400, hwnd, (HMENU)Edit_Code_ID, cs->hInstance, NULL),
0.0f / w,
50.0f / h,
630.0f / w,
400.0f / h,
};
}
void onDestroy(HWND hwnd)
{
PostQuitMessage(0);
}
void onCommand(HWND hwnd, int id, int event, HWND widget)
{
switch (id)
{
case Button_Open_ID:
Edit_Code.setText(getFileText());
break;
case Button_Save_ID:
saveToFile();
break;
case Button_Run_ID:
runCode();
break;
}
}
void onWindowPosChanging(HWND hwnd)
{
RECT rect;
GetWindowRect(hwnd, &rect);
int w = rect.right - rect.left;
int h = rect.bottom - rect.top;
//std::cout << w << "\t" << h << std::endl;
Button_Open.setSize(w, h);
Button_Save.setSize(w, h);
Button_Run.setSize(w, h);
Edit_Path.setSize(w, h);
Edit_Code.setSize(w, h);
}
std::string getFileText()
{
std::stringstream text;
filepath = Edit_Path.getText();
if (filepath.length() > 0)
{
std::ifstream fin;
fin.open(filepath);
std::string temp;
while (getline(fin, temp))
{
text << temp << "\r\n";
}
fin.close();
}
return text.str();
}
void saveToFile()
{
filepath = Edit_Path.getText();
if (filepath.length() > 0)
{
std::ofstream fout;
fout.open(filepath);
fout << Edit_Code.getText();
fout.close();
}
}
void runCode()
{
filepath = Edit_Path.getText();
if (filepath.length() > 0)
{
if (Edit_Code.getText().length() > 0)
{
saveToFile();
std::string newpath = filepath;
int pos = newpath.rfind(".");
newpath = newpath.substr(0,pos + 1);
newpath.append("exe");
//std::cout << newpath << std::endl;
std::stringstream command;
command << "mingw\\bin\\g++.exe " << filepath << " -o " << newpath;
system(command.str().data());
std::cout << "--------------------\n";
system(newpath.data());
//system("pause");
}
}
}
效果图
p.s.请把mingw64文件夹与程序放在同一个目录里或者直接改成command << "g++ " << filepath << " -o " << newpath;
(没试过,但肯定需要配置环境变量,参考这篇文章)
p.s.需要先对命令提示符窗口输入,否则主窗口程序会卡死
p.s.不要用ShowWindow(GetConsoleWindow(),SW_HIDE);
隐藏控制台窗口,不然也会卡死
p.s.把代码往里面一保存就长这样了
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!\n";
return 0;
}
不知道为什么