永发信息网

一个简单的Window程序,帮忙看看

答案:1  悬赏:10  手机版
解决时间 2021-04-29 07:48

主要功能是画线,然后遇到的问题很奇怪.有的时候生成的程序是对的,(而且程序基本没变化)作图速度很快,有的时候生成的程序就不对劲了,一般是画线之后很久线都不出来,等个几秒,就有线了,要不然随便点程序里的按钮,也会出来,或者用什么窗口挡住他,也会立即画出来.搞不懂怎么回事了....

(用的是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;
}


最佳答案
帮你调了一下,估计是画线后没有及时更新客户区的缘故,你在PaintWindows.cpp的WM_MOUSEMOVE消息最后加上一句UpdateWindow( hwnd )试试?

TrackMouseEvent(&MouseEvent);
UpdateWindow(hwnd);
ReleaseDC(hwnd, hdc);

我试验了加上以后下画图很流畅,没有问题,不过和在加这句之前也没有什么大的区别,你可以试试看。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
3GP/MP4手机网站是?
如何判定你是否喜欢一个人?
凯特威飚歌城怎么去啊,有知道地址的么
充值网站如何通过充值赚钱
为教室写一条标语,中国移动最经典的口号是什
沱牌特曲庆典装42度的多少钱呀????
dnf装备+10想加到13怎么+
张杰什么时候出的名?
超短裙有多少
做100的金 武器 水系的 要用什么材料好?
KARA的《Wanna》的正确中文歌词?
兔子为什么会吃自己的脚和兔BB
到底值得“拥有”有什麽?
陆通商务怎么去啊,有知道地址的么
成长95的神宠最多可以升到几级?
推荐资讯
拍拍两钻用户尽然不诚信,这该怎摸办?
天津狗不理包子多少钱一个?
两只都要宝宝的,算一次生二代,要三十级,生
我在东圃,请问有没有车直达佛山平洲的?
求足球小将世青篇漫画
贵阳学院新生什么时候开学
usb声卡怎么没有低音声音还很小的可怜
真想来碗清汤面在什么地方啊,我要过去处理事
亚洲天王是谁?
哪位大神有上瘾1到15集未删减版,求
关于经济学?
不 可以 呀 重新裝也不行 現在是警告碼0
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?