今天看了下《Windows程序设计》的第四章文本输出,现记录下学习心得。
这一章主要讲的是文本输出,今天试了下第一个例子,用到了一个叫SYSMETS.H的头文件。
头文件代码:
/*-----------------------------------------------
SYSMETS.H -- System metrics display structure
-----------------------------------------------*/
#define NUMLINES ((int) (sizeof sysmetrics / sizeof sysmetrics [0]))
struct
{
int iIndex ;
TCHAR *szLabel;
TCHAR *szDesc;
}
sysmetrics [] =
{
SM_CXSCREEN, TEXT ("SM_CXSCREEN"),
TEXT ("Screen width in pixels"),
SM_CYSCREEN, TEXT ("SM_CYSCREEN"),
TEXT ("Screen height in pixels"),
SM_CXVSCROLL, TEXT ("SM_CXVSCROLL"),
TEXT ("Vertical scroll width"),
SM_CYHSCROLL, TEXT ("SM_CYHSCROLL"),
TEXT ("Horizontal scroll height"),
SM_CYCAPTION, TEXT ("SM_CYCAPTION"),
TEXT ("Caption bar height"),
SM_CXBORDER, TEXT ("SM_CXBORDER"),
TEXT ("Window border width"),
SM_CYBORDER, TEXT ("SM_CYBORDER"),
TEXT ("Window border height"),
SM_CXFIXEDFRAME, TEXT ("SM_CXFIXEDFRAME"),
TEXT ("Dialog window frame width"),
SM_CYFIXEDFRAME, TEXT ("SM_CYFIXEDFRAME"),
TEXT ("Dialog window frame height"),
SM_CYVTHUMB, TEXT ("SM_CYVTHUMB"),
TEXT ("Vertical scroll thumb height"),
SM_CXHTHUMB, TEXT ("SM_CXHTHUMB"),
TEXT ("Horizontal scroll thumb width"),
SM_CXICON, TEXT ("SM_CXICON"),
TEXT ("Icon width"),
SM_CYICON, TEXT ("SM_CYICON"),
TEXT ("Icon height"),
SM_CXCURSOR, TEXT ("SM_CXCURSOR"),
TEXT ("Cursor width"),
SM_CYCURSOR, TEXT ("SM_CYCURSOR"),
TEXT ("Cursor height"),
SM_CYMENU, TEXT ("SM_CYMENU"),
TEXT ("Menu bar height"),
SM_CXFULLSCREEN, TEXT ("SM_CXFULLSCREEN"),
TEXT ("Full screen client area width"),
SM_CYFULLSCREEN, TEXT ("SM_CYFULLSCREEN"),
TEXT ("Full screen client area height"),
SM_CYKANJIWINDOW, TEXT ("SM_CYKANJIWINDOW"),
TEXT ("Kanji window height"),
SM_MOUSEPRESENT, TEXT ("SM_MOUSEPRESENT"),
TEXT ("Mouse present flag"),
SM_CYVSCROLL, TEXT ("SM_CYVSCROLL"),
TEXT ("Vertical scroll arrow height"),
SM_CXHSCROLL, TEXT ("SM_CXHSCROLL"),
TEXT ("Horizontal scroll arrow width"),
SM_DEBUG, TEXT ("SM_DEBUG"),
TEXT ("Debug version flag"),
SM_SWAPBUTTON, TEXT ("SM_SWAPBUTTON"),
TEXT ("Mouse buttons swapped flag"),
SM_CXMIN, TEXT ("SM_CXMIN"),
TEXT ("Minimum window width"),
SM_CYMIN, TEXT ("SM_CYMIN"),
TEXT ("Minimum window height"),
SM_CXSIZE, TEXT ("SM_CXSIZE"),
TEXT ("Min/Max/Close button width"),
SM_CYSIZE, TEXT ("SM_CYSIZE"),
TEXT ("Min/Max/Close button height"),
SM_CXSIZEFRAME, TEXT ("SM_CXSIZEFRAME"),
TEXT ("Window sizing frame width"),
SM_CYSIZEFRAME, TEXT ("SM_CYSIZEFRAME"),
TEXT ("Window sizing frame height"),
SM_CXMINTRACK, TEXT ("SM_CXMINTRACK"),
TEXT ("Minimum window tracking width"),
SM_CYMINTRACK, TEXT ("SM_CYMINTRACK"),
TEXT ("Minimum window tracking height"),
SM_CXDOUBLECLK, TEXT ("SM_CXDOUBLECLK"),
TEXT ("Double click x tolerance"),
SM_CYDOUBLECLK, TEXT ("SM_CYDOUBLECLK"),
TEXT ("Double click y tolerance"),
SM_CXICONSPACING, TEXT ("SM_CXICONSPACING"),
TEXT ("Horizontal icon spacing"),
SM_CYICONSPACING, TEXT ("SM_CYICONSPACING"),
TEXT ("Vertical icon spacing"),
SM_MENUDROPALIGNMENT, TEXT ("SM_MENUDROPALIGNMENT"),
TEXT ("Left or right menu drop"),
SM_PENWINDOWS, TEXT ("SM_PENWINDOWS"),
TEXT ("Pen extensions installed"),
SM_DBCSENABLED, TEXT ("SM_DBCSENABLED"),
TEXT ("Double-Byte Char Set enabled"),
SM_CMOUSEBUTTONS, TEXT ("SM_CMOUSEBUTTONS"),
TEXT ("Number of mouse buttons"),
SM_SECURE, TEXT ("SM_SECURE"),
TEXT ("Security present flag"),
SM_CXEDGE, TEXT ("SM_CXEDGE"),
TEXT ("3-D border width"),
SM_CYEDGE, TEXT ("SM_CYEDGE"),
TEXT ("3-D border height"),
SM_CXMINSPACING, TEXT ("SM_CXMINSPACING"),
TEXT ("Minimized window spacing width"),
SM_CYMINSPACING, TEXT ("SM_CYMINSPACING"),
TEXT ("Minimized window spacing height"),
SM_CXSMICON, TEXT ("SM_CXSMICON"),
TEXT ("Small icon width"),
SM_CYSMICON, TEXT ("SM_CYSMICON"),
TEXT ("Small icon height"),
SM_CYSMCAPTION, TEXT ("SM_CYSMCAPTION"),
TEXT ("Small caption height"),
SM_CXSMSIZE, TEXT ("SM_CXSMSIZE"),
TEXT ("Small caption button width"),
SM_CYSMSIZE, TEXT ("SM_CYSMSIZE"),
TEXT ("Small caption button height"),
SM_CXMENUSIZE, TEXT ("SM_CXMENUSIZE"),
TEXT ("Menu bar button width"),
SM_CYMENUSIZE, TEXT ("SM_CYMENUSIZE"),
TEXT ("Menu bar button height"),
SM_ARRANGE, TEXT ("SM_ARRANGE"),
TEXT ("How minimized windows arranged"),
SM_CXMINIMIZED, TEXT ("SM_CXMINIMIZED"),
TEXT ("Minimized window width"),
SM_CYMINIMIZED, TEXT ("SM_CYMINIMIZED"),
TEXT ("Minimized window height"),
SM_CXMAXTRACK, TEXT ("SM_CXMAXTRACK"),
TEXT ("Maximum dragable width"),
SM_CYMAXTRACK, TEXT ("SM_CYMAXTRACK"),
TEXT ("Maximum dragable height"),
SM_CXMAXIMIZED, TEXT ("SM_CXMAXIMIZED"),
TEXT ("Width of maximized window"),
SM_CYMAXIMIZED, TEXT ("SM_CYMAXIMIZED"),
TEXT ("Height of maximized window"),
SM_NETWORK, TEXT ("SM_NETWORK"),
TEXT ("Network present flag"),
SM_CLEANBOOT, TEXT ("SM_CLEANBOOT"),
TEXT ("How system was booted"),
SM_CXDRAG, TEXT ("SM_CXDRAG"),
TEXT ("Avoid drag x tolerance"),
SM_CYDRAG, TEXT ("SM_CYDRAG"),
TEXT ("Avoid drag y tolerance"),
SM_SHOWSOUNDS, TEXT ("SM_SHOWSOUNDS"),
TEXT ("Present sounds visually"),
SM_CXMENUCHECK, TEXT ("SM_CXMENUCHECK"),
TEXT ("Menu check-mark width"),
SM_CYMENUCHECK, TEXT ("SM_CYMENUCHECK"),
TEXT ("Menu check-mark height"),
SM_SLOWMACHINE, TEXT ("SM_SLOWMACHINE"),
TEXT ("Slow processor flag"),
SM_MIDEASTENABLED, TEXT ("SM_MIDEASTENABLED"),
TEXT ("Hebrew and Arabic enabled flag")
};
然后再新建一个SYSMETS.C的文件:
/*----------------------------------------------------------------------
SYSMETS1.C--System Metrics Display Program NO.1
---------------------------------------------------------------------*/
#include <windows.h>
#include "sysmets.h"
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[] = TEXT("SysMets1");
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;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This program requires Windows!"),szAppName,MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,TEXT("Get System Metrics No.1"),
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,cxGaps,cyChar;
HDC hdc;
int i;
PAINTSTRUCT ps;
TCHAR szBuffer[10];
TEXTMETRIC tm;
switch(message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc,&tm);
cxChar = tm.tmAveCharWidth;
cxGaps = (tm.tmPitchAndFamily&1?3:2)*cxChar/2;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd,hdc);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
for(i = 0 ;i< NUMLINES;i++)
{
TextOut(hdc,0,cyChar*i,sysmetrics[i].szLabel,lstrlen(sysmetrics[i].szLabel));
TextOut(hdc,22*cxGaps,cyChar*i,sysmetrics[i].szDesc,lstrlen(sysmetrics[i].szDesc));
SetTextAlign(hdc,TA_RIGHT|TA_TOP);
TextOut(hdc,22*cxGaps + 40*cxChar,cyChar*i,szBuffer,
wsprintf(szBuffer,TEXT("%5d"),GetSystemMetrics(sysmetrics[i].iIndex)));
SetTextAlign(hdc,TA_LEFT|TA_TOP);
}
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
最后的运行结果如下:
下面说一下跑这个例子遇到的问题吧,主要是头文件不对,我发现我照着书上的头文件写,会报错。因为从151行开始吧,到最后,会报头文件里面出错,于是我将那几行删掉就对了。
还有就是VC的调试功能很强大啊,主要就是设好断点之后,运用F10,既是单步执行。
还有就是一个快捷键:Ctrl+Alt+T就是类似Eclipse的代码提示,大家可以试试。
- 大小: 24.5 KB
分享到:
相关推荐
《Windows程序设计》第二版是王艳平教授的著作,这本书深入浅出地讲解了如何在Windows操作系统下进行程序开发。随书附带的程序代码是学习本书的重要辅助资源,包含了书中所有示例程序的完整工程源代码。通过这些源...
《Windows程序设计(第5版 珍藏版)》不仅是一本学习Windows编程的教材,更是帮助读者构建完整Windows程序设计知识体系的重要资源。通过本书的学习,读者可以深入了解Windows API的工作原理,掌握Windows程序设计的...
根据提供的信息,“MFC Windows程序设计第三章PDF”这一标题及描述主要涉及的是Microsoft Foundation Classes (MFC)在Windows程序开发中的应用。MFC是一套由Microsoft提供的C++类库,用于简化Windows应用程序的开发...
总之,《Windows程序设计(第5版 珍藏版)》是一本全面且深度足够的教材,无论是对初学者还是经验丰富的开发者,都能从中获益匪浅。通过这本书的学习,读者不仅可以提升Windows编程技能,还能对操作系统的工作原理有...
以上是对MFC Windows程序设计第七章和第八章主要内容的概括,这些知识对于理解和开发基于MFC的应用程序至关重要。通过学习这些内容,开发者能够熟练地创建具有用户友好的对话框以及高效的文档视图架构的应用程序。
像我的许多同行一样,我学习Windows编程是从读Petzold的书《Windows程序设计》——一本所有Windows程序员都使用的Windows编程圣经——开始的。在刚刚成为一名MFC程序员时,那天我冲进书店,想买一本能够与...
《Windows程序设计(北大第五版)7-11章》是深入探讨Windows操作系统下程序设计的一本经典教材,尤其适合计算机科学与技术专业的学生和软件开发者学习。这五章涵盖了Windows编程的核心概念和技术,让我们逐一解析...
《Windows程序设计PPT(1-6章)曹...此外,"CLR PPT 曹汉清"的标签可能意味着后续内容会涉及到.NET框架的Common Language Runtime(CLR),它是.NET应用程序执行的平台,对于深入学习Windows程序设计也是必不可少的。
《Windows程序设计》第五版是一本深受程序员喜爱的经典教材,由Charles Petzold撰写,它深入浅出地讲解了Windows操作系统下的程序开发...因此,这份光盘内容对于学习Windows程序设计的人来说,无疑是一份宝贵的资料。
《MFC Windows 程序设计》第二版是一本深入探讨MFC(Microsoft Foundation Classes)框架的权威书籍,旨在帮助开发者构建高效、稳定的Windows应用程序。这本书涵盖了从基础到高级的MFC编程技术,包括UI设计、事件...
- **Windows程序设计基础**:本书的第一章旨在为从未接触过Windows程序设计的读者提供基础知识。这一章通常会介绍Windows操作系统的基本概念、编程环境的搭建、以及如何编写第一个简单的程序(如“Hello, World!”)...
第4章 ActiveX控件:ActiveX是微软的一种组件技术,用于创建可重用的软件部件。本章将介绍如何在Windows程序中使用和创建ActiveX控件,以及如何进行跨平台通信。 第5章 文档/视图结构:这是MFC(Microsoft ...
【标题】"10级Windows应用程序设计第4章(1)共35页"提示我们,这是一份关于Windows应用程序开发的学习资料,可能是教材或课件,内容涵盖了第4章的第一部分,共计35页。这章可能深入探讨了Windows环境下程序设计的...
《Windows程序设计》是计算机科学领域的一本经典教材,主要针对Windows操作系统下的应用程序开发进行深入讲解。这本书的第五版以其清晰的阐述和详实的内容深受程序员们的喜爱。在这个压缩包中,包含的文件名为...