mfc深入浅出第一个例子疑问
真是很奇怪的问题:
这时mfc上的例子
全部代码如下:
//----------------------------------------------------------------
// 文件名:generic.h
//----------------------------------------------------------------
BOOL InitApplication(HANDLE);
BOOL InitInstance(HANDLE,int);
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK About(HWND,UINT,WPARAM,LPARAM);
//-----------------------------------
//文件名:resource.h
//-----------------------------------
#ifndef __RESOURCE_H__
#define __RESOURCE_H__
#define IDM_NEW 102
#define IDM_SAVE 103
#define IDM_EXIT 104
#define IDM_ABOUT 105
#endif
//----------------------------------------
//文件名:Generic.rc
//-----------------------------------------
#include "windows.h"
#include "resource.h"
jjhouricon ICON "jjhour.ico";
GenericMenu MENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "&New",IDM_NEW,GRAYED
MENUITEM "Save",IDM_SAVE,GRAYED
MENUITEM "E&xit",IDM_EXIT
END
POPUP "&Help"
BEGIN
MENUITEM "ABout...",IDM_ABOUT
END
END
AboutBox DIALOG 22,17,144,75
STYLE DS_MODALFRAME|WS_CAPTION|WS_SYSMENU
CAPTION "About generic"
BEGIN
CTEXT "Window2000",-1,0,5,144,8
CTEXT "Generic Application",-1,0,14,144,8
CTEXT "Version 1.0",-1,0,34,144,8
DEFPUSHBUTTON "OK",IDOK,53,59,32,14,WS_GROUP
END
//----------------------------------------------------------------
// GENERIC--WIN32程序的基础写法
//文件名:GENERIC.CPP
//
//----------------------------------------------------------------
#include <windows.h>
#include "resource.h"
#include "generic.h"
HINSTANCE _hInst;
HWND _hWnd;
char _szAppName[]="Generic";
char _szTitle[]="Generic Sample Application";
//----------------------------------------------------------------
// WinMain---程序进入点
//
//----------------------------------------------------------------
int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;
if (!hInstance)
if (!InitApplication(hInstance))
return FALSE;
//判断初始化是否成功
if (!InitInstance(hInstance,nCmdShow))
return FALSE;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
//----------------------------------------------------------------
//InitInstance--注册窗口类别
//
//----------------------------------------------------------------
BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASS wc;
wc.style =CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc =(WNDPROC)WndProc;
wc.cbClsExtra =0;
wc.cbWndExtra =0;
wc.hInstance =hInstance;
wc.hIcon =LoadIcon(hInstance,IDI_WINLOGO);
wc.hCursor =LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName =NULL;
wc.lpszClassName =_szAppName;
//注册创建窗体的资源
return RegisterClass(&wc);
}
//----------------------------------------------------------------
// InitInstance---产生窗体
//
//
//----------------------------------------------------------------
BOOL InitInstance(HINSTANCE hInstance,int nCmdShow)
{
_hInst=hInstance;
_hWnd=CreateWindow(_szAppName,_szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if (!_hWnd)
return FALSE;
ShowWindow(_hWnd,nCmdShow);
UpdateWindow(_hWnd);
return TRUE;
}
//----------------------------------------------------------------
// WndProc--窗体函数
//
//
//----------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
int wmId,wmEvent;
switch(message)
{
case WM_COMMAND:
wmId =LOWORD(wParam);
wmEvent =HIWORD(wParam);
switch(wmId)
{
case IDM_ABOUT:
DialogBox(_hInst,
"AboutBox",
hWnd,
(DLGPROC)About
);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}
//----------------------------------------------------------------
// About--弹出框函数
//
//
//----------------------------------------------------------------
LRESULT CALLBACK About(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam)==IDOK || LOWORD(wParam)==IDCANCEL)
{
EndDialog(hDlg,TRUE);
return TRUE;
}
break;
}
return FALSE;
}
以上代码不能通过编译
报
new_1 error LNK2019: 无法解析的外部符号 "int __cdecl InitInstance(void *,int)" (?InitInstance@@YAHPAXH@Z) ,该符号在函数 _WinMain@16 中被引用
new_1 error LNK2019: 无法解析的外部符号 "int __cdecl InitApplication(void *)" (?InitApplication@@YAHPAX@Z) ,该符号在函数 _WinMain@16 中被引用
new_1 fatal error LNK1120: 2 个无法解析的外部命令
而如果把InitApplication和InitInstance这两个基本点函数的内容直接写到winmain
中,就可以正常通过编译并执行.
如:
int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;
/*if (!hInstance)
if (!InitApplication(hInstance))
return FALSE;*/
WNDCLASS wc;
wc.style =CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc =(WNDPROC)WndProc;
wc.cbClsExtra =0;
wc.cbWndExtra =0;
wc.hInstance =hInstance;
wc.hIcon =LoadIcon(hInstance,IDI_WINLOGO);
wc.hCursor =LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName ="GenericMenu";
wc.lpszClassName =_szAppName;
RegisterClass(&wc);
_hInst=hInstance;
_hWnd=CreateWindow(_szAppName,
_szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
//if (!_hWnd)
//return FALSE;
ShowWindow(_hWnd,nCmdShow);
UpdateWindow(_hWnd);
//判断初始化是否成功
/*if (!InitInstance(hInstance,nCmdShow))
return FALSE;*/
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
这是为什么呢?如果写到函数里的话应该怎么写才正确
我的环境是vs2003
请各位帮忙。~~~~~~~~~