第一个窗口程序
#include "stdafx.h"
#include<windows.h>
#include<stdio.h>
//窗口程序
LRESULT CALLBACK WindowProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int APIENTRY WinMain(HINSTANCE hInstance,//handle to current instance 当前模块所在的位置
HINSTANCE hPrevInstance,//handle to previous instance
LPSTR lpCmdLine,// command line
int nCmdShow) //show state
{
// TODO: Place code here.
//HANDLE 内核句柄
//HWND 窗口句柄
//HDC 指向设备上下文的句柄
//HINSTANCE 真正对象在0环 这个东西只是一个索引 DWORD 指向一个模块
/*DWORD dwAddr = (DWORD) hInstance;
char szOutBuff[0x80];
sprintf(szOutBuff,"模块地址,%x",dwAddr);
OutputDebugString(szOutBuff);
*/
//第一步定义你的窗口是什么样的?
//创建要给字符串
char szOutBuff[0x80];
TCHAR className[] = TEXT("My First Window");
WNDCLASS wndclass ={0};
wndclass.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
wndclass.lpszClassName =className;
wndclass.hInstance = hInstance ;//当前程序属于哪个程序
wndclass.lpfnWndProc= WindowProc;//窗口处理程序 不是调用函数 窗口处理过程
//注册窗口类
RegisterClass(&wndclass);
// 2. 创建并显示窗口
HWND hwnd = CreateWindow(
className,
TEXT("我的第一个窗口"),
WS_OVERLAPPEDWINDOW,
10,//单位都是像素
10,
600,
300,
NULL,
NULL,
hInstance,
NULL
);
if(hwnd == NULL)
{
sprintf(szOutBuff,"Error : %d",GetLastError());
OutputDebugString(szOutBuff);
return 0;
}
//3.展示窗口,以及获取消息
ShowWindow(hwnd,SW_SHOW);
MSG msg;
BOOL bRet;
//GetMessage是从消息队列中取得窗口的信息 ,第一个参数是取出来放的地址,第二个参数是所有的窗口的所有消息
//判断是否qu'dao
while((bRet = GetMessage(&msg ,NULL,0 , 0))!=0){
if(bRet == -1)
{
//handle the error and possibly exit
}
else
{
//转发消息
TranslateMessage(&msg);
//分发消息,调用消息处理函数
DispatchMessage(&msg);
}
}
return 0;
}
LRESULT CALLBACK WindowProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
){
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
调用过程
