开学四个月后呢谭浩强老师的C++看了不少遍了,C++Primer呢也看了一边,但所编写的程序仍就是个“黑框”十分不安心。一次在图书馆里闲逛发现了一本关于MFC的书,当时感觉能编图形界面就借来看看,我勒个去,实在太难了根部看不懂就去请教老师。老师告诉我学点windows API 再看MFC会简单点,于是开始了漫长的windows API 的学习之路。
由于看的是比较经典的windows 程序设计所以呢感觉相对容易一些,没过多久就发现API比MFC麻烦,没办法硬着头皮看吧。当我看到窗口时我震惊了,一个窗口80多行代码,作者竟然说API十分的简洁,区区80多行代码就能写图形界面。不过学久了也就没觉得有什么了,感觉API十分强大。
不多说直接上个窗口代码:
#include "stdafx.h" #include<windows.h> LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevinstance,PSTR szCmdLine,int iCmdShow) { static TCHAR szAppName[]=TEXT("hello windows 7"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style=CS_HREDRAW|CS_VREDRAW; wndclass.lpfnWndProc=WndProc; wndclass.cbClsExtra=0; wndclass.cbWndExtra=0; wndclass.hInstance=hInstance; wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION); wndclass.hCursor=LoadCursor(NULL,IDC_ARROW); wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName=NULL; wndclass.lpszClassName=szAppName; RegisterClassW(&wndclass); hwnd=CreateWindow(szAppName,TEXT("Display"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL); ShowWindow(hwnd,iCmdShow); UpdateWindow(hwnd); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd,UINT message, WPARAM wParam,LPARAM lParam) { static int cxChar, cxCaps, cyChar ; HDC hdc; PAINTSTRUCT ps; int iLength; TCHAR szBuffer [50] ; int a[10][10]; int i,j; for(i=0;i<10;i++) { a[i][0]=1; a[i][i]=1; } for(i=2;i<10;i++) for(j=1;j<i;j++) a[i][j]=a[i-1][j]+a[i-1][j-1]; TEXTMETRIC tm ; switch(message) { case WM_CREATE: hdc = GetDC (hwnd) ; GetTextMetrics (hdc, &tm) ; cxChar = tm.tmAveCharWidth ; cyChar = tm.tmHeight + tm.tmExternalLeading ; cxCaps=(tm.tmPitchAndFamily&1?3:2)*cxChar/2; ReleaseDC (hwnd, hdc) ; return 0; case WM_PAINT: hdc=BeginPaint(hwnd,&ps); for(i=0;i<10;i++) for(j=0;j<=i;j++) { iLength = wsprintf (szBuffer, TEXT ("%i"),a[i][j]); TextOut(hdc,cxCaps*3*j,cyChar*(i)*2,szBuffer,iLength); } EndPaint(hwnd,&ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,message,wParam,lParam); }
激动一下终于写出了图形界面了