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

孙鑫c++教程笔记(1)

2017-05-26 17:00 295 查看
一、句柄类型

图标句柄HICO、光标句柄HCURSOR、窗口句柄HWND、应用程序实例句柄类型HINSTANCE

二、Windows程序的入口函数

int WINAPI WinMain(

  HINSTANCE hInstance,      // handle to current instance

  HINSTANCE hPrevInstance,  // handle to previous instance

LPSTR lpCmdLine,          // command line类似于传入参数 LP开头为长指针类型   int nCmdShow              // show state窗口如何显示

);

三、创建完整窗口步骤:

1.       设计一个窗口类

窗口类:WNDCLASS

typedef struct _WNDCLASS {

   UINT    style; 

   WNDPROC lpfnWndProc; //指定回调函数

   int     cbClsExtra; 

   int     cbWndExtra; //通常设为0

   HANDLE  hInstance; //实例号

   HICON   hIcon; //图标句柄

   HCURSOR  hCursor; //光标句柄

   HBRUSH  hbrBackground; //画刷,窗口背景颜色

   LPCTSTR  lpszMenuName; //菜单栏名字

   LPCTSTR  lpszClassName; //窗口名字

} WNDCLASS;

回调函数:WindowProc

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

2.       注册窗口类

RegisterClass(窗口类指针)

3.       创建窗口

HWDN hwdn;

Hwdn=CreateWindows(classname,title,style,x,y,width,父窗口,菜单,实例号,多文档附加参数)

4.       显示及更新窗口

ShowWindow(hwdn,status);

UpdateWindow(hwnd)

 

四、GetMessage()获得窗口消息

csdn在线查询:https://msdn.microsoft.com/en-us/library/ms950410.aspx

第一节代码:

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

LRESULT CALLBACK WinSunProc(
HWND hwnd,      // handle to window
UINT uMsg,      // message identifier
WPARAM wParam,  // first message parameter
LPARAM lParam   // second message parameter
);

int WINAPI WinMain(
HINSTANCE hInstance,      // handle to current instance
HINSTANCE hPrevInstance,  // handle to previous instance
LPSTR lpCmdLine,          // command line
int nCmdShow              // show state
)
{
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_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName="Weixin2003";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);

HWND hwnd;
hwnd=CreateWindow("Weixin2003","北京维新科学技术培训中心",WS_OVERLAPPEDWINDOW,
0,0,600,400,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 WinSunProc(
HWND hwnd,      // handle to window
UINT uMsg,      // message identifier
WPARAM wParam,  // first message parameter
LPARAM lParam   // second message parameter
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char is %d",wParam);
MessageBox(hwnd,szChar,"weixin",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","weixin",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"计算机编程语言培训",strlen("计算机编程语言培训"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"维新培训",strlen("维新培训"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","weixin",MB_YESNO))
{
DestroyWindow(hwnd);//quit windows
}
break;
case WM_DESTROY:
PostQuitMessage(0);//Getmessage() return 0;quit app ;
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);//default mes deal
}
return 0;
}

抱歉了,老铁们,以后不用c++了,不会有(2)了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c 孙鑫 笔记