如何在windows窗口客户区内的指定区域内绘
1个回答
展开全部
#include <stdio.h>
#include <fcntl.h>
#include <windows.h>
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 480
typedef struct _MYArea {
int x, y, w, h;
}MYArea;
static void Win32_GetClientBitmap( HBITMAP area_bitmap, MYArea area )
{
unsigned char *pixel_mem;
pixel_mem = malloc (area.w*area.h*3);
/* 像素数据全部填充为RGB(150,150,150) */
memset (pixel_mem, 150, area.w*area.h*3);
/* 设定位图数据 */
SetBitmapBits (area_bitmap, area.w*area.h*3, pixel_mem);
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdcClient,hdcArea;
PAINTSTRUCT ps;
HBITMAP area_bitmap;
MYArea area;
switch (message){
case WM_KEYDOWN:
printf("WM_KEYDOWN: %ld\n",lParam);
case WM_LBUTTONDOWN:
//PostEBMessage(mainApp.desktop.handle,EB_LBUTTONDOWN,wParam,lParam);
break;
case WM_MOUSEMOVE:
//PostEBMessage(mainApp.desktop.handle,EB_MOUSEMOVE,wParam,lParam);
break;
case WM_LBUTTONUP:
//PostEBMessage(mainApp.desktop.handle,EB_LBUTTONUP,wParam,lParam);
break;
case WM_PAINT:
BeginPaint (hwnd, &ps);
/* 获取区域坐标及尺寸 */
area.x = ps.rcPaint.left;
area.y = ps.rcPaint.top;
area.w = ps.rcPaint.right - area.x;
area.h = ps.rcPaint.bottom - area.y;
hdcClient = GetDC (hwnd);
/* 为区域创建一个DC */
hdcArea = CreateCompatibleDC (hdcClient);
/* 为区域创建一个位图 */
area_bitmap = CreateCompatibleBitmap (hdcArea, area.w, area.h);
/* 获取该区域内显示的图形数据 */
Win32_GetClientBitmap (area_bitmap, area);
/* 替换区域内显示的图形数据 */
SelectObject (hdcArea, area_bitmap);
/* 将区域内的图形更新至客户区内 */
BitBlt (hdcClient, area.x, area.y, area.w, area.h, hdcArea, 0, 0, SRCCOPY );
printf("area:%d,%d,%d,%d\n", area.x, area.y, area.w, area.h);
DeleteDC (hdcArea);
ReleaseDC (hwnd, hdcClient);
EndPaint (hwnd, &ps);
return 0;
break;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
/* 在运行程序时会打开控制台,以查看打印的调试信息 */
void InitConsoleWindow(void)
{
int hCrt;
FILE *hf;
AllocConsole();
hCrt=_open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE),_O_TEXT );
hf=_fdopen( hCrt, "w" );
*stdout=*hf;
setvbuf (stdout, NULL, _IONBF, 0);
// test code
printf ("InitConsoleWindow OK!\n");
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
static TCHAR szAppName[] = TEXT ("Typer") ;
InitConsoleWindow();
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass)) {
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (
szAppName,
TEXT ("test"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
800, 600,
NULL, NULL, hInstance, NULL);
ShowWindow (hwnd, iCmdShow);
UpdateWindow (hwnd);
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询