`
soulmachine
  • 浏览: 109802 次
  • 性别: Icon_minigender_1
  • 来自: 湖北武汉
社区版块
存档分类
最新评论

请高人改写GUI版本的Hello world!

阅读更多

改这个程序好久,没有头绪,请圈子里的老大给个示范。要保持是Unicode版本,没有黑黑的控制台。

C++代码如下,在Visual Studio 2005下编译通过:

#include <windows.h><windows.h></windows.h>
#include <tchar.h><tchar.h></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;
}

分享到:
评论
13 楼 tomqyp 2007-04-29  
编译时加上 -L/exet:nt/su:windows:4.0这个开关就没有控制台了吧
12 楼 smithfox 2007-04-28  
用 dmd main.d main.def
编译
11 楼 smithfox 2007-04-28  
还要写一个main.def
内容为:
EXETYPE NT
SUBSYSTEM WINDOWS,5.0
10 楼 smithfox 2007-04-28  
/* Compile with:
* 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;
}
9 楼 oldrev 2007-04-28  
D程序内部一般用utf-8,A结尾API用的是MBS,转换起来很麻烦,std.windows.registry就是没考虑到这点所以不支持中文。要用Win32API 还是用 W 的好,toUtf16z 就行了,这样避免NT内核再转换一次效率也比较高
8 楼 Colorful 2007-04-28  
为什么不能用W的WinAPI?
phobos库导入以A为结尾的API,NT内核会把这样的API自动转换成以W为结尾的,虽然不是所有的API都会转换。
7 楼 shawind 2007-04-28  
是的,有很多W的winapi不能用。
6 楼 soulmachine 2007-04-28  
好多Win32 API都不能用,让人郁闷啊
5 楼 oldrev 2007-04-28  
编译的时候加上 winsamp.def,就没有控制台了
unicode 的只要把 API 换成 W 结尾的不就行了,不过 phobos 里没有声明 unicode API  的原型,你可以选择使用 tango 或用 dousrce.org/projects/bindings 里的 win32 项目
4 楼 soulmachine 2007-04-28  
那个例子首先不是unicode版本的,其次启动时有个黑黑的控制台
3 楼 oldrev 2007-04-28  
/dmd/samples/winsamp.d
2 楼 qiezi 2007-04-28  
DMD里不是有个例子嘛。
1 楼 soulmachine 2007-04-28  
开头是#include <windows.h>和#include <tchar.h>,怎么无故不见了?

相关推荐

Global site tag (gtag.js) - Google Analytics