问题描述
我需要一些帮助来使一些对话框正常工作.我以前从未做过对话框,现在我知道为什么避免使用它们了:\.首次创建主窗口时,我希望弹出一个对话框,要求用户提供一些信息.然后,当他们按下对话框上的连接"按钮时,我希望它关闭,并且我的程序可以开始它的生意.所以我在主windowproc的WM_CREATE:中有此内容:
i''m in need of some assistance getting some dialog boxes to bloody work properly. I''ve never done dialog boxes before, and i now know why i avoided them :\ . When my main window is first created, I want a dialog box to pop asking for some information from the user. Then, when they press the "Connect" button on the dialog box, i wish for it to close, and for my program to go about it''s business. So i have this in my WM_CREATE: of my main windowproc:
int ret = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), hwnd, DlgProc);
效果很好,当我启动程序时,对话框打开,并让人员填写两个字段,一个用于服务器ip的ip控件,以及一个用于屏幕名的edit控件.这是dialogwindowproc,我确定一切都在这里进行:
which works fine, the dialog opens when i start the program, and lets the person fill in the two fields, an ip control for a server ip, and an edit control for a screenname. This is the dialogwindowproc, which i''m sure is where everything goes fubar:
BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_INITDIALOG:
{
return TRUE;
}
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDC_BUTTON1:
{
DWORD ipAddress = 0;
char * TempIp;
struct in_addr addr;
SendDlgItemMessage(hwnd, IDC_IPADDRESS1, IPM_GETADDRESS, 0, (lParam) &ipAddress);
addr.s_addr = (long)ipAddress;
TempIp = inet_ntoa(addr);
strcpy_s(ServerIp, TempIp);
GetDlgItemText(hwnd, IDC_EDIT1, username, 100);
SendMessage(mainWindow, WM_START_WINSOCK, 0, 0);
EndDialog(hwnd, IDOK);
return true;
}
}
break;
}
default:
return FALSE;
}
return TRUE;
}
所以我想我正在做的是,当按下带有对话框ID IDC_BUTTON1的按钮时,它将首先通过IPM_GETADDRESS消息以DWORD的形式获取ip地址,然后我将其转换为char字符串,然后存储在全局变量"ServerIp".然后,我通过GetDlgItemText函数获取输入的用户名,并将其存储在全局TCHAR字符串用户名"中.然后,我发送一个用户定义的消息,该消息由我的主要WinProc处理,但是我不担心这种情况,因为它不会进一步传播.我以为结束对话框功能应该完全关闭对话框,但是它保持打开状态,而主窗口的其余部分在它后面弹出,由于对话框仍然处于打开状态,因此无法单击.然后,当我尝试再次单击连接"按钮以将其关闭时,我从MSVC ++ Express中收到未处理的异常"/访问冲突"错误,并且我的程序崩溃了.所以,我做错了.有人可以帮我吗?
So what i thought i was doing was, when the button with the dialog ID IDC_BUTTON1 was pressed, it would first grab the ip address in the form of DWORD via the IPM_GETADDRESS message, which i then convert to a char string and then store in a global variable ''ServerIp''. I then grab the entered username via the GetDlgItemText function and store it in the global TCHAR string, ''username''. I then send a user-defined message, which is handled by my main WinProc, but i''m not worried about that cause it doesn''t get any farther. I thought the end dialog function was supposed to close the dialog box entirely, but it just stays open, and the rest of my main window pops up behind it, which i can''t click on because the dialog box is still up. Then, when i try and click the Connect button again to close it, i get an "Unhandeled exception" / "Access Violation" error from MSVC++ express and my program crashes. so yeh, I''m doing this wrong. Can someone help me out?
推荐答案
这篇关于Win32中对话框出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!