hWnd = CreateWindow()这句话为什么返回0啊?

wx376752150 2010-03-03 03:55:33
#include<windows.h>

LRESULT WINAPI WindowProc(HWND HWnd,UINT message,WPARAM wParam,LPARAM lParam);

//Insert code for WinMain() here (Listing OFWIN_1)
//Listing OFWIN_1
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevINstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WindowClass;

static LPCTSTR szAppName= L"OFWin";
HWND hWnd;
MSG msg;

WindowClass.cbSize=sizeof(WNDCLASSEX);

//Redraw the window if the size changes
WindowClass.style = CS_HREDRAW | CS_VREDRAW;

//Define the message handling function
WindowClass.lpfnWndProc=WindowProc;

WindowClass.cbClsExtra=0;
WindowClass.cbWndExtra=0;

WindowClass.hInstance=hInstance;

//Set default application icon
WindowClass.hIcon = LoadIcon(0,IDI_APPLICATION);

//Set window cursor to be the standard arrow
WindowClass.hCursor = LoadCursor(0,IDC_ARROW);

//set gray brush for background color
//WindowClass.hbrBackground= static_cast<HBRUSH>(GetStockObject(GRAY_BRUSH));
WindowClass.hbrBackground = CreateSolidBrush(RGB(0,0,255));
WindowClass.lpszMenuName = 0;
WindowClass.lpszClassName= szAppName;
WindowClass.hIcon=0;

//now register our window class
RegisterClassEx(&WindowClass);

//now we can create the window
hWnd = CreateWindow(
szAppName,
L"a basic window the hard way",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);


ShowWindow(hWnd,SW_MAXIMIZE); //display the window
UpdateWindow(hWnd);

//The message loop
while(GetMessage(&msg,0,0,0)==TRUE)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return static_cast<int>(msg.wParam);
}



//Insert code for WindowProc() here (Listing OFWIN_2)
//Listing OFWIN_2
LRESULT WINAPI WindowProc(HWND hWnd,UINT message,WPARAM wParam ,LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT PaintSt;
RECT aRect;

switch(message)
{
case WM_PAINT:
hDC = BeginPaint(hWnd,&PaintSt);

//Get upper left and lower right of client area
GetClientRect(hWnd,&aRect);

SetBkMode(hDC,TRANSPARENT);

//Now draw the text in window client area
DrawText(
hDC,
L"But,soft!what light through yonder window breaks?",
-1,
&aRect,
DT_SINGLELINE|DT_CENTER|DT_VCENTER);

EndPaint(hWnd,&PaintSt);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;

default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
}
...全文
332 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wx376752150 2010-03-09
  • 打赏
  • 举报
回复
引用 8 楼 pgplay 的回复:
WindowClass.hIcon=0; //是这里的有问题,应该WindowClass.hIconSm = 0;

大哥你说的对了,不过能告诉我这两者有啥区别吗?
耍宝王 2010-03-09
  • 打赏
  • 举报
回复
引用 9 楼 wx376752150 的回复:
大哥你说的对了,不过能告诉我这两者有啥区别吗?

hIcon 窗口类的图标(按Alt + Tab切换程序时看到的图标),如果hIconSm设为0,那它也是窗口左上角和任务栏上的图标
hIconSm 窗口类相关联的一个小图标,就是窗口左上角和任务栏上的图标,如果设为0,则自动搜索hIcon中相关的小图标

你可以设成:
WindowClass.hIcon = LoadIcon (NULL, IDI_QUESTION);//按Alt + Tab切换程序时看到的图标为问号
WindowClass.hIconSm = LoadIcon (NULL, DI_ERROR);//窗口左上角和任务栏上的图标为红叉
然后看看窗口打开后的左上角和任务栏上的图标,然后按Alt + Tab切换程序,看代表此程序的图标
wx376752150 2010-03-04
  • 打赏
  • 举报
回复
引用 3 楼 pgplay 的回复:
请LZ你是用什么IDE啊?
static LPCTSTR szAppName= L"OFWin";
L"a basic window the hard way"
由于你的类名和窗口名都定义为宽字符
相应的你要用CreateWindowW
如果你的IDE或你没有启用UNICODE宏的话,那CreateWindow映射的将是CreateWindowA
你可以直接使用CreateWindowW
或者把类名和窗口名改为
static LPCTSTR szAppName= TEXT("OFWin");
TEXT("a basic window the hard way")
试试看

试过了,没用啊,还有解答吗
耍宝王 2010-03-04
  • 打赏
  • 举报
回复
刚才的排版有点问题,详细位置看这里
WindowClass.hIcon = LoadIcon(0,IDI_APPLICATION);

//Set window cursor to be the standard arrow
WindowClass.hCursor = LoadCursor(0,IDC_ARROW);

//set gray brush for background color
//WindowClass.hbrBackground= static_cast <HBRUSH>(GetStockObject(GRAY_BRUSH));
WindowClass.hbrBackground = CreateSolidBrush(RGB(0,0,255));
WindowClass.lpszMenuName = 0;
WindowClass.lpszClassName= szAppName;
WindowClass.hIcon=0; //是这里的有问题,应该WindowClass.hIconSm = 0;
耍宝王 2010-03-04
  • 打赏
  • 举报
回复
除了我说的,还有就是你的代码中
WindowClass.hIcon = LoadIcon(0,IDI_APPLICATION);
WindowClass.hIcon = 0;
这里应该是
WindowClass.hIconSm = 0;

下面是从Code::Blocks的工程模板中复制出来的,如果还不行,可以参考一下
#include <windows.h>

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
char szClassName[ ] = "CodeBlocksWindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
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_DBLCLKS; /* 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 */
"Code::Blocks Template Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* 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)
{
switch (message) /* handle the messages */
{
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;
}
zero_226 2010-03-03
  • 打赏
  • 举报
回复
返回0也不是没有可能,hWnd是一个句柄,如果Createwindow函数调用失败,有可能就返回0,你可以仔细看下createwindow的返回值说明。
kakam 2010-03-03
  • 打赏
  • 举报
回复
楼上说得很对楼上说得很对
耍宝王 2010-03-03
  • 打赏
  • 举报
回复
请LZ你是用什么IDE啊?
static LPCTSTR szAppName= L"OFWin";
L"a basic window the hard way"
由于你的类名和窗口名都定义为宽字符
相应的你要用CreateWindowW
如果你的IDE或你没有启用UNICODE宏的话,那CreateWindow映射的将是CreateWindowA
你可以直接使用CreateWindowW
或者把类名和窗口名改为
static LPCTSTR szAppName= TEXT("OFWin");
TEXT("a basic window the hard way")
试试看
ypb362148418 2010-03-03
  • 打赏
  • 举报
回复
贴出错误,你可以查查MSDN对这个函数返回值的讲解
hazrael 2010-03-03
  • 打赏
  • 举报
回复
取一下错误值,看是什么

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧