win32 Application hello,world

hello.cpp

#include <windows.h>
//#include "resource.h"
#include "Hello.h"
HINSTANCE _hInst;
HWND _hWnd;

char _szAppName[ ] = "AC梦";
char _szTitle[ ] = "努力加油";

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR IpCmdLine,int nCmdShow)
{
     MSG msg;
     if( !hPrevInstance )
        if( !InitApplication(hInstance) )
              return (false);
     if( !InitInstance(hInstance, nCmdShow) )
              return (false);
     while( GetMessage(&msg,NULL,0,0) ) {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
     }
     return (msg.wParam);

}

//注册窗口类
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,"0");
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); 
    wc.lpszMenuName = NULL;
    wc.lpszClassName = _szAppName;
    return (RegisterClass(&wc));
}

//产生窗口
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;
}

//窗口函数
 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
     HDC         hdc;
     PAINTSTRUCT ps;
     RECT        rect;
 
     switch (message)
     {
     case WM_CREATE:
         return 0;
     case WM_PAINT:
         hdc = BeginPaint(hwnd, &ps);
         GetClientRect(hwnd, &rect);
         DrawText(hdc, TEXT("Hello World!................"), -1, &rect, 
             DT_SINGLELINE | DT_CENTER | DT_VCENTER);
         EndPaint(hwnd, &ps);
         return 0;
     case WM_DESTROY:
         PostQuitMessage(0);
         return 0;
     }
     return DefWindowProc(hwnd, message, wParam, lParam);
 }

hello.h

bool InitApplication(HINSTANCE);
bool InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

错误总结:

1.2.obj   :   error   LNK2001:   unresolved   external   symbol   "int   __cdecl   InitInstance(void   *,int) "   (?InitInstance@@YAHPAXH@Z) 
2.obj   :   error   LNK2001:   unresolved   external   symbol   "int   __cdecl   InitApplication(void   *) "   (?InitApplication@@YAHPAX@Z) 

函数声明和定义不一致 
你声明的是:BOOL   InitApplication(HANDLE); 
定义的时候变成了:BOOL   InitInstance(HINSTANCE   hInstance,int   nCmdShow) 
HANDLE和HINSTANCE是不一样的。 

WinDef.h有这么一句:typedef HANDLE HINSTANCE;

数据上两者是一样的,本质上没什么区别。

HANDLE是用来标记资源的,也就是handle to an object

HINSTANCEHandle to an instance, HANDLE的一种特殊情况,常用来标记

App实例。

HINSTANCE而不是HANDLE只是给用者一种说明的作用。

posted on 2012-10-18 09:08  more think, more gains  阅读(258)  评论(0编辑  收藏  举报

导航