ReadBmpArray

本文介绍了一个使用Windows API进行绘图的应用程序实例,包括如何加载并绘制位图文件(.bmp)到窗口中。通过具体的C语言代码,展示了如何定义绘图函数,并在窗口上显示指定的位图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//bmp.cpp
const unsigned char* gImage_bmp=Image_bmp;

////////////////////////////////////
//ReadBmpArray.cpp

#include <windows.h>   
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
extern HWND    g_hwnd;  // generic window handle
extern HDC        g_hdc; // graphics device context 

/*
void LCD_Paint_Bmp(int x0,int y0,int h,int l,unsigned char bmp[])
{
	int x,y;
	unsigned int c;
	int p = 0;
	
	for( y = y0 ; y < l ; y++ )
	{
		for( x = x0 ; x < h ; x++ )
		{
			//RGB888
			c = (bmp[p]) | (bmp[p+1]<<8) | bmp[p+2]<<16;
			if ( ( (x0+x) < LCD_XSIZE) && ( (y0+y) <LCD_YSIZE) )
			LCD_BUFFER[y0+y][x0+x] = c ;
			p = p+3;  

			
		}
	}
}
*/
/* 
 int index;
 for (index=0; index < 100; index++)
 {
 
int x = rand()%400;
int y = rand()%300;
COLORREF color = RGB(rand()%255,rand()%255,rand()%255);
SetPixel(hdc, x,y, color);
 
 }
 ReleaseDC(hwnd, hdc);
*/
void LCD_Paint_Bmp(int x0,int y0,int h,int l,const unsigned char bmp[])
{
	int x,y;
	//unsigned int c;
	int p = 0;
	
	for( y = y0 ; y < l ; y++ )
	{
		for( x = x0 ; x < h ; x++ )
		{
			//RGB888
			//c = (bmp[p]) | (bmp[p+1]<<8) | bmp[p+2]<<16;
			//if ( ( (x0+x) < LCD_XSIZE) && ( (y0+y) <LCD_YSIZE) )
			//LCD_BUFFER[y0+y][x0+x] = c ;
			//COLORREF color = RGB(bmp[p],bmp[p+1],bmp[p+2]);
			COLORREF color = RGB(bmp[p+2],bmp[p+1],bmp[p]);
			SetPixel(g_hdc, x,y, color);

			p = p+3;  

			
		}
	}

	 ReleaseDC(g_hwnd, g_hdc);
}

//////////////////////////////////
//SetPixel.cpp
/*file:SetPixel.c
illustrate the usage of SetPixel()
 
compile: gcc SetPixel.c -lgdi32 -luser32
*/
#include <windows.h>   
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
#define WINDOW_CLASS_NAME "WINCLASS1"
#define WINDOW_WIDTH  400
#define WINDOW_HEIGHT 300
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
HWND      main_window_handle = NULL; // globally track main window
HINSTANCE hinstance_app      = NULL; // globally track hinstance

HWND    g_hwnd;  // generic window handle
HDC        g_hdc; // graphics device context 
 ///////////////////////////////////////////////
//extern const unsigned char gImage_bmp[];//391680
extern const unsigned char* gImage_bmp;
void LCD_Paint_Bmp(int x0,int y0,int h,int l,const unsigned char bmp[]);
///////////////////////////////////////////////
int main()
{
WinMain(GetModuleHandle(0),NULL,NULL,0);
return 0;
}
 
 
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg,  WPARAM wparam, LPARAM lparam)
{
 
PAINTSTRUCT  ps;  // used in WM_PAINT
HDC    &hdc=g_hdc; // handle to a device context
switch(msg)
{ 
case WM_CREATE: 
{
 // do initialization stuff here
} break;
case WM_PAINT: 
{
  // simply validate the window 
//hdc = BeginPaint(hwnd,&ps);  
printf("WM_PAINT\n");

hdc = GetDC(hwnd); 
RECT rc;
SetRect(&rc,0,0, WINDOW_WIDTH, WINDOW_HEIGHT);
FillRect(hdc,&rc,(HBRUSH)GetStockObject(BLACK_BRUSH));//清屏
LCD_Paint_Bmp(0, 0, 480, 272, gImage_bmp); 
  // end painting
//EndPaint(hwnd,&ps);
} break;
 
case WM_DESTROY: 
{
 PostQuitMessage(0);
} break;
 
default:break;
} // end switch
 
return (DefWindowProc(hwnd, msg, wparam, lparam));
} // end WinProc
 
 
int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE hprevinstance,
     LPSTR lpcmdline,int ncmdshow)
{
WNDCLASSEX winclass; // this will hold the class we create
MSG     msg;   // generic message
HWND & hwnd= g_hwnd;  // generic window handle
HDC  &  hdc= g_hdc; // graphics device context 
 
winclass.cbSize         = sizeof(WNDCLASSEX);
winclass.style   = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra  = 0;
winclass.cbWndExtra  = 0;
winclass.hInstance  = hinstance;
winclass.hIcon   = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor  = LoadCursor(NULL, IDC_ARROW); 
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
// save hinstance in global
hinstance_app = hinstance;
// register the window class
if (!RegisterClassEx(&winclass))return (0);
// create the window
if (!(hwnd = CreateWindowEx(0,  // extended style
WINDOW_CLASS_NAME, // class
"Pixel Plotting Demo", // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0,   // initial x,y
WINDOW_WIDTH, // initial width
WINDOW_HEIGHT,// initial height
0,   // handle to parent 
0,   // handle to menu
hinstance,// instance of this application
0))) // extra creation parms
return (0);
 
// save main window handle
main_window_handle = hwnd;
// enter main event loop, but this time we use PeekMessage()
// instead of GetMessage() to retrieve messages
//while(TRUE)
while(GetMessage(&msg,NULL,0,0))
{
//Sleep(200);
// test if there is a message in queue, if so get it
//if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{ 
if (msg.message == WM_QUIT)break;
 
TranslateMessage(&msg);
DispatchMessage(&msg);
} 
/* 
 hdc = GetDC(hwnd);
 
RECT rc;
SetRect(&rc,0,0, WINDOW_WIDTH, WINDOW_HEIGHT);
FillRect(hdc,&rc,(HBRUSH)GetStockObject(BLACK_BRUSH));//清屏


 int index;
 for (index=0; index < 100; index++)
 {
 
int x = rand()%400;
int y = rand()%300;
COLORREF color = RGB(rand()%255,rand()%255,rand()%255);
SetPixel(hdc, x,y, color);
 
 }
 ReleaseDC(hwnd, hdc);
*/
//LCD_Paint_Bmp(0, 0, 480, 272, gImage_bmp);
 
if (KEYDOWN(VK_ESCAPE))
SendMessage(hwnd, WM_CLOSE, 0,0);//发送一个退出消息
 
} // end while
 
return (msg.wParam);
} // end WinMain

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值