Windows程序设计是利用API提供的接口进行的编写程序的过程,API(application programming interface)即应用程序编程接口。直接使用API编程是了解操作系统运行细节的最佳方式。
由于课程实习要学这个,所以花了一个星期照着书,编译成功了第一个windows应用程序
这个程序是由几个部分组成的,分别是/try.cpp/try.h/stdafx.h......主要是try.cpp,话不多说,直接看代码吧!
// try.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "try.h"
#include <windows.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING];
// 主窗口类名
static int s_nPreHour;
static int s_nPreMinute;
static int s_nPreSecond;
static int s_cxClient;
static int s_cyClient;
static BOOL s_bTopMost;
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM );
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TRY, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TRY));
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
inline ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
char szClassName[]="MainWClsass";
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance,(LPSTR)IDC_MYICON);//???
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(CreateSolidBrush(RGB(0xa6,0xca,0xf0)));//使窗口变成天蓝色
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_TRY);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
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;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
void DrawClockFace(HDC hdc)
{
const int SQUARESIZE = 20;
static POINT pt[]=
{
0,450,
225,390,
390,225,
450,0,
390,-225,
225,-390,
0,-450,
-225,-390,
-390,-225,
-450,0,
-390,225,
-225,390
};
SelectObject(hdc,GetStockObject(BLACK_BRUSH));
for(int i=0;i<12;i++)
{
Ellipse(hdc,pt[i].x-SQUARESIZE,pt[i].y+SQUARESIZE,
pt[i].x+SQUARESIZE,pt[i].y-SQUARESIZE);
}
}
void SetIsotropic(HDC hdc,int cx,int cy)
{
SetMapMode(hdc,MM_ISOTROPIC);
SetWindowExtEx(hdc,1000,1000,NULL);
SetViewportExtEx(hdc,cx,-cy,NULL);
SetViewportOrgEx(hdc,cx/2,cy/2,NULL);
}
void DrawHand(HDC hdc,int nLength,int nWidth,int nDegrees,COLORREF clrColor)
{
double nRadians=(double)nDegrees*0.0174533;
POINT pt[2];
pt[0].x=(int)(nLength*sin(nRadians));
pt[0].y=(int)(nLength*cos(nRadians));
pt[1].x=-pt[0].x/5;
pt[1].y=-pt[0].y/5;
HPEN hPen=CreatePen(PS_SOLID,nWidth,clrColor);
HPEN hOldPen=(HPEN)::SelectObject(hdc,hPen);
MoveToEx(hdc,pt[0].x,pt[0].y,NULL);
LineTo(hdc,pt[1].x,pt[1].y);
SelectObject(hdc,hOldPen);
DeleteObject(hPen);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
static int nNum;
static int bSetTimer;
char szText[56];
PAINTSTRUCT ps;
HDC hdc;
SetTimer(hWnd,1,250,NULL);
SetTimer(hWnd,2,10*1000,NULL);
switch(message)
{
case WM_CREATE:
SYSTEMTIME time;
GetLocalTime(&time);
s_nPreHour=time.wHour%12;
s_nPreMinute=time.wMinute;
s_nPreSecond=time.wSecond;
SetTimer(hWnd,1,1000,NULL);
break;
case WM_PAINT:
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd,&ps);
SetIsotropic(hdc,s_cxClient,s_cyClient);
DrawClockFace(hdc);
//一小时时针走30度,一分钟时针0.5度
DrawHand(hdc,200,8,s_nPreHour*30+s_nPreMinute/2,RGB(0,0,0));
//一分钟分针走6度
DrawHand(hdc,400,6,s_nPreMinute*6,RGB(0,0,0));
//一秒钟秒针走6度
DrawHand(hdc,400,1,s_nPreSecond*6,RGB(0,0,0));
EndPaint(hWnd,&ps);
break;
case WM_TIMER:
COLORREF crfColor;
crfColor =RGB(0xa6,0xca,0xf0);
if(IsIconic(hWnd))
return 0;
//SYSTEMTIME time;
GetLocalTime(&time);
//建标
hdc = GetDC(hWnd);
SetIsotropic(hdc,s_cxClient,s_cyClient);
//擦除时针和分针
if(time.wMinute != s_nPreMinute)
{
DrawHand(hdc,200,8,s_nPreHour*30+s_nPreMinute/2,crfColor);
DrawHand(hdc,400,6,s_nPreMinute*6,crfColor);
s_nPreHour=time.wHour;
s_nPreMinute=time.wMinute;
}
//如果秒针改变就擦除秒针,并重画所有指针
if(time.wSecond != s_nPreSecond)
{
DrawHand(hdc,400,1,s_nPreSecond*6,crfColor);//擦除秒针
//重画所有
DrawHand(hdc,400,1,time.wSecond*6,RGB(0,0,0));
DrawHand(hdc,200,8,time.wHour*30+time.wMinute/2,RGB(0,0,0));
DrawHand(hdc,400,6,time.wMinute*6,RGB(0,0,0));
s_nPreSecond=time.wSecond;
}
break;
case WM_CLOSE:
KillTimer(hWnd,1);
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SIZE:
s_cxClient = LOWORD(lParam);
s_cyClient = HIWORD(lParam);
break;
}
return DefWindowProc(hWnd,message,wParam,lParam);
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}