| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 710 人关注过本帖
标题:为什么把窗口布局过程放函数里窗口就不显示了
只看楼主 加入收藏
永久的守护
Rank: 1
等 级:新手上路
威 望:2
帖 子:425
专家分:6
注 册:2007-6-9
结帖率:75%
收藏
 问题点数:0 回复次数:7 
为什么把窗口布局过程放函数里窗口就不显示了

#include <windows.h>

#define IDB_NUM8 118

HWND hButton_Exit ; //子窗口控件句柄
HWND hwnd ;
HINSTANCE hInst ;

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int winLayout(void) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,

PSTR szCmdLine, int iCmdShow)
{

static TCHAR szAppName[] = TEXT ("HelloWin") ;


MSG msg ;

WNDCLASS wndclass ;


wndclass.style = CS_HREDRAW | CS_VREDRAW ;

wndclass.lpfnWndProc = WndProc ;

wndclass.cbClsExtra = 0 ;

wndclass.cbWndExtra = 0 ;

wndclass.hInstance = hInstance ;

wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;

wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;

wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;

wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName= szAppName ;


if (!RegisterClass (&wndclass))
{

MessageBox ( NULL, TEXT ("This program requires Windows NT!"),

szAppName, MB_ICONERROR) ;

return 0 ;

}

hwnd = CreateWindow( szAppName, // window class name

TEXT ("窗口与函数"), // window caption

WS_OVERLAPPEDWINDOW, // window style

10,// initial x position

10,// initial y position

600,// initial x size

400,// initial y size

NULL, // parent window handle

NULL, // window menu handle

hInstance, // program instance handle

NULL) ; // creation parameters

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}

return msg.wParam ;

}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;

switch (message)
{

case WM_CREATE:

winLayout() ;
return 0 ;


case WM_PAINT:

hdc = BeginPaint (hwnd, &ps) ;

EndPaint (hwnd, &ps) ;

return 0 ;

case WM_DESTROY:

PostQuitMessage (0) ;

return 0 ;

}

return DefWindowProc (hwnd, message, wParam, lParam) ;

}

int winLayout(void)
{
hButton_Exit=CreateWindow("BUTTON", //建立按钮7
"退出",
WS_CHILD | WS_VISIBLE,
230,180,
80,30,
hwnd,
(HMENU) IDB_NUM8,
hInst,
NULL);
}

搜索更多相关主题的帖子: 窗口 
2007-09-29 23:03
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 
你要明白WM_CREATE比WinMain()还有早执行.所hwnd还是没效的.

=×&D o I p R e E n C g T l X&×=
2007-09-29 23:26
永久的守护
Rank: 1
等 级:新手上路
威 望:2
帖 子:425
专家分:6
注 册:2007-6-9
收藏
得分:0 

也就是说窗口布局必须
case WM_CREATE:

return 0 ;
里面吧

难道没别的好方法了?


见了便做做了便放下了了有何不了,慧生于觉觉生于自在生生还是无生。
活则生变,简而至稳。
2007-09-30 00:09
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,

PSTR szCmdLine, int iCmdShow)
{

static TCHAR szAppName[] = TEXT ("HelloWin") ;


MSG msg ;

WNDCLASS wndclass ;


wndclass.style = CS_HREDRAW | CS_VREDRAW ;

wndclass.lpfnWndProc = WndProc ;

wndclass.cbClsExtra = 0 ;

wndclass.cbWndExtra = 0 ;

wndclass.hInstance = hInstance ;

wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;

wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;

wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;

wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName= szAppName ;


if (!RegisterClass (&wndclass))
{

MessageBox ( NULL, TEXT ("This program requires Windows NT!"),

szAppName, MB_ICONERROR) ;

return 0 ;

}

hwnd = CreateWindow( szAppName, // window class name

TEXT ("窗口与函数"), // window caption

WS_OVERLAPPEDWINDOW, // window style

10,// initial x position

10,// initial y position

600,// initial x size

400,// initial y size

NULL, // parent window handle

NULL, // window menu handle

hInstance, // program instance handle

NULL) ; // creation parameters

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
winLayout(void);

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}

return msg.wParam ;

}


=×&D o I p R e E n C g T l X&×=
2007-09-30 00:11
永久的守护
Rank: 1
等 级:新手上路
威 望:2
帖 子:425
专家分:6
注 册:2007-6-9
收藏
得分:0 
太感谢了

见了便做做了便放下了了有何不了,慧生于觉觉生于自在生生还是无生。
活则生变,简而至稳。
2007-09-30 00:15
Janlex
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:303
专家分:0
注 册:2006-9-12
收藏
得分:0 
以下是引用踏魔狼在2007-9-29 23:26:36的发言:
你要明白WM_CREATE比WinMain()还有早执行.所hwnd还是没效的.


并不同意.
WinMain()永远是入口函数,肯定是先执行的.
而是因为创建子控件时CreateWindow函数父窗口必须是父窗口句柄.
当WM_CREATE未返回时,全局变量hwnd是得不到有效的父窗口句柄的.
所以只有通过消息过程函数中的第一个参数h来得到有效的父窗口句柄.

#include <windows.h>

#define IDB_NUM8 118

HWND hButton_Exit ; //子窗口控件句柄
HWND hwnd ;
HINSTANCE hInst ;

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
void winLayout(HWND) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,

PSTR szCmdLine, int iCmdShow)
{

static TCHAR szAppName[] = TEXT ("HelloWin") ;


MSG msg ;

WNDCLASS wndclass ;


wndclass.style = CS_HREDRAW | CS_VREDRAW ;

wndclass.lpfnWndProc = WndProc ;

wndclass.cbClsExtra = 0 ;

wndclass.cbWndExtra = 0 ;

wndclass.hInstance = hInstance ;

wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;

wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;

wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;

wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName= szAppName ;


if (!RegisterClass (&wndclass))
{

MessageBox ( NULL, TEXT ("This program requires Windows NT!"),

szAppName, MB_ICONERROR) ;

return 0 ;

}

hwnd = CreateWindow( szAppName, // window class name

TEXT ("窗口与函数"), // window caption

WS_OVERLAPPEDWINDOW, // window style

10,// initial x position

10,// initial y position

600,// initial x size

400,// initial y size

NULL, // parent window handle

NULL, // window menu handle

hInstance, // program instance handle

NULL) ; // creation parameters

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}

return msg.wParam ;

}

LRESULT CALLBACK WndProc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
{

HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;

switch (message)
{

case WM_CREATE:

winLayout(h) ;
return 0 ;


case WM_PAINT:

hdc = BeginPaint (h, &ps) ;

EndPaint (h, &ps) ;

return 0 ;

case WM_DESTROY:

PostQuitMessage (0) ;

return 0 ;

}

return DefWindowProc (h, message, wParam, lParam) ;

}

void winLayout(HWND parenthwnd)
{
hButton_Exit=CreateWindow("BUTTON", //建立按钮7
"退出",
WS_CHILD | WS_VISIBLE,
230,180,
80,30,
parenthwnd,
(HMENU) IDB_NUM8,
hInst,
NULL);
}


★★★★★欢迎光临我的博客 ★★★★★
http://www.
2007-09-30 01:11
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 
果然如楼上所说!
从调试就可看出!

=×&D o I p R e E n C g T l X&×=
2007-09-30 01:24
永久的守护
Rank: 1
等 级:新手上路
威 望:2
帖 子:425
专家分:6
注 册:2007-6-9
收藏
得分:0 

学习了


见了便做做了便放下了了有何不了,慧生于觉觉生于自在生生还是无生。
活则生变,简而至稳。
2007-09-30 10:05
快速回复:为什么把窗口布局过程放函数里窗口就不显示了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015640 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved