问题不是很大

xspace_time 2011-05-22 11:48:17
有一个SDK风格的窗口,以后可以用来实现记事本,但是现在可以先把程序复制了运行一下再看问题

#include<windows.h>
#pragma comment(lib,"winmm.lib")
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"WinMainCRTStartup\"" )
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
#define IDC_EDIT 101
HWND myedit;

void ResizeEdit(HWND hwndEdit,HWND hwnd);
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow)
{
static TCHAR szAppName[]=TEXT("HelloWin") ;
HWND hwnd;
MSG msg ;
RECT rect;
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)CreateSolidBrush(RGB(0,0,255));
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("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
(GetSystemMetrics(SM_CXSCREEN)-800)/2,// initial x position
(GetSystemMetrics(SM_CYSCREEN)-600)/2,// initial y position
800,// initial x size
600,// initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
myedit=CreateWindowEx(0,"Edit", "",
WS_VSCROLL|WS_CHILD|ES_MULTILINE|ES_AUTOVSCROLL|WS_BORDER|WS_VISIBLE, GetClientRect(hwnd,&rect),
GetClientRect(hwnd,&rect), rect.right-rect.left-10,rect.bottom-rect.top,hwnd,(HMENU)IDC_EDIT,
hInstance,0);
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)
{
PAINTSTRUCT ps;
HDC hdc;

switch (message)
{
case WM_CREATE:
PlaySound(TEXT ("BuzzingBee.wav"),NULL,SND_FILENAME|SND_ASYNC) ;
return 0 ;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint (hwnd, &ps);
return 0 ;
case WM_SIZE:
ResizeEdit(myedit,hwnd);
return 0;
//case WM_KEYDOWN:
//myedit=NULL;
//return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0 ;
}
return DefWindowProc (hwnd,message,wParam,lParam) ;
}

void ResizeEdit(HWND hwndEdit,HWND hwnd)
{
RECT rect;
GetClientRect(hwnd,&rect);
int nWidth =rect.right-rect.left-10;
int nLeight =rect.bottom-rect.top;
//if(hwndEdit!=NULL)
MoveWindow(hwndEdit,GetClientRect(hwnd,&rect),GetClientRect(hwnd,&rect), rect.right-rect.left,rect.bottom-rect.top,SWP_SHOWWINDOW|SWP_NOZORDER);
//else SendMessage(myedit,WM_PAINT,0,0);
}

看到//注释的内容,在最后一句里应该使用
if(hwndEdit!=NULL)
MoveWindow(hwndEdit,GetClientRect(hwnd,&rect),GetClientRect(hwnd,&rect), rect.right-rect.left,rect.bottom-rect.top,SWP_SHOWWINDOW|SWP_NOZORDER);
else ……
,这里要判断Edit的句柄是否为0,如果为0,说明Edit创建失败,那么应该发送消息或者重新绘制Edit,但是该怎么写呢
不要说那个判断没必要,如果把这个集成到复杂程序里,判断是很有必要的
...全文
134 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
nakedavril 2011-05-23
  • 打赏
  • 举报
回复
对,在WinMain中判断就行了
Proteas 2011-05-23
  • 打赏
  • 举报
回复
myedit 是在 WinMain 中创建的,
后面很多操作都依赖这个句柄。

我觉得你应该在 WinMain 中判断,
不成功就返回错误或者抛出异常。

原因是:
这个句柄是你自己创建的,在你程序内部使用的,
你可以保证正确创建出来的。
请对比考虑下,如果这个句柄是从外部传进来的。
xspace_time 2011-05-23
  • 打赏
  • 举报
回复
我用
else
{
myedit=GetDlgItem(hwnd,IDC_EDIT);
ResizeEdit(myedit,hwnd);
}
重新找到了句柄,myedit置0也没有影响,但是要重新画还是一个问题,但是这个问题就先这么解决吧
如果是从外部传进来为0,可能应该外部先寻找句柄,如果寻找的结果为0,则应该重画,这就比较难解决了,主要是对消息和函数都不了解,也不熟悉
xspace_time 2011-05-23
  • 打赏
  • 举报
回复
我是想在正常情况下myedit是不为0的,但是可能在有些特殊情况下myedit为0
那么人为的制造一种特殊情况,发送消息并把myedit置0
然后这种myedit为0的特殊情况真的发生了,这个时候要处理这个意外,重新画Edit,并赋值给myedit,
那么程序就恢复正常了

但是不明白的是myedit为0了,Edit还在,就是说Edit并没有被销毁,那么应该也能重新找到Edit的句柄吧
老邓 2011-05-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 pink9527 的回复:]

改正
MoveWindows和CreateWindowEx,初始化rect后加了一句人为移动窗口把myedit置0,然后Edit就不动了,下面该怎么恢复呢
case WM_SIZE:
ResizeEdit(myedit,hwnd);
return 0;
case WM_MOVE:
MessageBox(hwnd,"XX","CC",0);
myedit=NULL;
return ……
[/Quote]
myedit的值对应这个EDIT窗口。
它是不可以置零的。
除非有备份。
xspace_time 2011-05-23
  • 打赏
  • 举报
回复
改正
MoveWindows和CreateWindowEx,初始化rect后加了一句人为移动窗口把myedit置0,然后Edit就不动了,下面该怎么恢复呢
case WM_SIZE:
ResizeEdit(myedit,hwnd);
return 0;
case WM_MOVE:
MessageBox(hwnd,"XX","CC",0);
myedit=NULL;
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0 ;
老邓 2011-05-23
  • 打赏
  • 举报
回复
问题一: rect要先初始化,再使用
RECT rect = { 0 };


问题二:调整窗口大小使用不正确,注意查MSDN里关于MoveWindow各个参数的意义
    int nLeight = rect.bottom - rect.top - 10;
//if(hwndEdit!=NULL)
MoveWindow(hwndEdit,
5,
5,
nWidth,
nLeight,
SWP_SHOWWINDOW | SWP_NOZORDER);
老邓 2011-05-23
  • 打赏
  • 举报
回复
#include<windows.h>
#pragma comment(lib,"winmm.lib")

#pragma comment( linker, "/subsystem:\"windows\" /entry:\"WinMainCRTStartup\"" )

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

#define IDC_EDIT 101

HWND myedit;

void ResizeEdit(HWND hwndEdit, HWND hwnd);

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("HelloWin");
HWND hwnd;
MSG msg;
RECT rect = { 0 };
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)CreateSolidBrush(RGB(0,0,255));
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("The Hello Program"),// window caption
WS_OVERLAPPEDWINDOW,// window style
(GetSystemMetrics(SM_CXSCREEN)-800)/2,// initial x position
(GetSystemMetrics(SM_CYSCREEN)-600)/2,// initial y position
800,// initial x size
600,// initial y size
NULL,// parent window handle
NULL,// window menu handle
hInstance,// program instance handle
NULL);// creation parameters
myedit = CreateWindowEx(0,
"Edit",
"",
WS_VSCROLL | WS_CHILD | ES_MULTILINE | ES_AUTOVSCROLL | WS_BORDER
| WS_VISIBLE,
GetClientRect(hwnd, &rect),
GetClientRect(hwnd, &rect),
rect.right - rect.left - 10,
rect.bottom - rect.top,
hwnd,
(HMENU)IDC_EDIT,
hInstance,
NULL);
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)
{
PAINTSTRUCT ps;
HDC hdc;

switch (message)
{
case WM_CREATE:
PlaySound(TEXT ("BuzzingBee.wav"), NULL, SND_FILENAME | SND_ASYNC);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return 0;
case WM_SIZE:
ResizeEdit(myedit, hwnd);
return 0;
//case WM_KEYDOWN:
//myedit=NULL;
//return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}

void ResizeEdit(HWND hwndEdit, HWND hwnd)
{
RECT rect;
GetClientRect(hwnd, &rect);
int nWidth = rect.right - rect.left - 10;
int nLeight = rect.bottom - rect.top - 10;
//if(hwndEdit!=NULL)
MoveWindow(hwndEdit,
5,
5,
nWidth,
nLeight,
SWP_SHOWWINDOW | SWP_NOZORDER);
//else SendMessage(myedit,WM_PAINT,0,0);
}
ryfdizuo 2011-05-23
  • 打赏
  • 举报
回复
	GetClientRect(hwnd, &rect);  //先获得rect,

myedit=CreateWindowEx(0,TEXT("Edit"), TEXT(""),
WS_VSCROLL|WS_CHILD|ES_MULTILINE|ES_AUTOVSCROLL|WS_BORDER|WS_VISIBLE, rect.left,
rect.top, rect.right-rect.left-10,rect.bottom-rect.top,hwnd,(HMENU)IDC_EDIT,
hInstance,0);
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

64,654

社区成员

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

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