请高人改写GUI版本的Hello world!
关键字: helloworld改这个程序好久,没有头绪,请圈子里的老大给个示范。要保持是Unicode版本,没有黑黑的控制台。
C++代码如下,在Visual Studio 2005下编译通过:
#include <windows.h>
#include <tchar.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
/* Make the class name into a global variable */
TCHAR szClassName[ ] = TEXT("HelloWorld");
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_HREDRAW | CS_VREDRAW; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
TEXT("SDK Verision"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
CW_USEDEFAULT, /* The programs width */
CW_USEDEFAULT, /* and height in pixels */
NULL, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
TCHAR szHello[] = TEXT("Hello world!");
switch (message) /* handle the messages */
{
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps); //begin painting
GetClientRect(hwnd,&rect); //get the size of client area
DrawText(hdc,szHello,_tcslen(szHello),&rect,DT_CENTER); //show "hello world"
EndPaint(hwnd,&ps); //end painting
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
评论
编译
内容为:
EXETYPE NT
SUBSYSTEM WINDOWS,5.0
* dmd main.d
* 请存为main.d,并且在UtrlEdit中将格式转换为UTF-8
* 我测试用bud编译时,链接不了,但是用dmd直接编译链接是可以地
*/
import std.c.windows.windows;
import std.c.stdio;
extern(Windows){
int DrawTextA(
HDC hDC, // handle to device context
LPCSTR lpString, // pointer to string to draw
int nCount, // string length, in characters
LPRECT lpRect, // pointer to struct with formatting dimensions
UINT uFormat // text-drawing flags
);
int DrawTextW(
HDC hDC, // handle to device context
LPCWSTR lpString, // pointer to string to draw
int nCount, // string length, in characters
LPRECT lpRect, // pointer to struct with formatting dimensions
UINT uFormat // text-drawing flags
);
}
extern(Windows)
int WindowProc(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
PAINTSTRUCT ps;
HDC dc = BeginPaint(hWnd, &ps);
RECT r;
GetClientRect(hWnd, &r);
wchar[] greeting = "这是一个测试!";
DrawTextW(dc,greeting.ptr,greeting.length,&r,0); //show "hello world"
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProcA(hWnd, uMsg, wParam, lParam);
}
int doit()
{
HINSTANCE hInst = GetModuleHandleA(null);
WNDCLASS wc;
wc.lpszClassName = "DWndClass";
wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = &WindowProc;
//wc.lpfnWndProc=nnd;
wc.hInstance = hInst;
wc.hIcon = LoadIconA(cast(HINSTANCE) null, IDI_APPLICATION);
wc.hCursor = LoadCursorA(cast(HINSTANCE) null, IDC_ARROW);
wc.hbrBackground = cast(HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = null;
wc.cbClsExtra = wc.cbWndExtra = 0;
wc.hbrBackground = cast(HBRUSH) COLOR_BACKGROUND;
auto a = RegisterClassA(&wc);
assert(a);
HWND hWnd, btnClick, btnDontClick;
hWnd = CreateWindowA("DWndClass", "Just a window", WS_THICKFRAME |
WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, HWND_DESKTOP,
cast(HMENU) null, hInst, null);
assert(hWnd);
MSG msg;
while (GetMessageA(&msg, cast(HWND) null, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
return 1;
}
/**********************************************************/
/* Note the similarity of this code to the console D startup
* code in \dmd\src\phobos\dmain2.d
* You'll also need a .def file with at least the following in it:
* EXETYPE NT
* SUBSYSTEM WINDOWS
*/
int main()
{
int result;
try
{
result = doit(); // insert user code here
}
catch (Object o) // catch any uncaught exceptions
{
MessageBoxA(null, cast(char *)o.toString(), "Error",MB_OK | MB_ICONEXCLAMATION);
result = 0; // failed
}
return result;
}
phobos库导入以A为结尾的API,NT内核会把这样的API自动转换成以W为结尾的,虽然不是所有的API都会转换。
unicode 的只要把 API 换成 W 结尾的不就行了,不过 phobos 里没有声明 unicode API 的原型,你可以选择使用 tango 或用 dousrce.org/projects/bindings 里的 win32 项目
发表评论
- 浏览: 53679 次
- 性别:

- 来自: 湖北武汉

- 详细资料
搜索本博客
我的相册
共 2 张
链接
最新评论
-
小结一下众高手的解答
基本类型的常量都是在符号表中,修改的时候会在内存产生一个临时变量,修改的是这个变 ...
-- by soulmachine -
小结一下众高手的解答
引用12. const数据可以被非const引用修改import std.std ...
-- by beyking -
争得好凶,不过过瘾!
设计!设计!好的分析和设计才最重要!
-- by RyanPoy -
高质量软件设计指南-C++/C ...
非常恶心的书,非常恶心的作者
-- by wdx04 -
使用netbeans 的五大理由
IDEA的生产率最高,不过5.5开始NetBean对jsf的支持实在很不错。
-- by fight_bird






评论排行榜