孙鑫 Lesson1 window

来源:互联网 发布:ubuntu 16.04 wine 编辑:程序博客网 时间:2024/06/15 00:00

#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=unsignC=const ptr=pointer  P=pointer cb=count of bytesSz=string terminate by 0、 n=numberAbbreviationLONG_PTR表示32位与64环境下都兼容,32位下4个字节,64位环境下8个字节

0 0