偶然使用FLTK时发现中文乱码
解决方法
直接从编译器入手,在代码中添加一行
#pragma execution_character_set("utf-8")
看!不乱码了吧
顺便把源码放在这里,有需要的朋友们可以自取:
#include <iostream>
#include <string>
#include <FL/Fl.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Repeat_Button.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#pragma execution_character_set("utf-8")
using namespace std;
namespace Examples {
class Main_Window : public Fl_Window {
public:
Main_Window() : Fl_Window{ 200, 100, 800, 600, "Button example" } {
button1.align(FL_ALIGN_INSIDE | FL_ALIGN_CLIP | FL_ALIGN_WRAP);
button1.callback([](Fl_Widget* sender, void* window) {
auto result = "Button 1 点击 " + to_string(++reinterpret_cast<Main_Window*>(window)->button1_clicked) + " times";
reinterpret_cast<Main_Window*>(window)->box1.copy_label(result.c_str());
}, this);
button2.align(FL_ALIGN_INSIDE | FL_ALIGN_CLIP | FL_ALIGN_WRAP);
button2.callback([](Fl_Widget* sender, void* window) {
auto result = "Button 2 点击 " + to_string(++reinterpret_cast<Main_Window*>(window)->button2_clicked) + " times";
reinterpret_cast<Main_Window*>(window)->box2.copy_label(result.c_str());
}, this);
box1.align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE | FL_ALIGN_CLIP);
box2.align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE | FL_ALIGN_CLIP);
}
private:
Fl_Button button1{ 50, 50, 75, 25, "按钮 1" };
Fl_Repeat_Button button2{ 50, 100, 200, 75, "按钮 2" };
Fl_Box box1{ 50, 200, 200, 20, "Button 1 clicked 0 times" };
Fl_Box box2{ 50, 230, 200, 25, "Button 2 clicked 0 times" };
int button1_clicked = 0;
int button2_clicked = 0;
};
}
auto main(int argc, char* argv[]) -> int {
Examples::Main_Window window;
window.resizable(&window);
window.show(argc, argv);
return Fl::run();
}
最后我要说一句,what the fuck构式CSDN,sb的流氓网页设计!!!