⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example11-3.htm

📁 Visual C++面向对象程序设计教程(配套习题资源)
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>例11-3</title>
</head>

<body>

<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">程序代码如下:</font></p>
<blockquote>
  <blockquote>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">//<font LANG="ZH-CN">头文件</font>diag.h<font LANG="ZH-CN">代码</font></font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">#define  
    IDM_HELP&nbsp;&nbsp;&nbsp;&nbsp; 1</font></p> 
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">#define  
    IDM_ABOUT&nbsp;&nbsp;&nbsp; 2</font></p> 
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">//<font LANG="ZH-CN">资源文件</font>diag.rc<font LANG="ZH-CN">代码</font></font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">//  
    Menu</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">ABOUT1 
    MENU DISCARDABLE</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">BEGIN</font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">POPUP  
      &quot;<font LANG="ZH-CN">帮助</font>(&amp;H)&quot;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">BEGIN</font></p>
      <blockquote>
        <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">MENUITEM  
        &quot;<font LANG="ZH-CN">帮助主题</font>(&amp;H)&quot;, IDM_HELP</font></p> 
        <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">MENUITEM  
        &quot;<font LANG="ZH-CN">关于</font>(&amp;A)&quot;, IDM_ABOUT</font></p> 
      </blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">END</font></p>
    </blockquote>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">END</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">////////////////////////////////////////////////////////////////////////////</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">// 
    Dialog <font FACE="??ì?,SimSun" LANG="ZH-CN">创建模态对话框资源</font></font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">ABOUTBOX  
    DIALOG DISCARDABLE 20, 20, 114, 71</font></p> 
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">STYLE 
    DS_MODALFRAME | WS_POPUP</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">FONT 
    10, &quot;System&quot;</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">BEGIN</font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">CTEXT  
      &quot;<font LANG="ZH-CN">关于本软件</font>&quot;,-1,30,12,84,8</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">ICON 
      &quot;About1&quot;,-1,8,8,20,20</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">CTEXT  
      &quot;<font LANG="ZH-CN">本软件是关于对话框的操作</font>&quot;,-1,0,36,114,8</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">CTEXT  
      &quot;(c) <font LANG="ZH-CN">曹</font> <font LANG="ZH-CN">建</font> <font LANG="ZH-CN">春</font>  
      2003&quot;,-1,0,48,114,8</font></p> 
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">DEFPUSHBUTTON 
      &quot;OK&quot;,IDOK,73,59,25,11,WS_GROUP</font></p>
    </blockquote>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">END</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">// 
    Dialog <font FACE="??ì?,SimSun" LANG="ZH-CN">创建非模态对话框资源</font></font> </p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">HELP  
    DIALOG DISCARDABLE 0, 0, 107, 54</font></p> 
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">STYLE 
    DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">CAPTION 
    &quot;Dialog&quot;</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">FONT 
    10, &quot;System&quot;</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">BEGIN</font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">DEFPUSHBUTTON 
      &quot;OK&quot;,IDOK,72,35,28,12</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">LTEXT  
      &quot;<font LANG="ZH-CN">本对话框为非模态对话框</font>&quot;,IDC_HELP,7,15,93,32</font></p>
    </blockquote>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">END</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">//<font LANG="ZH-CN">程序代码</font></font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">#include  
    &lt;windows.h&gt;</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">#include 
    &quot;diag.h&quot;</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">HWND 
    hWndModeless=NULL;</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">LRESULT  
    CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">//对话框窗口处理函数</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">BOOL  
    CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM);</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">BOOL 
    CALLBACK HelpDlgProc(HWND, UINT, WPARAM, LPARAM);</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int  
    WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,</font></p> 
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">PSTR 
    szCmdLine, int iCmdShow)</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">static 
      char szAppName[] = &quot;About1&quot; ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">MSG 
      msg ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">HWND 
      hwnd ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">WNDCLASSEX 
      wndclass ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.cbSize 
      = sizeof (wndclass) ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.style 
      = CS_HREDRAW | CS_VREDRAW ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.lpfnWndProc 
      = WndProc ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.cbClsExtra 
      = 0 ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.cbWndExtra 
      = 0 ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.hInstance 
      = hInstance ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.hIcon 
      = LoadIcon (hInstance, szAppName) ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.hCursor 
      = LoadCursor (NULL, IDC_ARROW) ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.hbrBackground 
      = (HBRUSH) GetStockObject (WHITE_BRUSH) ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.lpszMenuName 
      = szAppName ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.lpszClassName 
      = szAppName ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">wndclass.hIconSm 
      = LoadIcon (hInstance, szAppName) ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">RegisterClassEx 
      (&amp;wndclass) ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">hwnd  
      = CreateWindow (szAppName, &quot;<font LANG="ZH-CN">对话框示例</font>&quot;,</font></p> 
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">WS_OVERLAPPEDWINDOW,</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">CW_USEDEFAULT, 
      CW_USEDEFAULT,</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">CW_USEDEFAULT, 
      CW_USEDEFAULT,</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">NULL, 
      NULL, hInstance, NULL) ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">ShowWindow 
      (hwnd, iCmdShow) ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">UpdateWindow 
      (hwnd) ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">while 
      (GetMessage (&amp;msg, NULL, 0, 0))</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{//<font LANG="ZH-CN">截获非模态对话框消息并发往处理函数</font></font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">if(!IsDialogMessage(hWndModeless,&amp;msg))</font></p>
      <blockquote>
        <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
        <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">TranslateMessage 
        (&amp;msg) ;</font></p>
        <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">DispatchMessage 
        (&amp;msg) ;</font></p>
        <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
      </blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">return 
      msg.wParam ;</font></p>
    </blockquote>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
    </blockquote>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">LRESULT  
    CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)</font></p> 
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">static 
      WNDPROC lpfnAboutDlgProc ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">static 
      HINSTANCE hInstance ;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">switch 
      (iMsg)</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
      <blockquote>
        <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">case 
        WM_CREATE :</font></p>
        <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">hInstance 
        = ((LPCREATESTRUCT) lParam)-&gt;hInstance ;</font></p>
        <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">return 
        0 ;</font></p>
        <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">case 
        WM_COMMAND :</font></p>
        <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">switch 
        (LOWORD (wParam))</font></p>
        <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
        <blockquote>
          <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">case  
          IDM_ABOUT :</font></p> 
          <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">//<font FACE="??ì?,SimSun" LANG="ZH-CN">调用</font>DialogBox<font FACE="??ì?,SimSun" LANG="ZH-CN">函数显示模态对话框</font></font></p>
          <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">DialogBox 
          (hInstance, &quot;AboutBox&quot;, hwnd, AboutDlgProc) ;</font></p>
          <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">return  
          0 ;</font></p> 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -