您的位置:首页 > 编程语言 > C语言/C++

C++ 学习 二

2013-09-15 10:21 411 查看
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.csdn.net/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;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: