#include<windows.h>
#include "stdafx.h"
#include <stdio.h>ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
 LPSTR lpCmdLine,
 int nCmdShow){
MSG Msg;
MyRegisterClass(hInstance); if(!InitInstance(hInstance,nCmdShow))
{
return FALSE;
}
while (GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg); } return Msg.wParam;
}ATOM MyReisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize=sizeof(WNDCLASS);
wcex.style=CS_HREDRAW|CS_VREDRAW;
wcex.lpfnWndProc=(WNDPROC)WndProc;
wcex.cbClsExtra=0;
wcex.cbWndExtra=0;
wcex.hInstance=hInstance;
wcex.hIcon=NULL;
wcex.hCursor=NULL;
wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName=NULL;
wcex.lpszClassName="canvas";
wcex.hIconSm=NULL;
return RegisterClassEx(&wcex);
}BOOL InitInstance(HINSTANCE hInstance,int nCmdshow)
{
HWND hWnd;
hInst=hInstance;
hWnd=CreateWindow("canvas","绘图窗口",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,
CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
if(!hWnd)
{
return FALSE;
}
MoveWindow(hWnd,10,10,600,450,true);
ShowWindow(hWnd,nCmdshow);
UpdateWindow(hWnd); return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lparam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc=BeginPaint(hWnd,&ps);
EndPaint(hWnd,&ps);
break;
case WM_DESTROY:
default:
return DefWindowProc(hWnd,message,wParam,lparam); }
return 0;
}
这是我从书抄的,hInst=hInstance不知道是什么意思,,编译的时候,他说hInst未标识声明符,如果把hInst=hInstance去掉,又会出现这样的错误:
1>jf.obj : error LNK2019: 无法解析的外部符号 "unsigned short __cdecl MyRegisterClass(struct HINSTANCE__ *)" (?MyRegisterClass@@YAGPAUHINSTANCE__@@@Z),该符号在函数 _WinMain@16 中被引用
1>d:\我的文档\Visual Studio 2008\Projects\jf\Debug\jf.exe : fatal error LNK1120: 1 个无法解析的外部命令

解决方案 »

  1.   

    hInst=hInstance 的hInst变量本来就没有在你的程序中使用到,也没有声明,肯定编译不通过。后面链接错误和前面那个没关系,你这本书上的代码都没经过编译的,现在出书的人太不负责撒。
    WinMain中使用了MyRegisterClass整个函数,但是函数实现的地方写的是MyReisterClass,看到没,写错了嘛。
    当然编译可以通过,但是链接时候找不到符号了。
      

  2.   

    ATOM MyReisterClass(HINSTANCE hInstance) 
      

  3.   

    感觉您这段代码好像是《深入浅出MFC》书上的。