什么Windows函数可以写入像素的矩形

What Windows function writes a rectangle of pixels?

本文关键字:像素 Windows 函数 什么      更新时间:2024-04-28

我一直在使用WriteConsoleOutput函数,它所做的就是在命令提示符中写入一个矩形字符。然而,对于使用像素操作而不是字符操作的程序,Windows函数是什么来编写像素矩形?功能类似于WriteConsoleOutput,从数组中获取信息并写入,但使用像素而不是字符。我不是指在图像编辑器中看到的线条或填充形状。我的意思是,就像存储在数组中的图像一样,其中的一个矩形块被写入屏幕。

图形代码是这样完成的:

#include <windows.h>
#include <stdint.h>
#include <iomanip>
#include <string>
#include <cstring>
const int width = 640;
const int height = 480;
uint32_t bitmapdata[width*height];
BITMAP bitmap = {0, width, height, width*4, 1, 32, bitmapdata};
BITMAPINFOHEADER bitmapinfoheader = {sizeof(BITMAPINFOHEADER), width, -height, 1, 32, BI_RGB, width*height*4, 3779, 3779, 0, 0};
BITMAPINFO bitmapinfo = {bitmapinfoheader, 0};
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int main(){
// Register the window class.
const char CLASS_NAME[]  = "test2";
WNDCLASS wc = { };
wc.lpfnWndProc   = WindowProc;
wc.hInstance     = GetModuleHandle( 0 );
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0,                              // Optional window styles.
CLASS_NAME,                     // Window class
"test",    // Window text
WS_OVERLAPPEDWINDOW,            // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL,       // Parent window
NULL,       // Menu
GetModuleHandle( 0 ),  // Instance handle
NULL        // Additional application data
);
if (hwnd == NULL)
{
return -1;
}
ShowWindow(hwnd, 1);

MSG msg = { };
HDC screenhandle = GetDC(NULL);
HDC handle = GetDC(hwnd);
HDC screen = CreateCompatibleDC(handle);
HBITMAP bitmaphandle = CreateCompatibleBitmap(handle, width, height);
SelectObject(screen, bitmaphandle);
for(int i=0; i<width*height; i++){
bitmapdata[i] = i;
}
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
SetDIBits(screen, bitmaphandle, 0, height, bitmapdata, &bitmapinfo, 0);
BitBlt(handle, 0, 0, width, height, screen, 0, 0, SRCCOPY);
}
return 0;
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

它确实从像素数组中写入一个像素矩形,特别是由32位像素值组成的位图数据。while(GetMessage(&msg,NULL,0,0(({TranslateMessage(&aamp;msg(;DispatchMessage(&ap;msg;(}循环由每帧运行的代码组成,WindowProc指定在DispatchMessage中的事件期间执行的内容。此示例代码编写了一个640×480的测试矩形,其中像素分别为编号颜色。