您的位置:首页 > 其它

孙鑫 Lesson1 window

2016-07-29 11:45 267 查看
#include <Windows.h>

#include <stdio.h>

LRESULT CALLBACK WinSunProc(

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_ERROR);

wndcls.hInstance=hInstance;

wndcls.lpfnWndProc=WinSunProc;

wndcls.lpszClassName="Test2016";

wndcls.lpszMenuName=NULL;

wndcls.style=CS_HREDRAW | CS_VREDRAW;

RegisterClass(&wndcls); //注册窗口

//创建窗口、显示窗口、更新窗口

HWND hwnd;

hwnd=CreateWindow("Test2016","Test C++'s window",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,

UINT uMsg,

WPARAM wParam,

LPARAM lParam

)

{

switch(uMsg)

{

case WM_CHAR:

char szChar[20];

sprintf(szChar,"char is %d",wParam);

MessageBox(hwnd,szChar,"Test",0);

break;

case WM_LBUTTONDOWN:

MessageBox(hwnd,"mouse clicked","Test",0);

HDC hdc;

hdc=GetDC(hwnd);

TextOut(hdc,0,50,"计算机编程语言C++",strlen("计算机编程语言C++"));

ReleaseDC(hwnd,hdc);

break;

case WM_PAINT:

HDC hDc;

PAINTSTRUCT ps;

hDc=BeginPaint(hwnd,&ps);

TextOut(hDc,0,0,"testA",strlen("testA"));

EndPaint(hwnd,&ps);

break;

case WM_CLOSE:

if(IDYES==MessageBox(hwnd,"是否真的结束?","Test",0))

{

DestroyWindow(hwnd);

}

break;

case WM_DESTROY:

PostQuitMessage(0);

break;

default:

return DefWindowProc(hwnd,uMsg,wParam,lParam);

}

return 0;

}

LPCTSTR,其中T为宏	宏定义,用_T可以保证从ascii编码类型转换到unicode编码类型的时候,程序不需要修改, 用_T来保证兼容性
Fn=function、L=long、  W=WORD、 H=handle、 U=unsign
C=const ptr=pointer  P=pointer cb=count of bytes
Sz=string terminate by 0、 n=number	Abbreviation
LONG_PTR	表示32位与64环境下都兼容,32位下4个字节,64位环境下8个字节
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息