了解程度 :目前為止還只是找到了可以添加邏輯代碼的地方,用的還很不到位,有些語句比如wprintf函數、TextOut函數的一些用法還不是太了解,於是只能實現九九乘法表的結果輸出到顯示屏上,在網上也找了很多說明,但是沒能解決我的問題 。 先把代碼分享一下吧,不管怎麼樣,我的第一個用Windows API做出來的東西已經上市了 ,哈哈 ……這個問題在以後的學習中在解決吧 ……
Windows API代碼如下 :
[cpp]
/*
HELLOWIN.C -- Displays(顯示) * 九九乘法表 * in client(客戶端) area
在客戶端區域上 顯示九九乘法表
*/
# include<windows.h>
//#pragma comment(lib, "WINMM.LIB")
LRESULT CALLBACK WndProc (HWND ,UINT,WPARAM,LPARAM) ; //UINT 表示unsigned int. 這句是窗口過程的聲明
int WINAPI WinMain (HINSTANCE hInstance ,HINSTANCE hPrevInstance ,PSTR szCmdLine , int iCmdShow) //PSTR 是指向非寬字符串的指針
{
static TCHAR szAppname [] = TEXT ("HELLOWIN");
HWND hwnd ;
MSG msg;//消息結構
WNDCLASS wndclass ;//窗口類結構
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;//這是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 ; //
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("這是啥"),szAppname ,MB_ICONERROR) ;
return 0;
}
hwnd = CreateWindow (szAppname , //hwnd 是句柄 ,szappname是窗口類名稱
TEXT ("九九乘法表"), //窗口標題
WS_OVERLAPPEDWINDOW, //窗口風格(窗口格式)
CW_USEDEFAULT, //初始x坐標
CW_USEDEFAULT, //初始y坐標
CW_USEDEFAULT, //初始x方向尺寸
CW_USEDEFAULT, //初始y方向尺寸
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)
{
HDC hdc ;
PAINTSTRUCT ps ; //繪制結構
RECT rect ;//矩形結構
static int cxChar, cxCaps, cyChar ;
int iLength;
TCHAR szBuffer [50] ;
TEXTMETRIC tm ;
int i ,j,a[10][10];
for (j=1;j<10;j++)
{
for (i=1;i<=j;i++)
{
a[i][j]=i*j;
}
}
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 (j=1;j<10;j++)
{
for (i=1;i<=j;i++)
{
iLength = wsprintf (szBuffer, TEXT ("%i"),a[i][j]);
TextOut(hdc,cxCaps*4*i,cyChar*j*3,szBuffer,iLength);
}
}
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
運行結果: