////maindlg.h
#ifndef _MAIN_H
#define _MAIN_H
#include <windows.h>
BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam);
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);
void Main_OnClose(HWND hwnd);
#endif
////maindlg.cpp
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
#include "LoginDialog.h"
BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
}
return FALSE;
}
BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
return TRUE;
}
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch(id)
{
/* 打开新窗口,并传值到新窗口 */
case IDC_OK:
{
HINSTANCE hInstance=(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE);
//DialogBox(hInstance, MAKEINTRESOURCE(IDD_LoginDialog), NULL, LoginDialog_Proc);
TCHAR buff[256]="pukuimin";
LoginData ld;
ld.username="pkm";
ld.pwd="123456";
//模式窗口,主窗口会等待子窗口关闭才执行下面的代码
int result=DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_LoginDialog), NULL, LoginDialog_Proc,(LPARAM)&ld);
//result 为子窗口EndDialog的返回值
MessageBox(hwnd,ld.username,"",MB_OK);
//这里参数可以是任何数据的地址,再转成(LPARAM)传过去
}
break;
default:
break;
}
}
void Main_OnClose(HWND hwnd)
{
EndDialog(hwnd, 0);
}
////logindialog.h
#ifndef _LoginDialog_H
#define _LoginDialog_H
#include <windows.h>
BOOL WINAPI LoginDialog_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL LoginDialog_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam);
void LoginDialog_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);
void LoginDialog_OnClose(HWND hwnd);
typedef struct LoginData
{
TCHAR *username;
TCHAR *pwd;
};
#endif
////logindialog.cpp
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "LoginDialog.h"
BOOL WINAPI LoginDialog_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
HANDLE_MSG(hWnd, WM_INITDIALOG, LoginDialog_OnInitDialog);
HANDLE_MSG(hWnd, WM_COMMAND, LoginDialog_OnCommand);
HANDLE_MSG(hWnd,WM_CLOSE, LoginDialog_OnClose);
}
return FALSE;
}
BOOL LoginDialog_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
LoginData *para=(LoginData *) lParam;//从前一个窗口传过来的参数
SetDlgItemText(hwnd,IDC_EDITUserName,para->username);//显示到本窗口的控件上
SetDlgItemText(hwnd,IDC_EDITPwd,para->pwd);
//para->username="pkm1";//本窗口修改这个值,父窗口能接收到
SetWindowLong(hwnd,GWL_USERDATA,(LONG)para);//把数据与窗口关联
return TRUE;
}
void LoginDialog_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch(id)
{
case IDLoginOk:
{
TCHAR username[255]={0},pwd[255]={0};
GetDlgItemText(hwnd,IDC_EDITUserName,username,255);
GetDlgItemText(hwnd,IDC_EDITPwd,pwd,255);
LoginData* ld=(LoginData*) GetWindowLong(hwnd,GWL_USERDATA);//取出并修改关联窗口的数据
//ld->username=username;
//ld->pwd=pwd;
//ld->username=(TCHAR*)username;//局部变量,没值。
//lstrcpy(ld->username,username);
//lstrcpy(ld->pwd,pwd);
ld->username="test01";//可以
EndDialog(hwnd,0);//0为返回值
}
break;
case IDLoginCancel:
{
EndDialog(hwnd,1);//1为返回值
}
break;
default:
break;
}
}
void LoginDialog_OnClose(HWND hwnd)
{
EndDialog(hwnd, 0);
}