Windows程序设计是利用API提供的接口进行的编写程序的过程,API(application programming interface)即应用程序编程接口。直接使用API编程是了解操作系统运行细节的最佳方式。
由于课程实习要学这个,所以花了一个星期照着书,编译成功了第一个windows应用程序
这个程序是由几个部分组成的,分别是/try.cpp/try.h/stdafx.h......主要是try.cpp,话不多说,直接看代码吧!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | // 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; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步