1.5动手第一个Window程序

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#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,      // pointer to command line
  int nCmdShow          // show state of window
)
{
 WNDCLASS wndcls;
 wndcls.cbClsExtra=0;
 wndcls.cbWndExtra=0;
 wndcls.hbrBackground =(HBRUSH)GetStockObject(BLACK_BRUSH);
 //返回HGDIOBJ值,转换为一个画刷句柄
 wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
 //NULL表示为所有窗口加载光标,返回值就是光标句柄
 wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
 //图标与光标类似
 wndcls.hInstance=hInstance;
 wndcls.lpfnWndProc=WinSunProc;
 wndcls.lpszMenuName=NULL;
 wndcls.lpszClassName="Sunxin2006";
 wndcls.style=CS_HREDRAW|CS_VREDRAW;

RegisterClass(&wndcls);

HWND hwnd;
hwnd=CreateWindow("Sunxin2006","http://www.sunxin.org",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 msg.wParam;
}

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 code is %d",wParam);
  MessageBox(hwnd,szchar,"char",0);
  break;
 case WM_LBUTTONDOWN:
  MessageBox(hwnd,"mouse clicked","message",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,"http://www.sunxin.org",strlen("http://www.sunxin.org"));
  EndPaint(hwnd,&ps);
  break;
 case WM_CLOSE:
  if(IDYES==MessageBox(hwnd,"是否真的结束?","message",MB_YESNO))
  {
   DestroyWindow(hwnd);
  }
  break;
 case WM_DESTROY:
  PostQuitMessage(0);
  break;
 default:
  return DefWindowProc(hwnd,uMsg,wParam,lParam);
 }
 return 0;
}

1.6变量名的命名约定

匈牙利表示法:
a    数组
b    布尔值(int)
by    无符号字符
c    字符
cb    字节计数
rgb    保存RGB颜色值的长整型
cx,cy   计算x,y的长度
dw    无符号长整形
fn    函数
h    句柄
i    整数
m_    类的数据成员
n    短整型或整形
np    近指针
p    指针
l    长整型
lp    长指针
s    字符串
sz    以零结束的字符串
tm    正文大小
w    无符号整型
x,y    x,y的坐标

发布了47 篇原创文章 · 获赞 3 · 访问量 870

猜你喜欢

转载自blog.csdn.net/qq_42148307/article/details/105215098
今日推荐