Windows 下窗口画图---十天教会你俄罗斯方块

接上篇:https://blog.csdn.net/qq_41603898/article/details/80968333

1、WM_PAINT

2、Rectangle

3、兼容性DC

一.在回调函数中

补上一种case

case WM_PAINT://窗口变化时重绘

hDC = BeginPaint(hWnd,&ps);

       OnPaint(hDC);//自定义函数,包括Rectangle

  EndPaint(hWnd,&ps);

        break;

二.Rectangle 顾名思义矩形,画矩形

BOOL Rectangle(HDC hdc, // handle to DC

int nLeftRect, // x-coord of upper-left corner of rectangle

int nTopRect, // y-coord of upper-left corner of rectangle

int nRightRect, // x-coord of lower-right corner of rectangle

int nBottomRect // y-coord of lower-right corner of rectangle);//通过给左上角和右下角一个点对角线画矩形


将画矩形函数装在OnPaint函数中,为后面兼容DC准备

void OnPaint(HDC hDC){//画矩形框Rectangle(hDC,RECT_UPPER_X,RECT_UPPER_Y,RECT_LOWER_X,RECT_LOWER_Y);}


#define WND_HEIGHT 640//比RECT_LOWER_Y大

#define RECT_UPPER_X 0

#define RECT_UPPER_Y 0

#define RECT_LOWER_X 300

#define RECT_LOWER_Y 600//也就是两个点坐标,规范正式而已

写在后面记得声明

三.兼容性DC(详情

根据设备DC创建兼容性DC,并在兼容性DC上把绘图操作全部绘制完,

然后再一次性拷贝到源设备DC上,避免屏幕刷新闪烁

1.创建兼容性DC

2.创建兼容性位图

3.将位图与DC绑定在一起

4.释放位图

5.释放DC

将内存DC传递到窗口DC

BOOL BitBlt( HDC hdcDest, int nXDest, int nYDest, nt nWidth, int nHeight,HDC hdcSrc, nt nXSrc, int nYSrc, DWORD dwRop );

返回值:失败返回0, 成功返回非零

参数1: 目标DC,窗口DC

参数2,3: 目标的起始位置,注意是基于我们的窗口

参数4,5: 区域的大小

参数6: 源DC,也就是我们的内存DC

参数7,8: 内存图片的起始位置

参数9: 传递的方式

最后的OnPaint()

void OnPaint(HDC hDC)
{


HDC mDC = CreateCompatibleDC(hDC);


HBITMAP hBitmap = CreateCompatibleBitmap(hDC, 300, 600);


SelectObject(mDC, hBitmap);


Rectangle(hDC, 0, 0, 300, 600);


BitBlt(hDC, 0, 0, 300, 600, mDC, 0, 0, SRCCOPY);


DeleteObject(hBitmap);


DeleteDC(mDC);


}

结合上一篇文章更新的代码:

#include <windows.h>
#include "Resource.h"
#include "stdafx.h"
#define WND_POS_X 100
#define WND_POS_Y 100
#define WND_WIDTH 500
#define WND_HEIGHT 660
#define IDR_MENU1                       130
#define IDR_MENU3                       133
#define IDI_ICON2                       134




//声明
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void OnPaint(HDC hDC);




int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;//窗口句柄
MSG mSg;
HMENU hMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MENU3));


//创建窗口结构体
WNDCLASSEX wc;
wc.cbClsExtra = 0;//暂时不用
wc.cbSize = sizeof(wc);//结构体大小
wc.cbWndExtra = 0;//暂时不用
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;//背景颜色
wc.hCursor = LoadCursor(hInstance,MAKEINTRESOURCE(IDI_HAND));//光标
wc.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON2));//窗口左上角的图标
wc.hIconSm = NULL;//状态栏中的图标,默认与左上角图标一致
wc.hInstance = hInstance;//实例句柄
wc.lpfnWndProc = WndProc;//回调函数
wc.lpszClassName = "nanhang";//结构体名字
wc.lpszMenuName = ("MEAN");//菜单栏名字
wc.style = CS_HREDRAW | CS_VREDRAW;//窗口水平变化或是垂直变化时重绘窗口


  //注册窗口结构体
if (0 == RegisterClassEx(&wc))
{
int eNum = GetLastError();//注册失败的错误代码
return 0;//注册失败
}


//创建窗口
hWnd = CreateWindowEx(WS_EX_WINDOWEDGE, "nanhang",
"俄罗斯方块", WS_OVERLAPPEDWINDOW,
WND_POS_X, WND_POS_Y, WND_WIDTH, WND_HEIGHT,
NULL, hMenu, hInstance, NULL);
if (NULL == hWnd)
{
return 0;//创建失败
}
//显示窗口
ShowWindow(hWnd, nCmdShow);


//消息循环
while (GetMessage(&mSg, NULL, 0, 0))
{

//翻译消息
TranslateMessage(&mSg);
//分发消息
DispatchMessage(&mSg);
}




return 0;
}




//回调函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT uID, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hDC;
switch (uID)
{case WM_PAINT:
hDC = BeginPaint(hWnd, &ps);
   
OnPaint(hDC);


EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);//点击叉后,关闭窗口同时退出程序
break;


}
return DefWindowProc(hWnd, uID, wParam, lParam);//让系统自动处理一些默认消息
}
void OnPaint(HDC hDC)
{


HDC mDC = CreateCompatibleDC(hDC);


HBITMAP hBitmap = CreateCompatibleBitmap(hDC, 300, 600);


SelectObject(mDC, hBitmap);


Rectangle(hDC, 0, 0, 300, 600);


BitBlt(hDC, 0, 0, 300, 600, mDC, 0, 0, SRCCOPY);


DeleteObject(hBitmap);


DeleteDC(mDC);


}


猜你喜欢

转载自blog.csdn.net/qq_41603898/article/details/80980282