程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> winapi-大家幫我檢查一下好嗎?實在搞不明白,在對話框中輸入數據,在主窗口繪直線

winapi-大家幫我檢查一下好嗎?實在搞不明白,在對話框中輸入數據,在主窗口繪直線

編輯:編程綜合問答
大家幫我檢查一下好嗎?實在搞不明白,在對話框中輸入數據,在主窗口繪直線

#include
#include "resource.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK AboutDlgProc (HWND, UINT, WPARAM, LPARAM) ;
char chr1[10], chr2[10], chr3[10],chr4[10];
int number1, number2, number3,number4;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("About1") ;
MSG msg ;
HWND hwnd ;
WNDCLASS wndclass ;

 wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
 wndclass.lpfnWndProc   = WndProc ;
 wndclass.cbClsExtra    = 0 ;
 wndclass.cbWndExtra    = 0 ;
 wndclass.hInstance     = hInstance ;
 wndclass.hIcon         = LoadIcon (hInstance, szAppName) ;
 wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
 wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
 wndclass.lpszMenuName  = szAppName ;
 wndclass.lpszClassName = szAppName ;

 if (!RegisterClass (&wndclass))
 {
      MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                  szAppName, MB_ICONERROR) ;
      return 0 ;
 }

 hwnd = CreateWindow (szAppName, TEXT ("********大學"),
                      WS_OVERLAPPEDWINDOW,
                      CW_USEDEFAULT, CW_USEDEFAULT,
                      CW_USEDEFAULT, CW_USEDEFAULT,
                      NULL, NULL, hInstance, NULL) ;

 ShowWindow (hwnd, iCmdShow) ;
 UpdateWindow (hwnd) ; 

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

}
void PaintWindow (HWND hwnd)
{
HBRUSH hBrush ;
HDC hdc ;
RECT rect ;
hdc = GetDC (hwnd) ;
MoveToEx (hdc, number1, number2, NULL) ;
LineTo (hdc, number3,number4) ;
ReleaseDC (hwnd, hdc) ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HINSTANCE hInstance ;
HDC hdc ;
PAINTSTRUCT ps ;
switch (message)
{
case WM_CREATE :
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;

PaintWindow (hwnd);
EndPaint (hwnd, &ps) ;
return 0 ;

 case WM_COMMAND :
      switch (LOWORD (wParam))
      {
      case IDM_APP_ABOUT :
           DialogBox (hInstance, TEXT ("ABOUTBOX"), hwnd, AboutDlgProc) ;
           break ;
      }
      return 0 ;


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

}

BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{

switch (message)
 {
 case WM_INITDIALOG :
      return TRUE ;

 case WM_COMMAND :
      switch (LOWORD (wParam))
      {

      case IDOK :

GetDlgItem( hDlg,IDC_EDIT1);
GetWindowText(IDC_EDIT1,chr1,10);
GetDlgItem( hDlg,IDC_EDIT2);
GetWindowText(IDC_EDIT2,chr2,10);
GetDlgItem( hDlg,IDC_EDIT3);
GetWindowText(IDC_EDIT3,chr3,10);
GetDlgItem( hDlg,IDC_EDIT4);
GetWindowText(IDC_EDIT4,chr4,10);
number1=atoi(chr1);
number2=atoi(chr2);
number3=atoi(chr3);
number4=atoi(chr4);
EndDialog (hDlg, TRUE) ;
return TRUE ;
case IDCANCEL :
EndDialog (hDlg, 0) ;
return TRUE ;
}
break ;
}
return FALSE ;
}

最佳回答:


1。BeginPaint已經返回HDC了,不要再用GetDC獲取DC句柄了
2。如果直接在窗口上畫可以直接在wndproc裡用WM_PAINT,如果是控件需要先子類化控件
3。獲取數據後可以用代碼立即重畫窗口,否則需要在特定時機(比如最小化再還原)讓系統重畫

baidu_30118209
zuishikonghuan
baidu_30118209
zuishikonghuan
zuishikonghuan
baidu_30118209
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved