vcwindowsapi:有关Windows APIs编程的简单过程(VC篇)



首先建立工程:

你必须有Microsoft Visual C6.0并且运行它

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



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



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

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



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

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

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

# 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 d in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, );
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

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

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

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

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

msg.wParam;
}



//
// FUNCTION: MyRegisterClass
//
// PURPOSE: Registers the window .
//
// COMMENTS:
//
// This function and its usage is _disibledevent=>
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);

RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HANDLE, )
//
// PURPOSE: Saves instance handle and creates window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the program window.
//
BOOL InitInstance(HINSTANCE hInstance, 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);



(!hWnd)
{
FALSE;
}

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

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

TRUE;
}

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

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

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

// Mesage handler for about box.
/* 如果你点击菜单里Help->About应用会弹出对话框这里就是这个对话框消息循环体 */
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
WM_INITDIALOG:
TRUE;

WM_COMMAND:
(LOWORD(wParam) IDOK || LOWORD(wParam) IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
TRUE;
}
;
}
FALSE;
}

这只是个简单利用Windows APIs建立windows应用例程现在有了地基接下来就是盖高楼了 :)
若有什么不明白可以在论坛和我联系或E-Mail:[email protected]


Tags:  vc编程 vcwindows vcwindows服务 vcwindowsapi

延伸阅读

最新评论

发表评论