使用SetPixel绘制线 [英] Drawing Line with SetPixel

查看:157
本文介绍了使用SetPixel绘制线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在学习为Windows编写代码的过程,并且正在从一本书(Jonathan S. Harbour的《 Beginning Game Programming Second Edition》开始)中遵循示例,但是不幸的是,这些示例之一使用SetPixel函数.因此,当我创建一个窗口并尝试绘制一条线时,可以说整个窗口.看来在计算机上它仅画线的一半,而另一面是空白的.有谁知道这可能是什么原因?

我正在使用Visual Studio 2008,Direct X 11.0

链接到我所看到的屏幕截图:
[我的屏幕]

更新:下面列出的代码

Hello,

I am in the process of learning to code for windows and I am following examples out of a book (Beginning Game Programming Second Edition by Jonathan S. Harbour), but unfortunately one of these examples uses SetPixel function. So when I create a window and try to draw a line, lets say across the window. It seems that on computer it only draws half of the line and other side is blank. Does anyone know what might cause this?

I am using Visual Studio 2008, Direct X 11.0

Link to screen shot of what I see:
[My Screen]

Update: Code Listed below

//header files to include
#include<windows.h>
#include<stdlib.h>
#include<time.h>

//application title
#define APPTITLE "Hello World"

//function prototypes (forward declarations)
BOOL InitInstance(HINSTANCE, int);
ATOM MyRegisterClass(HINSTANCE);
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);

//the window event callback function
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	char *szHello = "Hello World!";
	RECT rt;
	int x, y, n;
	COLORREF c;

	switch (message)
	{
	case WM_PAINT:
		//get the dimensions of the window
		GetClientRect(hWnd, &rt);

		//start drawing on devicce context
		hdc = BeginPaint (hWnd, &ps);

		//draw some text
		DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);

        c = RGB(0, 0, 0);
        y = 300;
        for(x=0; x <= 1280; x++)
        {
            SetPixel(hdc, x, y, c);
        }

		//stop drawing
		EndPaint(hWnd, &ps);
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

//helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	//create the window class structure
	WNDCLASSEX wc;
	wc.cbSize = sizeof(WNDCLASSEX);

	//fill the struct with info
	wc.style                 = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc           = (WNDPROC)WinProc;
	wc.cbClsExtra            = 0;
	wc.cbWndExtra            = 0;
	wc.hInstance             = hInstance;
	wc.hIcon                 = NULL;
	wc.hCursor               = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground         = (HBRUSH) GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName          = NULL;
	wc.lpszClassName         = APPTITLE;
	wc.hIconSm               = NULL;

	//set up the window with the class info
	return RegisterClassEx(&wc);
}

//helper function to create the window and refresh it
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;

	//create a new window
	hWnd = CreateWindow(
		APPTITLE,	        //window class
		APPTITLE,		//title bar
		WS_OVERLAPPEDWINDOW,    //window style
		CW_USEDEFAULT,		//x position of window
		CW_USEDEFAULT,		//y position of window
		1280,		        //width of the window
		720,		        //height of the window
		NULL,		        //parent window
		NULL,			//menu
		hInstance,	        //application instance
		NULL);		        //window parameters


	//was there an error creating the window?
	if(!hWnd)
		return FALSE;

	//display the window
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	return TRUE;
}

//entry point for a Windows program
int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR			lpCmdLine,
				   int				nCmdShow)
{
	//declare variables
	MSG msg;

	//register the class
	MyRegisterClass(hInstance);

	//initialize application
	if(!InitInstance (hInstance, nCmdShow))
		return FALSE;

	//set random number seed
	srand(time(NULL));

	//main message loop
	while(GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

推荐答案

如果要使用SetPixel填充一定大小(1281x721)的矩形(这是一种非常低效的方式),如果您的窗口大于该大小,那么您会看到剩余的背景.
:)
You are filling a rectangle of definite size (1281x721) using SetPixel (that is in a very inefficient way), if your window is bigger that such size then you see the remaining background.
:)


也许尝试增加行的宽度?或者,也许您的坐标有误?发布您正在使用的代码.
Perhaps try increasing the width of the line? Or maybe you have the coordinates wrong? Post the code you are using.


SetPixel函数仅使用定义的颜色设置像素.您的示例也使用SetPixel填充了背景.我看不到您要在哪里画线.
SetPixel function just sets a pixel with the defined color. Your example fills the background with SetPixel as well. I can''t see where you are trying to draw a line.


这篇关于使用SetPixel绘制线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆