急问C++如何创建新窗口用来画图?!
纯新手,但急需.请给可执代码.需要在窗口中用RGB数据画象素点图.找到代码只能在桌面上画,望给创建窗口之法.谢谢!!找到的如下:#include<windows.h>in...
纯新手,但急需.请给可执代码.需要在窗口中用RGB数据画象素点图.
找到代码只能在桌面上画,望给创建窗口之法.谢谢!!
找到的如下:
#include<windows.h>
int main( void )
{
// 获取一个可供画图的DC,我这里就直接用桌面算了
HDC hdc = GetWindowDC( GetDesktopWindow() );
// 创建红色1像素宽度的实线画笔
HPEN hpen1 = CreatePen( PS_SOLID, 1, RGB(255,0,0) );
// 创建绿色5像素宽度的破折画笔,如果你想创建其他种类的画笔请参阅MSDN
HPEN hpen2 = CreatePen( PS_DASH, 5, RGB(0,255,0) );
// 创建一个实体蓝色画刷
HBRUSH hbrush1 = CreateSolidBrush( RGB(0,0,255) );
// 创造一个透明的画刷,如果你想创建其他种类的画刷请参阅MSDN
HBRUSH hbrush2 = (HBRUSH)GetStockObject( NULL_BRUSH );
// 将hpen1和hbrush1选进HDC,并保存HDC原来的画笔和画刷
HPEN hpen_old = (HPEN)SelectObject( hdc, hpen1 );
HBRUSH hbrush_old = (HBRUSH)SelectObject( hdc, hbrush1 );
// 在(40,30)处画一个宽200像素,高50像素的矩形
Rectangle( hdc, 40, 30, 40+200, 30+50 );
// 换hpen1和hbrush1,然后在(40,100)处也画一个矩形,看看有何差别
SelectObject( hdc, hpen2 );
SelectObject( hdc, hbrush2 );
Rectangle( hdc, 40, 100, 40+200, 100+50 );
// 画个椭圆看看
Ellipse( hdc, 40, 200, 40+200, 200+50 );
// 画个(0,600)到(800,0)的直线看看
MoveToEx( hdc, 0, 600, NULL );
LineTo( hdc, 800, 0 );
// 在(700,500)处画个黄点,不过这个点只有一像素大小,你细细的看才能找到
SetPixel( hdc, 700, 500, RGB(255,255,0) );
// 恢复原来的画笔和画刷
SelectObject( hdc, hpen_old );
SelectObject( hdc, hbrush_old );
return 0;
} 展开
找到代码只能在桌面上画,望给创建窗口之法.谢谢!!
找到的如下:
#include<windows.h>
int main( void )
{
// 获取一个可供画图的DC,我这里就直接用桌面算了
HDC hdc = GetWindowDC( GetDesktopWindow() );
// 创建红色1像素宽度的实线画笔
HPEN hpen1 = CreatePen( PS_SOLID, 1, RGB(255,0,0) );
// 创建绿色5像素宽度的破折画笔,如果你想创建其他种类的画笔请参阅MSDN
HPEN hpen2 = CreatePen( PS_DASH, 5, RGB(0,255,0) );
// 创建一个实体蓝色画刷
HBRUSH hbrush1 = CreateSolidBrush( RGB(0,0,255) );
// 创造一个透明的画刷,如果你想创建其他种类的画刷请参阅MSDN
HBRUSH hbrush2 = (HBRUSH)GetStockObject( NULL_BRUSH );
// 将hpen1和hbrush1选进HDC,并保存HDC原来的画笔和画刷
HPEN hpen_old = (HPEN)SelectObject( hdc, hpen1 );
HBRUSH hbrush_old = (HBRUSH)SelectObject( hdc, hbrush1 );
// 在(40,30)处画一个宽200像素,高50像素的矩形
Rectangle( hdc, 40, 30, 40+200, 30+50 );
// 换hpen1和hbrush1,然后在(40,100)处也画一个矩形,看看有何差别
SelectObject( hdc, hpen2 );
SelectObject( hdc, hbrush2 );
Rectangle( hdc, 40, 100, 40+200, 100+50 );
// 画个椭圆看看
Ellipse( hdc, 40, 200, 40+200, 200+50 );
// 画个(0,600)到(800,0)的直线看看
MoveToEx( hdc, 0, 600, NULL );
LineTo( hdc, 800, 0 );
// 在(700,500)处画个黄点,不过这个点只有一像素大小,你细细的看才能找到
SetPixel( hdc, 700, 500, RGB(255,255,0) );
// 恢复原来的画笔和画刷
SelectObject( hdc, hpen_old );
SelectObject( hdc, hbrush_old );
return 0;
} 展开
展开全部
这没办法给你把代码全贴上来,你自己用VC建一下吧很容易的。
打开VC->File菜单->New->Project下的选中Win32 Application,Project Name中输入5471,下一步->选中A typical “Hello World!”Application.点完成。把我下面的代码复制到你的5471.cpp 文件里面去替换原来5471。cpp文件里的所有内容。
// 5471.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
void DrawPixel(HWND hWnd);
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MY5471, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MY5471);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MY5471);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_MY5471;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
DrawPixel(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
void DrawPixel(HWND hWnd)
{
// 获取一个可供画图的DC,我这里就直接用桌面算了
HDC hdc = GetWindowDC( hWnd );
// 创建红色1像素宽度的实线画笔
HPEN hpen1 = CreatePen( PS_SOLID, 1, RGB(255,0,0) );
// 创建绿色5像素宽度的破折画笔,如果你想创建其他种类的画笔请参阅MSDN
HPEN hpen2 = CreatePen( PS_DASH, 5, RGB(0,255,0) );
// 创建一个实体蓝色画刷
HBRUSH hbrush1 = CreateSolidBrush( RGB(0,0,255) );
// 创造一个透明的画刷,如果你想创建其他种类的画刷请参阅MSDN
HBRUSH hbrush2 = (HBRUSH)GetStockObject( NULL_BRUSH );
// 将hpen1和hbrush1选进HDC,并保存HDC原来的画笔和画刷
HPEN hpen_old = (HPEN)SelectObject( hdc, hpen1 );
HBRUSH hbrush_old = (HBRUSH)SelectObject( hdc, hbrush1 );
// 在(40,30)处画一个宽200像素,高50像素的矩形
Rectangle( hdc, 40, 30, 40+200, 30+50 );
// 换hpen1和hbrush1,然后在(40,100)处也画一个矩形,看看有何差别
SelectObject( hdc, hpen2 );
SelectObject( hdc, hbrush2 );
Rectangle( hdc, 40, 100, 40+200, 100+50 );
// 画个椭圆看看
Ellipse( hdc, 40, 200, 40+200, 200+50 );
// 画个(0,600)到(800,0)的直线看看
MoveToEx( hdc, 0, 600, NULL );
LineTo( hdc, 800, 0 );
// 在(700,500)处画个黄点,不过这个点只有一像素大小,你细细的看才能找到
SetPixel( hdc, 700, 500, RGB(255,255,0) );
// 恢复原来的画笔和画刷
SelectObject( hdc, hpen_old );
SelectObject( hdc, hbrush_old );
ReleaseDC(hWnd,hdc);
}
打开VC->File菜单->New->Project下的选中Win32 Application,Project Name中输入5471,下一步->选中A typical “Hello World!”Application.点完成。把我下面的代码复制到你的5471.cpp 文件里面去替换原来5471。cpp文件里的所有内容。
// 5471.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
void DrawPixel(HWND hWnd);
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MY5471, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MY5471);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MY5471);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_MY5471;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
DrawPixel(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
void DrawPixel(HWND hWnd)
{
// 获取一个可供画图的DC,我这里就直接用桌面算了
HDC hdc = GetWindowDC( hWnd );
// 创建红色1像素宽度的实线画笔
HPEN hpen1 = CreatePen( PS_SOLID, 1, RGB(255,0,0) );
// 创建绿色5像素宽度的破折画笔,如果你想创建其他种类的画笔请参阅MSDN
HPEN hpen2 = CreatePen( PS_DASH, 5, RGB(0,255,0) );
// 创建一个实体蓝色画刷
HBRUSH hbrush1 = CreateSolidBrush( RGB(0,0,255) );
// 创造一个透明的画刷,如果你想创建其他种类的画刷请参阅MSDN
HBRUSH hbrush2 = (HBRUSH)GetStockObject( NULL_BRUSH );
// 将hpen1和hbrush1选进HDC,并保存HDC原来的画笔和画刷
HPEN hpen_old = (HPEN)SelectObject( hdc, hpen1 );
HBRUSH hbrush_old = (HBRUSH)SelectObject( hdc, hbrush1 );
// 在(40,30)处画一个宽200像素,高50像素的矩形
Rectangle( hdc, 40, 30, 40+200, 30+50 );
// 换hpen1和hbrush1,然后在(40,100)处也画一个矩形,看看有何差别
SelectObject( hdc, hpen2 );
SelectObject( hdc, hbrush2 );
Rectangle( hdc, 40, 100, 40+200, 100+50 );
// 画个椭圆看看
Ellipse( hdc, 40, 200, 40+200, 200+50 );
// 画个(0,600)到(800,0)的直线看看
MoveToEx( hdc, 0, 600, NULL );
LineTo( hdc, 800, 0 );
// 在(700,500)处画个黄点,不过这个点只有一像素大小,你细细的看才能找到
SetPixel( hdc, 700, 500, RGB(255,255,0) );
// 恢复原来的画笔和画刷
SelectObject( hdc, hpen_old );
SelectObject( hdc, hbrush_old );
ReleaseDC(hWnd,hdc);
}
2009-06-25
展开全部
请区分C++和VC!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |