步骤如下:
1.先建立window32 application(工程名为WinMain)
2.新建C++ source文件(文件名是:WinMain)
3.插入如下代码:
#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
LPSTR     lpCmdLine,
int       nCmdShow
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra =0;
wndcls.cbWndExtra =0;
wndcls.hbrBackground =(HBRUSH)GetStockObject(BLACK_BRUSH);
    wndcls.hCursor =LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon =LoadIcon(NULL,IDI_ERROR);
wndcls.lpfnWndProc =WinSunProc;
wndcls.lpszClassName ="Weixin2003";
wndcls.style =CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wndcls); HWND hwnd;
hwnd=CreateWindow("Weixin2003","东华理工学院",WS_OVERLAPPEDWINDOW,
0,0,600,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd); MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"Char is %d",wParam);
MessageBox(hwnd,szChar,"weixin",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"Mouse Click","weixin",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"计算机编程语言",strlen("计算机编程语言"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"信息与计算科学",strlen("信息与计算科学"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","weixin",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
4.调试产生错误(错误代码如下)
WinMain.obj : error LNK2001: unresolved external symbol "long __stdcall WinSunProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WinSunProc@@YGJPAUHWND__@@IIJ@Z)Debug/WinMain.exe : fatal error LNK1120: 1 unresolved externals请走过,路过的大侠看看错在哪?

解决方案 »

  1.   

    WinSunProc的名称在定义的时候写成了WindowSunProc
    兄弟你手误了!
    LRESULT CALLBACK WinSunProc(
    HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam
    )
    {
    switch(uMsg)
    {
    case WM_CHAR:
    char szChar[20];
    sprintf(szChar,"Char is %d",wParam);
    MessageBox(hwnd,szChar,"weixin",0);
    break;
    case WM_LBUTTONDOWN:
    MessageBox(hwnd,"Mouse Click","weixin",0);
    HDC hdc;
    hdc=GetDC(hwnd);
    TextOut(hdc,0,50,"计算机编程语言",strlen("计算机编程语言"));
    ReleaseDC(hwnd,hdc);
    break;
    case WM_PAINT:
    HDC hDC;
    PAINTSTRUCT ps;
    hDC=BeginPaint(hwnd,&ps);
    TextOut(hDC,0,0,"信息与计算科学",strlen("信息与计算科学"));
    EndPaint(hwnd,&ps);
    break;
    case WM_CLOSE:
    if(IDYES==MessageBox(hwnd,"是否真的结束?","weixin",MB_YESNO))
    {
    DestroyWindow(hwnd);
    }
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return 0;
    }
      

  2.   

    LRESULT CALLBACK WindowSunProc(
    HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam
    )
    改为
    LRESULT CALLBACK WinSunProc(
    HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam
    )
    以后敲程序仔细点!