求解决一个win32显示窗口的问题

sam132333 2011-11-14 03:06:31
我在VS6.0下用win32 application创建了一个工程,文件内容如下

#include <windows.h>
#include <stdio.h>

long CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
WNDCLASSEX stMyclass;
MSG stMsg; HWND hWindow;
//char* ClassName = "MyClass";
//char* CaptionName = "时钟";

// 注册窗口类
RtlZeroMemory(&stMyclass, sizeof(stMyclass));
stMyclass.cbSize = sizeof(stMyclass);
stMyclass.hInstance = hInstance;
stMyclass.hIcon = LoadIcon(0, IDI_APPLICATION);
stMyclass.hIconSm = LoadIcon(0, IDI_APPLICATION);
stMyclass.hCursor = LoadCursor(0, IDC_ARROW);
stMyclass.lpszClassName = "MyClass";
stMyclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
stMyclass.style = CS_HREDRAW | CS_VREDRAW;
stMyclass.lpfnWndProc = WinProc;
RegisterClassEx(&stMyclass);
// 创建窗口
hWindow = CreateWindowEx(WS_EX_CLIENTEDGE, "MyClass", "Clock",
WS_OVERLAPPEDWINDOW, 200, 200, 200, 200, NULL, NULL, hInstance, NULL);
ShowWindow(hWindow, SW_SHOWNORMAL);
UpdateWindow(hWindow);
// 消息循环
while (GetMessage(&stMsg, NULL, 0, 0))
{
TranslateMessage(&stMsg);
DispatchMessage(&stMsg);
}
return 0;
}

long CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_CREATE)
{
MessageBox(hWnd, "创建窗口", "aaa", MB_OK);
}
else if (uMsg == WM_CLOSE)
{
PostQuitMessage(0);
DestroyWindow(hWnd);
}
else
DefWindowProc(hWnd, uMsg, wParam, lParam);

return 0;
}


编译一点问题没有,为啥运行后窗口就是不出来呢。。。
...全文
84 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sam132333 2011-11-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 sunyongliang118 的回复:]

把你的窗口过程函数的最后一个语句return 0;改成return DefWindowProc(hWnd, uMsg, wParam, lParam);
C/C++ code

long CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_CREATE)
{
Mess……
[/Quote]

谢谢你的回答,但分已经给3L的兄弟了,不好意思O(∩_∩)O~
振翅高飞 2011-11-14
  • 打赏
  • 举报
回复
把你的窗口过程函数的最后一个语句return 0;改成return DefWindowProc(hWnd, uMsg, wParam, lParam);

long CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_CREATE)
{
MessageBox(hWnd, "创建窗口", "aaa", MB_OK);
}
else if (uMsg == WM_CLOSE)
{
PostQuitMessage(0);
DestroyWindow(hWnd);
}
else
DefWindowProc(hWnd, uMsg, wParam, lParam);

return DefWindowProc(hWnd, uMsg, wParam, lParam);
}


改完后的全部代码为:

#include <windows.h>
#include <stdio.h>

long CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
WNDCLASSEX stMyclass;
MSG stMsg; HWND hWindow;
//char* ClassName = "MyClass";
//char* CaptionName = "时钟";

// 注册窗口类
RtlZeroMemory(&stMyclass, sizeof(stMyclass));
stMyclass.cbSize = sizeof(stMyclass);
stMyclass.hInstance = hInstance;
stMyclass.hIcon = LoadIcon(0, IDI_APPLICATION);
stMyclass.hIconSm = LoadIcon(0, IDI_APPLICATION);
stMyclass.hCursor = LoadCursor(0, IDC_ARROW);
stMyclass.lpszClassName = "MyClass";
stMyclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
stMyclass.style = CS_HREDRAW | CS_VREDRAW;
stMyclass.lpfnWndProc = WinProc;
RegisterClassEx(&stMyclass);
// 创建窗口
hWindow = CreateWindowEx(WS_EX_CLIENTEDGE, "MyClass", "Clock",
WS_OVERLAPPEDWINDOW, 200, 200, 200, 200, NULL, NULL, hInstance, NULL);
ShowWindow(hWindow, SW_SHOWNORMAL);
UpdateWindow(hWindow);
// 消息循环
while (GetMessage(&stMsg, NULL, 0, 0))
{
TranslateMessage(&stMsg);
DispatchMessage(&stMsg);
}
return 0;
}

long CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_CREATE)
{
MessageBox(hWnd, "创建窗口", "aaa", MB_OK);
}
else if (uMsg == WM_CLOSE)
{
PostQuitMessage(0);
DestroyWindow(hWnd);
}
else
DefWindowProc(hWnd, uMsg, wParam, lParam);

return DefWindowProc(hWnd, uMsg, wParam, lParam);
}


记得把分给我。
sam132333 2011-11-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 gameslq 的回复:]

C/C++ code
long CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_CREATE)
{
MessageBox(hWnd, "创建窗口", "aaa", MB_OK);
}
……
[/Quote]

明白了,thank you
sam132333 2011-11-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 gameslq 的回复:]

C/C++ code
long CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_CREATE)
{
MessageBox(hWnd, "创建窗口", "aaa", MB_OK);
}
……
[/Quote]

呵呵,我的最终版本也是这么干的,谢谢了啊!
gameslq 2011-11-14
  • 打赏
  • 举报
回复
long CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_CREATE)
{
MessageBox(hWnd, "创建窗口", "aaa", MB_OK);
}
else if (uMsg == WM_CLOSE)
{
PostQuitMessage(0);
DestroyWindow(hWnd);
}
else
return DefWindowProc(hWnd, uMsg, wParam, lParam); //改这一句增加 return

return 0;
}

调用 DefWindowProc 后不要再 reuturn 0,否则系统认为“无事可干”了。
sam132333 2011-11-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 visualeleven 的回复:]

C/C++ code
long CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_CREATE)
{
MessageBox(hWnd, "创建窗口", "aaa", MB_OK);
}
else if (uMsg == WM_CLOSE)
{
PostQuit……
[/Quote]

我觉得照你这么做某些消息是不是会被处理两遍,求解?
sam132333 2011-11-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 visualeleven 的回复:]

C/C++ code
long CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_CREATE)
{
MessageBox(hWnd, "创建窗口", "aaa", MB_OK);
}
else if (uMsg == WM_CLOSE)
{
PostQuit……
[/Quote]

谢谢哈,明白了,改成这样子也可以,是吧O(∩_∩)O~

long CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_CREATE)
{
MessageBox(hWnd, "创建窗口", "aaa", MB_OK);
}
else if (uMsg == WM_CLOSE)
{
PostQuitMessage(0);
DestroyWindow(hWnd);
}
else
{
DefWindowProc(hWnd, uMsg, wParam, lParam);
return 1;
}

return 0;
}
sam132333 2011-11-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 nicklisir 的回复:]

WM_CREATE返回0;你说,它还能出来吗?
WinProc设计有问题
[/Quote]

我问的就是为什么返回0,我用win32汇编写运行都是正常的
Eleven 2011-11-14
  • 打赏
  • 举报
回复
long CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_CREATE)
{
MessageBox(hWnd, "创建窗口", "aaa", MB_OK);
}
else if (uMsg == WM_CLOSE)
{
PostQuitMessage(0);
DestroyWindow(hWnd);
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);;
}
nicklisir 2011-11-14
  • 打赏
  • 举报
回复
WM_CREATE返回0;你说,它还能出来吗?
WinProc设计有问题
sam132333 2011-11-14
  • 打赏
  • 举报
回复
CreateWindowEx返回的hWindow是0,有木有人说说呀

15,976

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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