- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习 C++,但我在将图像放到屏幕上时遇到了问题。
我已经在互联网上搜索了帮助,但找不到。
我正在尝试创建一个窗口,并在调用 WM_Paint 消息时在客户区放置一个简单的颜色,但程序像往常一样只显示灰色屏幕。我正在使用 code::blocks 10.05。
#include <windows.h>
#include <iostream>
using namespace std;
int winx = 500;
int winy = 500;
int winbpp = 24;
static char m_bibuf[ sizeof(BITMAPINFOHEADER) + 12 ];
static BITMAPINFO &m_bi = *(BITMAPINFO*)&m_bibuf;
static BITMAPINFOHEADER &m_bih = m_bi.bmiHeader;
int* buffer = new int[winx*winy*winbpp];
int setbuffer()
{
for(int x = 0; x < winx; x++)
{
for(int y=0; y < winy; y++)
{
for(int z =0; z < winbpp; z++)
{
buffer[x*y*z] = 1;
}
}
}
return 0;
}
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "CodeBlocksWindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
" project 1 ", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
winx, /* The programs width */
winy, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
{
m_bih.biWidth = winx;
m_bih.biHeight = winy;
m_bih.biBitCount = winbpp;
m_bih.biSize = sizeof(m_bih);
m_bih.biPlanes = 1; // DIBs are upside down
m_bih.biCompression = BI_BITFIELDS;
m_bih.biSizeImage = 0;
m_bih.biXPelsPerMeter = 0;
m_bih.biYPelsPerMeter = 0;
m_bih.biClrUsed = 0;
m_bih.biClrImportant = 0;
setbuffer();
InvalidateRect(hwnd, NULL, TRUE);
} break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hwnd, &ps);
RECT client;
GetClientRect(hwnd, &client);
StretchDIBits ( hDC,
0, // Destination top left hand
// corner X Position
0, // Destination top left hand
// corner Y Position
client.right, // Destinations width
client.bottom, // Destinations height
0, // Source top left hand
// corner's X Position
0, // Source top left hand
// corner's Y Position
winx, // Sources width
winy, // Sources height
buffer, // Source's data
&m_bi, // Bitmap Info
DIB_RGB_COLORS, // operations
SRCCOPY);
EndPaint(hwnd, &ps);
} break;
case WM_DESTROY:
delete buffer;
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
程序编译正常,但是没有在客户区显示'buffer'的内容,它只是像往常一样显示灰色。
“缓冲区”的格式是否正确?
=下面的更新代码=
#include <windows.h>
int winx = 500;
int winy = 400;
int bpp = 24;
size_t pwidth;
int scanlinewidth = 0;
int numscanlines = 0;
bool setscanline = 1;
bool setbitmap = 1;
BITMAPINFO m_bi;
struct BGR{ char blue; char green; char red;};
BGR* buffer;
void setframebuffer()
{
m_bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_bi.bmiHeader.biPlanes = 1;
m_bi.bmiHeader.biBitCount = bpp;
m_bi.bmiHeader.biCompression = BI_RGB;
m_bi.bmiHeader.biSizeImage = 0;
m_bi.bmiHeader.biXPelsPerMeter = 100;
m_bi.bmiHeader.biYPelsPerMeter = 100;
m_bi.bmiHeader.biClrUsed = 0;
m_bi.bmiHeader.biClrImportant = 0;
if (setbitmap)
{
m_bi.bmiHeader.biWidth = scanlinewidth;
m_bi.bmiHeader.biHeight = numscanlines;
setbitmap = 0;
}
if (!setbitmap)
{
pwidth = (scanlinewidth * 3 + 3) & ~3;
buffer = new BGR[(scanlinewidth + pwidth)*numscanlines];
}
for (int i = 0; i < ((scanlinewidth + pwidth) * numscanlines); i++)
{
buffer[i].blue = 0;
buffer[i].green = 0;
buffer[i].red = 255;
}
}
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "CodeBlocksWindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"framebuffer project for win32", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
winx, /* The programs width */
winy, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
DWORD result;
switch (message) /* handle the messages */
{
case WM_CREATE:
{
InvalidateRect(hwnd,0,0);
}break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hwnd, &ps);
RECT client;
GetClientRect(hwnd,&client);
if(setscanline) {
scanlinewidth = client.right;
numscanlines = client.bottom;
setscanline = 0;
}
setframebuffer();
result = StretchDIBits(hDC,
0, 0,
client.right, client.bottom,
0, 0,
scanlinewidth, numscanlines,
buffer, &m_bi, DIB_RGB_COLORS, SRCCOPY);
if(result != winy)
{
//Drawing failed
DebugBreak();
}
EndPaint(hwnd, &ps);
}break;
case WM_KEYDOWN:{ int escpressed = GetAsyncKeyState(27); if(escpressed){PostQuitMessage(0);}}break;
case WM_DESTROY: {
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
}break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
编辑将 int setbuffer 更改为 WM_create 中的 setbuffer
编辑 #2 将 getdc 更改为开始绘制。
编辑 #3 将 bpp 更改为 24,还更改了 WM_Create 以设置所有位图属性
编辑 #4 感谢您的帮助,我找到了解决方案。以前我不明白扫描线填充,但现在我完全理解了。更新后的代码在屏幕上打印出纯色,您可以自己编译并更改颜色。感谢您帮助我解决了我的问题,现在我可以开始使用我自己的代码在屏幕上绘图了。
最佳答案
我不确定所有这些问题是否仍然相关,因为我在您开始编辑之前复制了代码。无论哪种方式,这都有效,您应该能够很容易地看到差异。
BITMAPINFO
结构未正确初始化。
缓冲区创建不正确,它太大并且不一定正确对齐以用作位图。宽度需要填充为 4 的倍数,这对于 500 的宽度是可以的,但对于 499 的宽度则不行。
我还添加了一个检查以确保 StretchDIBits
成功,如果失败,它会将您转储到调试器中。如果愿意,您可以添加更合适的错误检查。
我还删除了一些评论,以尽可能缩短内容。
#include <windows.h>
const int winx = 500;
const int winy = 500;
const int winbpp = 3;
BITMAPINFO m_bi;
char* buffer = 0;
void setbuffer()
{
m_bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_bi.bmiHeader.biWidth = winx;
m_bi.bmiHeader.biHeight = winy;
m_bi.bmiHeader.biPlanes = 1;
m_bi.bmiHeader.biBitCount = 24;
m_bi.bmiHeader.biCompression = BI_RGB;
m_bi.bmiHeader.biSizeImage = 0;
m_bi.bmiHeader.biXPelsPerMeter = 100;
m_bi.bmiHeader.biYPelsPerMeter = 100;
m_bi.bmiHeader.biClrUsed = 0;
m_bi.bmiHeader.biClrImportant = 0;
size_t paddedWidth = (winx * 3 + 3) & ~3;
buffer = new char[paddedWidth * winy * winbpp];
for(int y = 0; y < winy; ++y)
{
for(int x = 0; x < winx; ++x)
{
for(int z = 0; z < 3; ++z)
{
buffer[y * paddedWidth + x * winbpp + z] = z * x;
}
}
}
}
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
char szClassName[] = "CodeBlocksWindowsApp";
int WINAPI WinMain(HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
wincl.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
if(!RegisterClassEx(&wincl))
return 0;
hwnd = CreateWindowEx(
0, /* Extended possibilites for variation */
szClassName, /* Classname */
" project 1 ", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
winx, /* The programs width */
winy, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
ShowWindow(hwnd, nCmdShow);
while(GetMessage(&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hDC;
RECT client;
DWORD result;
switch(message) /* handle the messages */
{
case WM_CREATE:
setbuffer();
InvalidateRect(hwnd, NULL, TRUE);
break;
case WM_PAINT:
hDC = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &client);
result = StretchDIBits(hDC,
0, 0,
client.right, client.bottom,
0, 0,
winx, winy,
buffer, &m_bi, DIB_RGB_COLORS, SRCCOPY);
if(result != winy)
{
//Drawing failed
DebugBreak();
}
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
delete buffer;
PostQuitMessage(0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
关于c++ - win32 C++ 在屏幕上放置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20156874/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!