Win32API Window and Tango

Zarathustra adam.chrapkowski at gmail.com
Wed Aug 27 07:24:52 PDT 2008


How to create WIN32Window with Tango?
Why window handle is null?

module test;
import tango.sys.win32.UserGdi;
 
extern (C) void rt_init( void delegate(Exception) dg = null );
extern (C) void rt_term( void delegate(Exception) dg = null );

extern(Windows)HWND    CreateWindowExA(DWORD, LPCSTR, LPCSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID);
extern(Windows)HGDIOBJ GetStockObject(int);

//_____________________________________________________________________________________________________________________
//	C functions
// 

// function WinMain
//_____________________________________________________________________________________________________________________
extern (Windows) int 
WinMain(HINSTANCE o_hInstance, HINSTANCE o_hPrevInstance, LPSTR o_lpCmdLine, int o_nCmdShow){

  rt_init();

	int l_result;
 
	try{
		l_result = Main(o_hInstance, o_hPrevInstance, o_lpCmdLine, o_nCmdShow);
	}
	catch(Object o){
		MessageBoxA(null, cast(char*)o.toString(), "Fatal Error", MB_OK | MB_ICONERROR);
		l_result = 0;
	}
	rt_term(); 
	return l_result;
}

// function WindowProc
//_____________________________________________________________________________________________________________________
extern (Windows) int
WindowProc(HWND o_hWindow, uint o_message, WPARAM o_wParam, LPARAM o_lParam){
	switch(o_message){
	
		case WM_CREATE:
			return 0;
					
		case WM_CLOSE:
			PostQuitMessage(0);
			return 0;
	
		case WM_DESTROY:
			return 0;
	
		case WM_KEYDOWN:
			switch(o_wParam){
				default:
					return 0;
			}
			return 0;
	
		default:
			return DefWindowProcA(o_hWindow, o_message, o_wParam, o_lParam);
  }
}

//_____________________________________________________________________________________________________________________
//	D functions
//

// function Main
//_____________________________________________________________________________________________________________________
int 
Main(HINSTANCE o_hInstance, HINSTANCE o_hPrevInstance,	LPSTR o_lpCmdLine, int o_nCmdShow){

	// register window class
	//____________________________________________________________________
	WNDCLASS l_cWindow;
	with(l_cWindow){
		style         = CS_OWNDC;
		lpfnWndProc   = &WindowProc;
		cbClsExtra    = 0;
		cbWndExtra    = 0;
		hInstance     = hInstance;
		hIcon         = LoadIconA(cast(HINSTANCE)null, IDI_APPLICATION);
		hCursor       = LoadCursorA(cast(HINSTANCE)null, IDC_ARROW);
		hbrBackground = cast(HBRUSH)GetStockObject(BLACK_BRUSH);
		lpszMenuName  = null;
		lpszClassName = "WINDOWCLASS";
	}
	assert(RegisterClassA(&l_cWindow));	

	// create main window
	//____________________________________________________________________
	HWND l_hWindow = CreateWindowExA(
		0,
		"WINDOWCLASS",
		"WINDOWCLASS",
		WS_CAPTION | WS_VISIBLE | WS_OVERLAPPEDWINDOW,
		100, 
		100, 
		640, 
		640,
		HWND_DESKTOP,
		cast(HMENU)null,
		o_hInstance,
		null
	);
	assert(l_hWindow);	// <- error l_hWindow is null
	
	// show window
	//____________________________________________________________________
	ShowWindow(l_hWindow, SW_SHOW); 
	UpdateWindow(l_hWindow); 

	// program main loop
	//____________________________________________________________________
	MSG l_message;
	msgsPomp:
	while(true){
		
    // check for messages  
    if(PeekMessageA(&l_message, cast(HWND)null, 0, 0, PM_REMOVE)){ 
		
      // handle or dispatch messages
      if(l_message.message == WM_QUIT){ 
        break msgsPomp; 
      } 
      else{ 
        TranslateMessage(&l_message); 
        DispatchMessageA(&l_message); 
      } 
    } 
    else{
			Sleep(20);
		} 
  } 
	return l_message.wParam;
} 


More information about the Digitalmars-d-learn mailing list