Objective :
I want to draw a buffer of pixel (DWORD buf[WIDTH*HEIGHT]) to the whole client window. I am somewhat familiar with Winapi but not with GDI, reading the MSDN and other sources on the internet, I have came up with the following program.
Problem :
The code is not working. I have initialised all elements of my buffer ( buf[] ) to 0. So I should get a black screen on my window, but I getting a regular white window. Can somebody point me what's wrong am I doing ?
Code :
#include<Windows.h>
#include "stdafx.h"
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
const int HEIGHT = 512;
const int WIDTH = 512;
DWORD buf[WIDTH * HEIGHT];
BITMAPINFO bmi = { 0 };
HDC hWinDC = NULL;
HDC hbitDC = NULL;
HBITMAP hBitmap = NULL;
int WINAPI wWinMain(HINSTANCE hInstace, HINSTANCE hPrevInstace, LPWSTR lpCmdLine, int nCmdShow) {
memset(buf, 0, sizeof(buf)/sizeof(DWORD));
MSG msg = { 0 };
WNDCLASS wnd = { 0 };
wnd.lpfnWndProc = WndProc;
wnd.hInstance = hInstace;
wnd.lpszClassName = L"Window";
if (!RegisterClass(&wnd)) {
return 0;
}
HWND hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, wnd.lpszClassName, L"Window",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, NULL, NULL, hInstace, NULL);
if (!hwnd) {
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
HDC hWdc = NULL;
switch (msg){
case WM_CREATE:
bmi.bmiHeader.biSize = sizeof(BITMAPCOREHEADER);
bmi.bmiHeader.biWidth = WIDTH;
bmi.bmiHeader.biHeight = HEIGHT;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
break;
case WM_PAINT:
PAINTSTRUCT ps;
hWdc = BeginPaint(hwnd, &ps);
hWinDC = GetDC(hwnd);
hbitDC = CreateCompatibleDC(hWinDC);
hBitmap = CreateDIBSection(hWinDC, &bmi, DIB_RGB_COLORS, (void**)&buf, NULL, NULL);
SelectObject(hbitDC, hBitmap);
BitBlt(hWdc, 0, 0, WIDTH, HEIGHT, hbitDC, 0, 0, SRCCOPY);
EndPaint(hwnd, &ps);
break;
case WM_KEYUP:
if (wParam == 0x41) {
SendMessage(hwnd, WM_PAINT, NULL, NULL);
}
break;
case WM_DESTROY:
DeleteDC(hbitDC);
ReleaseDC(hwnd, hWinDC);
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}