网页功能: 加入收藏 设为首页 网站搜索  
关于Windows APIs编程的简单过程(VC6篇)
发表日期:2007-01-16作者:[转贴] 出处:  

首先建立工程:

第一步,你必须有Microsoft Visual C++6.0,并且运行它。

第二步,点击File->New,弹出New对话框,选择左上角标签的"Projects",在列表框中选择"Win32 Application",并且将右边的"Project name"和"Location"分别设置好(工程名与工程路径,在VC下写代码一般都以工程的形式,不像DOS下以单一文件),点击OK。

   

第三步,点击OK后,又弹出一个对话框,有三个选项"An empty project","A simple Win32 
application","A typeical "Hello World!" application",选择第三个并按Finish。这时会弹出一个叫"New Project Information"的对话框,不管它,点击OK继续。

   

好了,现在我们的工程建立完毕,已经可以编译运行了,我们已经有了最基本的windows窗口了。

在左边的"Workspace"窗口下有个File标签,

       

将其打开,里面就有一些由VC帮你建立的文件,打开xxx.cpp文件,让我们看看这个.cpp文件里有什么;你现在应该找到了一个很熟悉的函数吧,就是叫WinMain(...)函数,这就是我们windows窗口的主函数,就和DOS下的main()函数一样,怎么样?是不是觉得简单多了?

这就是Microsoft Vistual C++为我们所建立的主文件
// Test.cpp : Defines the entry point for the application.
//

/* "stdafx.h"这个文件是本工程的预处理文件,一般你要包含的标准头文件都可以包含在这个文件里 */
#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

/* WinMain()函数就是我们所喜欢的主函数,也是程序的入口。 */
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
    // TODO: Place code here.
    MSG msg;
    HACCEL hAccelTable;

    // Initialize global strings
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_TEST, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow)) 
    {
        return FALSE;
    }

    hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TEST);
    /* 你可以在这里进行你游戏需要的初始化等等 */
    // Main message loop:
    /* 可以很明显看出,下面的部分是一个循环体,这就是windows应用程序的循环,可将游戏代码放入这个循环之中,作为游戏的主体 */
    while (GetMessage(&msg, NULL, 0, 0)) 
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return msg.wParam;
}



//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX); 

    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = (WNDPROC)WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TEST);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName = (LPCSTR)IDC_TEST;
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

    return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    HWND hWnd;

    hInst = hInstance; // Store instance handle in our global variable

    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

    if (!hWnd)
    {
        return FALSE;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

   
/* 你也可以在这里进行你游戏需要的初始化等等
   我一般都喜欢把游戏图形初始化部分放在这里  */


    return TRUE;
}

//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//

/* 这个就是所谓的windows消息循环体,里面处理着来至菜单和窗口的消息 */
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR szHello[MAX_LOADSTRING];
    LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

    switch (message) 
    {
    case WM_COMMAND:  /* 应用程序的菜单响应部分 */
        wmId = LOWORD(wParam); 
        wmEvent = HIWORD(wParam); 
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code here...
        RECT rt;
        GetClientRect(hWnd, &rt);
        DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Mesage handler for about box.
/* 如果你点击菜单里的Help->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, LOWORD(wParam));
            return TRUE;
        }
        break;
    }
    return FALSE;
}

这只是一个简单利用Windows APIs建立windows应用程序的例程,现在有了地基,接下来就是盖高楼了。 :)
若有什么不明白可以在论坛与我联系或E-Mail:
lindehui@263.net

2001.7.7
我来说两句】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 关于Windows APIs编程的简单过程(VC6篇)
本类热点文章
  如何用D3D/OpenGL在制作2D游戏中高效地..
  自己绘制True type font字体
  使用CPU时间戳进行高精度计时
  阵汉字显示
  程序员的超强外挂——Visual Assist .N..
  用VC++实现console程序显示彩色文本 wx..
  制做自己的点阵字库的方法和程序
  “变速齿轮”再研究
  一个简单的线程管理机制
  VC编程中常用快捷键
  Delpih中的Windows API编程初步
  BIG5到GB的转换技术
最新分类信息我要发布 
最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放  
Copyright ©2003-2024 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.00468