【发布时间】:2010-10-20 02:27:33
【问题描述】:
适用于 Windows / Linux / Mac OS X 的 C++ 中的 API 级 Unicode GUI 本机应用程序。
我正在寻找编写一个简单的 Unicode、GUI、Native 应用程序,无需任何非标准库即可运行,用 GNU-GCC (g++) 编译的 C++ 编写。
不是
我不是指一个代码源在任何地方运行,而是 3 个(Win/Linux/Mac)代码源!无库运行(本机应用程序)。
-
原生应用
应用程序无需任何非标准库即可运行,只需要操作系统 C++ 运行时(如 Windows 上的 MSVCRT)。
-
Unicode 应用程序
从右到左的窗口布局(支持从右到左的阅读语言),有两个按钮 [Message] 在消息框中显示 UTF-8 字符串(“اهلا بالعالم”),以及 [Exit] 到。 ..我想退出! :p
Windows 解决方案(Windows 7)
- 编译器:MinGW g++ 4.5.0
- 命令行:
g++ -Wl,--enable-auto-import -O2 -fno-strict-aliasing -DWIN32_LEAN_AND_MEAN -D_UNICODE -DUNICODE -mwindows -Wall test.cpp -o test.exe
#include (windows.h)
#include (tchar.h)
#include (string)
typedef std::basic_string<TCHAR> ustring;
LONG StandardExtendedStyle;
TCHAR buffer_1[1024];
TCHAR buffer_2[1024];
static HWND button_1;
static HWND button_2;
inline int ErrMsg(const ustring& s)
{
return MessageBox(0,s.c_str(),_T("ERROR"),MB_OK|MB_ICONEXCLAMATION);
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
button_1=CreateWindow(L"button",L"UTF-8 Message",WS_CHILD|WS_VISIBLE,10,10,120,25,hwnd,(HMENU)1,NULL,NULL);
button_2=CreateWindow(L"button",L"Exit",WS_CHILD|WS_VISIBLE,10,50,120,25,hwnd,(HMENU)2,NULL,NULL);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case 1:
_stprintf(buffer_1,L"اهلا بالعالم");
_stprintf(buffer_2,L"Hello World in Arabic !");
MessageBoxW(hwnd,buffer_1,buffer_2,MB_ICONINFORMATION|MB_OK|MB_RTLREADING|MB_RIGHT);
break;
case 2:
PostQuitMessage(0);
break;
}break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR pStr,int nCmd)
{
ustring classname=_T("window");
WNDCLASSEX window={0};
window.cbSize = sizeof(WNDCLASSEX);
window.lpfnWndProc = WndProc;
window.hInstance = hInst;
window.hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(100), IMAGE_ICON, 16, 16, 0);
window.hCursor = reinterpret_cast<HCURSOR>(LoadImage(0,IDC_ARROW,IMAGE_CURSOR,0,0,LR_SHARED));
window.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BTNFACE+1);
window.lpszClassName = classname.c_str();
if (!RegisterClassEx(&window))
{
ErrMsg(_T("Failed to register wnd class"));return -1;
}
int desktopwidth=GetSystemMetrics(SM_CXSCREEN);
int desktopheight=GetSystemMetrics(SM_CYSCREEN);
HWND hwnd=CreateWindowEx(0,classname.c_str(),_T("The solution for Windows"),WS_OVERLAPPEDWINDOW,desktopwidth/4,desktopheight/4,270,150,0,0,
hInst,0);
if (!hwnd)
{
ErrMsg(_T("Failed to create wnd"));
return -1;
}
StandardExtendedStyle=GetWindowLong(hwnd,GWL_EXSTYLE);
SetWindowLong(hwnd,GWL_EXSTYLE,StandardExtendedStyle|WS_EX_LAYOUTRTL);
ShowWindow(hwnd,nCmd);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg,0,0,0)>0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return static_cast<int>(msg.wParam);
}
注意:此应用程序仅附带 MSVCRT.DLL,这意味着它是本机 Windows C++ 应用程序。
Linux 解决方案
请帮忙!
如何在不告诉用户安装这个和这个..原生 Linux 应用程序的情况下在 Linux 上运行应用程序!
- 文件格式必须是什么?精灵,斌...?
- X11 是原生 Linux GUI 库吗?或 WxWidgets、QT、GTK+、gtkmm.. ???!!!
- 可以在 Gnome 和 KDE 上运行吗?还是需要不同的代码源?
有人知道 Linux 的解决方案吗?
Mac OS X 的解决方案
请帮忙!
我认为 Mac OS X 的解决方案是 Cocoa in C++ with G++!但我不确定!
- G++ 能否使用 Cocoa 构建原生 Mac OS 应用程序?
【问题讨论】:
-
我认为您的意思是“API 级”应用程序,您可以编辑您的问题以指定它吗?另外,您所说的“完整”到底是什么意思?最简单的 Unicode API 级 Windows 应用只是在
main中调用MessageBox,仅此而已... -
是的,我的意思是 API 级别,Linux 上的等价物是什么? POSIX 和 GTK ? Mac 呢?可可?
标签: c++ user-interface utf-8 cross-platform native