主要功能是画线,然后遇到的问题很奇怪.有的时候生成的程序是对的,(而且程序基本没变化)作图速度很快,有的时候生成的程序就不对劲了,一般是画线之后很久线都不出来,等个几秒,就有线了,要不然随便点程序里的按钮,也会出来,或者用什么窗口挡住他,也会立即画出来.搞不懂怎么回事了....
(用的是VS2008 + win7)
CODE:
ExternVar.h
-----------------------------------------------------------------
#pragma once
extern HINSTANCE g_hInstance;
extern HWND g_hwnd;
extern HWND g_hwndPaint;
extern HWND g_hwndButton[3];
extern INT g_cxClient;
extern INT g_cyClient;
-------------------------------------------------------
GlobalVar.h
---------------------------------------------------
#pragma once
#include <windows.h>
static TCHAR g_szAppName[] = TEXT("DrawLine");
static TCHAR g_szPaintName[] = TEXT("Paint");
#define ID_PAINT 10
#define ID_COLOR 0
#define ID_WIDTH 1
#define ID_EXIT 2
#define WM_PENCHANGE 10000
static TCHAR g_szButtonName[][10] = { TEXT("Color"), TEXT("Width"), TEXT("Exit") };
----------------------------------------------------
MainWindows.h----------------------------------------------------
#pragma once
#include <windows.h>
BOOL RegisterMainWindows(WNDCLASS& WindowClass);
LRESULT CALLBACK WndMainProc(HWND,UINT,WPARAM,LPARAM);
--------------------------------------------------------
PaintWindows.h
------------------------------------------------------
#pragma once
#include <windows.h>
BOOL RegisterMainWindows(WNDCLASS& WindowClass);
LRESULT CALLBACK WndMainProc(HWND,UINT,WPARAM,LPARAM);
------------------------------------------------------------
MainWindows.cpp
------------------------------------------------------------
#include <Windows.h>
#include "GlobalVar.h"
#include "ExternVar.h"
#include "MainWindows.h"
BOOL RegisterMainWindows(WNDCLASS& WindowClass)
{
WindowClass.style = CS_HREDRAW | CS_VREDRAW;
WindowClass.lpfnWndProc = WndMainProc;
WindowClass.cbClsExtra = 0;
WindowClass.cbWndExtra = 0;
WindowClass.hInstance = g_hInstance;
WindowClass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
WindowClass.hCursor = LoadCursor(NULL,IDC_ARROW);
WindowClass.hbrBackground = (HBRUSH) GetStockObject(GRAY_BRUSH);
WindowClass.lpszMenuName = NULL;
WindowClass.lpszClassName = g_szAppName;
if(!RegisterClass(&WindowClass))
{
MessageBox(NULL,TEXT("ERROR! Registe faile!"),TEXT("ERROR"),MB_ICONERROR);
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK WndMainProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static int cxChar,cyChar;
//PAINTSTRUCT ps;
//HDC hdc;
switch(message)
{
case WM_CREATE:
g_hwnd = hwnd;
cxChar = LOWORd(GetDialogBaseUnits() );
cyChar = HIWORd(GetDialogBaseUnits() );
g_hwndPaint = CreateWindow(g_szPaintName, NULL, WS_CHILDWINDOW | WS_VISIBLE ,
0, 0, 0, 0, g_hwnd, (HMENU)ID_PAINT, g_hInstance, NULL);
for(int iCount = 0;iCount < 3;iCount++)
g_hwndButton[iCount] = CreateWindow(TEXT("button"), g_szButtonName[iCount], WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, 0, 0, g_hwnd, (HMENU)iCount, g_hInstance, NULL);
return 0;
case WM_SIZE:
g_cxClient = LOWORd(lParam);
g_cyClient = HIWORd(lParam);
MoveWindow(g_hwndPaint, 0, 0, (int)g_cxClient * 4 / 5, g_cyClient, TRUE);
for(int iCount = 0;iCount <3;iCount++)
MoveWindow(g_hwndButton[iCount], (int)g_cxClient * 82 / 100, (int)g_cyClient * 1/10 + (int)(iCount * 2.5 * cyChar), (int)g_cxClient * 16 / 100,(int)cyChar * 2,TRUE);
return 0;
//case WM_PAINT:
// hdc = BeginPaint(hwnd,&ps);
// EndPaint(hwnd,&ps);
// return 0;
case WM_COMMAND:
switch(wParam)
{
case ID_COLOR:
case ID_WIDTH:
MessageBeep(0);
break;
case ID_EXIT:
PostQuitMessage(0);
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
-----------------------------------------------
PaintWindows.cpp
-----------------------------------------------
#include <Windows.h>
#include "GlobalVar.h"
#include "ExternVar.h"
#include "PaintWindows.h"
BOOL RegisterPaintWindows(WNDCLASS& WindowClass)
{
WindowClass.style = CS_HREDRAW | CS_VREDRAW;
WindowClass.lpfnWndProc = WndPaintProc;
WindowClass.cbClsExtra = 0;
WindowClass.cbWndExtra = 0;
WindowClass.hInstance = g_hInstance;
WindowClass.hIcon = NULL;
WindowClass.hCursor = LoadCursor(NULL,IDC_CROSS);
WindowClass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
WindowClass.lpszClassName = g_szPaintName;
if(!RegisterClass(&WindowClass))
{
MessageBox(NULL,TEXT("ERROR! Registe faile!"),TEXT("ERROR"),MB_ICONERROR);
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK WndPaintProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static POINT LastPoint;
static POINT StartPoint;
static BOOL bDrawing = FALSE;
static HPEN hPen;
//PAINTSTRUCT ps;
HDC hdc;
switch(message)
{
case WM_CREATE:
hPen = CreatePen(PS_SOLID, 3, RGB(0, 0, 0) );
return 0;
case WM_DESTROY:
DeleteObject(hPen);
PostQuitMessage(0);
return 0;
//case WM_PAINT:
// hdc = BeginPaint(hwnd,&ps);
// EndPaint(hwnd,&ps);
// return 0;
case WM_LBUTTONDOWN:
bDrawing = true;//描画状态开始
StartPoint.x = LOWORd(lParam);//MAKEPOINT(lParam); //当前点保存于NowPoint中
StartPoint.y = HIWORd(lParam);
return 0;
case WM_MOUSEMOVE:
if(wParam & MK_LBUTTON)
{
hdc = GetDC(hwnd);
SelectObject(hdc, hPen);
if (bDrawing == false)//如果是描画状态
{
StartPoint.x = LOWORd(lParam);
StartPoint.y = HIWORd(lParam);
bDrawing = true;
}
LastPoint.x = LOWORd(lParam);//MAKEPOINT(lParam); //当前点保存于LastPoint中
LastPoint.y = HIWORd(lParam);
MoveToEx(hdc, StartPoint.x,StartPoint.y,NULL);
LineTo(hdc,LastPoint.x, LastPoint.y);
StartPoint = LastPoint; //当前点设置为旧的点
TRACKMOUSEEVENT MouseEvent;//注册鼠标事件,检查是否离开了绘图区
MouseEvent.cbSize = sizeof(MouseEvent);
MouseEvent.dwFlags = TME_LEAVE;
MouseEvent.dwHoverTime = 10;
MouseEvent.hwndTrack = g_hwndPaint;
TrackMouseEvent(&MouseEvent);
ReleaseDC(hwnd, hdc);
}
return 0;
case WM_LBUTTONUP:
bDrawing = false; //描画状态结束
return 0;
case WM_MOUSELEAVE:
bDrawing = false; //描画状态结束
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
----------------------------------------------------
WinApp.cpp---------------------------------------------------
#include <windows.h>
#include "GlobalVar.h"
#include "PaintWindows.h"
#include "MainWindows.h"
HINSTANCE g_hInstance;
HWND g_hwnd;
HWND g_hwndPaint;
HWND g_hwndButton[3];
INT g_cxClient;
INT g_cyClient;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR g_szCmdLine,int iCmdShow)
{
MSG msg;
WNDCLASS wndclass;
g_hInstance = hInstance;
RegisterMainWindows(wndclass);
RegisterPaintWindows(wndclass);
g_hwnd = CreateWindow(g_szAppName,
TEXT("DrawLines"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
//绘制第一个窗口
ShowWindow(g_hwnd,iCmdShow);
UpdateWindow(g_hwnd);
while(GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}