C++ 学习 二

本文详细介绍了如何使用Windows API创建窗口,并通过事件处理实现简单的窗口交互功能,包括字符输入、鼠标点击和文本显示。同时,还提供了一个递归函数的例子,展示了其在编程中的应用。

1. 利用windows api 创建窗口

 

 正确代码示例:

# include<windows.h>
# include<stdio.h>

 

LRESULT CALLBACK WindowLiProc(
    HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam);


int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_WINLOGO);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WindowLiProc;
wndcls.lpszClassName="CheersLi01";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd=CreateWindow("CheersLi01","Cheers Li Api 学习",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);

MSG msg;

while(GetMessage(&msg,NULL,0,0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
return 0;

}

 

 LRESULT CALLBACK WindowLiProc(
    HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
 LPARAM lParam)
 {
  switch(uMsg)
  {
  case WM_CHAR :
   char szChar[20];
   sprintf(szChar,"Char is %d",wParam);
   MessageBox(hwnd,szChar,"CheersLi01",0);
   break;
  case WM_LBUTTONDOWN :
   MessageBox(hwnd,"mouse click","CheersLi01",0);
   HDC hdc;
   hdc=GetDC(hwnd);
   TextOut(hdc,0,50,"我的C++学习",ARRAYSIZE("我的C++学习"));
   ReleaseDC(hwnd,hdc);
  case WM_PAINT :
   HDC hDc;
   PAINTSTRUCT ps;
   hDc=BeginPaint(hwnd,&ps);
   TextOut(hDc,0,50,"C++ Programming",ARRAYSIZE("C++ Programming"));
   EndPaint(hwnd,&ps);
   break;
  case WM_CLOSE :
   if(IDYES==MessageBox(hwnd,"是否真的结束?","Hints for you",MB_YESNO))
   {
    DestroyWindow(hwnd);
   }
   break;
  case WM_DESTROY :
   PostQuitMessage(0);
   break;
  default:
   return DefWindowProc(hwnd,uMsg,wParam,lParam);
  }

   return 0;
 }

 

曾经出错,问题解决帖子原文。

http://topic.youkuaiyun.com/u/20100726/22/b6e33032-471c-44f5-81a0-0ae82c70e0e8.html?seed=219286239&r=67257704#r_67257704

 2. 递归的一个例子:

 

#include <iostream.h>

void countdown(int n);

int main()
{
countdown(4);
return 0;
}

void countdown(int n)
{
cout<<"Counting down : "<<n<<endl;

if(n>0)
countdown(n-1);

cout<<"cout down ... "<<n<<endl;
}

 

 

 

 

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style> .AlignLeft { text-align: left; } .AlignCenter { text-align: center; } .AlignRight { text-align: right; } body { font-family: sans-serif; font-size: 11pt; } td { vertical-align: top; padding-left: 4px; padding-right: 4px; } tr.SectionGap td { font-size: 4px; border-left: none; border-top: none; border-bottom: 1px solid Black; border-right: 1px solid Black; } tr.SectionAll td { border-left: none; border-top: none; border-bottom: 1px solid Black; border-right: 1px solid Black; } tr.SectionBegin td { border-left: none; border-top: none; border-right: 1px solid Black; } tr.SectionEnd td { border-left: none; border-top: none; border-bottom: 1px solid Black; border-right: 1px solid Black; } tr.SectionMiddle td { border-left: none; border-top: none; border-right: 1px solid Black; } tr.SubsectionAll td { border-left: none; border-top: none; border-bottom: 1px solid Gray; border-right: 1px solid Black; } tr.SubsectionEnd td { border-left: none; border-top: none; border-bottom: 1px solid Gray; border-right: 1px solid Black; } table.fc { border-top: 1px solid Black; border-left: 1px solid Black; width: 100%; font-family: monospace; font-size: 10pt; } td.TextItemInsigAdd { color: #000000; background-color: #EEEEFF; } td.TextItemInsigDel { color: #000000; background-color: #EEEEFF; text-decoration: line-through; } td.TextItemInsigMod { color: #000000; background-color: #EEEEFF; } td.TextItemInsigOrphan { color: #000000; background-color: #FAEEFF; } td.TextItemNum { color: #696969; background-color: #F0F0F0; } td.TextItemSame { color: #000000; background-color: #FFFFFF; } td.TextItemSigAdd { color: #000000; background-color: #FFE3E3; } td.TextItemSigDel { color: #000000; background-color: #FFE3E3; text-decoration: line-through; } td.TextItemSigMod { color: #000000; background-color: #FFE3E3; } td.TextItemSigOrphan { color: #000000; background-color: #F1E3FF; } .TextSegInsigDiff { color: #0000FF; } .TextSegReplacedDiff { color: #0000FF; font-style: italic; } .TextSegSigDiff { color: #FF0000; } .TextSegElement_20851_38190_23383 { font-weight: bold; } .TextSegElement_35782_21035_31526 { } .TextSegElement_25968_23383 { color: #2E9269; } .TextSegElement_23383_31526_20018 { color: #3A7726; } .TextSegElement_32534_35793_22120_25351_20196 { color: #681717; } .TextSegElement_27880_37322 { color: #786A41; } .TextSegElement_25805_20316_31526 { } </style> <title>GA_D82DD83D_00-00-05 VS GA_D82DD83D_00-00-04_Warning</title> </head> <body> GA_D82DD83D_00-00-05 VS GA_D82DD83D_00-00-04_Warning<br/> 已产生: 2025/10/20 11:20:35<br/> &nbsp; &nbsp; <br/> 模式:&nbsp; 全部 &nbsp; <br/> 左边文件: E:\1_临时代码仓\GA_D37D_03-00-01\mainline\spa_traveo\src\IpcApplication\diagClient\canTp\canTp.c &nbsp; <br/> 右边文件: E:\1_临时代码仓\GA_D37D_02-00-04\mainline\spa_traveo\src\IpcApplication\diagClient\canTp\canTp.c &nbsp; <br/> <table class="fc" cellspacing="0" cellpadding="0"> <tr class="SectionBegin"> <td class="TextItemNum AlignRight">1</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*===================================================================================================================================*/</span></td> <td class="AlignCenter">=</td> <td class="TextItemNum AlignRight">1</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*===================================================================================================================================*/</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">2</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*&nbsp; Copyright DENSO Corporation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */</span></td> <td class="AlignCenter">&nbsp;</td> <td class="TextItemNum AlignRight">2</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*&nbsp; Copyright DENSO Corporation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">3</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*===================================================================================================================================*/</span></td> <td class="AlignCenter">&nbsp;</td> <td class="TextItemNum AlignRight">3</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*===================================================================================================================================*/</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">4</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*&nbsp; Version&nbsp; Date&nbsp; &nbsp; &nbsp; &nbsp; Author&nbsp;&nbsp; Change Description&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span></td> <td class="AlignCenter">&nbsp;</td> <td class="TextItemNum AlignRight">4</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*&nbsp; Version&nbsp; Date&nbsp; &nbsp; &nbsp; &nbsp; Author&nbsp;&nbsp; Change Description&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">5</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/* --------- ----------&nbsp; -------&nbsp; -------------------------------------------------------------------------------------------------- */</span></td> <td class="AlignCenter">&nbsp;</td> <td class="TextItemNum AlignRight">5</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/* --------- ----------&nbsp; -------&nbsp; -------------------------------------------------------------------------------------------------- */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">6</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*&nbsp; 1.0.0&nbsp; &nbsp; 3/6/2019&nbsp; &nbsp; LW&nbsp; &nbsp; &nbsp;&nbsp; New.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span></td> <td class="AlignCenter">&nbsp;</td> <td class="TextItemNum AlignRight">6</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*&nbsp; 1.0.0&nbsp; &nbsp; 3/6/2019&nbsp; &nbsp; LW&nbsp; &nbsp; &nbsp;&nbsp; New.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">7</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*&nbsp; 2.0.0&nbsp; &nbsp; 9/26/2021&nbsp;&nbsp; DC&nbsp; &nbsp; &nbsp;&nbsp; add a new function which is sending&nbsp; multi frame request&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span></td> <td class="AlignCenter">&nbsp;</td> <td class="TextItemNum AlignRight">7</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*&nbsp; 2.0.0&nbsp; &nbsp; 9/26/2021&nbsp;&nbsp; DC&nbsp; &nbsp; &nbsp;&nbsp; add a new function which is sending&nbsp; multi frame request&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span></td> </tr>取得canTp.c或者canTp.h
最新发布
10-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值