启动Win32 API [英] Starting Win32 API

查看:107
本文介绍了启动Win32 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,..
我正试图在Visual Studio 2008 .NET的Win32应用程序中创建一个窗口...代码在这里

Hai,..
I was tring to create a window in a Win32 Application in Visual Studio 2008 .NET... The code is here

#include<windows.h>
int WINAPI WinMain(HINSTANCE hi,HINSTANCE pi,LPSTR str,int i)
{
	HWND hwnd;
	WNDCLASSEX wcex;
	wcex.style=0;
	wcex.lpszClassName="Hello";
	wcex.lpszMenuName=NULL;
	wcex.cbSize=sizeof(&wcex);
	wcex.cbClsExtra=NULL;
	wcex.cbWndExtra=NULL;
	RegisterClassEx(&wcex);
	hwnd=CreateWindowEx(NULL,"Hello","Hai",WS_OVERLAPPEDWINDOW,0,0,500,400,NULL,NULL,hi,NULL);
	ShowWindow(hwnd,i);
	UpdateWindow(hwnd);
	return 0;
}


这显示执行时没有错误...但是没有窗口出现,并且应用程序退出时说(本机已退出,代码为0(0x0)),...

几个月前,我已经在旧版本(Visual Studio 6)中成功创建了一些应用程序,并且在Visual Studio 2008 .NET中也没有进行直接字符串分配.编译器是否可能存在任何问题,或者是否需要某些项目设置.

我已经尝试过简单的消息框显示..并且可以正常工作..所以问题一定是在代码中...我检查了断点,我认为应用程序终止是由于窗口句柄创建错误( HWND)..实际的问题是什么...帮帮我..

谢谢...
Dinesh Balu


This shows no errors on execution... but no window appeared and the application exited says(Native has exited with code 0 (0x0)),...

I have created some applications successfully some months before in some older version ( Visual studio 6) and Direct String assignment is also not taken in this Visual studio 2008 .NET,.,. Is there can be any Problems with the compiler or Some project settings needed.

I have tried that for simple Message Box display..and that''s working.. so the problem must be in code... I check with breakpoints and I think the application termination is due to error in creation of handle for window (HWND).. What''s the actual problem ... help me..

Thanks You...
Dinesh Balu

推荐答案

您确实需要多读一些关于为什么将函数参数定义为指针的内容.它应该是这样的:
You really need to do some more reading about why a function parameter is defined as a pointer. This is what it should look like:
    MSG msg;
// ...
while(GetMessage(&msg,hwnd,NULL,NULL))
{
    DispatchMessage(&msg);
    TranslateMessage(&msg);
}



对照此检查您的代码:
Hi,
Check your code against this:
// Mini.cpp : minimal Win32 application.
#include<windows.h>
#include <tchar.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (message == WM_DESTROY)
		PostQuitMessage(0);
	return DefWindowProc(hWnd, message, wParam, lParam);
}

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int nCmdShow)
{
	LPCTSTR psText = _T("Hello");
	WNDCLASSEX wc = { sizeof WNDCLASSEX, 0, WndProc, 0, 0, hInstance, LoadIcon(NULL, IDI_APPLICATION), 
		LoadCursor(NULL, IDC_ARROW), GetSysColorBrush(COLOR_WINDOW), NULL, _T("Mini") };
	if (RegisterClassEx(&wc))
		if (HWND hWnd = CreateWindow(wc.lpszClassName, psText, WS_OVERLAPPEDWINDOW, 
			CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL))
		{
			ShowWindow(hWnd, nCmdShow);
			UpdateWindow(hWnd);
			MSG msg = {0};
			while (GetMessage(&msg, NULL, 0, 0))
				DispatchMessage(&msg);
			return (int)msg.wParam;
		}
		else 
			psText = _T("CreateWindow failed");
	else
		psText = _T("RegisterClass failed");

	return MessageBox(NULL, psText, wc.lpszClassName, MB_OK | MB_ICONHAND);
}


欢呼声,
AR


cheers,
AR


您应该阅读有关窗口过程 [ ^ ].您当前没有代码处理事件,是消息循环 [ ^ ],以及代码立即退出.
You should read some about window procedures[^]. You currently have no code handling events, a message loop[^], and the code exits immediately.


这篇关于启动Win32 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆