📄 thebitmap.cpp
字号:
#include <Windows.h>
#include "TheBitMap.h"
const struct MessageDo myMSG[]={
WM_CREATE,DoCreate,
WM_PAINT,DoPaint,
WM_DESTROY,DoDestroy,
};
const TCHAR winName[]=TEXT("bitmap");
BYTE bitData[48*48*3];
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPWSTR lpCmdLine,int nShowCmd)
{
MSG msg;
HWND hWnd = InitApp(hInstance,lpCmdLine,nShowCmd);
if(!IsWindow(hWnd)) return 0;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return TermApp(hInstance,msg.wParam);
}
HWND InitApp(HINSTANCE hInstance,LPWSTR lpCmdLine,int nShowCmd)
{
WNDCLASS wc;
HWND hWnd;
#if defined(WIN32_PLATFORM_PSPC)
hWnd=FindWindow(winName,NULL);
if(hWnd)
{
SetForegroundWindow((HWND)(((DWORD)hWnd)|0x10));
return 0;
}
#endif
wc.style=0;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hIcon=NULL;
wc.hInstance=hInstance;
wc.lpfnWndProc=MainWndProc;
wc.lpszClassName=winName;
wc.lpszMenuName=NULL;
if(RegisterClass(&wc)==0) return 0;
hWnd=CreateWindow(winName,
TEXT("位图处理示例"),
WS_BORDER|WS_CAPTION|WS_SYSMENU,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if(!IsWindow(hWnd)) return 0;
ShowWindow(hWnd,nShowCmd);
UpdateWindow(hWnd);
return hWnd;
}
int TermApp(HINSTANCE hInstance,int nDefRec)
{
return nDefRec;
}
LRESULT CALLBACK MainWndProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam)
{
for(int i=0;i<dim(myMSG);i++)
{
if(wMsg==myMSG[i].code)
{
return (*myMSG[i].Fxn)(hWnd,wMsg,wParam,lParam);
}
}
return DefWindowProc(hWnd,wMsg,wParam,lParam);
}
LRESULT DoCreate(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
memset(bitData,0x7f,sizeof(bitData));
return 0;
}
LRESULT DoPaint(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
HBITMAP htp;
PAINTSTRUCT ps;
RECT rect;
HDC hdc,hdcMemo;
GetClientRect(hWnd,&rect);
//获取当前图形显示设备对象
hdc = BeginPaint(hWnd,&ps);
//创建一个虚拟设备显示对像
hdcMemo=CreateCompatibleDC(hdc);
//建立设备相关位图对像
//两者都是创建设备相关位图,返回hbitmap位图对象
//不通之处在于,createbitmap返回根据位图格式设定的位图对象
//createcompatiblebitmap返回的是当前设备色置相关的位图对象
//htp=CreateBitmap(48,48,1,24,bitData);
htp=CreateCompatibleBitmap(hdc,40,40);
DrawText(hdc,TEXT("位图示例"),-1,&rect,DT_CENTER);
//将设备相关位图对象设进虚拟设备显示对象
SelectObject(hdcMemo,htp);
//转换虚拟设备显示对象到当前设备显示
BitBlt(hdc,20,20,48,48,hdcMemo,0,0,SRCCOPY);
//清理
DeleteDC(hdcMemo);
EndPaint(hWnd,&ps);
return 0;
}
LRESULT DoDestroy(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
PostQuitMessage(0);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -