今天回头把Windows API 的第三、第四章看了一遍,想写一个九九乘法表的窗口程序,我想实现的窗口是这样的:第一行是1*1=1;第二行是1*2=2;以此类推 。问题是不知道怎么让“1*1=”(也就是下面代码中的i*j)显示到屏幕上。
了解程度 :目前为止还只是找到了可以添加逻辑代码的地方,用的还很不到位,有些语句比如wprintf函数、TextOut函数的一些用法还不是太了解,于是只能实现九九乘法表的结果输出到显示屏上,在网上也找了很多说明,但是没能解决我的问题 。 先把代码分享一下吧,不管怎么样,我的第一个用Windows API做出来的东西已经上市了 ,哈哈 ……这个问题在以后的学习中在解决吧 ……
Windows API代码如下 :
/*
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) ;
}
运行结果: