VC++深入详解(孙鑫著)之MFC读书笔记一.2017.12.15

来源:互联网 发布:如何自建cdn源码 编辑:程序博客网 时间:2024/06/02 01:29

第一、创建一个窗口的Win32程序的7个步骤:

1、编写WinMain函数,可以在MSDN上查找和复制;

2、设计窗口类WNDCLASS;

3、注册窗口类;

4、创建窗口类;

5、显示并更新窗口;

6、编写消息循环;

7、编写窗口过程函数;窗口过程函数的语法,可以通过MSDN查看WNDCLASS的lpfnWndProc成员变量,在这个成员的解释中可以查到。

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


//1、编写WinMain函数


int WINAPI WinMain(
  HINSTANCE hInstance,      // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,          // command line
  int nCmdShow              // show state
)


//2、设计窗口类
{
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="sunxin2006";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;


//3、注册窗口类


RegisterClass(&wndcls);

//4、创建窗口

HWND hwnd;
hwnd=CreateWindow("sunxin2006","http://www.sunxin.org",WS_OVERLAPPEDWINDOW,
0,0,600,400,NULL,NULL,hInstance,NULL);

//5、显示及更新窗口
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);

//6、定义消息结构体、开始消息循环
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

//7、编写窗口过程函数


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;
}


VS2008运行的时候会报错


错误1error C2440: “=”: 无法从“const char [11]”转换为“LPCWSTR”
错误2error C2664: “CreateWindowExW”: 不能将参数 2 从“const char [11]”转换为“LPCWSTR”
错误3error C2664: “MessageBoxW”: 不能将参数 2 从“char [20]”转换为“LPCWSTR”
错误4error C2664: “MessageBoxW”: 不能将参数 2 从“const char [14]”转换为“LPCWSTR”
错误5error C2664: “TextOutW”: 不能将参数 4 从“const char [19]”转换为“LPCWSTR”
错误6error C2664: “TextOutW”: 不能将参数 4 从“const char [9]”转换为“LPCWSTR”
错误7error C2664: “MessageBoxW”: 不能将参数 2 从“const char [15]”转换为“LPCWSTR”

出现上述错误的原因是创建项目时,默认设置的字符集为Unicode,改为使用多字符集就可以:

右键单击项目,选择“属性“->"配置属性"->"常规"->"字符集",更改为使用多字符集。


LRESULT=long

CALLBACK=_stdcall

LPCTSTR=CONST CHAR*指向字符常量的指针。

第一课就是介绍如何编程实现一个窗口。有固定的流程,有固定的方法。


阅读全文
0 0
原创粉丝点击